Skip to content

Commit 90c6d6e

Browse files
Adam Vartanianandroid-build-team Robot
authored andcommitted
Adjust URI host parsing to stop on \ character.
The WHATWG URL parsing algorithm [1] used by browsers says that for "special" URL schemes (which is basically all commonly-used hierarchical schemes, including http, https, ftp, and file), the host portion ends if a \ character is seen, whereas this class previously continued to consider characters part of the hostname. This meant that a malicious URL could be seen as having a "safe" host when viewed by an app but navigate to a different host when passed to a browser. [1] https://url.spec.whatwg.org/#host-state Bug: 71360761 Test: vogar frameworks/base/core/tests/coretests/src/android/net/UriTest.java (on NYC branch) Test: cts -m CtsNetTestCases (on NYC branch) Change-Id: Id53f7054d1be8d59bbcc7e219159e59a2425106e (cherry picked from commit fa3afbd)
1 parent 826fec9 commit 90c6d6e

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

core/java/android/net/Uri.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,10 @@ static String parseAuthority(String uriString, int ssi) {
720720
LOOP: while (end < length) {
721721
switch (uriString.charAt(end)) {
722722
case '/': // Start of path
723+
case '\\':// Start of path
724+
// Per http://url.spec.whatwg.org/#host-state, the \ character
725+
// is treated as if it were a / character when encountered in a
726+
// host
723727
case '?': // Start of query
724728
case '#': // Start of fragment
725729
break LOOP;
@@ -758,6 +762,10 @@ static String parsePath(String uriString, int ssi) {
758762
case '#': // Start of fragment
759763
return ""; // Empty path.
760764
case '/': // Start of path!
765+
case '\\':// Start of path!
766+
// Per http://url.spec.whatwg.org/#host-state, the \ character
767+
// is treated as if it were a / character when encountered in a
768+
// host
761769
break LOOP;
762770
}
763771
pathStart++;

core/tests/coretests/src/android/net/UriTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ public void testAuthorityParsing() {
192192
assertEquals("a:a@example.com:a@example2.com", uri.getAuthority());
193193
assertEquals("example2.com", uri.getHost());
194194
assertEquals(-1, uri.getPort());
195+
assertEquals("/path", uri.getPath());
196+
197+
uri = Uri.parse("http://a.foo.com\\.example.com/path");
198+
assertEquals("a.foo.com", uri.getHost());
199+
assertEquals(-1, uri.getPort());
200+
assertEquals("\\.example.com/path", uri.getPath());
195201
}
196202

197203
@SmallTest

0 commit comments

Comments
 (0)