Skip to content

Commit

Permalink
resolve abnormal urls in compliance with rfc3986 (#1482)
Browse files Browse the repository at this point in the history
  • Loading branch information
morokosi authored Jul 9, 2021
1 parent 661523f commit 8db724e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/org/jsoup/internal/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.Stack;
import java.util.regex.Pattern;

/**
A minimal String utility class. Designed for <b>internal</b> jsoup use only - the API and outcome may change without
Expand Down Expand Up @@ -259,6 +260,7 @@ public static boolean isAscii(String string) {
return true;
}

private static Pattern extraDotSegmentsPattern = Pattern.compile("^/((\\.{1,2}/)+)");
/**
* Create a new absolute URL, from a provided existing absolute URL and a relative URL component.
* @param base the existing absolute base URL
Expand All @@ -271,10 +273,12 @@ public static URL resolve(URL base, String relUrl) throws MalformedURLException
if (relUrl.startsWith("?"))
relUrl = base.getPath() + relUrl;
// workaround: //example.com + ./foo = //example.com/./foo, not //example.com/foo
if (relUrl.indexOf('.') == 0 && base.getFile().indexOf('/') != 0) {
base = new URL(base.getProtocol(), base.getHost(), base.getPort(), "/" + base.getFile());
URL url = new URL(base, relUrl);
String fixedFile = extraDotSegmentsPattern.matcher(url.getFile()).replaceFirst("/");
if (url.getRef() != null) {
fixedFile = fixedFile + "#" + url.getRef();
}
return new URL(base, relUrl);
return new URL(url.getProtocol(), url.getHost(), url.getPort(), fixedFile);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/jsoup/internal/StringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ public void join() {
assertEquals("ftp://example.com/one", resolve("ftp://example.com/two/", "../one"));
assertEquals("ftp://example.com/one/two.c", resolve("ftp://example.com/one/", "./two.c"));
assertEquals("ftp://example.com/one/two.c", resolve("ftp://example.com/one/", "two.c"));
// examples taken from rfc3986 section 5.4.2
assertEquals("http://example.com/g", resolve("http://example.com/b/c/d;p?q", "../../../g"));
assertEquals("http://example.com/g", resolve("http://example.com/b/c/d;p?q", "../../../../g"));
assertEquals("http://example.com/g", resolve("http://example.com/b/c/d;p?q", "/./g"));
assertEquals("http://example.com/g", resolve("http://example.com/b/c/d;p?q", "/../g"));
assertEquals("http://example.com/b/c/g.", resolve("http://example.com/b/c/d;p?q", "g."));
assertEquals("http://example.com/b/c/.g", resolve("http://example.com/b/c/d;p?q", ".g"));
assertEquals("http://example.com/b/c/g..", resolve("http://example.com/b/c/d;p?q", "g.."));
assertEquals("http://example.com/b/c/..g", resolve("http://example.com/b/c/d;p?q", "..g"));
assertEquals("http://example.com/b/g", resolve("http://example.com/b/c/d;p?q", "./../g"));
assertEquals("http://example.com/b/c/g/", resolve("http://example.com/b/c/d;p?q", "./g/."));
assertEquals("http://example.com/b/c/g/h", resolve("http://example.com/b/c/d;p?q", "g/./h"));
assertEquals("http://example.com/b/c/h", resolve("http://example.com/b/c/d;p?q", "g/../h"));
assertEquals("http://example.com/b/c/g;x=1/y", resolve("http://example.com/b/c/d;p?q", "g;x=1/./y"));
assertEquals("http://example.com/b/c/y", resolve("http://example.com/b/c/d;p?q", "g;x=1/../y"));
assertEquals("http://example.com/b/c/g?y/./x", resolve("http://example.com/b/c/d;p?q", "g?y/./x"));
assertEquals("http://example.com/b/c/g?y/../x", resolve("http://example.com/b/c/d;p?q", "g?y/../x"));
assertEquals("http://example.com/b/c/g#s/./x", resolve("http://example.com/b/c/d;p?q", "g#s/./x"));
assertEquals("http://example.com/b/c/g#s/../x", resolve("http://example.com/b/c/d;p?q", "g#s/../x"));
}

@Test
Expand Down

0 comments on commit 8db724e

Please sign in to comment.