Title: [234147] trunk
Revision
234147
Author
[email protected]
Date
2018-07-24 08:15:34 -0700 (Tue, 24 Jul 2018)

Log Message

WebCore::URL::hostIsIPAddress needs a Windows implementation
https://bugs.webkit.org/show_bug.cgi?id=187859

Reviewed by Fujii Hironori.

Source/WebCore:

* platform/URL.cpp:
(WebCore::isIPv4Address): Added.
(WebCore::isIPv6Address): Added.
(WebCore::URL::hostIsIPAddress):
Turn this stub into a platform-agnostic default implementation.

Tools:

* TestWebKitAPI/Tests/WebCore/URL.cpp:
Check for a few other ways an IP address could be invalid.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (234146 => 234147)


--- trunk/Source/WebCore/ChangeLog	2018-07-24 15:04:43 UTC (rev 234146)
+++ trunk/Source/WebCore/ChangeLog	2018-07-24 15:15:34 UTC (rev 234147)
@@ -1,3 +1,16 @@
+2018-07-24  Ross Kirsling  <[email protected]>
+
+        WebCore::URL::hostIsIPAddress needs a Windows implementation
+        https://bugs.webkit.org/show_bug.cgi?id=187859
+
+        Reviewed by Fujii Hironori.
+
+        * platform/URL.cpp:
+        (WebCore::isIPv4Address): Added.
+        (WebCore::isIPv6Address): Added.
+        (WebCore::URL::hostIsIPAddress):
+        Turn this stub into a platform-agnostic default implementation.
+
 2018-07-24  Eric Carlson  <[email protected]>
 
         [MediaStream] Restructure getDisplayMedia classes

Modified: trunk/Source/WebCore/platform/URL.cpp (234146 => 234147)


--- trunk/Source/WebCore/platform/URL.cpp	2018-07-24 15:04:43 UTC (rev 234146)
+++ trunk/Source/WebCore/platform/URL.cpp	2018-07-24 15:15:34 UTC (rev 234147)
@@ -1062,10 +1062,96 @@
 }
 
 #if !PLATFORM(COCOA) && !USE(SOUP)
+static bool isIPv4Address(StringView string)
+{
+    auto count = 0;
+
+    for (const auto octet : string.split('.', StringView::AllowEmptyEntries)) {
+        if (count >= 4)
+            return false;
+
+        const auto length = octet.length();
+        if (!length || length > 3)
+            return false;
+
+        auto value = 0;
+        for (auto i = 0u; i < length; ++i) {
+            const auto digit = octet[i];
+
+            // Prohibit leading zeroes.
+            if (digit > '9' || digit < (!i && length > 1 ? '1' : '0'))
+                return false;
+
+            value = 10 * value + (digit - '0');
+        }
+
+        if (value > 255)
+            return false;
+
+        count++;
+    }
+
+    return (count == 4);
+}
+
+static bool isIPv6Address(StringView string)
+{
+    enum SkipState { None, WillSkip, Skipping, Skipped, Final };
+    auto skipState = None;
+    auto count = 0;
+
+    for (const auto hextet : string.split(':', StringView::AllowEmptyEntries)) {
+        if (count >= 8 || skipState == Final)
+            return false;
+
+        const auto length = hextet.length();
+        if (!length) {
+            // :: may be used anywhere to skip 1 to 8 hextets, but only once.
+            if (skipState == Skipped)
+                return false;
+
+            if (skipState == None)
+                skipState = !count ? WillSkip : Skipping;
+            else if (skipState == WillSkip)
+                skipState = Skipping;
+            else
+                skipState = Final;
+            continue;
+        }
+
+        if (skipState == WillSkip)
+            return false;
+
+        if (skipState == Skipping)
+            skipState = Skipped;
+
+        if (length > 4) {
+            // An IPv4 address may be used in place of the final two hextets.
+            if ((skipState == None && count != 6) || (skipState == Skipped && count >= 6) || !isIPv4Address(hextet))
+                return false;
+
+            skipState = Final;
+            continue;
+        }
+
+        for (const auto codeUnit : hextet.codeUnits()) {
+            // IPv6 allows leading zeroes.
+            if (!isASCIIHexDigit(codeUnit))
+                return false;
+        }
+
+        count++;
+    }
+
+    return (count == 8 && skipState == None) || skipState == Skipped || skipState == Final;
+}
+
 bool URL::hostIsIPAddress(StringView host)
 {
-    // Assume that any host that ends with a digit is trying to be an IP address.
-    return !host.isEmpty() && isASCIIDigit(host[host.length() - 1]);
+    if (host.find(':') == notFound)
+        return isIPv4Address(host);
+
+    return isIPv6Address(host);
 }
 #endif
 

Modified: trunk/Tools/ChangeLog (234146 => 234147)


--- trunk/Tools/ChangeLog	2018-07-24 15:04:43 UTC (rev 234146)
+++ trunk/Tools/ChangeLog	2018-07-24 15:15:34 UTC (rev 234147)
@@ -1,3 +1,13 @@
+2018-07-24  Ross Kirsling  <[email protected]>
+
+        WebCore::URL::hostIsIPAddress needs a Windows implementation
+        https://bugs.webkit.org/show_bug.cgi?id=187859
+
+        Reviewed by Fujii Hironori.
+
+        * TestWebKitAPI/Tests/WebCore/URL.cpp:
+        Check for a few other ways an IP address could be invalid.
+
 2018-07-24  Charlie Turner  <[email protected]>
 
         [Flatpak] Avoid consuming unknown arguments in flatpak wrapper

Modified: trunk/Tools/TestWebKitAPI/Tests/WebCore/URL.cpp (234146 => 234147)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/URL.cpp	2018-07-24 15:04:43 UTC (rev 234146)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/URL.cpp	2018-07-24 15:15:34 UTC (rev 234147)
@@ -345,17 +345,24 @@
     EXPECT_FALSE(URL::hostIsIPAddress(" 127.0.0.1"));
     EXPECT_FALSE(URL::hostIsIPAddress("127..0.0.1"));
     EXPECT_FALSE(URL::hostIsIPAddress("127.0.0."));
+    EXPECT_FALSE(URL::hostIsIPAddress("256.0.0.1"));
     EXPECT_FALSE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:ba98"));
     EXPECT_FALSE(URL::hostIsIPAddress("012x:4567:89AB:cdef:3210:7654:ba98:FeDc"));
 #if !PLATFORM(COCOA)
     // FIXME: This fails in Mac.
+    EXPECT_FALSE(URL::hostIsIPAddress("127.0.0.01"));
     EXPECT_FALSE(URL::hostIsIPAddress("00123:4567:89AB:cdef:3210:7654:ba98:FeDc"));
 #endif
     EXPECT_FALSE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:123.45.67.89"));
     EXPECT_FALSE(URL::hostIsIPAddress(":::"));
+    EXPECT_FALSE(URL::hostIsIPAddress("0123::89AB:cdef:3210:7654::FeDc"));
+    EXPECT_FALSE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:ba98:"));
+    EXPECT_FALSE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:ba98:FeDc:"));
+    EXPECT_FALSE(URL::hostIsIPAddress(":4567:89AB:cdef:3210:7654:ba98:FeDc"));
+    EXPECT_FALSE(URL::hostIsIPAddress(":0123:4567:89AB:cdef:3210:7654:ba98:FeDc"));
 
     EXPECT_TRUE(URL::hostIsIPAddress("127.0.0.1"));
-    EXPECT_TRUE(URL::hostIsIPAddress("123.45.67.89"));
+    EXPECT_TRUE(URL::hostIsIPAddress("255.1.10.100"));
     EXPECT_TRUE(URL::hostIsIPAddress("0.0.0.0"));
     EXPECT_TRUE(URL::hostIsIPAddress("::1"));
     EXPECT_TRUE(URL::hostIsIPAddress("::"));
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to