Skip to content

Commit 07f0be6

Browse files
committed
Make sure history.jsp doesn't strip off too much when creating a relative path name.
Fixes #649.
1 parent 9ee3db4 commit 07f0be6

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

src/org/opensolaris/opengrok/web/Util.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,4 +923,33 @@ public static String jsStringLiteral(String str) {
923923
sb.append('"');
924924
return sb.toString();
925925
}
926+
927+
/**
928+
* Make a path relative by stripping off a prefix. If the path does not
929+
* have the given prefix, return the full path unchanged.
930+
*
931+
* @param prefix the prefix to strip off
932+
* @param fullPath the path from which to remove the prefix
933+
* @return a path relative to {@code prefix} if {@code prefix} is a
934+
* parent directory of {@code fullPath}; otherwise, {@code fullPath}
935+
*/
936+
public static String stripPathPrefix(String prefix, String fullPath) {
937+
// Find the length of the prefix to strip off. The prefix should
938+
// represent a directory, so it could end with a slash. In case it
939+
// doesn't end with a slash, increase the length by one so that we
940+
// strip off the leading slash from the relative path.
941+
int prefixLength = prefix.length();
942+
if (!prefix.endsWith("/")) {
943+
prefixLength++;
944+
}
945+
946+
// If the full path starts with the prefix, strip off the prefix.
947+
if (fullPath.length() > prefixLength && fullPath.startsWith(prefix)
948+
&& fullPath.charAt(prefixLength - 1) == '/') {
949+
return fullPath.substring(prefixLength);
950+
}
951+
952+
// Otherwise, return the full path.
953+
return fullPath;
954+
}
926955
}

test/org/opensolaris/opengrok/web/UtilTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,5 +246,22 @@ public void jsStringLiteral() {
246246
assertEquals("\"abc\\n\\r\\\"\\\\\"",
247247
Util.jsStringLiteral("abc\n\r\"\\"));
248248
}
249+
250+
@Test
251+
public void stripPathPrefix() {
252+
assertEquals("/", Util.stripPathPrefix("/", "/"));
253+
assertEquals("/abc", Util.stripPathPrefix("/abc", "/abc"));
254+
assertEquals("/abc/", Util.stripPathPrefix("/abc", "/abc/"));
255+
assertEquals("/abc", Util.stripPathPrefix("/abc/", "/abc"));
256+
assertEquals("/abc/", Util.stripPathPrefix("/abc/", "/abc/"));
257+
assertEquals("abc", Util.stripPathPrefix("/", "/abc"));
258+
assertEquals("abc/def", Util.stripPathPrefix("/", "/abc/def"));
259+
assertEquals("def", Util.stripPathPrefix("/abc", "/abc/def"));
260+
assertEquals("def", Util.stripPathPrefix("/abc/", "/abc/def"));
261+
assertEquals("/abcdef", Util.stripPathPrefix("/abc", "/abcdef"));
262+
assertEquals("/abcdef", Util.stripPathPrefix("/abc/", "/abcdef"));
263+
assertEquals("def/ghi", Util.stripPathPrefix("/abc", "/abc/def/ghi"));
264+
assertEquals("def/ghi", Util.stripPathPrefix("/abc/", "/abc/def/ghi"));
265+
}
249266
}
250267

web/history.jsp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ information: Portions Copyright [yyyy] [name of copyright owner]
1818
1919
CDDL HEADER END
2020
21-
Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
21+
Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2222
2323
Portions Copyright 2011 Jens Elkner.
2424
@@ -204,14 +204,7 @@ document.domReady.push(function() {domReadyHistory();});
204204
if (files != null) {
205205
%><span class="filelist-hidden"><br/><%
206206
for (String ifile : files) {
207-
String jfile = ifile;
208-
if ("/".equals(path)) {
209-
jfile = ifile.substring(1);
210-
} else if (ifile.startsWith(path)
211-
&& ifile.length() > (path.length() + 1))
212-
{
213-
jfile = ifile.substring(path.length() + 1);
214-
}
207+
String jfile = Util.stripPathPrefix(path, ifile);
215208
if (rev == "") {
216209
%>
217210
<a class="h" href="<%= context + Prefix.XREF_P + ifile %>"><%= jfile %></a><br/><%

0 commit comments

Comments
 (0)