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