Changeset 251814 in webkit


Ignore:
Timestamp:
Oct 30, 2019 3:39:31 PM (4 years ago)
Author:
achristensen@apple.com
Message:

WKContentRuleLists should block requests from service workers
https://bugs.webkit.org/show_bug.cgi?id=201980
<rdar://problem/55516735>

Reviewed by Chris Dumez.

Source/WebKit:

Test: http/tests/contentextensions/service-worker.https.html

Also covered by an API test.

  • Shared/ServiceWorkerInitializationData.cpp: Added.

(WebKit::ServiceWorkerInitializationData::encode const):
(WebKit::ServiceWorkerInitializationData::decode):

  • Shared/ServiceWorkerInitializationData.h: Added.
  • Sources.txt:
  • UIProcess/UserContent/WebUserContentControllerProxy.cpp:

(WebKit::WebUserContentControllerProxy::addProcess):
(WebKit::WebUserContentControllerProxy::contentRuleListData):

  • UIProcess/UserContent/WebUserContentControllerProxy.h:
  • UIProcess/WebProcessPool.cpp:

(WebKit::WebProcessPool::establishWorkerContextConnectionToNetworkProcess):
(WebKit::WebProcessPool::createWebPage):

  • UIProcess/WebProcessPool.h:
  • UIProcess/WebProcessProxy.cpp:

(WebKit::WebProcessProxy::createForServiceWorkers):
(WebKit::WebProcessProxy::establishServiceWorkerContext):
(WebKit::contentRuleListsFromIdentifier):
(WebKit::WebProcessProxy::enableServiceWorkers):

  • UIProcess/WebProcessProxy.h:
  • WebKit.xcodeproj/project.pbxproj:
  • WebProcess/Storage/WebSWContextManagerConnection.cpp:

(WebKit::WebSWContextManagerConnection::WebSWContextManagerConnection):
(WebKit::m_userAgent):
(WebKit::WebSWContextManagerConnection::installServiceWorker):

  • WebProcess/Storage/WebSWContextManagerConnection.h:
  • WebProcess/WebProcess.cpp:

(WebKit::WebProcess::establishWorkerContextConnectionToNetworkProcess):

  • WebProcess/WebProcess.h:
  • WebProcess/WebProcess.messages.in:

Tools:

NSString initWithContentsOfURL doesn't work with https URLs with certificates without a trusted root,
so I use an ephemeral NSURLSession instead so I can tell it to accept any connection, even our WebKit httpd server.
I also added an API test.

  • TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm:
  • WebKitTestRunner/mac/TestControllerMac.mm:

(-[WKTRSessionDelegate URLSession:task:didReceiveChallenge:completionHandler:]):
(WTR::TestController::configureContentExtensionForTest):

LayoutTests:

  • http/tests/contentextensions/resources/fetch-worker.js: Added.

(event.fetch.string_appeared_here.then):
(event.catch):

  • http/tests/contentextensions/resources/serviceworkertest.js: Added.

(testServiceWorker):
(test):

  • http/tests/contentextensions/service-worker.https-expected.txt: Added.
  • http/tests/contentextensions/service-worker.https.html: Added.
  • http/tests/contentextensions/service-worker.https.html.json: Added.
Location:
trunk
Files:
7 added
18 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r251811 r251814  
     12019-10-30  Alex Christensen  <achristensen@webkit.org>
     2
     3        WKContentRuleLists should block requests from service workers
     4        https://bugs.webkit.org/show_bug.cgi?id=201980
     5        <rdar://problem/55516735>
     6
     7        Reviewed by Chris Dumez.
     8
     9        * http/tests/contentextensions/resources/fetch-worker.js: Added.
     10        (event.fetch.string_appeared_here.then):
     11        (event.catch):
     12        * http/tests/contentextensions/resources/serviceworkertest.js: Added.
     13        (testServiceWorker):
     14        (test):
     15        * http/tests/contentextensions/service-worker.https-expected.txt: Added.
     16        * http/tests/contentextensions/service-worker.https.html: Added.
     17        * http/tests/contentextensions/service-worker.https.html.json: Added.
     18
    1192019-10-30  Daniel Bates  <dabates@apple.com>
    220
  • trunk/Source/WebKit/ChangeLog

    r251798 r251814  
     12019-10-30  Alex Christensen  <achristensen@webkit.org>
     2
     3        WKContentRuleLists should block requests from service workers
     4        https://bugs.webkit.org/show_bug.cgi?id=201980
     5        <rdar://problem/55516735>
     6
     7        Reviewed by Chris Dumez.
     8
     9        Test: http/tests/contentextensions/service-worker.https.html
     10
     11        Also covered by an API test.
     12
     13        * Shared/ServiceWorkerInitializationData.cpp: Added.
     14        (WebKit::ServiceWorkerInitializationData::encode const):
     15        (WebKit::ServiceWorkerInitializationData::decode):
     16        * Shared/ServiceWorkerInitializationData.h: Added.
     17        * Sources.txt:
     18        * UIProcess/UserContent/WebUserContentControllerProxy.cpp:
     19        (WebKit::WebUserContentControllerProxy::addProcess):
     20        (WebKit::WebUserContentControllerProxy::contentRuleListData):
     21        * UIProcess/UserContent/WebUserContentControllerProxy.h:
     22        * UIProcess/WebProcessPool.cpp:
     23        (WebKit::WebProcessPool::establishWorkerContextConnectionToNetworkProcess):
     24        (WebKit::WebProcessPool::createWebPage):
     25        * UIProcess/WebProcessPool.h:
     26        * UIProcess/WebProcessProxy.cpp:
     27        (WebKit::WebProcessProxy::createForServiceWorkers):
     28        (WebKit::WebProcessProxy::establishServiceWorkerContext):
     29        (WebKit::contentRuleListsFromIdentifier):
     30        (WebKit::WebProcessProxy::enableServiceWorkers):
     31        * UIProcess/WebProcessProxy.h:
     32        * WebKit.xcodeproj/project.pbxproj:
     33        * WebProcess/Storage/WebSWContextManagerConnection.cpp:
     34        (WebKit::WebSWContextManagerConnection::WebSWContextManagerConnection):
     35        (WebKit::m_userAgent):
     36        (WebKit::WebSWContextManagerConnection::installServiceWorker):
     37        * WebProcess/Storage/WebSWContextManagerConnection.h:
     38        * WebProcess/WebProcess.cpp:
     39        (WebKit::WebProcess::establishWorkerContextConnectionToNetworkProcess):
     40        * WebProcess/WebProcess.h:
     41        * WebProcess/WebProcess.messages.in:
     42
    1432019-10-30  Andres Gonzalez  <andresg_22@apple.com>
    244
  • trunk/Source/WebKit/Sources.txt

    r251765 r251814  
    134134Shared/RTCNetwork.cpp
    135135Shared/RTCPacketOptions.cpp
     136Shared/ServiceWorkerInitializationData.cpp
    136137Shared/SessionState.cpp
    137138Shared/ShareableBitmap.cpp @no-unify
  • trunk/Source/WebKit/UIProcess/UserContent/WebUserContentControllerProxy.cpp

    r251361 r251814  
    122122#if ENABLE(CONTENT_EXTENSIONS)
    123123    ASSERT(parameters.contentRuleLists.isEmpty());
     124    parameters.contentRuleLists = contentRuleListData();
     125#endif
     126}
     127
     128#if ENABLE(CONTENT_EXTENSIONS)
     129Vector<std::pair<String, WebCompiledContentRuleListData>> WebUserContentControllerProxy::contentRuleListData()
     130{
     131    Vector<std::pair<String, WebCompiledContentRuleListData>> data;
     132    data.reserveInitialCapacity(m_contentRuleLists.size());
    124133    for (const auto& contentRuleList : m_contentRuleLists.values())
    125         parameters.contentRuleLists.append(std::make_pair(contentRuleList->name(), contentRuleList->compiledRuleList().data()));
    126 #endif
    127 }
     134        data.uncheckedAppend(std::make_pair(contentRuleList->name(), contentRuleList->compiledRuleList().data()));
     135    return data;
     136}
     137#endif
    128138
    129139void WebUserContentControllerProxy::removeProcess(WebProcessProxy& webProcessProxy)
  • trunk/Source/WebKit/UIProcess/UserContent/WebUserContentControllerProxy.h

    r251361 r251814  
    6161class WebScriptMessageHandler;
    6262struct FrameInfoData;
     63class WebCompiledContentRuleListData;
    6364struct WebPageCreationParameters;
    6465enum class InjectUserScriptImmediately : bool;
     
    105106    void removeAllContentRuleLists();
    106107    const HashMap<String, RefPtr<API::ContentRuleList>>& contentExtensionRules() { return m_contentRuleLists; }
     108    Vector<std::pair<String, WebCompiledContentRuleListData>> contentRuleListData();
    107109#endif
    108110
  • trunk/Source/WebKit/UIProcess/WebProcessPool.cpp

    r251778 r251814  
    7979#include "WebProcessProxy.h"
    8080#include "WebProcessProxyMessages.h"
     81#include "WebUserContentControllerProxy.h"
    8182#include "WebsiteDataStore.h"
    8283#include "WebsiteDataStoreParameters.h"
     
    723724
    724725            serviceWorkerProcessProxy = process.get();
    725             serviceWorkerProcessProxy->enableServiceWorkers();
     726            serviceWorkerProcessProxy->enableServiceWorkers(userContentControllerIdentifierForServiceWorkers());
    726727
    727728            RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), ServiceWorker, "WebProcessPool::establishWorkerContextConnectionToNetworkProcess reusing an existing web process %p, process identifier %d", serviceWorkerProcessProxy, serviceWorkerProcessProxy->processIdentifier());
     
    12281229        process = &processForRegistrableDomain(*pageConfiguration->websiteDataStore(), nullptr, { });
    12291230
     1231    RefPtr<WebUserContentControllerProxy> userContentController = pageConfiguration->userContentController();
     1232   
    12301233    ASSERT(process);
    12311234
     
    12381241            serviceWorkerProcess->updateServiceWorkerPreferencesStore(*m_serviceWorkerPreferences);
    12391242    }
     1243    if (userContentController)
     1244        m_userContentControllerIDForServiceWorker = userContentController->identifier();
    12401245#endif
    12411246
  • trunk/Source/WebKit/UIProcess/WebProcessPool.h

    r251778 r251814  
    394394    bool allowsAnySSLCertificateForServiceWorker() const { return m_allowsAnySSLCertificateForServiceWorker; }
    395395    void updateServiceWorkerUserAgent(const String& userAgent);
     396    const Optional<UserContentControllerIdentifier>& userContentControllerIdentifierForServiceWorkers() const { return m_userContentControllerIDForServiceWorker; }
    396397#endif
    397398
     
    612613    String m_serviceWorkerUserAgent;
    613614    Optional<WebPreferencesStore> m_serviceWorkerPreferences;
     615    Optional<UserContentControllerIdentifier> m_userContentControllerIDForServiceWorker;
    614616#endif
    615617
  • trunk/Source/WebKit/UIProcess/WebProcessProxy.cpp

    r251778 r251814  
    144144    auto proxy = adoptRef(*new WebProcessProxy(processPool, &websiteDataStore, IsPrewarmed::No));
    145145    proxy->m_registrableDomain = WTFMove(registrableDomain);
    146     proxy->enableServiceWorkers();
     146    proxy->enableServiceWorkers(processPool.userContentControllerIdentifierForServiceWorkers());
    147147    proxy->connect();
    148148    return proxy;
     
    15421542void WebProcessProxy::establishServiceWorkerContext(const WebPreferencesStore& store)
    15431543{
    1544     send(Messages::WebProcess::EstablishWorkerContextConnectionToNetworkProcess { processPool().defaultPageGroup().pageGroupID(), m_serviceWorkerInformation->serviceWorkerPageProxyID, m_serviceWorkerInformation->serviceWorkerPageID, store, *m_registrableDomain }, 0);
     1544    send(Messages::WebProcess::EstablishWorkerContextConnectionToNetworkProcess { processPool().defaultPageGroup().pageGroupID(), m_serviceWorkerInformation->serviceWorkerPageProxyID, m_serviceWorkerInformation->serviceWorkerPageID, store, *m_registrableDomain, m_serviceWorkerInformation->initializationData }, 0);
    15451545}
    15461546
     
    15731573}
    15741574
    1575 void WebProcessProxy::enableServiceWorkers()
     1575#if ENABLE(CONTENT_EXTENSIONS)
     1576static Vector<std::pair<String, WebCompiledContentRuleListData>> contentRuleListsFromIdentifier(const Optional<UserContentControllerIdentifier>& userContentControllerIdentifier)
     1577{
     1578    if (!userContentControllerIdentifier) {
     1579        ASSERT_NOT_REACHED();
     1580        return { };
     1581    }
     1582
     1583    auto* userContentController = WebUserContentControllerProxy::get(*userContentControllerIdentifier);
     1584    if (!userContentController) {
     1585        ASSERT_NOT_REACHED();
     1586        return { };
     1587    }
     1588
     1589    return userContentController->contentRuleListData();
     1590}
     1591#endif
     1592
     1593void WebProcessProxy::enableServiceWorkers(const Optional<UserContentControllerIdentifier>& userContentControllerIdentifier)
    15761594{
    15771595    ASSERT(m_registrableDomain && !m_registrableDomain->isEmpty());
    15781596    ASSERT(!m_serviceWorkerInformation);
    1579     m_serviceWorkerInformation = ServiceWorkerInformation { WebPageProxyIdentifier::generate(), PageIdentifier::generate() };
     1597    m_serviceWorkerInformation = ServiceWorkerInformation {
     1598        WebPageProxyIdentifier::generate(),
     1599        PageIdentifier::generate(),
     1600        ServiceWorkerInitializationData {
     1601            userContentControllerIdentifier,
     1602#if ENABLE(CONTENT_EXTENSIONS)
     1603            contentRuleListsFromIdentifier(userContentControllerIdentifier),
     1604#endif
     1605        },
     1606    };
    15801607}
    15811608
  • trunk/Source/WebKit/UIProcess/WebProcessProxy.h

    r251791 r251814  
    3636#include "ProcessThrottlerClient.h"
    3737#include "ResponsivenessTimer.h"
     38#include "ServiceWorkerInitializationData.h"
     39#include "UserContentControllerIdentifier.h"
    3840#include "VisibleWebPageCounter.h"
    3941#include "WebConnectionToWebProcess.h"
     
    7577class VisitedLinkStore;
    7678class WebBackForwardListItem;
     79class WebCompiledContentRuleListData;
    7780class WebFrameProxy;
    7881class WebPageGroup;
     
    132135    bool isInProcessCache() const { return m_isInProcessCache; }
    133136
    134     void enableServiceWorkers();
     137    void enableServiceWorkers(const Optional<UserContentControllerIdentifier>&);
    135138    void disableServiceWorkers();
    136139
     
    523526        WebPageProxyIdentifier serviceWorkerPageProxyID;
    524527        WebCore::PageIdentifier serviceWorkerPageID;
     528        ServiceWorkerInitializationData initializationData;
    525529    };
    526530    Optional<ServiceWorkerInformation> m_serviceWorkerInformation;
  • trunk/Source/WebKit/WebKit.xcodeproj/project.pbxproj

    r251765 r251814  
    36703670                5C7FB46E21E97C0B009E3241 /* WebCookieJar.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebCookieJar.cpp; sourceTree = "<group>"; };
    36713671                5C7FB46F21E97C0C009E3241 /* WebCookieJar.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebCookieJar.h; sourceTree = "<group>"; };
     3672                5C80B3DB23690D8D0086E6DE /* ServiceWorkerInitializationData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ServiceWorkerInitializationData.h; sourceTree = "<group>"; };
     3673                5C80B3DD23690F100086E6DE /* ServiceWorkerInitializationData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ServiceWorkerInitializationData.cpp; sourceTree = "<group>"; };
    36723674                5C84CF901F96AC4E00B6705A /* NetworkSessionCreationParameters.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkSessionCreationParameters.h; sourceTree = "<group>"; };
    36733675                5C85C7861C3F23C50061A4FA /* PendingDownload.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PendingDownload.cpp; sourceTree = "<group>"; };
     
    53315333                                1AAB4A8C1296F0A20023952F /* SandboxExtension.h */,
    53325334                                E1E552C316AE065E004ED653 /* SandboxInitializationParameters.h */,
     5335                                5C80B3DD23690F100086E6DE /* ServiceWorkerInitializationData.cpp */,
     5336                                5C80B3DB23690D8D0086E6DE /* ServiceWorkerInitializationData.h */,
    53335337                                1AFDE6571954A42B00C48FFA /* SessionState.cpp */,
    53345338                                1AFDE6581954A42B00C48FFA /* SessionState.h */,
  • trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.cpp

    r251488 r251814  
    3535#include "NetworkProcessMessages.h"
    3636#include "ServiceWorkerFetchTaskMessages.h"
     37#include "ServiceWorkerInitializationData.h"
    3738#include "WebCacheStorageProvider.h"
     39#include "WebCompiledContentRuleListData.h"
    3840#include "WebCoreArgumentCoders.h"
    3941#include "WebDatabaseProvider.h"
     
    4850#include "WebServiceWorkerFetchTaskClient.h"
    4951#include "WebSocketProvider.h"
     52#include "WebUserContentController.h"
    5053#include <WebCore/EditorClient.h>
    5154#include <WebCore/EmptyClients.h>
     
    8487}
    8588
    86 WebSWContextManagerConnection::WebSWContextManagerConnection(Ref<IPC::Connection>&& connection, WebCore::RegistrableDomain&& registrableDomain, uint64_t pageGroupID, WebPageProxyIdentifier webPageProxyID, PageIdentifier pageID, const WebPreferencesStore& store)
     89WebSWContextManagerConnection::WebSWContextManagerConnection(Ref<IPC::Connection>&& connection, WebCore::RegistrableDomain&& registrableDomain, uint64_t pageGroupID, WebPageProxyIdentifier webPageProxyID, PageIdentifier pageID, const WebPreferencesStore& store, ServiceWorkerInitializationData&& initializationData)
    8790    : m_connectionToNetworkProcess(WTFMove(connection))
    8891    , m_registrableDomain(WTFMove(registrableDomain))
     
    9699#endif
    97100{
     101    if (initializationData.userContentControllerIdentifier) {
     102        m_userContentController = WebUserContentController::getOrCreate(*initializationData.userContentControllerIdentifier);
     103#if ENABLE(CONTENT_EXTENSIONS)
     104        m_userContentController->addContentRuleLists(WTFMove(initializationData.contentRuleLists));
     105#endif
     106    }
     107
    98108    updatePreferencesStore(store);
    99109    m_connectionToNetworkProcess->send(Messages::NetworkConnectionToWebProcess::EstablishSWContextConnection { m_registrableDomain }, 0);
     
    129139#endif
    130140    pageConfiguration.socketProvider = WebSocketProvider::create();
     141    pageConfiguration.userContentProvider = m_userContentController;
    131142
    132143    auto effectiveUserAgent =  WTFMove(userAgent);
  • trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.h

    r251445 r251814  
    3030#include "Connection.h"
    3131#include "MessageReceiver.h"
     32#include "UserContentControllerIdentifier.h"
    3233#include "WebPageProxyIdentifier.h"
    3334#include "WebSWContextManagerConnectionMessagesReplies.h"
     
    5051
    5152class ServiceWorkerFrameLoaderClient;
     53struct ServiceWorkerInitializationData;
    5254struct WebPreferencesStore;
     55class WebUserContentController;
    5356
    5457class WebSWContextManagerConnection final : public WebCore::SWContextManager::Connection, public IPC::MessageReceiver {
    5558public:
    56     WebSWContextManagerConnection(Ref<IPC::Connection>&&, WebCore::RegistrableDomain&&, uint64_t pageGroupID, WebPageProxyIdentifier, WebCore::PageIdentifier, const WebPreferencesStore&);
     59    WebSWContextManagerConnection(Ref<IPC::Connection>&&, WebCore::RegistrableDomain&&, uint64_t pageGroupID, WebPageProxyIdentifier, WebCore::PageIdentifier, const WebPreferencesStore&, ServiceWorkerInitializationData&&);
    5760    ~WebSWContextManagerConnection();
    5861
     
    116119    String m_userAgent;
    117120    bool m_isThrottleable { true };
     121    RefPtr<WebUserContentController> m_userContentController;
    118122};
    119123
  • trunk/Source/WebKit/WebProcess/WebProcess.cpp

    r251599 r251814  
    17391739
    17401740#if ENABLE(SERVICE_WORKER)
    1741 void WebProcess::establishWorkerContextConnectionToNetworkProcess(uint64_t pageGroupID, WebPageProxyIdentifier webPageProxyID, PageIdentifier pageID, const WebPreferencesStore& store, RegistrableDomain&& registrableDomain)
     1741void WebProcess::establishWorkerContextConnectionToNetworkProcess(uint64_t pageGroupID, WebPageProxyIdentifier webPageProxyID, PageIdentifier pageID, const WebPreferencesStore& store, RegistrableDomain&& registrableDomain, ServiceWorkerInitializationData&& initializationData)
    17421742{
    17431743    // We are in the Service Worker context process and the call below establishes our connection to the Network Process
     
    17451745    // NetworkProcessConnection for synchronization purposes.
    17461746    auto& ipcConnection = ensureNetworkProcessConnection().connection();
    1747     SWContextManager::singleton().setConnection(makeUnique<WebSWContextManagerConnection>(ipcConnection, WTFMove(registrableDomain), pageGroupID, webPageProxyID, pageID, store));
     1747    SWContextManager::singleton().setConnection(makeUnique<WebSWContextManagerConnection>(ipcConnection, WTFMove(registrableDomain), pageGroupID, webPageProxyID, pageID, store, WTFMove(initializationData)));
    17481748}
    17491749
  • trunk/Source/WebKit/WebProcess/WebProcess.h

    r251599 r251814  
    3636#include "StorageAreaIdentifier.h"
    3737#include "TextCheckerState.h"
     38#include "UserContentControllerIdentifier.h"
    3839#include "ViewUpdateDispatcher.h"
    3940#include "WebInspectorInterruptDispatcher.h"
     
    110111class NetworkProcessConnection;
    111112class ObjCObjectGraph;
     113struct ServiceWorkerInitializationData;
    112114class StorageAreaMap;
    113115class UserData;
     
    115117class WebAutomationSessionProxy;
    116118class WebCacheStorageProvider;
     119class WebCompiledContentRuleListData;
    117120class WebConnectionToUIProcess;
    118121class WebFrame;
     
    372375
    373376#if ENABLE(SERVICE_WORKER)
    374     void establishWorkerContextConnectionToNetworkProcess(uint64_t pageGroupID, WebPageProxyIdentifier, WebCore::PageIdentifier, const WebPreferencesStore&, WebCore::RegistrableDomain&&);
     377    void establishWorkerContextConnectionToNetworkProcess(uint64_t pageGroupID, WebPageProxyIdentifier, WebCore::PageIdentifier, const WebPreferencesStore&, WebCore::RegistrableDomain&&, ServiceWorkerInitializationData&&);
    375378    void registerServiceWorkerClients();
    376379#endif
  • trunk/Source/WebKit/WebProcess/WebProcess.messages.in

    r251599 r251814  
    107107
    108108#if ENABLE(SERVICE_WORKER)
    109     EstablishWorkerContextConnectionToNetworkProcess(uint64_t pageGroupID, WebKit::WebPageProxyIdentifier webPageProxyID, WebCore::PageIdentifier pageID, struct WebKit::WebPreferencesStore store, WebCore::RegistrableDomain domain)
     109    EstablishWorkerContextConnectionToNetworkProcess(uint64_t pageGroupID, WebKit::WebPageProxyIdentifier webPageProxyID, WebCore::PageIdentifier pageID, struct WebKit::WebPreferencesStore store, WebCore::RegistrableDomain domain, struct WebKit::ServiceWorkerInitializationData initializationData)
    110110    RegisterServiceWorkerClients()
    111111#endif
  • trunk/Tools/ChangeLog

    r251813 r251814  
     12019-10-30  Alex Christensen  <achristensen@webkit.org>
     2
     3        WKContentRuleLists should block requests from service workers
     4        https://bugs.webkit.org/show_bug.cgi?id=201980
     5        <rdar://problem/55516735>
     6
     7        Reviewed by Chris Dumez.
     8
     9        NSString initWithContentsOfURL doesn't work with https URLs with certificates without a trusted root,
     10        so I use an ephemeral NSURLSession instead so I can tell it to accept any connection, even our WebKit httpd server.
     11        I also added an API test.
     12
     13        * TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm:
     14        * WebKitTestRunner/mac/TestControllerMac.mm:
     15        (-[WKTRSessionDelegate URLSession:task:didReceiveChallenge:completionHandler:]):
     16        (WTR::TestController::configureContentExtensionForTest):
     17
    1182019-10-30  Daniel Bates  <dabates@apple.com>
    219
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm

    r251439 r251814  
    19141914    EXPECT_EQ(2U, processPool._serviceWorkerProcessCount);
    19151915}
     1916
     1917static const char* contentRuleListWorkerScript =
     1918"self.addEventListener('message', (event) => {"
     1919"    fetch('blockedsubresource').then(() => {"
     1920"        event.source.postMessage('FAIL - should have blocked first request');"
     1921"    }).catch(() => {"
     1922"        fetch('allowedsubresource').then(() => {"
     1923"            event.source.postMessage('PASS - blocked first request, allowed second');"
     1924"        }).catch(() => {"
     1925"            event.source.postMessage('FAIL - should have allowed second request');"
     1926"        });"
     1927"    });"
     1928"});";
     1929
     1930TEST(ServiceWorkers, ContentRuleList)
     1931{
     1932    [WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins];
     1933
     1934    __block bool doneCompiling = false;
     1935    __block RetainPtr<WKContentRuleList> contentRuleList;
     1936    [[WKContentRuleListStore defaultStore] compileContentRuleListForIdentifier:@"ServiceWorkerRuleList" encodedContentRuleList:@"[{\"action\":{\"type\":\"block\"},\"trigger\":{\"url-filter\":\"blockedsubresource\"}}]" completionHandler:^(WKContentRuleList *list, NSError *error) {
     1937        EXPECT_NOT_NULL(list);
     1938        EXPECT_NULL(error);
     1939        contentRuleList = list;
     1940        doneCompiling = true;
     1941    }];
     1942    TestWebKitAPI::Util::run(&doneCompiling);
     1943
     1944    // Start with a clean slate data store
     1945    [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() {
     1946        done = true;
     1947    }];
     1948    TestWebKitAPI::Util::run(&done);
     1949    done = false;
     1950
     1951    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     1952
     1953    auto messageHandler = adoptNS([[SWMessageHandlerWithExpectedMessage alloc] init]);
     1954    [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"];
     1955    [[configuration userContentController] addContentRuleList:contentRuleList.get()];
     1956
     1957    using namespace TestWebKitAPI;
     1958    TCPServer server([] (int socket) {
     1959        auto respond = [socket] (const char* body, const char* mimeType) {
     1960            NSString *format = @"HTTP/1.1 200 OK\r\n"
     1961            "Content-Type: %s\r\n"
     1962            "Content-Length: %d\r\n\r\n"
     1963            "%s";
     1964            NSString *response = [NSString stringWithFormat:format, mimeType, strlen(body), body];
     1965            TCPServer::write(socket, response.UTF8String, response.length);
     1966        };
     1967        TCPServer::read(socket);
     1968        respond(mainBytes, "text/html");
     1969        TCPServer::read(socket);
     1970        respond(contentRuleListWorkerScript, "application/javascript");
     1971        auto lastRequest = TCPServer::read(socket);
     1972        EXPECT_TRUE(strstr((const char*)lastRequest.data(), "allowedsubresource"));
     1973        respond("successful fetch", "application/octet-stream");
     1974    });
     1975
     1976    expectedMessage = @"Message from worker: PASS - blocked first request, allowed second";
     1977
     1978    auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
     1979    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://127.0.0.1:%d/", server.port()]]]];
     1980    TestWebKitAPI::Util::run(&done);
     1981   
     1982    __block bool doneRemoving = false;
     1983    [[WKContentRuleListStore defaultStore] removeContentRuleListForIdentifier:@"ServiceWorkerRuleList" completionHandler:^(NSError *error) {
     1984        EXPECT_NULL(error);
     1985        doneRemoving = true;
     1986    }];
     1987    TestWebKitAPI::Util::run(&doneRemoving);
     1988}
  • trunk/Tools/WebKitTestRunner/mac/TestControllerMac.mm

    r249327 r251814  
    5151@end
    5252
     53@interface WKTRSessionDelegate : NSObject <NSURLSessionDataDelegate>
     54@end
     55@implementation WKTRSessionDelegate
     56- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
     57{
     58    completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
     59}
     60@end
     61
    5362namespace WTR {
    5463
     
    126135    NSURL *filterURL = [(__bridge NSURL *)testURL.get() URLByAppendingPathExtension:@"json"];
    127136
    128     NSStringEncoding encoding;
    129     NSString *contentExtensionString = [[NSString alloc] initWithContentsOfURL:filterURL usedEncoding:&encoding error:NULL];
    130     if (!contentExtensionString)
    131         return;
    132    
     137    __block NSString *contentExtensionString;
     138    __block bool doneFetchingContentExtension = false;
     139    auto delegate = adoptNS([WKTRSessionDelegate new]);
     140    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration] delegate:delegate.get() delegateQueue:[NSOperationQueue mainQueue]];
     141    NSURLSessionDataTask *task = [session dataTaskWithRequest:[NSURLRequest requestWithURL:filterURL] completionHandler:^(NSData * data, NSURLResponse *response, NSError *error) {
     142        ASSERT(data);
     143        ASSERT(response);
     144        ASSERT(!error);
     145        contentExtensionString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
     146        doneFetchingContentExtension = true;
     147    }];
     148    [task resume];
     149    platformRunUntil(doneFetchingContentExtension, noTimeout);
     150
    133151    __block bool doneCompiling = false;
    134152
Note: See TracChangeset for help on using the changeset viewer.