80d67e3382220b7933235ea614f6a2a11e8ce214
[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 "ContentSecurityPolicy.h"
38 #include "Document.h"
39 #include "Frame.h"
40 #include "FrameLoader.h"
41 #include "FrameLoaderClient.h"
42 #include "InspectorInstrumentation.h"
43 #include "LoaderStrategy.h"
44 #include "Page.h"
45 #include "PlatformStrategies.h"
46 #include "ProgressTracker.h"
47 #include "ResourceHandle.h"
48 #include "ResourceLoadInfo.h"
49 #include "ResourceRequest.h"
50 #include "ResourceResponse.h"
51 #include "SecurityOrigin.h"
52 #include "SecurityPolicy.h"
53 #include "UserContentController.h"
54 #include <wtf/text/CString.h>
55
56 namespace WebCore {
57
58 #if !ENABLE(CONTENT_EXTENSIONS)
59
60 // Returns true if we should block the load.
61 static inline bool processContentExtensionRulesForLoad(const Frame&, ResourceRequest&, ResourceType)
62 {
63     return false;
64 }
65
66 #else
67
68 // Returns true if we should block the load.
69 static bool processContentExtensionRulesForLoad(const Frame& frame, ResourceRequest& request, ResourceType resourceType)
70 {
71     auto* documentLoader = frame.loader().documentLoader();
72     if (!documentLoader)
73         return false;
74     auto* page = frame.page();
75     if (!page)
76         return false;
77     auto status = page->userContentProvider().processContentExtensionRulesForLoad(request.url(), resourceType, *documentLoader);
78     applyBlockedStatusToRequest(status, page, request);
79     return status.blockedLoad;
80 }
81
82 #endif
83
84 void PingLoader::loadImage(Frame& frame, const URL& url)
85 {
86     ASSERT(frame.document());
87     auto& document = *frame.document();
88
89     if (!document.securityOrigin().canDisplay(url)) {
90         FrameLoader::reportLocalLoadFailed(&frame, url);
91         return;
92     }
93
94     ResourceRequest request(url);
95     if (processContentExtensionRulesForLoad(frame, request, ResourceType::Image))
96         return;
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);
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 (processContentExtensionRulesForLoad(frame, request, ResourceType::Raw))
122         return;
123
124     auto& document = *frame.document();
125     document.contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(request, ContentSecurityPolicy::InsecureRequestType::Load);
126
127     request.setHTTPMethod("POST");
128     request.setHTTPContentType("text/ping");
129     request.setHTTPBody(FormData::create("PING"));
130     request.setHTTPHeaderField(HTTPHeaderName::CacheControl, "max-age=0");
131
132     HTTPHeaderMap originalRequestHeader = request.httpHeaderFields();
133
134     frame.loader().addExtraFieldsToSubresourceRequest(request);
135
136     auto& sourceOrigin = document.securityOrigin();
137     FrameLoader::addHTTPOriginIfNeeded(request, sourceOrigin.toString());
138     request.setHTTPHeaderField(HTTPHeaderName::PingTo, destinationURL);
139     if (!SecurityPolicy::shouldHideReferrer(pingURL, frame.loader().outgoingReferrer())) {
140         request.setHTTPHeaderField(HTTPHeaderName::PingFrom, document.url());
141         if (!sourceOrigin.isSameSchemeHostPort(SecurityOrigin::create(pingURL).get())) {
142             String referrer = SecurityPolicy::generateReferrerHeader(document.referrerPolicy(), pingURL, frame.loader().outgoingReferrer());
143             if (!referrer.isEmpty())
144                 request.setHTTPReferrer(referrer);
145         }
146     }
147
148     startPingLoad(frame, request, WTFMove(originalRequestHeader), ShouldFollowRedirects::Yes);
149 }
150
151 void PingLoader::sendViolationReport(Frame& frame, const URL& reportURL, Ref<FormData>&& report, ViolationReportType reportType)
152 {
153     ASSERT(frame.document());
154
155     ResourceRequest request(reportURL);
156     if (processContentExtensionRulesForLoad(frame, request, ResourceType::Raw))
157         return;
158
159     auto& document = *frame.document();
160     document.contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(request, ContentSecurityPolicy::InsecureRequestType::Load);
161
162     request.setHTTPMethod(ASCIILiteral("POST"));
163     request.setHTTPBody(WTFMove(report));
164     switch (reportType) {
165     case ViolationReportType::ContentSecurityPolicy:
166         request.setHTTPContentType(ASCIILiteral("application/csp-report"));
167         break;
168     case ViolationReportType::XSSAuditor:
169         request.setHTTPContentType(ASCIILiteral("application/json"));
170         break;
171     }
172
173     bool removeCookies = true;
174     if (document.securityOrigin().isSameSchemeHostPort(SecurityOrigin::create(reportURL).get()))
175         removeCookies = false;
176     if (removeCookies)
177         request.setAllowCookies(false);
178
179     HTTPHeaderMap originalRequestHeader = request.httpHeaderFields();
180
181     frame.loader().addExtraFieldsToSubresourceRequest(request);
182
183     String referrer = SecurityPolicy::generateReferrerHeader(document.referrerPolicy(), reportURL, frame.loader().outgoingReferrer());
184     if (!referrer.isEmpty())
185         request.setHTTPReferrer(referrer);
186
187     startPingLoad(frame, request, WTFMove(originalRequestHeader), ShouldFollowRedirects::No);
188 }
189
190 void PingLoader::startPingLoad(Frame& frame, ResourceRequest& request, HTTPHeaderMap&& originalRequestHeaders, ShouldFollowRedirects shouldFollowRedirects)
191 {
192     unsigned long identifier = frame.page()->progress().createUniqueIdentifier();
193     // FIXME: Why activeDocumentLoader? I would have expected documentLoader().
194     // Itseems like the PingLoader should be associated with the current
195     // Document in the Frame, but the activeDocumentLoader will be associated
196     // with the provisional DocumentLoader if there is a provisional
197     // DocumentLoader.
198     bool shouldUseCredentialStorage = frame.loader().client().shouldUseCredentialStorage(frame.loader().activeDocumentLoader(), identifier);
199     FetchOptions options;
200     options.credentials = shouldUseCredentialStorage ? FetchOptions::Credentials::Include : FetchOptions::Credentials::Omit;
201     options.redirect = shouldFollowRedirects == ShouldFollowRedirects::Yes ? FetchOptions::Redirect::Follow : FetchOptions::Redirect::Error;
202
203     InspectorInstrumentation::continueAfterPingLoader(frame, identifier, frame.loader().activeDocumentLoader(), request, ResourceResponse());
204
205     platformStrategies()->loaderStrategy()->startPingLoad(frame, request, WTFMove(originalRequestHeaders), options);
206 }
207
208 }