Title: [275078] trunk
Revision
275078
Author
achristen...@apple.com
Date
2021-03-25 22:24:51 -0700 (Thu, 25 Mar 2021)

Log Message

Allow WKContentRuleList to block only in frames or only in main frame
https://bugs.webkit.org/show_bug.cgi?id=219001
Source/WebCore:

<rdar://problem/71382045>

Reviewed by Ben Poulain.

This adds load-context to the trigger, which can have an array containing main-frame or iframe.
This makes the trigger only happen in such loading contexts.

* Modules/websockets/ThreadableWebSocketChannel.cpp:
(WebCore::ThreadableWebSocketChannel::validateURL):
* contentextensions/ContentExtension.cpp:
(WebCore::ContentExtensions::ContentExtension::populateConditionCacheIfNeeded):
* contentextensions/ContentExtensionParser.cpp:
(WebCore::ContentExtensions::getTypeFlags):
(WebCore::ContentExtensions::loadTrigger):
* contentextensions/ContentExtensionsBackend.cpp:
(WebCore::ContentExtensions::ContentExtensionsBackend::processContentRuleListsForLoad):
(WebCore::ContentExtensions::ContentExtensionsBackend::processContentRuleListsForPingLoad):
* contentextensions/DFABytecodeInterpreter.cpp:
(WebCore::ContentExtensions::DFABytecodeInterpreter::interpretTestFlagsAndAppendAction):
* loader/FrameLoader.cpp:
(WebCore::FrameLoader::loadResourceSynchronously):
* loader/NetscapePlugInStreamLoader.cpp:
(WebCore::NetscapePlugInStreamLoader::NetscapePlugInStreamLoader):
* loader/PingLoader.cpp:
(WebCore::PingLoader::sendPing):
(WebCore::PingLoader::sendViolationReport):
* loader/ResourceLoadInfo.cpp:
(WebCore::ContentExtensions::toResourceType):
(WebCore::ContentExtensions::readResourceType):
(WebCore::ContentExtensions::readLoadType):
(WebCore::ContentExtensions::readLoadContext):
(WebCore::ContentExtensions::ResourceLoadInfo::getResourceFlags const):
* loader/ResourceLoadInfo.h:

Source/WebKit:


Reviewed by Ben Poulain.

* UIProcess/API/APIContentRuleListStore.cpp:
(API::ContentRuleListStore::getContentRuleListSource):
* UIProcess/API/APIContentRuleListStore.h:

Tools:


Reviewed by Ben Poulain.

* TestWebKitAPI/Tests/WebCore/ContentExtensions.cpp:
(TestWebKitAPI::TEST_F):
* TestWebKitAPI/Tests/WebKitCocoa/ContentRuleListNotification.mm:
(TEST):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (275077 => 275078)


--- trunk/Source/WebCore/ChangeLog	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebCore/ChangeLog	2021-03-26 05:24:51 UTC (rev 275078)
@@ -1,3 +1,41 @@
+2021-03-25  Alex Christensen  <achristen...@webkit.org>
+
+        Allow WKContentRuleList to block only in frames or only in main frame
+        https://bugs.webkit.org/show_bug.cgi?id=219001
+        <rdar://problem/71382045>
+
+        Reviewed by Ben Poulain.
+
+        This adds load-context to the trigger, which can have an array containing main-frame or iframe.
+        This makes the trigger only happen in such loading contexts.
+
+        * Modules/websockets/ThreadableWebSocketChannel.cpp:
+        (WebCore::ThreadableWebSocketChannel::validateURL):
+        * contentextensions/ContentExtension.cpp:
+        (WebCore::ContentExtensions::ContentExtension::populateConditionCacheIfNeeded):
+        * contentextensions/ContentExtensionParser.cpp:
+        (WebCore::ContentExtensions::getTypeFlags):
+        (WebCore::ContentExtensions::loadTrigger):
+        * contentextensions/ContentExtensionsBackend.cpp:
+        (WebCore::ContentExtensions::ContentExtensionsBackend::processContentRuleListsForLoad):
+        (WebCore::ContentExtensions::ContentExtensionsBackend::processContentRuleListsForPingLoad):
+        * contentextensions/DFABytecodeInterpreter.cpp:
+        (WebCore::ContentExtensions::DFABytecodeInterpreter::interpretTestFlagsAndAppendAction):
+        * loader/FrameLoader.cpp:
+        (WebCore::FrameLoader::loadResourceSynchronously):
+        * loader/NetscapePlugInStreamLoader.cpp:
+        (WebCore::NetscapePlugInStreamLoader::NetscapePlugInStreamLoader):
+        * loader/PingLoader.cpp:
+        (WebCore::PingLoader::sendPing):
+        (WebCore::PingLoader::sendViolationReport):
+        * loader/ResourceLoadInfo.cpp:
+        (WebCore::ContentExtensions::toResourceType):
+        (WebCore::ContentExtensions::readResourceType):
+        (WebCore::ContentExtensions::readLoadType):
+        (WebCore::ContentExtensions::readLoadContext):
+        (WebCore::ContentExtensions::ResourceLoadInfo::getResourceFlags const):
+        * loader/ResourceLoadInfo.h:
+
 2021-03-25  Wenson Hsieh  <wenson_hs...@apple.com>
 
         Don't add `-webkit-user-select: none;` on image elements with `draggable=true`

Modified: trunk/Source/WebCore/Modules/websockets/ThreadableWebSocketChannel.cpp (275077 => 275078)


--- trunk/Source/WebCore/Modules/websockets/ThreadableWebSocketChannel.cpp	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebCore/Modules/websockets/ThreadableWebSocketChannel.cpp	2021-03-26 05:24:51 UTC (rev 275078)
@@ -93,7 +93,7 @@
             return { };
 #if ENABLE(CONTENT_EXTENSIONS)
         if (auto* documentLoader = document.loader()) {
-            auto results = page->userContentProvider().processContentRuleListsForLoad(*page, validatedURL.url, { ContentExtensions::ResourceType::Raw, ContentExtensions::ResourceType::WebSocket }, *documentLoader);
+            auto results = page->userContentProvider().processContentRuleListsForLoad(*page, validatedURL.url, ContentExtensions::ResourceType::WebSocket, *documentLoader);
             if (results.summary.blockedLoad)
                 return { };
             if (results.summary.madeHTTPS) {

Modified: trunk/Source/WebCore/contentextensions/ContentExtension.cpp (275077 => 275078)


--- trunk/Source/WebCore/contentextensions/ContentExtension.cpp	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebCore/contentextensions/ContentExtension.cpp	2021-03-26 05:24:51 UTC (rev 275078)
@@ -121,7 +121,7 @@
 {
     if (m_cachedTopURL != topURL) {
         DFABytecodeInterpreter interpreter(m_compiledExtension->topURLFiltersBytecode(), m_compiledExtension->topURLFiltersBytecodeLength());
-        const uint16_t allLoadTypesAndResourceTypes = LoadTypeMask | ResourceTypeMask;
+        constexpr uint16_t allLoadTypesAndResourceTypes = LoadTypeMask | ResourceTypeMask | LoadContextMask;
         String string = m_compiledExtension->conditionsApplyOnlyToDomain() ? topURL.host().toString() : topURL.string();
         auto topURLActions = interpreter.interpret(string.utf8(), allLoadTypesAndResourceTypes);
         

Modified: trunk/Source/WebCore/contentextensions/ContentExtensionParser.cpp (275077 => 275078)


--- trunk/Source/WebCore/contentextensions/ContentExtensionParser.cpp	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebCore/contentextensions/ContentExtensionParser.cpp	2021-03-26 05:24:51 UTC (rev 275078)
@@ -95,7 +95,7 @@
 }
 
 template<typename T>
-static std::error_code getTypeFlags(JSGlobalObject& lexicalGlobalObject, const JSValue& typeValue, ResourceFlags& flags, Optional<OptionSet<T>> (*stringToType)(const String&))
+static std::error_code getTypeFlags(JSGlobalObject& lexicalGlobalObject, const JSValue& typeValue, ResourceFlags& flags, Optional<OptionSet<T>> (*stringToType)(StringView))
 {
     VM& vm = lexicalGlobalObject.vm();
     auto scope = DECLARE_THROW_SCOPE(vm);
@@ -117,7 +117,7 @@
             return ContentExtensionError::JSONInvalidObjectInTriggerFlagsArray;
         
         String name = value.toWTFString(&lexicalGlobalObject);
-        auto type = stringToType(name);
+        auto type = stringToType(StringView(name));
         if (!type)
             return ContentExtensionError::JSONInvalidStringInTriggerFlagsArray;
 
@@ -171,6 +171,14 @@
     } else if (!loadTypeValue.isUndefined())
         return makeUnexpected(ContentExtensionError::JSONInvalidTriggerFlagsArray);
 
+    const JSValue loadContextValue = triggerObject.get(&lexicalGlobalObject, Identifier::fromString(vm, "load-context"));
+    if (!scope.exception() && loadContextValue.isObject()) {
+        auto typeFlagsError = getTypeFlags(lexicalGlobalObject, loadContextValue, trigger.flags, readLoadContext);
+        if (typeFlagsError)
+            return makeUnexpected(typeFlagsError);
+    } else if (!loadContextValue.isUndefined())
+        return makeUnexpected(ContentExtensionError::JSONInvalidTriggerFlagsArray);
+
     const JSValue ifDomainValue = triggerObject.get(&lexicalGlobalObject, Identifier::fromString(vm, "if-domain"));
     if (!scope.exception() && ifDomainValue.isObject()) {
         auto ifDomain = getDomainList(lexicalGlobalObject, asObject(ifDomainValue));

Modified: trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.cpp (275077 => 275078)


--- trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.cpp	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebCore/contentextensions/ContentExtensionsBackend.cpp	2021-03-26 05:24:51 UTC (rev 275078)
@@ -174,8 +174,10 @@
 {
     Document* currentDocument = nullptr;
     URL mainDocumentURL;
+    bool mainFrameContext = false;
 
     if (auto* frame = initiatingDocumentLoader.frame()) {
+        mainFrameContext = frame->isMainFrame();
         currentDocument = frame->document();
 
         if (initiatingDocumentLoader.isLoadingMainResource()
@@ -186,7 +188,7 @@
             mainDocumentURL = mainDocument->url();
     }
 
-    ResourceLoadInfo resourceLoadInfo = { url, mainDocumentURL, resourceType };
+    ResourceLoadInfo resourceLoadInfo = { url, mainDocumentURL, resourceType, mainFrameContext };
     auto actions = actionsForResourceLoad(resourceLoadInfo);
 
     ContentRuleListResults results;
@@ -266,7 +268,7 @@
 
 ContentRuleListResults ContentExtensionsBackend::processContentRuleListsForPingLoad(const URL& url, const URL& mainDocumentURL)
 {
-    ResourceLoadInfo resourceLoadInfo = { url, mainDocumentURL, ResourceType::Raw };
+    ResourceLoadInfo resourceLoadInfo = { url, mainDocumentURL, ResourceType::Ping };
     auto actions = actionsForResourceLoad(resourceLoadInfo);
 
     ContentRuleListResults results;

Modified: trunk/Source/WebCore/contentextensions/DFABytecodeInterpreter.cpp (275077 => 275078)


--- trunk/Source/WebCore/contentextensions/DFABytecodeInterpreter.cpp	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebCore/contentextensions/DFABytecodeInterpreter.cpp	2021-03-26 05:24:51 UTC (rev 275078)
@@ -112,12 +112,14 @@
     uint16_t flagsToCheck = getBits<uint16_t>(m_bytecode, m_bytecodeLength, programCounter + sizeof(DFABytecodeInstruction));
 
     uint16_t loadTypeFlags = flagsToCheck & LoadTypeMask;
+    uint16_t loadContextFlags = flagsToCheck & LoadContextMask;
     uint16_t resourceTypeFlags = flagsToCheck & ResourceTypeMask;
-    
+
     bool loadTypeMatches = loadTypeFlags ? (loadTypeFlags & flags) : true;
+    bool loadContextMatches = loadContextFlags ? (loadContextFlags & flags) : true;
     bool resourceTypeMatches = resourceTypeFlags ? (resourceTypeFlags & flags) : true;
     
-    if (loadTypeMatches && resourceTypeMatches) {
+    if (loadTypeMatches && loadContextMatches && resourceTypeMatches) {
         uint64_t actionAndFlags = (ifCondition ? IfConditionFlag : 0) | (static_cast<uint64_t>(flagsToCheck) << 32) | static_cast<uint64_t>(getBits<uint32_t>(m_bytecode, m_bytecodeLength, programCounter + sizeof(DFABytecodeInstruction) + sizeof(uint16_t)));
         if (!m_topURLActions || matchesCondition(actionAndFlags, *m_topURLActions))
             actions.add(actionAndFlags);

Modified: trunk/Source/WebCore/loader/FrameLoader.cpp (275077 => 275078)


--- trunk/Source/WebCore/loader/FrameLoader.cpp	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebCore/loader/FrameLoader.cpp	2021-03-26 05:24:51 UTC (rev 275078)
@@ -3081,7 +3081,7 @@
     if (error.isNull()) {
         if (auto* page = m_frame.page()) {
             if (m_documentLoader) {
-                auto results = page->userContentProvider().processContentRuleListsForLoad(*page, newRequest.url(), ContentExtensions::ResourceType::Raw, *m_documentLoader);
+                auto results = page->userContentProvider().processContentRuleListsForLoad(*page, newRequest.url(), ContentExtensions::ResourceType::Fetch, *m_documentLoader);
                 bool blockedLoad = results.summary.blockedLoad;
                 ContentExtensions::applyResultsToRequest(WTFMove(results), page, newRequest);
                 if (blockedLoad) {

Modified: trunk/Source/WebCore/loader/NetscapePlugInStreamLoader.cpp (275077 => 275078)


--- trunk/Source/WebCore/loader/NetscapePlugInStreamLoader.cpp	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebCore/loader/NetscapePlugInStreamLoader.cpp	2021-03-26 05:24:51 UTC (rev 275078)
@@ -60,7 +60,7 @@
     , m_client(makeWeakPtr(client))
 {
 #if ENABLE(CONTENT_EXTENSIONS)
-    m_resourceType = { ContentExtensions::ResourceType::PlugInStream };
+    m_resourceType = { ContentExtensions::ResourceType::Other };
 #endif
 }
 

Modified: trunk/Source/WebCore/loader/PingLoader.cpp (275077 => 275078)


--- trunk/Source/WebCore/loader/PingLoader.cpp	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebCore/loader/PingLoader.cpp	2021-03-26 05:24:51 UTC (rev 275078)
@@ -122,7 +122,7 @@
     request.setRequester(ResourceRequest::Requester::Ping);
 
 #if ENABLE(CONTENT_EXTENSIONS)
-    if (processContentRuleListsForLoad(frame, request, { ContentExtensions::ResourceType::Raw, ContentExtensions::ResourceType::Ping }))
+    if (processContentRuleListsForLoad(frame, request, ContentExtensions::ResourceType::Ping))
         return;
 #endif
 
@@ -153,7 +153,7 @@
 
     ResourceRequest request(reportURL);
 #if ENABLE(CONTENT_EXTENSIONS)
-    if (processContentRuleListsForLoad(frame, request, ContentExtensions::ResourceType::Raw))
+    if (processContentRuleListsForLoad(frame, request, ContentExtensions::ResourceType::Other))
         return;
 #endif
 

Modified: trunk/Source/WebCore/loader/ResourceLoadInfo.cpp (275077 => 275078)


--- trunk/Source/WebCore/loader/ResourceLoadInfo.cpp	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebCore/loader/ResourceLoadInfo.cpp	2021-03-26 05:24:51 UTC (rev 275078)
@@ -63,7 +63,7 @@
     case CachedResource::Type::RawResource:
         if (requester == ResourceRequestBase::Requester::XHR
             || requester == ResourceRequestBase::Requester::Fetch)
-            return {{ ResourceType::Raw, ResourceType::Fetch }};
+            return { ResourceType::Fetch };
         FALLTHROUGH;
     case CachedResource::Type::Beacon:
     case CachedResource::Type::Ping:
@@ -74,7 +74,7 @@
 #if ENABLE(APPLICATION_MANIFEST)
     case CachedResource::Type::ApplicationManifest:
 #endif
-        return {{ ResourceType::Raw, ResourceType::Other }};
+        return { ResourceType::Other };
 
     case CachedResource::Type::TextTrackResource:
         return { ResourceType::Media };
@@ -84,7 +84,7 @@
     return { };
 }
 
-Optional<OptionSet<ResourceType>> readResourceType(const String& name)
+Optional<OptionSet<ResourceType>> readResourceType(StringView name)
 {
     if (name == "document")
         return { ResourceType::Document };
@@ -97,13 +97,13 @@
     if (name == "font")
         return { ResourceType::Font };
     if (name == "raw")
-        return { ResourceType::Raw };
+        return {{ ResourceType::Fetch, ResourceType::WebSocket, ResourceType::Other, ResourceType::Ping }};
     if (name == "websocket")
         return { ResourceType::WebSocket };
     if (name == "fetch")
         return { ResourceType::Fetch };
     if (name == "other")
-        return { ResourceType::Other };
+        return {{ ResourceType::Other, ResourceType::Ping }};
     if (name == "svg-document")
         return { ResourceType::SVGDocument };
     if (name == "media")
@@ -115,7 +115,7 @@
     return WTF::nullopt;
 }
 
-Optional<OptionSet<LoadType>> readLoadType(const String& name)
+Optional<OptionSet<LoadType>> readLoadType(StringView name)
 {
     if (name == "first-party")
         return { LoadType::FirstParty };
@@ -124,6 +124,15 @@
     return WTF::nullopt;
 }
 
+Optional<OptionSet<LoadContext>> readLoadContext(StringView name)
+{
+    if (name == "top-frame")
+        return { LoadContext::TopFrame };
+    if (name == "child-frame")
+        return { LoadContext::ChildFrame };
+    return WTF::nullopt;
+}
+
 bool ResourceLoadInfo::isThirdParty() const
 {
     return !RegistrableDomain(mainDocumentURL).matches(resourceURL);
@@ -135,6 +144,7 @@
     ASSERT(!type.isEmpty());
     flags |= type.toRaw();
     flags |= isThirdParty() ? static_cast<ResourceFlags>(LoadType::ThirdParty) : static_cast<ResourceFlags>(LoadType::FirstParty);
+    flags |= mainFrameContext ? static_cast<ResourceFlags>(LoadContext::TopFrame) : static_cast<ResourceFlags>(LoadContext::ChildFrame);
     return flags;
 }
 

Modified: trunk/Source/WebCore/loader/ResourceLoadInfo.h (275077 => 275078)


--- trunk/Source/WebCore/loader/ResourceLoadInfo.h	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebCore/loader/ResourceLoadInfo.h	2021-03-26 05:24:51 UTC (rev 275078)
@@ -40,26 +40,31 @@
     StyleSheet = 0x0004,
     Script = 0x0008,
     Font = 0x0010,
-    Raw = 0x0020, // This bit can be reused next time we increment CurrentContentRuleListFileVersion. It is equivalent to using Fetch | WebSocket | Other.
-    SVGDocument = 0x0040,
-    Media = 0x0080,
-    PlugInStream = 0x0100,
-    Popup = 0x0200,
-    // 0x0400 and 0x0800 are used by LoadType.
-    Ping = 0x1000,
-    Fetch = 0x2000,
-    WebSocket = 0x4000,
-    Other = 0x8000,
+    SVGDocument = 0x0020,
+    Media = 0x0040,
+    Popup = 0x0080,
+    Ping = 0x0100,
+    Fetch = 0x0200,
+    WebSocket = 0x0400,
+    Other = 0x0800,
 };
-const uint16_t ResourceTypeMask = 0xF3FF;
+const uint16_t ResourceTypeMask = 0x0FFF;
 
 enum class LoadType : uint16_t {
-    FirstParty = 0x0400,
-    ThirdParty = 0x0800,
+    FirstParty = 0x1000,
+    ThirdParty = 0x2000,
 };
-const uint16_t LoadTypeMask = 0x0C00;
+const uint16_t LoadTypeMask = 0x3000;
 
+enum class LoadContext : uint16_t {
+    TopFrame = 0x4000,
+    ChildFrame = 0x8000,
+};
+const uint16_t LoadContextMask = 0xC000;
+
 static_assert(!(ResourceTypeMask & LoadTypeMask), "ResourceTypeMask and LoadTypeMask should be mutually exclusive because they are stored in the same uint16_t");
+static_assert(!(ResourceTypeMask & LoadContextMask), "ResourceTypeMask and LoadContextMask should be mutually exclusive because they are stored in the same uint16_t");
+static_assert(!(LoadContextMask & LoadTypeMask), "LoadContextMask and LoadTypeMask should be mutually exclusive because they are stored in the same uint16_t");
 
 typedef uint16_t ResourceFlags;
 
@@ -74,13 +79,15 @@
 const uint64_t IfConditionFlag = 0x0001000000000000;
 
 OptionSet<ResourceType> toResourceType(CachedResource::Type, ResourceRequestBase::Requester);
-Optional<OptionSet<ResourceType>> readResourceType(const String&);
-Optional<OptionSet<LoadType>> readLoadType(const String&);
+Optional<OptionSet<ResourceType>> readResourceType(StringView);
+Optional<OptionSet<LoadType>> readLoadType(StringView);
+Optional<OptionSet<LoadContext>> readLoadContext(StringView);
 
 struct ResourceLoadInfo {
     URL resourceURL;
     URL mainDocumentURL;
     OptionSet<ResourceType> type;
+    bool mainFrameContext { false };
 
     bool isThirdParty() const;
     ResourceFlags getResourceFlags() const;

Modified: trunk/Source/WebKit/ChangeLog (275077 => 275078)


--- trunk/Source/WebKit/ChangeLog	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebKit/ChangeLog	2021-03-26 05:24:51 UTC (rev 275078)
@@ -1,3 +1,14 @@
+2021-03-25  Alex Christensen  <achristen...@webkit.org>
+
+        Allow WKContentRuleList to block only in frames or only in main frame
+        https://bugs.webkit.org/show_bug.cgi?id=219001
+
+        Reviewed by Ben Poulain.
+
+        * UIProcess/API/APIContentRuleListStore.cpp:
+        (API::ContentRuleListStore::getContentRuleListSource):
+        * UIProcess/API/APIContentRuleListStore.h:
+
 2021-03-25  Megan Gardner  <megan_gard...@apple.com>
 
         Have App Highlights be a Configuration Setting instead of an internal Preference setting.

Modified: trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.cpp (275077 => 275078)


--- trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.cpp	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.cpp	2021-03-26 05:24:51 UTC (rev 275078)
@@ -576,6 +576,7 @@
         switch (contentRuleList->metaData.version) {
         case 9:
         case 10:
+        case 11:
             if (!contentRuleList->metaData.sourceSize) {
                 complete({ });
                 return;

Modified: trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.h (275077 => 275078)


--- trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.h	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Source/WebKit/UIProcess/API/APIContentRuleListStore.h	2021-03-26 05:24:51 UTC (rev 275078)
@@ -57,7 +57,7 @@
     // Also update ContentRuleListStore::getContentRuleListSource to be able to find the original JSON
     // source from old versions.
     // Update ContentRuleListStore::getContentRuleListSource with this.
-    static constexpr uint32_t CurrentContentRuleListFileVersion = 10;
+    static constexpr uint32_t CurrentContentRuleListFileVersion = 11;
 
     static ContentRuleListStore& defaultStore(bool legacyFilename);
     static Ref<ContentRuleListStore> storeWithPath(const WTF::String& storePath, bool legacyFilename);

Modified: trunk/Tools/ChangeLog (275077 => 275078)


--- trunk/Tools/ChangeLog	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Tools/ChangeLog	2021-03-26 05:24:51 UTC (rev 275078)
@@ -1,3 +1,15 @@
+2021-03-25  Alex Christensen  <achristen...@webkit.org>
+
+        Allow WKContentRuleList to block only in frames or only in main frame
+        https://bugs.webkit.org/show_bug.cgi?id=219001
+
+        Reviewed by Ben Poulain.
+
+        * TestWebKitAPI/Tests/WebCore/ContentExtensions.cpp:
+        (TestWebKitAPI::TEST_F):
+        * TestWebKitAPI/Tests/WebKitCocoa/ContentRuleListNotification.mm:
+        (TEST):
+
 2021-03-25  Wenson Hsieh  <wenson_hs...@apple.com>
 
         It should be possible to drag images with overlay content

Modified: trunk/Tools/TestWebKitAPI/Tests/WebCore/ContentExtensions.cpp (275077 => 275078)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/ContentExtensions.cpp	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/ContentExtensions.cpp	2021-03-26 05:24:51 UTC (rev 275078)
@@ -1164,7 +1164,7 @@
     testRequest(backend, mainDocumentRequest("http://block_all_types.org", ResourceType::StyleSheet), { ContentExtensions::ActionType::BlockLoad });
     testRequest(backend, mainDocumentRequest("http://block_all_types.org", ResourceType::Script), { ContentExtensions::ActionType::BlockLoad });
     testRequest(backend, mainDocumentRequest("http://block_all_types.org", ResourceType::Font), { ContentExtensions::ActionType::BlockLoad });
-    testRequest(backend, mainDocumentRequest("http://block_all_types.org", ResourceType::Raw), { ContentExtensions::ActionType::BlockLoad });
+    testRequest(backend, mainDocumentRequest("http://block_all_types.org", ResourceType::Other), { ContentExtensions::ActionType::BlockLoad });
     testRequest(backend, mainDocumentRequest("http://block_all_types.org", ResourceType::SVGDocument), { ContentExtensions::ActionType::BlockLoad });
     testRequest(backend, mainDocumentRequest("http://block_all_types.org", ResourceType::Media), { ContentExtensions::ActionType::BlockLoad });
     testRequest(backend, mainDocumentRequest("http://block_all_types.org", ResourceType::Popup), { ContentExtensions::ActionType::BlockLoad });

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ContentRuleListNotification.mm (275077 => 275078)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ContentRuleListNotification.mm	2021-03-26 05:09:24 UTC (rev 275077)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ContentRuleListNotification.mm	2021-03-26 05:24:51 UTC (rev 275078)
@@ -349,6 +349,77 @@
         EXPECT_FALSE([WKContentRuleList _supportsRegularExpression:regex]);
 }
 
+TEST(ContentRuleList, TopFrameChildFrame)
+{
+    auto handler = [[TestURLSchemeHandler new] autorelease];
+    __block bool loadedIFrame = false;
+    handler.startURLSchemeTaskHandler = ^(WKWebView *, id<WKURLSchemeTask> task) {
+        auto respond = [task] (const char* html) {
+            NSURLResponse *response = [[[NSURLResponse alloc] initWithURL:task.request.URL MIMEType:@"text/html" expectedContentLength:strlen(html) textEncodingName:nil] autorelease];
+            [task didReceiveResponse:response];
+            [task didReceiveData:[NSData dataWithBytes:html length:strlen(html)]];
+            [task didFinish];
+        };
+        NSString *path = task.request.URL.path;
+        if ([path isEqualToString:@"/main.html"])
+            return respond("<iframe src=''></iframe>");
+        if ([path isEqualToString:@"/frame.html"]) {
+            EXPECT_FALSE(loadedIFrame);
+            loadedIFrame = true;
+            return respond("hi");
+        }
+        if ([path isEqualToString:@"/fetch_main.html"]) {
+            return respond("<script>"
+                "function addiframe() { var iframe = document.createElement('iframe'); iframe.src = ''; document.body.appendChild(iframe); };"
+                "function testfetch() { fetch('/fetched.txt').then(()=>{alert('main frame fetched successfully');addiframe()}).catch(()=>{alert('main frame fetch failed');addiframe();}) }"
+                "</script><body _onload_='testfetch()'/>");
+        }
+        if ([path isEqualToString:@"/fetched.txt"])
+            return respond("hi");
+        if ([path isEqualToString:@"/fetch_iframe.html"]) {
+            return respond("<script>"
+                "function testfetch() { fetch('/fetched.txt').then(()=>{alert('iframe fetched successfully')}).catch(()=>{alert('iframe fetch failed');}) }"
+                "</script><body _onload_='testfetch()'/>");
+        }
+        ASSERT_NOT_REACHED();
+    };
+    auto configuration = [[WKWebViewConfiguration new] autorelease];
+    [configuration setURLSchemeHandler:handler forURLScheme:@"test"];
+    configuration.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
+    auto webView = [[[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration] autorelease];
+    WKUserContentController *userContentController = webView.configuration.userContentController;
+
+    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"test:///main.html"]]];
+    [webView _test_waitForDidFinishNavigation];
+    EXPECT_TRUE(loadedIFrame);
+
+    [userContentController addContentRuleList:makeContentRuleList(@"[{\"action\":{\"type\":\"block\"},\"trigger\":{\"url-filter\":\"test\",\"load-context\":[\"child-frame\"]}}]").get()];
+    loadedIFrame = false;
+    [webView reload];
+    [webView _test_waitForDidFinishNavigation];
+    EXPECT_FALSE(loadedIFrame);
+    
+    [userContentController removeAllContentRuleLists];
+    
+    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"test:///fetch_main.html"]]];
+    EXPECT_WK_STREQ([webView _test_waitForAlert], "main frame fetched successfully");
+    EXPECT_WK_STREQ([webView _test_waitForAlert], "iframe fetched successfully");
+
+    auto listWithLoadContext = [] (const char* type) {
+        return makeContentRuleList([NSString stringWithFormat:@"[{\"action\":{\"type\":\"block\"},\"trigger\":{\"url-filter\":\"test\",\"load-context\":[\"%s\"],\"resource-type\":[\"fetch\"]}}]", type]);
+    };
+    [userContentController addContentRuleList:listWithLoadContext("child-frame").get()];
+    [webView reload];
+    EXPECT_WK_STREQ([webView _test_waitForAlert], "main frame fetched successfully");
+    EXPECT_WK_STREQ([webView _test_waitForAlert], "iframe fetch failed");
+
+    [userContentController removeAllContentRuleLists];
+    [userContentController addContentRuleList:listWithLoadContext("top-frame").get()];
+    [webView reload];
+    EXPECT_WK_STREQ([webView _test_waitForAlert], "main frame fetch failed");
+    EXPECT_WK_STREQ([webView _test_waitForAlert], "iframe fetched successfully");
+}
+
 #if HAVE(SSL)
 
 TEST(WebKit, RedirectToPlaintextHTTPSUpgrade)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to