Changeset 228261 in webkit


Ignore:
Timestamp:
Feb 7, 2018 10:37:10 PM (6 years ago)
Author:
Carlos Garcia Campos
Message:

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.

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):

Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r228260 r228261  
     12018-02-05  Carlos Garcia Campos  <cgarcia@igalia.com>
     2
     3        Add a way to check if a host is an IP address
     4        https://bugs.webkit.org/show_bug.cgi?id=182427
     5
     6        Reviewed by Alex Christensen.
     7
     8        There are several places where this is needed. We currently just assume that any host ending in a digit is an IP
     9        address, except in PublicSuffix where platform specific code is used. This patch adds URL::hostIsIPAddress()
     10        platform specific implementations, falling back to current assumption if there isn't an implementation for the
     11        platform.
     12
     13        * page/OriginAccessEntry.cpp:
     14        (WebCore::OriginAccessEntry::OriginAccessEntry): Use URL::hostIsIPAddress().
     15        * platform/URL.cpp:
     16        (WebCore::URL::hostIsIPAddress): Fallback implementation.
     17        * platform/URL.h:
     18        * platform/mac/PublicSuffixMac.mm:
     19        (WebCore::topPrivatelyControlledDomain): Use URL::hostIsIPAddress().
     20        * platform/mac/URLMac.mm:
     21        (WebCore::URL::hostIsIPAddress): Move implementation from PublicSuffixMac.mm.
     22        * platform/network/curl/CookieUtil.cpp:
     23        (WebCore::CookieUtil::isIPAddress): Use URL::hostIsIPAddress().
     24        * platform/soup/URLSoup.cpp:
     25        (WebCore::URL::hostIsIPAddress): Use g_hostname_is_ip_address().
     26
    1272018-01-13  Darin Adler  <darin@apple.com>
    228
  • trunk/Source/WebCore/page/OriginAccessEntry.cpp

    r228118 r228261  
    4141    , m_subdomainSettings(subdomainSetting)
    4242    , m_ipAddressSettings(ipAddressSetting)
     43    , m_hostIsIPAddress(URL::hostIsIPAddress(m_host))
    4344{
    4445    ASSERT(subdomainSetting == AllowSubdomains || subdomainSetting == DisallowSubdomains);
    45 
    46     // Assume that any host that ends with a digit is trying to be an IP address.
    47     m_hostIsIPAddress = !m_host.isEmpty() && isASCIIDigit(m_host[m_host.length() - 1]);
    4846}
    4947
     
    7068    // IP addresses are not domains: https://url.spec.whatwg.org/#concept-domain
    7169    // Don't try to do subdomain matching on IP addresses.
    72     if (m_hostIsIPAddress && m_ipAddressSettings == TreatIPAddressAsIPAddress)
     70    if (m_ipAddressSettings == TreatIPAddressAsIPAddress && (m_hostIsIPAddress || URL::hostIsIPAddress(origin.host())))
    7371        return false;
    7472   
  • trunk/Source/WebCore/platform/URL.cpp

    r228118 r228261  
    10361036}
    10371037
     1038#if !PLATFORM(COCOA) && !USE(SOUP)
     1039bool URL::hostIsIPAddress(const String& host)
     1040{
     1041    // Assume that any host that ends with a digit is trying to be an IP address.
     1042    return !host.isEmpty() && isASCIIDigit(host[host.length() - 1]);
     1043}
     1044#endif
     1045
    10381046} // namespace WebCore
  • trunk/Source/WebCore/platform/URL.h

    r228118 r228261  
    176176    unsigned hostEnd() const;
    177177
     178    WEBCORE_EXPORT static bool hostIsIPAddress(const String&);
     179
    178180    unsigned pathStart() const;
    179181    unsigned pathEnd() const;
  • trunk/Source/WebCore/platform/mac/PublicSuffixMac.mm

    r228118 r228261  
    2929#if ENABLE(PUBLIC_SUFFIX_LIST)
    3030
    31 #import "WebCoreNSURLExtras.h"
     31#import "URL.h"
    3232#import <pal/spi/cf/CFNetworkSPI.h>
    33 
    34 @interface NSString (WebCoreNSURLExtras)
    35 - (BOOL)_web_looksLikeIPAddress;
    36 @end
    3733
    3834namespace WebCore {
     
    4642String topPrivatelyControlledDomain(const String& domain)
    4743{
    48     if ([domain _web_looksLikeIPAddress])
     44    if (URL::hostIsIPAddress(domain))
    4945        return domain;
    5046
  • trunk/Source/WebCore/platform/mac/URLMac.mm

    r228118 r228261  
    2929#import "CFURLExtras.h"
    3030#import "URLParser.h"
     31#import "WebCoreNSURLExtras.h"
    3132#import <wtf/ObjcRuntimeExtras.h>
    3233#import <wtf/text/CString.h>
     34
     35@interface NSString (WebCoreNSURLExtras)
     36- (BOOL)_web_looksLikeIPAddress;
     37@end
    3338
    3439namespace WebCore {
     
    7580}
    7681
     82bool URL::hostIsIPAddress(const String& host)
     83{
     84    return [host _web_looksLikeIPAddress];
    7785}
     86
     87}
  • trunk/Source/WebCore/platform/network/curl/CookieUtil.cpp

    r228118 r228261  
    5454bool isIPAddress(const String& hostname)
    5555{
    56     // Assuming that hosts ending in a digit are IP Addresses
    57     return !hostname.isEmpty() && isASCIIDigit(hostname[hostname.length() - 1]);
     56    return URL::hostIsIPAddress(hostname);
    5857}
    5958
  • trunk/Source/WebCore/platform/soup/URLSoup.cpp

    r228118 r228261  
    6767}
    6868
     69bool URL::hostIsIPAddress(const String& host)
     70{
     71    return !host.isEmpty() && g_hostname_is_ip_address(host.utf8().data());
     72}
     73
    6974} // namespace WebCore
    7075
  • trunk/Tools/ChangeLog

    r228253 r228261  
     12018-02-05  Carlos Garcia Campos  <cgarcia@igalia.com>
     2
     3        Add a way to check if a host is an IP address
     4        https://bugs.webkit.org/show_bug.cgi?id=182427
     5
     6        Reviewed by Alex Christensen.
     7
     8        Add unit test for URL::hostIsIPAddress().
     9
     10        * TestWebKitAPI/Tests/WebCore/URL.cpp:
     11        (TestWebKitAPI::TEST_F):
     12
    1132018-02-07  Tim Horton  <timothy_horton@apple.com>
    214
  • trunk/Tools/TestWebKitAPI/Tests/WebCore/URL.cpp

    r228118 r228261  
    232232}
    233233
     234TEST_F(URLTest, HostIsIPAddress)
     235{
     236    EXPECT_FALSE(URL::hostIsIPAddress({ }));
     237    EXPECT_FALSE(URL::hostIsIPAddress(""));
     238    EXPECT_FALSE(URL::hostIsIPAddress("localhost"));
     239    EXPECT_FALSE(URL::hostIsIPAddress("127.localhost"));
     240    EXPECT_FALSE(URL::hostIsIPAddress("localhost.127"));
     241    EXPECT_FALSE(URL::hostIsIPAddress("127.0.0"));
     242    EXPECT_FALSE(URL::hostIsIPAddress("127.0 .0.1"));
     243    EXPECT_FALSE(URL::hostIsIPAddress(" 127.0.0.1"));
     244    EXPECT_FALSE(URL::hostIsIPAddress("127..0.0.1"));
     245    EXPECT_FALSE(URL::hostIsIPAddress("127.0.0."));
     246    EXPECT_FALSE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:ba98"));
     247    EXPECT_FALSE(URL::hostIsIPAddress("012x:4567:89AB:cdef:3210:7654:ba98:FeDc"));
     248#if !PLATFORM(COCOA)
     249    // FIXME: This fails in Mac.
     250    EXPECT_FALSE(URL::hostIsIPAddress("00123:4567:89AB:cdef:3210:7654:ba98:FeDc"));
     251#endif
     252    EXPECT_FALSE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:123.45.67.89"));
     253    EXPECT_FALSE(URL::hostIsIPAddress(":::"));
     254
     255    EXPECT_TRUE(URL::hostIsIPAddress("127.0.0.1"));
     256    EXPECT_TRUE(URL::hostIsIPAddress("123.45.67.89"));
     257    EXPECT_TRUE(URL::hostIsIPAddress("0.0.0.0"));
     258    EXPECT_TRUE(URL::hostIsIPAddress("::1"));
     259    EXPECT_TRUE(URL::hostIsIPAddress("::"));
     260    EXPECT_TRUE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:ba98:FeDc"));
     261    EXPECT_TRUE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:ba98::"));
     262    EXPECT_TRUE(URL::hostIsIPAddress("::4567:89AB:cdef:3210:7654:ba98:FeDc"));
     263    EXPECT_TRUE(URL::hostIsIPAddress("0123:4567:89AB:cdef:3210:7654:123.45.67.89"));
     264    EXPECT_TRUE(URL::hostIsIPAddress("::123.45.67.89"));
     265}
     266
    234267} // namespace TestWebKitAPI
Note: See TracChangeset for help on using the changeset viewer.