URLParser: make parsing invalid IPv4 addresses more robust and correct
authorachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 29 Sep 2016 20:21:54 +0000 (20:21 +0000)
committerachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 29 Sep 2016 20:21:54 +0000 (20:21 +0000)
https://bugs.webkit.org/show_bug.cgi?id=162746

Reviewed by Tim Horton.

Source/WebCore:

If parsing an IPv4 address fails, the characters are just treated as a regular domain.

Covered by new API tests.

* platform/URLParser.cpp:
(WebCore::URLParser::parseIPv4Number):
(WebCore::URLParser::parseIPv4Host):

Tools:

* TestWebKitAPI/Tests/WebCore/URLParser.cpp:
(TestWebKitAPI::TEST_F):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@206609 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/URLParser.cpp
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebCore/URLParser.cpp

index 1e5e21f..c02f104 100644 (file)
@@ -1,5 +1,20 @@
 2016-09-29  Alex Christensen  <achristensen@webkit.org>
 
+        URLParser: make parsing invalid IPv4 addresses more robust and correct
+        https://bugs.webkit.org/show_bug.cgi?id=162746
+
+        Reviewed by Tim Horton.
+
+        If parsing an IPv4 address fails, the characters are just treated as a regular domain.
+
+        Covered by new API tests.
+
+        * platform/URLParser.cpp:
+        (WebCore::URLParser::parseIPv4Number):
+        (WebCore::URLParser::parseIPv4Host):
+
+2016-09-29  Alex Christensen  <achristensen@webkit.org>
+
         URLParser: IPv6 addresses followed by a colon are invalid
         https://bugs.webkit.org/show_bug.cgi?id=162747
 
index 04dbadb..accbd89 100644 (file)
@@ -2043,7 +2043,6 @@ Optional<uint32_t> URLParser::parseIPv4Number(CodePointIterator<CharacterType>&
     bool didSeeSyntaxViolation = false;
     while (!iterator.atEnd()) {
         if (*iterator == '.') {
-            ++iterator;
             ASSERT(!value.hasOverflowed());
             return value.unsafeGet();
         }
@@ -2123,8 +2122,16 @@ Optional<URLParser::IPv4Address> URLParser::parseIPv4Host(CodePointIterator<Char
             items.append(item.value());
         else
             return Nullopt;
+        if (!iterator.atEnd()) {
+            if (items.size() >= 4)
+                return Nullopt;
+            if (*iterator == '.')
+                ++iterator;
+            else
+                return Nullopt;
+        }
     }
-    if (!items.size() || items.size() > 4)
+    if (!iterator.atEnd() || !items.size() || items.size() > 4)
         return Nullopt;
     if (items.size() > 2) {
         for (size_t i = 0; i < items.size() - 2; i++) {
index a1f83fe..20ca6a4 100644 (file)
@@ -1,5 +1,15 @@
 2016-09-29  Alex Christensen  <achristensen@webkit.org>
 
+        URLParser: make parsing invalid IPv4 addresses more robust and correct
+        https://bugs.webkit.org/show_bug.cgi?id=162746
+
+        Reviewed by Tim Horton.
+
+        * TestWebKitAPI/Tests/WebCore/URLParser.cpp:
+        (TestWebKitAPI::TEST_F):
+
+2016-09-29  Alex Christensen  <achristensen@webkit.org>
+
         URLParser: IPv6 addresses followed by a colon are invalid
         https://bugs.webkit.org/show_bug.cgi?id=162747
 
index 7001f6c..5d2e5a4 100644 (file)
@@ -289,6 +289,9 @@ TEST_F(URLParserTest, Basic)
     checkURL("http:////\t////user:@webkit.org:99?foo", {"http", "user", "", "webkit.org", 99, "/", "foo", "", "http://user@webkit.org:99/?foo"});
     checkURL("http://\t//\\///user:@webkit.org:99?foo", {"http", "user", "", "webkit.org", 99, "/", "foo", "", "http://user@webkit.org:99/?foo"});
     checkURL("http:/\\user:@webkit.org:99?foo", {"http", "user", "", "webkit.org", 99, "/", "foo", "", "http://user@webkit.org:99/?foo"});
+    checkURL("http://127.0.0.1", {"http", "", "", "127.0.0.1", 0, "/", "", "", "http://127.0.0.1/"});
+    checkURL("http://127.0.0.1.", {"http", "", "", "127.0.0.1.", 0, "/", "", "", "http://127.0.0.1./"});
+    checkURL("http://127.0.0.1./", {"http", "", "", "127.0.0.1.", 0, "/", "", "", "http://127.0.0.1./"});
 
     // This disagrees with the web platform test for http://:@www.example.com but agrees with Chrome and URL::parse,
     // and Firefox fails the web platform test differently. Maybe the web platform test ought to be changed.
@@ -748,6 +751,21 @@ TEST_F(URLParserTest, ParserDifferences)
     checkURLDifferences("http://[1:2:3:4:5:6:7:::]/",
         {"", "", "", "", 0, "", "", "", "http://[1:2:3:4:5:6:7:::]/"},
         {"http", "", "", "[1:2:3:4:5:6:7:::]", 0, "/", "", "", "http://[1:2:3:4:5:6:7:::]/"});
+    checkURLDifferences("http://127.0.0.1~/",
+        {"http", "", "", "127.0.0.1~", 0, "/", "", "", "http://127.0.0.1~/"},
+        {"", "", "", "", 0, "", "", "", "http://127.0.0.1~/"});
+    checkURLDifferences("http://127.0.1~/",
+        {"http", "", "", "127.0.1~", 0, "/", "", "", "http://127.0.1~/"},
+        {"", "", "", "", 0, "", "", "", "http://127.0.1~/"});
+    checkURLDifferences("http://127.0.1./",
+        {"http", "", "", "127.0.0.1", 0, "/", "", "", "http://127.0.0.1/"},
+        {"http", "", "", "127.0.1.", 0, "/", "", "", "http://127.0.1./"});
+    checkURLDifferences("http://127.0.1.~/",
+        {"http", "", "", "127.0.1.~", 0, "/", "", "", "http://127.0.1.~/"},
+        {"", "", "", "", 0, "", "", "", "http://127.0.1.~/"});
+    checkURLDifferences("http://127.0.1.~",
+        {"http", "", "", "127.0.1.~", 0, "/", "", "", "http://127.0.1.~/"},
+        {"", "", "", "", 0, "", "", "", "http://127.0.1.~"});
 }
 
 TEST_F(URLParserTest, DefaultPort)