Title: [228323] trunk
Revision
228323
Author
commit-qu...@webkit.org
Date
2018-02-09 09:32:57 -0800 (Fri, 09 Feb 2018)

Log Message

Add a way to check if a host is an IP address
https://bugs.webkit.org/show_bug.cgi?id=182427

Patch by Carlos Garcia Campos <cgar...@igalia.com> on 2018-02-09
Reviewed by Alex Christensen.

Source/WebCore:

There are several places where this is needed. We currently just assume that any host ending in a digit is an IP
address, except in PublicSuffix where platform specific code is used. This patch adds URL::hostIsIPAddress()
platform specific implementations, falling back to current assumption if there isn't an implementation for the
platform.

* page/OriginAccessEntry.cpp:
(WebCore::OriginAccessEntry::OriginAccessEntry): Use URL::hostIsIPAddress().
* platform/URL.cpp:
(WebCore::URL::hostIsIPAddress): Fallback implementation.
* platform/URL.h:
* platform/mac/PublicSuffixMac.mm:
(WebCore::topPrivatelyControlledDomain): Use URL::hostIsIPAddress().
* platform/mac/URLMac.mm:
(WebCore::URL::hostIsIPAddress): Move implementation from PublicSuffixMac.mm.
* platform/network/curl/CookieUtil.cpp:
(WebCore::CookieUtil::isIPAddress): Use URL::hostIsIPAddress().
* platform/soup/URLSoup.cpp:
(WebCore::URL::hostIsIPAddress): Use g_hostname_is_ip_address().

Tools:

Add unit test for URL::hostIsIPAddress().

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

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (228322 => 228323)


--- trunk/Source/WebCore/ChangeLog	2018-02-09 17:26:47 UTC (rev 228322)
+++ trunk/Source/WebCore/ChangeLog	2018-02-09 17:32:57 UTC (rev 228323)
@@ -1,3 +1,29 @@
+2018-02-09  Carlos Garcia Campos  <cgar...@igalia.com>
+
+        Add a way to check if a host is an IP address
+        https://bugs.webkit.org/show_bug.cgi?id=182427
+
+        Reviewed by Alex Christensen.
+
+        There are several places where this is needed. We currently just assume that any host ending in a digit is an IP
+        address, except in PublicSuffix where platform specific code is used. This patch adds URL::hostIsIPAddress()
+        platform specific implementations, falling back to current assumption if there isn't an implementation for the
+        platform.
+
+        * page/OriginAccessEntry.cpp:
+        (WebCore::OriginAccessEntry::OriginAccessEntry): Use URL::hostIsIPAddress().
+        * platform/URL.cpp:
+        (WebCore::URL::hostIsIPAddress): Fallback implementation.
+        * platform/URL.h:
+        * platform/mac/PublicSuffixMac.mm:
+        (WebCore::topPrivatelyControlledDomain): Use URL::hostIsIPAddress().
+        * platform/mac/URLMac.mm:
+        (WebCore::URL::hostIsIPAddress): Move implementation from PublicSuffixMac.mm.
+        * platform/network/curl/CookieUtil.cpp:
+        (WebCore::CookieUtil::isIPAddress): Use URL::hostIsIPAddress().
+        * platform/soup/URLSoup.cpp:
+        (WebCore::URL::hostIsIPAddress): Use g_hostname_is_ip_address().
+
 2018-02-08  Philippe Normand  <pnorm...@igalia.com>
 
         [GStreamer][WebAudio] Winamp2-js woes

Modified: trunk/Source/WebCore/page/OriginAccessEntry.cpp (228322 => 228323)


--- trunk/Source/WebCore/page/OriginAccessEntry.cpp	2018-02-09 17:26:47 UTC (rev 228322)
+++ trunk/Source/WebCore/page/OriginAccessEntry.cpp	2018-02-09 17:32:57 UTC (rev 228323)
@@ -40,11 +40,9 @@
     , m_host(host.convertToASCIILowercase())
     , m_subdomainSettings(subdomainSetting)
     , m_ipAddressSettings(ipAddressSetting)
+    , m_hostIsIPAddress(URL::hostIsIPAddress(m_host))
 {
     ASSERT(subdomainSetting == AllowSubdomains || subdomainSetting == DisallowSubdomains);
-
-    // Assume that any host that ends with a digit is trying to be an IP address.
-    m_hostIsIPAddress = !m_host.isEmpty() && isASCIIDigit(m_host[m_host.length() - 1]);
 }
 
 bool OriginAccessEntry::matchesOrigin(const SecurityOrigin& origin) const
@@ -69,7 +67,7 @@
 
     // IP addresses are not domains: https://url.spec.whatwg.org/#concept-domain
     // Don't try to do subdomain matching on IP addresses.
-    if (m_hostIsIPAddress && m_ipAddressSettings == TreatIPAddressAsIPAddress)
+    if (m_ipAddressSettings == TreatIPAddressAsIPAddress && (m_hostIsIPAddress || URL::hostIsIPAddress(origin.host())))
         return false;
     
     // Match subdomains.

Modified: trunk/Source/WebCore/platform/URL.cpp (228322 => 228323)


--- trunk/Source/WebCore/platform/URL.cpp	2018-02-09 17:26:47 UTC (rev 228322)
+++ trunk/Source/WebCore/platform/URL.cpp	2018-02-09 17:32:57 UTC (rev 228323)
@@ -1035,4 +1035,12 @@
     return ts;
 }
 
+#if !PLATFORM(COCOA) && !USE(SOUP)
+bool URL::hostIsIPAddress(const String& 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]);
+}
+#endif
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/platform/URL.h (228322 => 228323)


--- trunk/Source/WebCore/platform/URL.h	2018-02-09 17:26:47 UTC (rev 228322)
+++ trunk/Source/WebCore/platform/URL.h	2018-02-09 17:32:57 UTC (rev 228323)
@@ -175,6 +175,8 @@
     unsigned hostStart() const;
     unsigned hostEnd() const;
 
+    WEBCORE_EXPORT static bool hostIsIPAddress(const String&);
+
     unsigned pathStart() const;
     unsigned pathEnd() const;
     unsigned pathAfterLastSlash() const;

Modified: trunk/Source/WebCore/platform/mac/PublicSuffixMac.mm (228322 => 228323)


--- trunk/Source/WebCore/platform/mac/PublicSuffixMac.mm	2018-02-09 17:26:47 UTC (rev 228322)
+++ trunk/Source/WebCore/platform/mac/PublicSuffixMac.mm	2018-02-09 17:32:57 UTC (rev 228323)
@@ -28,13 +28,10 @@
 
 #if ENABLE(PUBLIC_SUFFIX_LIST)
 
+#import "URL.h"
 #import "WebCoreNSURLExtras.h"
 #import <pal/spi/cf/CFNetworkSPI.h>
 
-@interface NSString (WebCoreNSURLExtras)
-- (BOOL)_web_looksLikeIPAddress;
-@end
-
 namespace WebCore {
 
 bool isPublicSuffix(const String& domain)
@@ -45,7 +42,7 @@
 
 String topPrivatelyControlledDomain(const String& domain)
 {
-    if ([domain _web_looksLikeIPAddress])
+    if (URL::hostIsIPAddress(domain))
         return domain;
 
     if (!domain.isAllASCII())

Modified: trunk/Source/WebCore/platform/mac/URLMac.mm (228322 => 228323)


--- trunk/Source/WebCore/platform/mac/URLMac.mm	2018-02-09 17:26:47 UTC (rev 228322)
+++ trunk/Source/WebCore/platform/mac/URLMac.mm	2018-02-09 17:32:57 UTC (rev 228323)
@@ -28,9 +28,14 @@
 
 #import "CFURLExtras.h"
 #import "URLParser.h"
+#import "WebCoreNSURLExtras.h"
 #import <wtf/ObjcRuntimeExtras.h>
 #import <wtf/text/CString.h>
 
+@interface NSString (WebCoreNSURLExtras)
+- (BOOL)_web_looksLikeIPAddress;
+@end
+
 namespace WebCore {
 
 URL::URL(NSURL *url)
@@ -74,4 +79,9 @@
     return createCFURLFromBuffer(buffer.data(), buffer.size());
 }
 
+bool URL::hostIsIPAddress(const String& host)
+{
+    return [host _web_looksLikeIPAddress];
 }
+
+}

Modified: trunk/Source/WebCore/platform/network/curl/CookieUtil.cpp (228322 => 228323)


--- trunk/Source/WebCore/platform/network/curl/CookieUtil.cpp	2018-02-09 17:26:47 UTC (rev 228322)
+++ trunk/Source/WebCore/platform/network/curl/CookieUtil.cpp	2018-02-09 17:32:57 UTC (rev 228323)
@@ -53,8 +53,7 @@
 
 bool isIPAddress(const String& hostname)
 {
-    // Assuming that hosts ending in a digit are IP Addresses
-    return !hostname.isEmpty() && isASCIIDigit(hostname[hostname.length() - 1]);
+    return URL::hostIsIPAddress(hostname);
 }
 
 bool domainMatch(const String& cookieDomain, const String& host)

Modified: trunk/Source/WebCore/platform/soup/URLSoup.cpp (228322 => 228323)


--- trunk/Source/WebCore/platform/soup/URLSoup.cpp	2018-02-09 17:26:47 UTC (rev 228322)
+++ trunk/Source/WebCore/platform/soup/URLSoup.cpp	2018-02-09 17:32:57 UTC (rev 228323)
@@ -66,6 +66,11 @@
     return GUniquePtr<SoupURI>(soup_uri_new(string().utf8().data()));
 }
 
+bool URL::hostIsIPAddress(const String& host)
+{
+    return !host.isEmpty() && g_hostname_is_ip_address(host.utf8().data());
+}
+
 } // namespace WebCore
 
 #endif

Modified: trunk/Tools/ChangeLog (228322 => 228323)


--- trunk/Tools/ChangeLog	2018-02-09 17:26:47 UTC (rev 228322)
+++ trunk/Tools/ChangeLog	2018-02-09 17:32:57 UTC (rev 228323)
@@ -1,3 +1,15 @@
+2018-02-09  Carlos Garcia Campos  <cgar...@igalia.com>
+
+        Add a way to check if a host is an IP address
+        https://bugs.webkit.org/show_bug.cgi?id=182427
+
+        Reviewed by Alex Christensen.
+
+        Add unit test for URL::hostIsIPAddress().
+
+        * TestWebKitAPI/Tests/WebCore/URL.cpp:
+        (TestWebKitAPI::TEST_F):
+
 2018-02-09  Philippe Normand  <pnorm...@igalia.com>
 
         [GStreamer] Layout test fast/replaced/border-radius-clip.html crashes with GStreamer-CRITICAL **: gst_segment_to_stream_time: assertion 'segment->format == format' failed in gst_segment_to_stream_time()

Modified: trunk/Tools/TestWebKitAPI/Tests/WebCore/URL.cpp (228322 => 228323)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/URL.cpp	2018-02-09 17:26:47 UTC (rev 228322)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/URL.cpp	2018-02-09 17:32:57 UTC (rev 228323)
@@ -231,4 +231,37 @@
     EXPECT_TRUE(protocolIsInHTTPFamily("https://!@#$%^&*()"));
 }
 
+TEST_F(URLTest, HostIsIPAddress)
+{
+    EXPECT_FALSE(URL::hostIsIPAddress({ }));
+    EXPECT_FALSE(URL::hostIsIPAddress(""));
+    EXPECT_FALSE(URL::hostIsIPAddress("localhost"));
+    EXPECT_FALSE(URL::hostIsIPAddress("127.localhost"));
+    EXPECT_FALSE(URL::hostIsIPAddress("localhost.127"));
+    EXPECT_FALSE(URL::hostIsIPAddress("127.0.0"));
+    EXPECT_FALSE(URL::hostIsIPAddress("127.0 .0.1"));
+    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("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("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_TRUE(URL::hostIsIPAddress("127.0.0.1"));
+    EXPECT_TRUE(URL::hostIsIPAddress("123.45.67.89"));
+    EXPECT_TRUE(URL::hostIsIPAddress("0.0.0.0"));
+    EXPECT_TRUE(URL::hostIsIPAddress("::1"));
+    EXPECT_TRUE(URL::hostIsIPAddress("::"));
+    EXPECT_TRUE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:ba98:FeDc"));
+    EXPECT_TRUE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:ba98::"));
+    EXPECT_TRUE(URL::hostIsIPAddress("::4567:89AB:cdef:3210:7654:ba98:FeDc"));
+    EXPECT_TRUE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:123.45.67.89"));
+    EXPECT_TRUE(URL::hostIsIPAddress("::123.45.67.89"));
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to