Changeset 195650 in webkit


Ignore:
Timestamp:
Jan 26, 2016 7:05:49 PM (8 years ago)
Author:
commit-queue@webkit.org
Message:

Source/WebKit2:
Implement wildcard matching for plug-in policy host.
https://bugs.webkit.org/show_bug.cgi?id=153090

Patch by I-Ting Liu <iting_liu@apple.com> on 2016-01-26
Reviewed by Darin Adler.

WebPlatformStrategies decides the plug-in load policy for a host by looking
for a matched hostname in the list of plug-in policies sent by Safari. This
patch adds support for wildcard matching -- if there's a policy with hostname
"*.example.com," the policy for "foo.example.com" would be replaced with that
of "*.example.com" if there's no policy for this hostname. If there is more
than one wildcard hostname, the host with the longest wildcard hostname will
be used.

This patch adds a helper function in StringUtilites that matches a string to
another string, which may contain wildcard ('*') characters.

  • Platform/mac/StringUtilities.h:
  • Add WebKit::stringMatchesWildcardString.
  • Remove #if ENABLE(TELEPHONE_NUMBER_DETECTION) && PLATFORM(MAC) flag so that

the compiler allows exposing StringUtilities.h.

  • Add #if OBJC to allow the file to be included in WebPltformStrategies.cpp
  • Platform/mac/StringUtilities.mm:

(WebKit::stringMatchesWildcardString):
Return true if the entire first given String matches the second. The second string
may contain wildcard characters.
(WebKit::wildcardRegexPatternString):
Return the regex expression from a wildcard string by replacing the wildcard
character ('*') with (".*") and escaping regular expression metacharacters.
(WebKit::formattedPhoneNumberString):
To expose StringUtilities.h for tests, we removed #if ENABLE(TELEPHONE_NUMBER_DETECTION)
&& PLATFORM(MAC) flag in the header. Add a function that returns nil for
#if !(ENABLE(TELEPHONE_NUMBER_DETECTION) && PLATFORM(MAC)) to fix the removal
of the flag in the header file.

  • WebKit2.xcodeproj/project.pbxproj:

Change the file attribute of StringUtilities.h from Project to Private for
testing in TestWebKitAPI.

  • WebProcess/WebCoreSupport/WebPlatformStrategies.cpp:

(WebKit::WebPlatformStrategies::longestMatchedWildcardHostForHost):
Return the wildcard hostname whose matched substring with the host is the longest.
(WebKit::WebPlatformStrategies::replaceHostWithMatchedWildcardHost):
Replace the look-up host with a matched wildcard host if there is a match and that
the matched wildcard host's plug-in identifier is the same as that of the host.
(WebKit::WebPlatformStrategies::pluginLoadClientPolicyForHost):
Try to replace the look-up host with the wildcard host if there's no policy for
the host. Restructure the function to reduce hashmap lookup.

  • WebProcess/WebCoreSupport/WebPlatformStrategies.h:

Declare the methods.

Source/WTF:
Implement wildcard matching for plug-in policy host.
https://bugs.webkit.org/show_bug.cgi?id=153090

Patch by I-Ting Liu <iting_liu@apple.com> on 2016-01-26
Reviewed by Darin Adler.

  • wtf/text/AtomicString.h:

(WTF::AtomicString::AtomicString):
Add bridge to allow compilation.

Tools:
Add a test for WebKit::stringMatchesWildcardString.
https://bugs.webkit.org/show_bug.cgi?id=153090

Patch by I-Ting Liu <iting_liu@apple.com> on 2016-01-26
Reviewed by Darin Adler.

  • TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:

Add the file to the project.

  • TestWebKitAPI/Tests/WebKit2/mac/StringUtilities.mm: Added.

(TestWebKitAPI::TEST):
Test that a string matches another string that may contain wildcard characters.

Location:
trunk
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r195644 r195650  
     12016-01-26  I-Ting Liu  <iting_liu@apple.com>
     2
     3        Implement wildcard matching for plug-in policy host.
     4        https://bugs.webkit.org/show_bug.cgi?id=153090
     5
     6        Reviewed by Darin Adler.
     7
     8        * wtf/text/AtomicString.h:
     9        (WTF::AtomicString::AtomicString):
     10        Add __bridge to allow compilation.
     11
    1122016-01-26  Joseph Pecoraro  <pecoraro@apple.com>
    213
  • trunk/Source/WTF/wtf/text/AtomicString.h

    r195501 r195650  
    298298#ifdef __OBJC__
    299299inline AtomicString::AtomicString(NSString* s)
    300     : m_string(AtomicStringImpl::add((CFStringRef)s))
     300    : m_string(AtomicStringImpl::add((__bridge CFStringRef)s))
    301301{
    302302}
  • trunk/Source/WebKit2/ChangeLog

    r195647 r195650  
     12016-01-26  I-Ting Liu  <iting_liu@apple.com>
     2
     3        Implement wildcard matching for plug-in policy host.
     4        https://bugs.webkit.org/show_bug.cgi?id=153090
     5
     6        Reviewed by Darin Adler.
     7
     8        WebPlatformStrategies decides the plug-in load policy for a host by looking
     9        for a matched hostname in the list of plug-in policies sent by Safari. This
     10        patch adds support for wildcard matching -- if there's a policy with hostname
     11        "*.example.com," the policy for "foo.example.com" would be replaced with that
     12        of "*.example.com" if there's no policy for this hostname. If there is more
     13        than one wildcard hostname, the host with the longest wildcard hostname will
     14        be used.
     15
     16        This patch adds a helper function in StringUtilites that matches a string to
     17        another string, which may contain wildcard ('*') characters.
     18
     19        * Platform/mac/StringUtilities.h:
     20        - Add WebKit::stringMatchesWildcardString.
     21        - Remove #if ENABLE(TELEPHONE_NUMBER_DETECTION) && PLATFORM(MAC) flag so that
     22        the compiler allows exposing StringUtilities.h.
     23        - Add #if __OBJC__ to allow the file to be included in WebPltformStrategies.cpp
     24
     25        * Platform/mac/StringUtilities.mm:
     26        (WebKit::stringMatchesWildcardString):
     27        Return true if the entire first given String matches the second. The second string
     28        may contain wildcard characters.
     29        (WebKit::wildcardRegexPatternString):
     30        Return the regex expression from a wildcard string by replacing the wildcard
     31        character ('*') with (".*") and escaping regular expression metacharacters.
     32        (WebKit::formattedPhoneNumberString):
     33        To expose StringUtilities.h for tests, we removed #if ENABLE(TELEPHONE_NUMBER_DETECTION)
     34        && PLATFORM(MAC) flag in the header. Add a function that returns nil for
     35        #if !(ENABLE(TELEPHONE_NUMBER_DETECTION) && PLATFORM(MAC)) to fix the removal
     36        of the flag in the header file.
     37
     38        * WebKit2.xcodeproj/project.pbxproj:
     39        Change the file attribute of StringUtilities.h from Project to Private for
     40        testing in TestWebKitAPI.
     41
     42        * WebProcess/WebCoreSupport/WebPlatformStrategies.cpp:
     43        (WebKit::WebPlatformStrategies::longestMatchedWildcardHostForHost):
     44        Return the wildcard hostname whose matched substring with the host is the longest.
     45        (WebKit::WebPlatformStrategies::replaceHostWithMatchedWildcardHost):
     46        Replace the look-up host with a matched wildcard host if there is a match and that
     47        the matched wildcard host's plug-in identifier is the same as that of the host.
     48        (WebKit::WebPlatformStrategies::pluginLoadClientPolicyForHost):
     49        Try to replace the look-up host with the wildcard host if there's no policy for
     50        the host. Restructure the function to reduce hashmap lookup.
     51
     52        * WebProcess/WebCoreSupport/WebPlatformStrategies.h:
     53        Declare the methods.
     54
    1552016-01-26  Anders Carlsson  <andersca@apple.com>
    256
  • trunk/Source/WebKit2/Platform/mac/StringUtilities.h

    r194318 r195650  
    2727#define StringUtilities_h
    2828
     29#import <WebKit/WKDeclarationSpecifiers.h>
    2930#import <wtf/Forward.h>
    3031
    3132namespace WebKit {
    3233
     34#ifdef __OBJC__
     35
    3336// NOTE: This does not use String::operator NSString*() since that function
    3437// expects to be called on the thread running WebCore.
    3538NSString *nsStringFromWebCoreString(const String&);
     39NSString *formattedPhoneNumberString(NSString *originalPhoneNumber);
    3640
    37 #if ENABLE(TELEPHONE_NUMBER_DETECTION) && PLATFORM(MAC)
    38 NSString *formattedPhoneNumberString(NSString *originalPhoneNumber);
    39 #endif
     41#endif // defined(__OBJC__)
     42
     43WK_EXPORT bool stringMatchesWildcardString(const String& stringToBeMatched, const String& wildcardString);
    4044
    4145}
  • trunk/Source/WebKit2/Platform/mac/StringUtilities.mm

    r194318 r195650  
    3131#import <WebCore/SoftLinking.h>
    3232#import <wtf/ObjcRuntimeExtras.h>
    33 #import <wtf/text/WTFString.h>
     33#import <wtf/text/StringBuilder.h>
     34#import <yarr/RegularExpression.h>
    3435
    3536namespace WebKit {
     
    3839{
    3940    return string.isEmpty() ? @"" : CFBridgingRelease(WKStringCopyCFString(0, toAPI(string.impl())));
     41}
     42
     43static String wildcardRegexPatternString(const String& string)
     44{
     45    String metaCharacters = ".|+?()[]{}^$";
     46    UChar escapeCharacter = '\\';
     47    UChar wildcardCharacter = '*';
     48
     49    StringBuilder stringBuilder;
     50
     51    stringBuilder.append('^');
     52    for (unsigned i = 0; i < string.length(); i++) {
     53        auto character = string[i];
     54        if (metaCharacters.contains(character) || character == escapeCharacter)
     55            stringBuilder.append(escapeCharacter);
     56        else if (character == wildcardCharacter)
     57            stringBuilder.append('.');
     58
     59        stringBuilder.append(character);
     60    }
     61    stringBuilder.append('$');
     62
     63    return stringBuilder.toString();
     64}
     65
     66bool stringMatchesWildcardString(const String& string, const String& wildcardString)
     67{
     68    return JSC::Yarr::RegularExpression(wildcardRegexPatternString(wildcardString), TextCaseInsensitive).match(string) != -1;
    4069}
    4170
     
    73102}
    74103
     104#else
     105
     106NSString *formattedPhoneNumberString(NSString *)
     107{
     108    return nil;
     109}
     110
    75111#endif // ENABLE(TELEPHONE_NUMBER_DETECTION) && PLATFORM(MAC)
    76112
  • trunk/Source/WebKit2/WebKit2.xcodeproj/project.pbxproj

    r195647 r195650  
    580580                293EBEAC1627D9C9005F89F1 /* WKDOMText.mm in Sources */ = {isa = PBXBuildFile; fileRef = 293EBEAA1627D9C9005F89F1 /* WKDOMText.mm */; };
    581581                29501724162A4504004A9D71 /* WKWebProcessPlugInBrowserContextControllerPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = 29501723162A4504004A9D71 /* WKWebProcessPlugInBrowserContextControllerPrivate.h */; settings = {ATTRIBUTES = (Private, ); }; };
    582                 296BD85D15019BC30071F424 /* StringUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = 296BD85B15019BC30071F424 /* StringUtilities.h */; };
     582                296BD85D15019BC30071F424 /* StringUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = 296BD85B15019BC30071F424 /* StringUtilities.h */; settings = {ATTRIBUTES = (Private, ); }; };
    583583                296BD85E15019BC30071F424 /* StringUtilities.mm in Sources */ = {isa = PBXBuildFile; fileRef = 296BD85C15019BC30071F424 /* StringUtilities.mm */; };
    584584                2984F57C164B915F004BC0C6 /* CustomProtocolManagerProxyMessageReceiver.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2984F57A164B915F004BC0C6 /* CustomProtocolManagerProxyMessageReceiver.cpp */; };
  • trunk/Source/WebKit2/WebProcess/WebCoreSupport/WebPlatformStrategies.cpp

    r194367 r195650  
    6868#include <wtf/Atomics.h>
    6969
     70#if PLATFORM(MAC)
     71#include "StringUtilities.h"
     72#endif
     73
    7074using namespace WebCore;
    7175
     
    240244#if ENABLE(NETSCAPE_PLUGIN_API)
    241245#if PLATFORM(MAC)
     246
     247String WebPlatformStrategies::longestMatchedWildcardHostForHost(const String& host) const
     248{
     249    String longestMatchedHost;
     250
     251    for (auto& key : m_hostsToPluginIdentifierData.keys()) {
     252        if (key.contains('*') && key != "*" && stringMatchesWildcardString(host, key)) {
     253            if (key.length() > longestMatchedHost.length())
     254                longestMatchedHost = key;
     255            else if (key.length() == longestMatchedHost.length() && codePointCompareLessThan(key, longestMatchedHost))
     256                longestMatchedHost = key;
     257        }
     258    }
     259
     260    return longestMatchedHost;
     261}
     262
     263bool WebPlatformStrategies::replaceHostWithMatchedWildcardHost(String& host, const String& identifier) const
     264{
     265    String matchedWildcardHost = longestMatchedWildcardHostForHost(host);
     266
     267    if (matchedWildcardHost.isNull())
     268        return false;
     269
     270    auto plugInIdentifierData = m_hostsToPluginIdentifierData.find(matchedWildcardHost);
     271    if (plugInIdentifierData == m_hostsToPluginIdentifierData.end() || !plugInIdentifierData->value.contains(identifier))
     272        return false;
     273
     274    host = matchedWildcardHost;
     275    return true;
     276}
     277
    242278bool WebPlatformStrategies::pluginLoadClientPolicyForHost(const String& host, const PluginInfo& info, PluginLoadClientPolicy& policy) const
    243279{
    244280    String hostToLookUp = host;
    245     if (!m_hostsToPluginIdentifierData.contains(hostToLookUp))
    246         hostToLookUp = "*";
    247     if (!m_hostsToPluginIdentifierData.contains(hostToLookUp))
    248         return false;
    249 
    250     PluginPolicyMapsByIdentifier policiesByIdentifier = m_hostsToPluginIdentifierData.get(hostToLookUp);
    251281    String identifier = info.bundleIdentifier;
    252     if (!identifier || !policiesByIdentifier.contains(identifier))
     282
     283    auto policiesByIdentifierIterator = m_hostsToPluginIdentifierData.find(hostToLookUp);
     284
     285    if (!identifier.isNull() && policiesByIdentifierIterator == m_hostsToPluginIdentifierData.end()) {
     286        if (!replaceHostWithMatchedWildcardHost(hostToLookUp, identifier))
     287            hostToLookUp = "*";
     288        policiesByIdentifierIterator = m_hostsToPluginIdentifierData.find(hostToLookUp);
     289        if (hostToLookUp != "*" && policiesByIdentifierIterator == m_hostsToPluginIdentifierData.end()) {
     290            hostToLookUp = "*";
     291            policiesByIdentifierIterator = m_hostsToPluginIdentifierData.find(hostToLookUp);
     292        }
     293    }
     294    if (policiesByIdentifierIterator == m_hostsToPluginIdentifierData.end())
     295        return false;
     296
     297    auto& policiesByIdentifier = policiesByIdentifierIterator->value;
     298
     299    if (!identifier)
    253300        identifier = "*";
    254     if (!policiesByIdentifier.contains(identifier))
    255         return false;
    256 
    257     PluginLoadClientPoliciesByBundleVersion versionsToPolicies = policiesByIdentifier.get(identifier);
     301
     302    auto identifierPolicyIterator = policiesByIdentifier.find(identifier);
     303    if (identifier != "*" && identifierPolicyIterator == policiesByIdentifier.end()) {
     304        identifier = "*";
     305        identifierPolicyIterator = policiesByIdentifier.find(identifier);
     306    }
     307    if (identifierPolicyIterator == policiesByIdentifier.end())
     308        return false;
     309
     310    auto& versionsToPolicies = identifierPolicyIterator->value;
     311
    258312    String version = info.versionString;
    259     if (!version || !versionsToPolicies.contains(version))
     313    if (!version)
    260314        version = "*";
    261     if (!versionsToPolicies.contains(version))
    262         return false;
    263 
    264     policy = versionsToPolicies.get(version);
     315    auto policyIterator = versionsToPolicies.find(version);
     316    if (version != "*" && policyIterator == versionsToPolicies.end()) {
     317        version = "*";
     318        policyIterator = versionsToPolicies.find(version);
     319    }
     320
     321    if (policyIterator == versionsToPolicies.end())
     322        return false;
     323
     324    policy = policyIterator->value;
    265325    return true;
    266326}
  • trunk/Source/WebKit2/WebProcess/WebCoreSupport/WebPlatformStrategies.h

    r192995 r195650  
    113113    HashMap<String, PluginPolicyMapsByIdentifier> m_hostsToPluginIdentifierData;
    114114    bool pluginLoadClientPolicyForHost(const String&, const WebCore::PluginInfo&, WebCore::PluginLoadClientPolicy&) const;
     115    String longestMatchedWildcardHostForHost(const String& host) const;
     116    bool replaceHostWithMatchedWildcardHost(String& host, const String& identifier) const;
    115117#endif // PLATFORM(MAC)
    116118#endif // ENABLE(NETSCAPE_PLUGIN_API)
  • trunk/Tools/ChangeLog

    r195617 r195650  
     12016-01-26  I-Ting Liu  <iting_liu@apple.com>
     2
     3        Add a test for WebKit::stringMatchesWildcardString.
     4        https://bugs.webkit.org/show_bug.cgi?id=153090
     5
     6        Reviewed by Darin Adler.
     7
     8        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
     9        Add the file to the project.
     10
     11        * TestWebKitAPI/Tests/WebKit2/mac/StringUtilities.mm: Added.
     12        (TestWebKitAPI::TEST):
     13        Test that a string matches another string that may contain wildcard characters.
     14
    1152016-01-26  Konstantin Tokarev  <annulen@yandex.ru>
    216
  • trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj

    r194950 r195650  
    261261                7CEFA9661AC0B9E200B910FD /* _WKUserContentExtensionStore.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7CEFA9641AC0B9E200B910FD /* _WKUserContentExtensionStore.mm */; };
    262262                7CFBCAE51743238F00B2BFCF /* WillLoad_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CFBCAE31743238E00B2BFCF /* WillLoad_Bundle.cpp */; };
     263                83CF1C301C4F1B8B00688447 /* StringUtilities.mm in Sources */ = {isa = PBXBuildFile; fileRef = 83CF1C2C1C4F19AE00688447 /* StringUtilities.mm */; };
    263264                930AD402150698D00067970F /* lots-of-text.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 930AD401150698B30067970F /* lots-of-text.html */; };
    264265                9361002914DC95A70061379D /* lots-of-iframes.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 9361002814DC957B0061379D /* lots-of-iframes.html */; };
     
    629630                7CFBCAE31743238E00B2BFCF /* WillLoad_Bundle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WillLoad_Bundle.cpp; sourceTree = "<group>"; };
    630631                81B50192140F232300D9EB58 /* StringBuilder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StringBuilder.cpp; sourceTree = "<group>"; };
     632                83CF1C2C1C4F19AE00688447 /* StringUtilities.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = StringUtilities.mm; sourceTree = "<group>"; };
    631633                86BD19971A2DB05B006DCF0A /* RefCounter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RefCounter.cpp; sourceTree = "<group>"; };
    632634                8A2C750D16CED9550024F352 /* ResizeWindowAfterCrash.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ResizeWindowAfterCrash.cpp; sourceTree = "<group>"; };
     
    14501452                                C0C5D3BD14598B6F00A802A6 /* GetBackingScaleFactor_Bundle.mm */,
    14511453                                1AEF994817A09F5300998EF0 /* GetPIDAfterAbortedProcessLaunch.cpp */,
     1454                                83CF1C2C1C4F19AE00688447 /* StringUtilities.mm */,
    14521455                        );
    14531456                        path = mac;
     
    17411744                                7CCE7F0D1A411AE600447C4C /* ReloadPageAfterCrash.cpp in Sources */,
    17421745                                7CCE7EC91A411A7E00447C4C /* RenderedImageFromDOMNode.mm in Sources */,
     1746                                83CF1C301C4F1B8B00688447 /* StringUtilities.mm in Sources */,
    17431747                                7CCE7ECA1A411A7E00447C4C /* RenderedImageFromDOMRange.mm in Sources */,
    17441748                                51CD1C6C1B38CE4300142CA5 /* ModalAlerts.mm in Sources */,
Note: See TracChangeset for help on using the changeset viewer.