Skip to content

Commit ce84da3

Browse files
committed
Link preconnect and DNS prefetch ignores content extension rules
https://bugs.webkit.org/show_bug.cgi?id=289572 rdar://144699810 Reviewed by Alex Christensen. Make content extension rules apply to DNS prefetch and preconnect, just as they already do for prefetch and normal loads. * LayoutTests/http/tests/contentextensions/dns-prefetch-blocked-expected.txt: Added. * LayoutTests/http/tests/contentextensions/dns-prefetch-blocked.html: Added. * LayoutTests/http/tests/contentextensions/dns-prefetch-blocked.html.json: Added. * LayoutTests/http/tests/contentextensions/preconnect-blocked-expected.txt: Added. * LayoutTests/http/tests/contentextensions/preconnect-blocked.html: Added. * LayoutTests/http/tests/contentextensions/preconnect-blocked.html.json: Added. * Source/WebCore/loader/FrameLoader.cpp: (WebCore::FrameLoader::prefetchDNSIfNeeded): * Source/WebCore/loader/FrameLoader.h: * Source/WebCore/loader/LinkLoader.cpp: (WebCore::LinkLoader::preconnectIfNeeded): (WebCore::LinkLoader::loadLink): * Source/WebCore/loader/LoaderStrategy.h: * Source/WebKit/WebProcess/Network/WebLoaderStrategy.cpp: (WebKit::WebLoaderStrategy::preconnectTo): * Source/WebKit/WebProcess/Network/WebLoaderStrategy.h: * Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm: (WebKit::WebPage::sendTapHighlightForNodeIfNecessary): * Source/WebKitLegacy/WebCoreSupport/WebResourceLoadScheduler.cpp: (WebResourceLoadScheduler::preconnectTo): * Source/WebKitLegacy/WebCoreSupport/WebResourceLoadScheduler.h: Originally-landed-as: 289651.274@safari-7621-branch (3af4bf0). rdar://151707262 Canonical link: https://commits.webkit.org/295844@main
1 parent d59f05a commit ce84da3

File tree

16 files changed

+105
-24
lines changed

16 files changed

+105
-24
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONSOLE MESSAGE: Content blocker prevented frame displaying http://127.0.0.1:8000/contentextensions/dns-prefetch-blocked.html from loading a resource from http://127.0.0.1:8000/contentextensions/resources/should-not-load.html
2+
This page prefetches a Document.
3+
The prefetch should be blocked by the content extension filter
4+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This page prefetches a Document.<br>
2+
The prefetch should be blocked by the content extension filter<br>
3+
<script>
4+
if (window.testRunner) {
5+
testRunner.dumpAsText();
6+
}
7+
</script>
8+
<link rel="dns-prefetch" href="resources/should-not-load.html">
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"action": {
4+
"type": "block"
5+
},
6+
"trigger": {
7+
"url-filter": "should-not-load",
8+
"resource-type": ["ping"]
9+
}
10+
}
11+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONSOLE MESSAGE: Content blocker prevented frame displaying http://127.0.0.1:8000/contentextensions/preconnect-blocked.html from loading a resource from http://127.0.0.1:8000/contentextensions/resources/should-not-load.html
2+
This page preconnects a Document.
3+
The preconnect should be blocked by the content extension filter
4+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This page preconnects a Document.<br>
2+
The preconnect should be blocked by the content extension filter<br>
3+
<script>
4+
if (window.testRunner) {
5+
testRunner.dumpAsText();
6+
}
7+
</script>
8+
<link rel="preconnect" href="resources/should-not-load.html">
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"action": {
4+
"type": "block"
5+
},
6+
"trigger": {
7+
"url-filter": "should-not-load",
8+
"resource-type": ["ping"]
9+
}
10+
}
11+
]

Source/WebCore/html/HTMLAnchorElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ void HTMLAnchorElement::handleClick(Event& event)
599599
// Preconnect to the link's target for improved page load time.
600600
if (completedURL.protocolIsInHTTPFamily() && document->settings().linkPreconnectEnabled() && ((frame->isMainFrame() && isSelfTargetFrameName(effectiveTarget)) || isBlankTargetFrameName(effectiveTarget))) {
601601
auto storageCredentialsPolicy = frame->page() && frame->page()->canUseCredentialStorage() ? StoredCredentialsPolicy::Use : StoredCredentialsPolicy::DoNotUse;
602-
platformStrategies()->loaderStrategy()->preconnectTo(frame->loader(), completedURL, storageCredentialsPolicy, LoaderStrategy::ShouldPreconnectAsFirstParty::Yes, [] (ResourceError) { });
602+
platformStrategies()->loaderStrategy()->preconnectTo(frame->loader(), ResourceRequest { WTFMove(completedURL) }, storageCredentialsPolicy, LoaderStrategy::ShouldPreconnectAsFirstParty::Yes, [] (ResourceError) { });
603603
}
604604
}
605605

Source/WebCore/loader/FrameLoader.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4841,6 +4841,26 @@ RefPtr<DocumentLoader> FrameLoader::loaderForWebsitePolicies(CanIncludeCurrentDo
48414841
return loader;
48424842
}
48434843

4844+
void FrameLoader::prefetchDNSIfNeeded(const URL& url)
4845+
{
4846+
#if ENABLE(CONTENT_EXTENSIONS)
4847+
RefPtr page = m_frame->page();
4848+
if (!page)
4849+
return;
4850+
4851+
RefPtr documentLoader = m_documentLoader;
4852+
if (!documentLoader)
4853+
return;
4854+
4855+
auto results = page->protectedUserContentProvider()->processContentRuleListsForLoad(*page, url, ContentExtensions::ResourceType::Ping, *documentLoader);
4856+
if (results.summary.blockedLoad)
4857+
return;
4858+
#endif
4859+
4860+
if (url.isValid() && !url.isEmpty() && url.protocolIsInHTTPFamily())
4861+
client().prefetchDNS(url.host().toString());
4862+
}
4863+
48444864
} // namespace WebCore
48454865

48464866
#undef PAGE_ID

Source/WebCore/loader/FrameLoader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ class FrameLoader final : public CanMakeWeakPtr<FrameLoader> {
361361
void setRequiredCookiesVersion(uint64_t version) { m_requiredCookiesVersion = version; }
362362
uint64_t requiredCookiesVersion() const { return m_requiredCookiesVersion; }
363363

364+
WEBCORE_EXPORT void prefetchDNSIfNeeded(const URL&);
365+
364366
private:
365367
enum FormSubmissionCacheLoadPolicy {
366368
MayAttemptCacheOnlyLoadForFormSubmissionItem,

Source/WebCore/loader/LinkLoader.cpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include "Settings.h"
6363
#include "SizesAttributeParser.h"
6464
#include "StyleResolver.h"
65+
#include "UserContentProvider.h"
6566
#include <wtf/text/MakeString.h>
6667

6768
namespace WebCore {
@@ -277,15 +278,32 @@ bool LinkLoader::isSupportedType(CachedResource::Type resourceType, const String
277278

278279
void LinkLoader::preconnectIfNeeded(const LinkLoadParameters& params, Document& document)
279280
{
280-
const URL href = params.href;
281-
if (!params.relAttribute.isLinkPreconnect || !href.isValid() || !params.href.protocolIsInHTTPFamily() || !document.frame())
281+
if (!params.relAttribute.isLinkPreconnect || !params.href.isValid() || !params.href.protocolIsInHTTPFamily() || !document.frame())
282282
return;
283+
284+
ResourceRequest request { URL { params.href } };
285+
#if ENABLE(CONTENT_EXTENSIONS)
286+
RefPtr page = document.page();
287+
if (!page)
288+
return;
289+
290+
RefPtr documentLoader = document.loader();
291+
if (!documentLoader)
292+
return;
293+
294+
auto results = page->protectedUserContentProvider()->processContentRuleListsForLoad(*page, params.href, ContentExtensions::ResourceType::Ping, *documentLoader);
295+
if (results.summary.blockedLoad)
296+
return;
297+
298+
ContentExtensions::applyResultsToRequest(WTFMove(results), page.get(), request);
299+
#endif
300+
283301
ASSERT(document.settings().linkPreconnectEnabled());
284302
StoredCredentialsPolicy storageCredentialsPolicy = StoredCredentialsPolicy::Use;
285-
if (equalLettersIgnoringASCIICase(params.crossOrigin, "anonymous"_s) && !document.protectedSecurityOrigin()->isSameOriginDomain(SecurityOrigin::create(href)))
303+
if (equalLettersIgnoringASCIICase(params.crossOrigin, "anonymous"_s) && !document.protectedSecurityOrigin()->isSameOriginDomain(SecurityOrigin::create(params.href)))
286304
storageCredentialsPolicy = StoredCredentialsPolicy::DoNotUse;
287305
ASSERT(document.frame()->loader().networkingContext());
288-
platformStrategies()->loaderStrategy()->preconnectTo(document.protectedFrame()->protectedLoader(), href, storageCredentialsPolicy, LoaderStrategy::ShouldPreconnectAsFirstParty::No, [weakDocument = WeakPtr { document }, href](ResourceError error) {
306+
platformStrategies()->loaderStrategy()->preconnectTo(document.protectedFrame()->protectedLoader(), WTFMove(request), storageCredentialsPolicy, LoaderStrategy::ShouldPreconnectAsFirstParty::No, [weakDocument = WeakPtr { document }, href = params.href](ResourceError error) {
289307
RefPtr document = weakDocument.get();
290308
if (!document)
291309
return;
@@ -417,13 +435,11 @@ void LinkLoader::cancelLoad()
417435
void LinkLoader::loadLink(const LinkLoadParameters& params, Document& document)
418436
{
419437
if (params.relAttribute.isDNSPrefetch) {
420-
if (params.href.isValid() && !params.href.isEmpty() && params.href.protocolIsInHTTPFamily() && document.frame())
421-
document.protectedFrame()->protectedLoader()->client().prefetchDNS(params.href.host().toString());
422-
}
423-
424-
preconnectIfNeeded(params, document);
425-
426-
if (params.relAttribute.isLinkPrefetch) {
438+
if (RefPtr frame = document.frame())
439+
frame->loader().prefetchDNSIfNeeded(params.href);
440+
} else if (params.relAttribute.isLinkPreconnect)
441+
preconnectIfNeeded(params, document);
442+
else if (params.relAttribute.isLinkPrefetch) {
427443
prefetchIfNeeded(params, document);
428444
return;
429445
}

0 commit comments

Comments
 (0)