| 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, OptionSet<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 = 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, ContentExtensions::ResourceType::Ping })) |
| 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 | request.setPriority(ResourceLoadPriority::VeryLow); |
| 134 | |
| 135 | HTTPHeaderMap = request.httpHeaderFields(); |
| 136 | |
| 137 | frame.loader().addExtraFieldsToSubresourceRequest(request); |
| 138 | |
| 139 | auto& sourceOrigin = document.securityOrigin(); |
| 140 | FrameLoader::addHTTPOriginIfNeeded(request, sourceOrigin.toString()); |
| 141 | request.setHTTPHeaderField(HTTPHeaderName::PingTo, destinationURL); |
| 142 | if (!SecurityPolicy::shouldHideReferrer(pingURL, frame.loader().outgoingReferrer())) { |
| 143 | request.setHTTPHeaderField(HTTPHeaderName::PingFrom, document.url()); |
| 144 | if (!sourceOrigin.isSameSchemeHostPort(SecurityOrigin::create(pingURL).get())) { |
| 145 | String referrer = SecurityPolicy::generateReferrerHeader(document.referrerPolicy(), pingURL, frame.loader().outgoingReferrer()); |
| 146 | if (!referrer.isEmpty()) |
| 147 | request.setHTTPReferrer(referrer); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | startPingLoad(frame, request, WTFMove(originalRequestHeader), ShouldFollowRedirects::Yes, ContentSecurityPolicyImposition::DoPolicyCheck, request.httpReferrer().isEmpty() ? ReferrerPolicy::NoReferrer : ReferrerPolicy::UnsafeUrl); |
| 152 | } |
| 153 | |
| 154 | void PingLoader::sendViolationReport(Frame& frame, const URL& reportURL, Ref<FormData>&& report, ViolationReportType reportType) |
| 155 | { |
| 156 | ASSERT(frame.document()); |
| 157 | |
| 158 | ResourceRequest request(reportURL); |
| 159 | #if ENABLE(CONTENT_EXTENSIONS) |
| 160 | if (processContentRuleListsForLoad(frame, request, ContentExtensions::ResourceType::Raw)) |
| 161 | return; |
| 162 | #endif |
| 163 | |
| 164 | auto& document = *frame.document(); |
| 165 | document.contentSecurityPolicy()->upgradeInsecureRequestIfNeeded(request, ContentSecurityPolicy::InsecureRequestType::Load); |
| 166 | |
| 167 | request.setHTTPMethod("POST"_s ); |
| 168 | request.setHTTPBody(WTFMove(report)); |
| 169 | switch (reportType) { |
| 170 | case ViolationReportType::ContentSecurityPolicy: |
| 171 | request.setHTTPContentType("application/csp-report"_s ); |
| 172 | break; |
| 173 | case ViolationReportType::XSSAuditor: |
| 174 | request.setHTTPContentType("application/json"_s ); |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | bool removeCookies = true; |
| 179 | if (document.securityOrigin().isSameSchemeHostPort(SecurityOrigin::create(reportURL).get())) |
| 180 | removeCookies = false; |
| 181 | if (removeCookies) |
| 182 | request.setAllowCookies(false); |
| 183 | |
| 184 | HTTPHeaderMap = request.httpHeaderFields(); |
| 185 | |
| 186 | frame.loader().addExtraFieldsToSubresourceRequest(request); |
| 187 | |
| 188 | String referrer = SecurityPolicy::generateReferrerHeader(document.referrerPolicy(), reportURL, frame.loader().outgoingReferrer()); |
| 189 | if (!referrer.isEmpty()) |
| 190 | request.setHTTPReferrer(referrer); |
| 191 | |
| 192 | startPingLoad(frame, request, WTFMove(originalRequestHeader), ShouldFollowRedirects::No, ContentSecurityPolicyImposition::SkipPolicyCheck, ReferrerPolicy::EmptyString); |
| 193 | } |
| 194 | |
| 195 | void PingLoader::(Frame& frame, ResourceRequest& request, HTTPHeaderMap&& , ShouldFollowRedirects shouldFollowRedirects, ContentSecurityPolicyImposition policyCheck, ReferrerPolicy referrerPolicy) |
| 196 | { |
| 197 | unsigned long identifier = frame.page()->progress().createUniqueIdentifier(); |
| 198 | // FIXME: Why activeDocumentLoader? I would have expected documentLoader(). |
| 199 | // It seems like the PingLoader should be associated with the current |
| 200 | // Document in the Frame, but the activeDocumentLoader will be associated |
| 201 | // with the provisional DocumentLoader if there is a provisional |
| 202 | // DocumentLoader. |
| 203 | bool shouldUseCredentialStorage = frame.loader().client().shouldUseCredentialStorage(frame.loader().activeDocumentLoader(), identifier); |
| 204 | ResourceLoaderOptions options; |
| 205 | options.credentials = shouldUseCredentialStorage ? FetchOptions::Credentials::Include : FetchOptions::Credentials::Omit; |
| 206 | options.redirect = shouldFollowRedirects == ShouldFollowRedirects::Yes ? FetchOptions::Redirect::Follow : FetchOptions::Redirect::Error; |
| 207 | options.keepAlive = true; |
| 208 | options.contentSecurityPolicyImposition = policyCheck; |
| 209 | options.referrerPolicy = referrerPolicy; |
| 210 | options.sendLoadCallbacks = SendCallbackPolicy::SendCallbacks; |
| 211 | options.cache = FetchOptions::Cache::NoCache; |
| 212 | |
| 213 | // FIXME: Deprecate the ping load code path. |
| 214 | if (platformStrategies()->loaderStrategy()->usePingLoad()) { |
| 215 | InspectorInstrumentation::willSendRequestOfType(&frame, identifier, frame.loader().activeDocumentLoader(), request, InspectorInstrumentation::LoadType::Ping); |
| 216 | |
| 217 | platformStrategies()->loaderStrategy()->startPingLoad(frame, request, WTFMove(originalRequestHeaders), options, policyCheck, [protectedFrame = makeRef(frame), identifier] (const ResourceError& error, const ResourceResponse& response) { |
| 218 | if (!response.isNull()) |
| 219 | InspectorInstrumentation::didReceiveResourceResponse(protectedFrame, identifier, protectedFrame->loader().activeDocumentLoader(), response, nullptr); |
| 220 | if (!error.isNull()) { |
| 221 | InspectorInstrumentation::didFailLoading(protectedFrame.ptr(), protectedFrame->loader().activeDocumentLoader(), identifier, error); |
| 222 | return; |
| 223 | } |
| 224 | InspectorInstrumentation::didFinishLoading(protectedFrame.ptr(), protectedFrame->loader().activeDocumentLoader(), identifier, { }, nullptr); |
| 225 | }); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | CachedResourceRequest cachedResourceRequest { ResourceRequest { request }, options }; |
| 230 | frame.document()->cachedResourceLoader().requestPingResource(WTFMove(cachedResourceRequest)); |
| 231 | } |
| 232 | |
| 233 | } |
| 234 | |