Bug 219001 - Allow WKContentRuleList to block only in frames or only in main frame
Summary: Allow WKContentRuleList to block only in frames or only in main frame
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Alex Christensen
URL:
Keywords: InRadar
: 153559 (view as bug list)
Depends on:
Blocks:
 
Reported: 2020-11-16 12:36 PST by Alex Christensen
Modified: 2021-05-19 14:20 PDT (History)
14 users (show)

See Also:


Attachments
Patch (13.34 KB, patch)
2020-11-16 12:37 PST, Alex Christensen
no flags Details | Formatted Diff | Diff
Patch (23.31 KB, patch)
2020-12-21 18:43 PST, Alex Christensen
no flags Details | Formatted Diff | Diff
Patch (28.03 KB, patch)
2021-03-24 13:32 PDT, Alex Christensen
no flags Details | Formatted Diff | Diff
Patch (27.84 KB, patch)
2021-03-24 17:27 PDT, Alex Christensen
no flags Details | Formatted Diff | Diff
Patch (28.01 KB, patch)
2021-03-25 20:41 PDT, Alex Christensen
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Alex Christensen 2020-11-16 12:36:59 PST
Add top-document and iframe-document resource types to WKContentRuleList syntax
Comment 1 Alex Christensen 2020-11-16 12:37:53 PST
Created attachment 414271 [details]
Patch
Comment 2 Radar WebKit Bug Importer 2020-11-26 03:20:50 PST
<rdar://problem/71747736>
Comment 3 Alex Christensen 2020-12-21 18:43:04 PST
Created attachment 416645 [details]
Patch
Comment 4 Benjamin Poulain 2020-12-22 13:38:33 PST
I think we can fulfill the original request while making something more powerful.

Instead of creating a new resource type, add a new field that specify the context: All, MainFrame, Iframe, SubFrame. Something like that.

The whole rule would be ignored if not in the right context.

This would allow any rule to be applied specifically based on the context. I assume it is common to block things when in frames but not in the top document (say tracking cookies from Facebook for example).

The downside is more useless matching if we filter a posteriori. If that becomes an issue, it can be fixed by splitting the filter by context a priori.

What do you think?
Comment 5 Alex Christensen 2020-12-22 16:40:43 PST
Sounds like something to discuss next year.  That might be necessary to fix rdar://problem/72203352 too.
Comment 6 Alex Christensen 2021-02-02 14:31:33 PST
That would not allow us the same functionality as having a nested-frame resource type.  An iframe's main resource is loaded in the context of its parent frame, so we would be unable to specify that we would like to block iframe main resources with your suggestion.
Comment 7 Alex Christensen 2021-03-24 13:32:17 PDT
Created attachment 424175 [details]
Patch
Comment 8 Geoffrey Garen 2021-03-24 13:58:29 PDT
Comment on attachment 424175 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=424175&action=review

> Source/WebCore/loader/ResourceLoadInfo.cpp:132
> +    if (name == "iframe")
> +        return { LoadContext::IFrame };

I think 'iframe' is the wrong name here because 'iframe' implies only <iframe> elements, but child frames can also be <frame>, <object>, or <embed>. 

Here's an alternative that matches web speak a little more: { top-frame, child-frame }

Here's an alternative that matches our parlance a little more: { main-frame, sub-frame }

I'd be happy with either of those, or something else that avoids 'iframe'. I don't think we should do 'iframe'.
Comment 9 Alex Christensen 2021-03-24 17:27:08 PDT
Created attachment 424204 [details]
Patch
Comment 10 Benjamin Poulain 2021-03-25 19:42:50 PDT
Comment on attachment 424204 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=424204&action=review

> Source/WebCore/contentextensions/ContentExtensionsBackend.cpp:271
> +    ResourceLoadInfo resourceLoadInfo = { url, mainDocumentURL, { ResourceType::Ping, ResourceType::Other } };

Why not add ping to the "other" resource type in readResourceType?
Wouldn't this be more consistent with the other loaded resources?

> Source/WebCore/loader/PingLoader.cpp:125
> +    if (processContentRuleListsForLoad(frame, request, { ContentExtensions::ResourceType::Other, ContentExtensions::ResourceType::Ping }))

Ditto here regarding ping flags.

> Tools/TestWebKitAPI/Tests/WebKitCocoa/ContentRuleListNotification.mm:307
> +    auto handler = [[TestURLSchemeHandler new] autorelease];

I hope TEST has an autorelease pool :-D

> Tools/TestWebKitAPI/Tests/WebKitCocoa/ContentRuleListNotification.mm:373
> +    [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");

I like those tests!
Comment 11 Alex Christensen 2021-03-25 20:41:02 PDT
Created attachment 424314 [details]
Patch
Comment 12 EWS 2021-03-25 22:20:58 PDT
Ben Poulain found in /Volumes/Data/worker/Commit-Queue/build/Source/WebCore/ChangeLog does not appear to be a valid reviewer according to contributors.json.
/Volumes/Data/worker/Commit-Queue/build/Source/WebCore/ChangeLog neither lists a valid reviewer nor contains the string "Unreviewed" or "Rubber stamp" (case insensitive).
Comment 13 Benjamin Poulain 2021-03-26 12:49:06 PDT
Update looks good to me!

(In reply to EWS from comment #12)
> Ben Poulain found in
> /Volumes/Data/worker/Commit-Queue/build/Source/WebCore/ChangeLog does not
> appear to be a valid reviewer according to contributors.json.
> /Volumes/Data/worker/Commit-Queue/build/Source/WebCore/ChangeLog neither
> lists a valid reviewer nor contains the string "Unreviewed" or "Rubber
> stamp" (case insensitive).

You may need to use my full name.
Comment 14 Alex Christensen 2021-03-29 13:29:04 PDT
r275078
Comment 15 Alex Christensen 2021-05-19 14:20:08 PDT
*** Bug 153559 has been marked as a duplicate of this bug. ***