[Beacon] Add support for CORS-preflighting for WK2 / NETWORK_SESSION
[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, 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     String referrer = SecurityPolicy::generateReferrerHeader(document.referrerPolicy(), request.url(), frame.loader().outgoingReferrer());
102     if (!referrer.isEmpty())
103         request.setHTTPReferrer(referrer);
104     frame.loader().addExtraFieldsToSubresourceRequest(request);
105
106     startPingLoad(frame, request, document.securityOrigin(), ShouldFollowRedirects::Yes);
107 }
108
109 // http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#hyperlink-auditing
110 void PingLoader::sendPing(Frame& frame, const URL& pingURL, const URL& destinationURL)
111 {
112     ASSERT(frame.document());
113
114     if (!pingURL.protocolIsInHTTPFamily())
115         return;
116
117     ResourceRequest request(pingURL);
118     if (processContentExtensionRulesForLoad(frame, request, ResourceType::Raw))
119         return;
120
121     auto& document = *frame.document();
122     document.contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(request, ContentSecurityPolicy::InsecureRequestType::Load);
123
124     request.setHTTPMethod("POST");
125     request.setHTTPContentType("text/ping");
126     request.setHTTPBody(FormData::create("PING"));
127     request.setHTTPHeaderField(HTTPHeaderName::CacheControl, "max-age=0");
128     frame.loader().addExtraFieldsToSubresourceRequest(request);
129
130     auto& sourceOrigin = document.securityOrigin();
131     FrameLoader::addHTTPOriginIfNeeded(request, sourceOrigin.toString());
132     request.setHTTPHeaderField(HTTPHeaderName::PingTo, destinationURL);
133     if (!SecurityPolicy::shouldHideReferrer(pingURL, frame.loader().outgoingReferrer())) {
134         request.setHTTPHeaderField(HTTPHeaderName::PingFrom, document.url());
135         if (!sourceOrigin.isSameSchemeHostPort(SecurityOrigin::create(pingURL).get())) {
136             String referrer = SecurityPolicy::generateReferrerHeader(document.referrerPolicy(), pingURL, frame.loader().outgoingReferrer());
137             if (!referrer.isEmpty())
138                 request.setHTTPReferrer(referrer);
139         }
140     }
141
142     startPingLoad(frame, request, sourceOrigin, ShouldFollowRedirects::Yes);
143 }
144
145 void PingLoader::sendViolationReport(Frame& frame, const URL& reportURL, Ref<FormData>&& report, ViolationReportType reportType)
146 {
147     ASSERT(frame.document());
148
149     ResourceRequest request(reportURL);
150     if (processContentExtensionRulesForLoad(frame, request, ResourceType::Raw))
151         return;
152
153     auto& document = *frame.document();
154     document.contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(request, ContentSecurityPolicy::InsecureRequestType::Load);
155
156     request.setHTTPMethod(ASCIILiteral("POST"));
157     request.setHTTPBody(WTFMove(report));
158     switch (reportType) {
159     case ViolationReportType::ContentSecurityPolicy:
160         request.setHTTPContentType(ASCIILiteral("application/csp-report"));
161         break;
162     case ViolationReportType::XSSAuditor:
163         request.setHTTPContentType(ASCIILiteral("application/json"));
164         break;
165     }
166
167     bool removeCookies = true;
168     if (document.securityOrigin().isSameSchemeHostPort(SecurityOrigin::create(reportURL).get()))
169         removeCookies = false;
170     if (removeCookies)
171         request.setAllowCookies(false);
172
173     frame.loader().addExtraFieldsToSubresourceRequest(request);
174
175     String referrer = SecurityPolicy::generateReferrerHeader(document.referrerPolicy(), reportURL, frame.loader().outgoingReferrer());
176     if (!referrer.isEmpty())
177         request.setHTTPReferrer(referrer);
178
179     startPingLoad(frame, request, document.securityOrigin(), ShouldFollowRedirects::No);
180 }
181
182 void PingLoader::startPingLoad(Frame& frame, ResourceRequest& request, SecurityOrigin& sourceOrigin, ShouldFollowRedirects shouldFollowRedirects)
183 {
184     unsigned long identifier = frame.page()->progress().createUniqueIdentifier();
185     // FIXME: Why activeDocumentLoader? I would have expected documentLoader().
186     // Itseems like the PingLoader should be associated with the current
187     // Document in the Frame, but the activeDocumentLoader will be associated
188     // with the provisional DocumentLoader if there is a provisional
189     // DocumentLoader.
190     bool shouldUseCredentialStorage = frame.loader().client().shouldUseCredentialStorage(frame.loader().activeDocumentLoader(), identifier);
191     FetchOptions options;
192     options.credentials = shouldUseCredentialStorage ? FetchOptions::Credentials::Include : FetchOptions::Credentials::Omit;
193     options.redirect = shouldFollowRedirects == ShouldFollowRedirects::Yes ? FetchOptions::Redirect::Follow : FetchOptions::Redirect::Error;
194
195     InspectorInstrumentation::continueAfterPingLoader(frame, identifier, frame.loader().activeDocumentLoader(), request, ResourceResponse());
196
197     platformStrategies()->loaderStrategy()->createPingHandle(frame.loader().networkingContext(), request, sourceOrigin, options);
198 }
199
200 }