Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

Fixed MultiProtocolURL.resolveBackpath to handle remaining '..' segments

luccioman3ccd89e27457
diff --git a/source/net/yacy/cora/document/id/MultiProtocolURL.java b/source/net/yacy/cora/document/id/MultiProtocolURL.java
index c6cddd319..f9c48b0a4 100644
--- a/source/net/yacy/cora/document/id/MultiProtocolURL.java
+++ b/source/net/yacy/cora/document/id/MultiProtocolURL.java
@@ -474,7 +474,17 @@ public class MultiProtocolURL implements Serializable, Comparable<MultiProtocolU
escape();
}
- // resolve '..'
+ /**
+ * Resolve '..' segments in the path.
+ * For standard pseudo algorithms, see :
+ * <ul>
+ * <li>https://tools.ietf.org/html/rfc3986#section-5.2.4</li>
+ * <li>https://url.spec.whatwg.org/#path-state</li>
+ * <li>https://www.w3.org/TR/url/#relative-path-state</li>
+ * </ul>
+ * @param path URL path part : must not be null
+ * @return the path with '..' segments resolved
+ */
private static final String resolveBackpath(final String path) {
String p = path;
if (p.isEmpty() || p.charAt(0) != '/') { p = "/" + p; }
@@ -486,6 +496,14 @@ public class MultiProtocolURL implements Serializable, Comparable<MultiProtocolU
p = matcher.replaceAll("");
matcher.reset(p);
}
+ /* Let's remove any eventual remaining but inappropriate '..' segments at the beginning.
+ * See https://tools.ietf.org/html/rfc3986#section-5.2.4 -> parts 2.C and 2.D */
+ while(p.startsWith("/../")) {
+ p = p.substring(3);
+ }
+ if(p.equals("/..")) {
+ p = "/";
+ }
return p.equals("") ? "/" : p;
}
diff --git a/test/java/net/yacy/cora/document/id/MultiProtocolURLTest.java b/test/java/net/yacy/cora/document/id/MultiProtocolURLTest.java
index c8d012a7f..3236d84f6 100644
--- a/test/java/net/yacy/cora/document/id/MultiProtocolURLTest.java
+++ b/test/java/net/yacy/cora/document/id/MultiProtocolURLTest.java
@@ -38,15 +38,17 @@ public class MultiProtocolURLTest {
String[][] testStrings = new String[][]{
new String[]{"/..home", "/..home"},
new String[]{"/test/..home/test.html", "/test/..home/test.html"},
- new String[]{"/../", "/../"},
- new String[]{"/..", "/.."},
new String[]{"/test/..", "/"},
new String[]{"/test/../", "/"},
new String[]{"/test/test2/..", "/test"},
new String[]{"/test/test2/../", "/test/"},
new String[]{"/test/test2/../hallo", "/test/hallo"},
new String[]{"/test/test2/../hallo/", "/test/hallo/"},
- new String[]{"/home/..test/../hallo/../", "/home/"}
+ new String[]{"/home/..test/../hallo/../", "/home/"},
+ /* No path segments prior to the '..' segment : '..' still has to be removed (See https://tools.ietf.org/html/rfc3986#section-5.2.4 -> parts 2.C and 2.D )*/
+ new String[]{"/../", "/"},
+ new String[]{"/..", "/"},
+ new String[]{"/../../../image.jpg", "/image.jpg"}
};
String testhost = "http://localhost";
for (int i = 0; i < testStrings.length; i++) {
@@ -223,7 +225,7 @@ public class MultiProtocolURLTest {
* Test of getFileExtension method, of class MultiProtocolURL.
*/
@Test
- public void testGetFileExtension() throws MalformedURLException {
+ public void testGetFileExtension() {
Map<String, String> testurls = new HashMap<String, String>();
// key=testurl, value=result
testurls.put("path/file.xml","xml"); // easiest