Use normal loading path for ping loads
[WebKit-https.git] / Source / WebCore / loader / PingLoader.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  * Copyright (C) 2015 Roopesh Chander (roop@roopc.net)
4  * Copyright (C) 2015-2017 Apple Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  *     * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33
34 #include "config.h"
35 #include "PingLoader.h"
36
37 #include "CachedResourceLoader.h"
38 #include "CachedResourceRequest.h"
39 #include "ContentRuleListResults.h"
40 #include "ContentSecurityPolicy.h"
41 #include "Document.h"
42 #include "Frame.h"
43 #include "FrameLoader.h"
44 #include "FrameLoaderClient.h"
45 #include "InspectorInstrumentation.h"
46 #include "LoaderStrategy.h"
47 #include "NetworkLoadMetrics.h"
48 #include "Page.h"
49 #include "PlatformStrategies.h"
50 #include "ProgressTracker.h"
51 #include "ResourceError.h"
52 #include "ResourceHandle.h"
53 #include "ResourceLoadInfo.h"
54 #include "ResourceRequest.h"
55 #include "ResourceResponse.h"
56 #include "SecurityOrigin.h"
57 #include "SecurityPolicy.h"
58 #include "UserContentController.h"
59 #include <wtf/text/CString.h>
60
61 namespace WebCore {
62
63 #if ENABLE(CONTENT_EXTENSIONS)
64
65 // Returns true if we should block the load.
66 static bool processContentRuleListsForLoad(const Frame& frame, ResourceRequest& request, ContentExtensions::ResourceType resourceType)
67 {
68     auto* documentLoader = frame.loader().documentLoader();
69     if (!documentLoader)
70         return false;
71     auto* page = frame.page();
72     if (!page)
73         return false;
74     auto results = page->userContentProvider().processContentRuleListsForLoad(request.url(), resourceType, *documentLoader);
75     bool result = results.summary.blockedLoad;
76     ContentExtensions::applyResultsToRequest(WTFMove(results), page, request);
77     return result;
78 }
79
80 #endif
81
82 void PingLoader::loadImage(Frame& frame, const URL& url)
83 {
84     ASSERT(frame.document());
85     auto& document = *frame.document();
86
87     if (!document.securityOrigin().canDisplay(url)) {
88         FrameLoader::reportLocalLoadFailed(&frame, url);
89         return;
90     }
91
92     ResourceRequest request(url);
93 #if ENABLE(CONTENT_EXTENSIONS)
94     if (processContentRuleListsForLoad(frame, request, ContentExtensions::ResourceType::Image))
95         return;
96 #endif
97
98     document.contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(request, ContentSecurityPolicy::InsecureRequestType::Load);
99
100     request.setHTTPHeaderField(HTTPHeaderName::CacheControl, "max-age=0");
101
102     HTTPHeaderMap originalRequestHeader = request.httpHeaderFields();
103
104     String referrer = SecurityPolicy::generateReferrerHeader(document.referrerPolicy(), request.url(), frame.loader().outgoingReferrer());
105     if (!referrer.isEmpty())
106         request.setHTTPReferrer(referrer);
107     frame.loader().addExtraFieldsToSubresourceRequest(request);
108
109     startPingLoad(frame, request, WTFMove(originalRequestHeader), ShouldFollowRedirects::Yes, ContentSecurityPolicyImposition::DoPolicyCheck, ReferrerPolicy::EmptyString);
110 }
111
112 // http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#hyperlink-auditing
113 void PingLoader::sendPing(Frame& frame, const URL& pingURL, const URL& destinationURL)
114 {
115     ASSERT(frame.document());
116
117     if (!pingURL.protocolIsInHTTPFamily())
118         return;
119
120     ResourceRequest request(pingURL);
121 #if ENABLE(CONTENT_EXTENSIONS)
122     if (processContentRuleListsForLoad(frame, request, ContentExtensions::ResourceType::Raw))
123         return;
124 #endif
125
126     auto& document = *frame.document();
127     document.contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(request, ContentSecurityPolicy::InsecureRequestType::Load);
128
129     request.setHTTPMethod("POST");
130     request.setHTTPContentType("text/ping");
131     request.setHTTPBody(FormData::create("PING"));
132     request.setHTTPHeaderField(HTTPHeaderName::CacheControl, "max-age=0");
133
134     HTTPHeaderMap originalRequestHeader = request.httpHeaderFields();
135
136     frame.loader().addExtraFieldsToSubresourceRequest(request);
137
138     auto& sourceOrigin = document.securityOrigin();
139     FrameLoader::addHTTPOriginIfNeeded(request, sourceOrigin.toString());
140     request.setHTTPHeaderField(HTTPHeaderName::PingTo, destinationURL);
141     if (!SecurityPolicy::shouldHideReferrer(pingURL, frame.loader().outgoingReferrer())) {
142         request.setHTTPHeaderField(HTTPHeaderName::PingFrom, document.url());
143         if (!sourceOrigin.isSameSchemeHostPort(SecurityOrigin::create(pingURL).get())) {
144             String referrer = SecurityPolicy::generateReferrerHeader(document.referrerPolicy(), pingURL, frame.loader().outgoingReferrer());
145             if (!referrer.isEmpty())
146                 request.setHTTPReferrer(referrer);
147         }
148     }
149
150     startPingLoad(frame, request, WTFMove(originalRequestHeader), ShouldFollowRedirects::Yes, ContentSecurityPolicyImposition::DoPolicyCheck, request.httpReferrer().isEmpty() ? ReferrerPolicy::NoReferrer : ReferrerPolicy::UnsafeUrl);
151 }
152
153 void PingLoader::sendViolationReport(Frame& frame, const URL& reportURL, Ref<FormData>&& report, ViolationReportType reportType)
154 {
155     ASSERT(frame.document());
156
157     ResourceRequest request(reportURL);
158 #if ENABLE(CONTENT_EXTENSIONS)
159     if (processContentRuleListsForLoad(frame, request, ContentExtensions::ResourceType::Raw))
160         return;
161 #endif
162
163     auto& document = *frame.document();
164     document.contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(request, ContentSecurityPolicy::InsecureRequestType::Load);
165
166     request.setHTTPMethod("POST"_s);
167     request.setHTTPBody(WTFMove(report));
168     switch (reportType) {
169     case ViolationReportType::ContentSecurityPolicy:
170         request.setHTTPContentType("application/csp-report"_s);
171         break;
172     case ViolationReportType::XSSAuditor:
173         request.setHTTPContentType("application/json"_s);
174         break;
175     }
176
177     bool removeCookies = true;
178     if (document.securityOrigin().isSameSchemeHostPort(SecurityOrigin::create(reportURL).get()))
179         removeCookies = false;
180     if (removeCookies)
181         request.setAllowCookies(false);
182
183     HTTPHeaderMap originalRequestHeader = request.httpHeaderFields();
184
185     frame.loader().addExtraFieldsToSubresourceRequest(request);
186
187     String referrer = SecurityPolicy::generateReferrerHeader(document.referrerPolicy(), reportURL, frame.loader().outgoingReferrer());
188     if (!referrer.isEmpty())
189         request.setHTTPReferrer(referrer);
190
191     startPingLoad(frame, request, WTFMove(originalRequestHeader), ShouldFollowRedirects::No, ContentSecurityPolicyImposition::SkipPolicyCheck, ReferrerPolicy::EmptyString);
192 }
193
194 void PingLoader::startPingLoad(Frame& frame, ResourceRequest& request, HTTPHeaderMap&& originalRequestHeaders, ShouldFollowRedirects shouldFollowRedirects, ContentSecurityPolicyImposition policyCheck, ReferrerPolicy referrerPolicy)
195 {
196     unsigned long identifier = frame.page()->progress().createUniqueIdentifier();
197     // FIXME: Why activeDocumentLoader? I would have expected documentLoader().
198     // It seems like the PingLoader should be associated with the current
199     // Document in the Frame, but the activeDocumentLoader will be associated
200     // with the provisional DocumentLoader if there is a provisional
201     // DocumentLoader.
202     bool shouldUseCredentialStorage = frame.loader().client().shouldUseCredentialStorage(frame.loader().activeDocumentLoader(), identifier);
203     ResourceLoaderOptions options;
204     options.credentials = shouldUseCredentialStorage ? FetchOptions::Credentials::Include : FetchOptions::Credentials::Omit;
205     options.redirect = shouldFollowRedirects == ShouldFollowRedirects::Yes ? FetchOptions::Redirect::Follow : FetchOptions::Redirect::Error;
206     options.keepAlive = true;
207     options.contentSecurityPolicyImposition = policyCheck;
208     options.referrerPolicy = referrerPolicy;
209     options.sendLoadCallbacks = SendCallbackPolicy::SendCallbacks;
210     options.cache = FetchOptions::Cache::NoCache;
211
212     // FIXME: Deprecate the ping load code path.
213     if (platformStrategies()->loaderStrategy()->usePingLoad()) {
214         InspectorInstrumentation::willSendRequestOfType(&frame, identifier, frame.loader().activeDocumentLoader(), request, InspectorInstrumentation::LoadType::Ping);
215
216         platformStrategies()->loaderStrategy()->startPingLoad(frame, request, WTFMove(originalRequestHeaders), options, policyCheck, [protectedFrame = makeRef(frame), identifier] (const ResourceError& error, const ResourceResponse& response) {
217             if (!response.isNull())
218                 InspectorInstrumentation::didReceiveResourceResponse(protectedFrame, identifier, protectedFrame->loader().activeDocumentLoader(), response, nullptr);
219             if (!error.isNull()) {
220                 InspectorInstrumentation::didFailLoading(protectedFrame.ptr(), protectedFrame->loader().activeDocumentLoader(), identifier, error);
221                 return;
222             }
223             InspectorInstrumentation::didFinishLoading(protectedFrame.ptr(), protectedFrame->loader().activeDocumentLoader(), identifier, { }, nullptr);
224         });
225         return;
226     }
227
228     CachedResourceRequest cachedResourceRequest { ResourceRequest { request }, options };
229     frame.document()->cachedResourceLoader().requestPingResource(WTFMove(cachedResourceRequest));
230 }
231
232 }