0a1b2b0e0d31ec514d910cdd3546aa666d071289
[WebKit-https.git] / Source / WebCore / loader / DocumentThreadableLoader.cpp
1 /*
2  * Copyright (C) 2011, 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "DocumentThreadableLoader.h"
33
34 #include "CachedRawResource.h"
35 #include "CachedResourceLoader.h"
36 #include "CachedResourceRequest.h"
37 #include "CachedResourceRequestInitiators.h"
38 #include "CrossOriginAccessControl.h"
39 #include "CrossOriginPreflightChecker.h"
40 #include "CrossOriginPreflightResultCache.h"
41 #include "DOMWindow.h"
42 #include "Document.h"
43 #include "Frame.h"
44 #include "FrameLoader.h"
45 #include "InspectorInstrumentation.h"
46 #include "LoadTiming.h"
47 #include "LoaderStrategy.h"
48 #include "Performance.h"
49 #include "ProgressTracker.h"
50 #include "ResourceError.h"
51 #include "ResourceRequest.h"
52 #include "ResourceTiming.h"
53 #include "RuntimeEnabledFeatures.h"
54 #include "SchemeRegistry.h"
55 #include "SecurityOrigin.h"
56 #include "SharedBuffer.h"
57 #include "SubresourceIntegrity.h"
58 #include "SubresourceLoader.h"
59 #include "ThreadableLoaderClient.h"
60 #include <wtf/Assertions.h>
61 #include <wtf/Ref.h>
62
63 namespace WebCore {
64
65 void DocumentThreadableLoader::loadResourceSynchronously(Document& document, ResourceRequest&& request, ThreadableLoaderClient& client, const ThreadableLoaderOptions& options, RefPtr<SecurityOrigin>&& origin, std::unique_ptr<ContentSecurityPolicy>&& contentSecurityPolicy)
66 {
67     // The loader will be deleted as soon as this function exits.
68     Ref<DocumentThreadableLoader> loader = adoptRef(*new DocumentThreadableLoader(document, client, LoadSynchronously, WTFMove(request), options, WTFMove(origin), WTFMove(contentSecurityPolicy), String(), ShouldLogError::Yes));
69     ASSERT(loader->hasOneRef());
70 }
71
72 void DocumentThreadableLoader::loadResourceSynchronously(Document& document, ResourceRequest&& request, ThreadableLoaderClient& client, const ThreadableLoaderOptions& options)
73 {
74     loadResourceSynchronously(document, WTFMove(request), client, options, nullptr, nullptr);
75 }
76
77 RefPtr<DocumentThreadableLoader> DocumentThreadableLoader::create(Document& document, ThreadableLoaderClient& client,
78 ResourceRequest&& request, const ThreadableLoaderOptions& options, RefPtr<SecurityOrigin>&& origin,
79 std::unique_ptr<ContentSecurityPolicy>&& contentSecurityPolicy, String&& referrer, ShouldLogError shouldLogError)
80 {
81     RefPtr<DocumentThreadableLoader> loader = adoptRef(new DocumentThreadableLoader(document, client, LoadAsynchronously, WTFMove(request), options, WTFMove(origin), WTFMove(contentSecurityPolicy), WTFMove(referrer), shouldLogError));
82     if (!loader->isLoading())
83         loader = nullptr;
84     return loader;
85 }
86
87 RefPtr<DocumentThreadableLoader> DocumentThreadableLoader::create(Document& document, ThreadableLoaderClient& client, ResourceRequest&& request, const ThreadableLoaderOptions& options, String&& referrer)
88 {
89     return create(document, client, WTFMove(request), options, nullptr, nullptr, WTFMove(referrer), ShouldLogError::Yes);
90 }
91
92 static inline bool shouldPerformSecurityChecks()
93 {
94     return platformStrategies()->loaderStrategy()->shouldPerformSecurityChecks();
95 }
96
97 bool DocumentThreadableLoader::shouldSetHTTPHeadersToKeep() const
98 {
99     if (m_options.mode == FetchOptions::Mode::Cors && shouldPerformSecurityChecks())
100         return true;
101
102 #if ENABLE(SERVICE_WORKER)
103     if (m_options.serviceWorkersMode == ServiceWorkersMode::All && m_async)
104         return m_options.serviceWorkerRegistrationIdentifier || m_document.activeServiceWorker();
105 #endif
106
107     return false;
108 }
109
110 DocumentThreadableLoader::DocumentThreadableLoader(Document& document, ThreadableLoaderClient& client, BlockingBehavior blockingBehavior, ResourceRequest&& request, const ThreadableLoaderOptions& options, RefPtr<SecurityOrigin>&& origin, std::unique_ptr<ContentSecurityPolicy>&& contentSecurityPolicy, String&& referrer, ShouldLogError shouldLogError)
111     : m_client(&client)
112     , m_document(document)
113     , m_options(options)
114     , m_origin(WTFMove(origin))
115     , m_referrer(WTFMove(referrer))
116     , m_sameOriginRequest(securityOrigin().canRequest(request.url()))
117     , m_simpleRequest(true)
118     , m_async(blockingBehavior == LoadAsynchronously)
119     , m_delayCallbacksForIntegrityCheck(!m_options.integrity.isEmpty())
120     , m_contentSecurityPolicy(WTFMove(contentSecurityPolicy))
121     , m_shouldLogError(shouldLogError)
122 {
123     relaxAdoptionRequirement();
124
125     // Setting a referrer header is only supported in the async code path.
126     ASSERT(m_async || m_referrer.isEmpty());
127
128     // Referrer and Origin headers should be set after the preflight if any.
129     ASSERT(!request.hasHTTPReferrer() && !request.hasHTTPOrigin());
130
131     ASSERT_WITH_SECURITY_IMPLICATION(isAllowedByContentSecurityPolicy(request.url(), ContentSecurityPolicy::RedirectResponseReceived::No));
132
133     m_options.storedCredentialsPolicy = (m_options.credentials == FetchOptions::Credentials::Include || (m_options.credentials == FetchOptions::Credentials::SameOrigin && m_sameOriginRequest)) ? StoredCredentialsPolicy::Use : StoredCredentialsPolicy::DoNotUse;
134
135     ASSERT(!request.httpHeaderFields().contains(HTTPHeaderName::Origin));
136
137     // Copy headers if we need to replay the request after a redirection.
138     if (m_options.mode == FetchOptions::Mode::Cors)
139         m_originalHeaders = request.httpHeaderFields();
140
141     if (shouldSetHTTPHeadersToKeep())
142         m_options.httpHeadersToKeep = httpHeadersToKeepFromCleaning(request.httpHeaderFields());
143
144     if (document.page() && document.page()->isRunningUserScripts() && SchemeRegistry::isUserExtensionScheme(request.url().protocol().toStringWithoutCopying())) {
145         m_options.mode = FetchOptions::Mode::NoCors;
146         m_options.filteringPolicy = ResponseFilteringPolicy::Disable;
147     }
148
149     m_options.cspResponseHeaders = m_options.contentSecurityPolicyEnforcement != ContentSecurityPolicyEnforcement::DoNotEnforce ? this->contentSecurityPolicy().responseHeaders() : ContentSecurityPolicyResponseHeaders { };
150
151     // As per step 11 of https://fetch.spec.whatwg.org/#main-fetch, data scheme (if same-origin data-URL flag is set) and about scheme are considered same-origin.
152     if (request.url().protocolIsData())
153         m_sameOriginRequest = options.sameOriginDataURLFlag == SameOriginDataURLFlag::Set;
154
155     if (m_sameOriginRequest || m_options.mode == FetchOptions::Mode::NoCors || m_options.mode == FetchOptions::Mode::Navigate) {
156         loadRequest(WTFMove(request), SecurityCheckPolicy::DoSecurityCheck);
157         return;
158     }
159
160     if (m_options.mode == FetchOptions::Mode::SameOrigin) {
161         logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, request.url(), "Cross origin requests are not allowed when using same-origin fetch mode."));
162         return;
163     }
164
165     makeCrossOriginAccessRequest(WTFMove(request));
166 }
167
168 bool DocumentThreadableLoader::checkURLSchemeAsCORSEnabled(const URL& url)
169 {
170     // Cross-origin requests are only allowed for HTTP and registered schemes. We would catch this when checking response headers later, but there is no reason to send a request that's guaranteed to be denied.
171     if (!SchemeRegistry::shouldTreatURLSchemeAsCORSEnabled(url.protocol().toStringWithoutCopying())) {
172         logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Cross origin requests are only supported for HTTP.", ResourceError::Type::AccessControl));
173         return false;
174     }
175     return true;
176 }
177
178 void DocumentThreadableLoader::makeCrossOriginAccessRequest(ResourceRequest&& request)
179 {
180     ASSERT(m_options.mode == FetchOptions::Mode::Cors);
181
182     if ((m_options.preflightPolicy == PreflightPolicy::Consider && isSimpleCrossOriginAccessRequest(request.httpMethod(), request.httpHeaderFields())) || m_options.preflightPolicy == PreflightPolicy::Prevent || shouldPerformSecurityChecks()) {
183         if (checkURLSchemeAsCORSEnabled(request.url()))
184             makeSimpleCrossOriginAccessRequest(WTFMove(request));
185     } else {
186 #if ENABLE(SERVICE_WORKER)
187         if (m_options.serviceWorkersMode == ServiceWorkersMode::All && m_async) {
188             if (m_options.serviceWorkerRegistrationIdentifier || document().activeServiceWorker()) {
189                 ASSERT(!m_bypassingPreflightForServiceWorkerRequest);
190                 m_bypassingPreflightForServiceWorkerRequest = WTFMove(request);
191                 m_options.serviceWorkersMode = ServiceWorkersMode::Only;
192                 loadRequest(ResourceRequest { m_bypassingPreflightForServiceWorkerRequest.value() }, SecurityCheckPolicy::SkipSecurityCheck);
193                 return;
194             }
195         }
196 #endif
197         if (!checkURLSchemeAsCORSEnabled(request.url()))
198             return;
199
200         m_simpleRequest = false;
201         if (CrossOriginPreflightResultCache::singleton().canSkipPreflight(securityOrigin().toString(), request.url(), m_options.storedCredentialsPolicy, request.httpMethod(), request.httpHeaderFields()))
202             preflightSuccess(WTFMove(request));
203         else
204             makeCrossOriginAccessRequestWithPreflight(WTFMove(request));
205     }
206 }
207
208 void DocumentThreadableLoader::makeSimpleCrossOriginAccessRequest(ResourceRequest&& request)
209 {
210     ASSERT(m_options.preflightPolicy != PreflightPolicy::Force || shouldPerformSecurityChecks());
211     ASSERT(m_options.preflightPolicy == PreflightPolicy::Prevent || isSimpleCrossOriginAccessRequest(request.httpMethod(), request.httpHeaderFields()) || shouldPerformSecurityChecks());
212
213     updateRequestForAccessControl(request, securityOrigin(), m_options.storedCredentialsPolicy);
214     loadRequest(WTFMove(request), SecurityCheckPolicy::DoSecurityCheck);
215 }
216
217 void DocumentThreadableLoader::makeCrossOriginAccessRequestWithPreflight(ResourceRequest&& request)
218 {
219     if (m_async) {
220         m_preflightChecker.emplace(*this, WTFMove(request));
221         m_preflightChecker->startPreflight();
222         return;
223     }
224     CrossOriginPreflightChecker::doPreflight(*this, WTFMove(request));
225 }
226
227 DocumentThreadableLoader::~DocumentThreadableLoader()
228 {
229     if (m_resource)
230         m_resource->removeClient(*this);
231 }
232
233 void DocumentThreadableLoader::cancel()
234 {
235     Ref<DocumentThreadableLoader> protectedThis(*this);
236
237     // Cancel can re-enter and m_resource might be null here as a result.
238     if (m_client && m_resource) {
239         // FIXME: This error is sent to the client in didFail(), so it should not be an internal one. Use FrameLoaderClient::cancelledError() instead.
240         ResourceError error(errorDomainWebKitInternal, 0, m_resource->url(), "Load cancelled", ResourceError::Type::Cancellation);
241         m_client->didFail(error);
242     }
243     clearResource();
244     m_client = nullptr;
245 }
246
247 void DocumentThreadableLoader::setDefersLoading(bool value)
248 {
249     if (m_resource)
250         m_resource->setDefersLoading(value);
251     if (m_preflightChecker)
252         m_preflightChecker->setDefersLoading(value);
253 }
254
255 void DocumentThreadableLoader::clearResource()
256 {
257     // Script can cancel and restart a request reentrantly within removeClient(),
258     // which could lead to calling CachedResource::removeClient() multiple times for
259     // this DocumentThreadableLoader. Save off a copy of m_resource and clear it to
260     // prevent the reentrancy.
261     if (CachedResourceHandle<CachedRawResource> resource = m_resource) {
262         m_resource = nullptr;
263         resource->removeClient(*this);
264     }
265     if (m_preflightChecker)
266         m_preflightChecker = std::nullopt;
267 }
268
269 void DocumentThreadableLoader::redirectReceived(CachedResource& resource, ResourceRequest&& request, const ResourceResponse& redirectResponse, CompletionHandler<void(ResourceRequest&&)>&& completionHandler)
270 {
271     ASSERT(m_client);
272     ASSERT_UNUSED(resource, &resource == m_resource);
273
274     Ref<DocumentThreadableLoader> protectedThis(*this);
275     --m_options.maxRedirectCount;
276
277     // FIXME: We restrict this check to Fetch API for the moment, as this might disrupt WorkerScriptLoader.
278     // Reassess this check based on https://github.com/whatwg/fetch/issues/393 discussions.
279     // We should also disable that check in navigation mode.
280     if (!request.url().protocolIsInHTTPFamily() && m_options.initiator == cachedResourceRequestInitiators().fetch) {
281         reportRedirectionWithBadScheme(request.url());
282         clearResource();
283         return completionHandler(WTFMove(request));
284     }
285
286     if (platformStrategies()->loaderStrategy()->havePerformedSecurityChecks(redirectResponse)) {
287         completionHandler(WTFMove(request));
288         return;
289     }
290
291     if (!isAllowedByContentSecurityPolicy(request.url(), redirectResponse.isNull() ? ContentSecurityPolicy::RedirectResponseReceived::No : ContentSecurityPolicy::RedirectResponseReceived::Yes)) {
292         reportContentSecurityPolicyError(redirectResponse.url());
293         clearResource();
294         return completionHandler(WTFMove(request));
295     }
296
297     // Allow same origin requests to continue after allowing clients to audit the redirect.
298     if (isAllowedRedirect(request.url()))
299         return completionHandler(WTFMove(request));
300
301     // Force any subsequent request to use these checks.
302     m_sameOriginRequest = false;
303
304     ASSERT(m_resource);
305     ASSERT(m_originalHeaders);
306
307     // Use a unique for subsequent loads if needed.
308     // https://fetch.spec.whatwg.org/#concept-http-redirect-fetch (Step 10).
309     ASSERT(m_options.mode == FetchOptions::Mode::Cors);
310     if (!securityOrigin().canRequest(redirectResponse.url()) && !protocolHostAndPortAreEqual(redirectResponse.url(), request.url()))
311         m_origin = SecurityOrigin::createUnique();
312
313     // Except in case where preflight is needed, loading should be able to continue on its own.
314     // But we also handle credentials here if it is restricted to SameOrigin.
315     if (m_options.credentials != FetchOptions::Credentials::SameOrigin && m_simpleRequest && isSimpleCrossOriginAccessRequest(request.httpMethod(), *m_originalHeaders))
316         return completionHandler(WTFMove(request));
317
318     if (m_options.credentials == FetchOptions::Credentials::SameOrigin)
319         m_options.storedCredentialsPolicy = StoredCredentialsPolicy::DoNotUse;
320
321     clearResource();
322
323     // Let's fetch the request with the original headers (equivalent to request cloning specified by fetch algorithm).
324     // Do not copy the Authorization header if removed by the network layer.
325     if (!request.httpHeaderFields().contains(HTTPHeaderName::Authorization))
326         m_originalHeaders->remove(HTTPHeaderName::Authorization);
327     request.setHTTPHeaderFields(*m_originalHeaders);
328
329 #if ENABLE(SERVICE_WORKER)
330     if (redirectResponse.source() != ResourceResponse::Source::ServiceWorker && redirectResponse.source() != ResourceResponse::Source::MemoryCache)
331         m_options.serviceWorkersMode = ServiceWorkersMode::None;
332 #endif
333     makeCrossOriginAccessRequest(ResourceRequest(request));
334     completionHandler(WTFMove(request));
335 }
336
337 void DocumentThreadableLoader::dataSent(CachedResource& resource, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
338 {
339     ASSERT(m_client);
340     ASSERT_UNUSED(resource, &resource == m_resource);
341     m_client->didSendData(bytesSent, totalBytesToBeSent);
342 }
343
344 void DocumentThreadableLoader::responseReceived(CachedResource& resource, const ResourceResponse& response, CompletionHandler<void()>&& completionHandler)
345 {
346     ASSERT_UNUSED(resource, &resource == m_resource);
347     didReceiveResponse(m_resource->identifier(), response);
348
349     if (completionHandler)
350         completionHandler();
351 }
352
353 void DocumentThreadableLoader::didReceiveResponse(unsigned long identifier, const ResourceResponse& response)
354 {
355     ASSERT(m_client);
356     ASSERT(response.type() != ResourceResponse::Type::Error);
357
358     InspectorInstrumentation::didReceiveThreadableLoaderResponse(*this, identifier);
359
360     if (m_delayCallbacksForIntegrityCheck)
361         return;
362
363     if (options().filteringPolicy == ResponseFilteringPolicy::Disable) {
364         m_client->didReceiveResponse(identifier, response);
365         return;
366     }
367
368     if (response.type() == ResourceResponse::Type::Default) {
369         m_client->didReceiveResponse(identifier, ResourceResponseBase::filter(response));
370         if (response.tainting() == ResourceResponse::Tainting::Opaque) {
371             clearResource();
372             if (m_client)
373                 m_client->didFinishLoading(identifier);
374         }
375         return;
376     }
377     ASSERT(response.type() == ResourceResponse::Type::Opaqueredirect || response.source() == ResourceResponse::Source::ServiceWorker || response.source() == ResourceResponse::Source::MemoryCache);
378     m_client->didReceiveResponse(identifier, response);
379 }
380
381 void DocumentThreadableLoader::dataReceived(CachedResource& resource, const char* data, int dataLength)
382 {
383     ASSERT_UNUSED(resource, &resource == m_resource);
384     didReceiveData(m_resource->identifier(), data, dataLength);
385 }
386
387 void DocumentThreadableLoader::didReceiveData(unsigned long, const char* data, int dataLength)
388 {
389     ASSERT(m_client);
390
391     if (m_delayCallbacksForIntegrityCheck)
392         return;
393
394     m_client->didReceiveData(data, dataLength);
395 }
396
397 void DocumentThreadableLoader::finishedTimingForWorkerLoad(CachedResource& resource, const ResourceTiming& resourceTiming)
398 {
399     ASSERT(m_client);
400     ASSERT_UNUSED(resource, &resource == m_resource);
401
402     finishedTimingForWorkerLoad(resourceTiming);
403 }
404
405 void DocumentThreadableLoader::finishedTimingForWorkerLoad(const ResourceTiming& resourceTiming)
406 {
407     ASSERT(m_options.initiatorContext == InitiatorContext::Worker);
408
409     m_client->didFinishTiming(resourceTiming);
410 }
411
412 void DocumentThreadableLoader::notifyFinished(CachedResource& resource)
413 {
414     ASSERT(m_client);
415     ASSERT_UNUSED(resource, &resource == m_resource);
416
417     if (m_resource->errorOccurred())
418         didFail(m_resource->identifier(), m_resource->resourceError());
419     else
420         didFinishLoading(m_resource->identifier());
421 }
422
423 void DocumentThreadableLoader::didFinishLoading(unsigned long identifier)
424 {
425     ASSERT(m_client);
426
427     if (m_delayCallbacksForIntegrityCheck) {
428         if (!matchIntegrityMetadata(*m_resource, m_options.integrity)) {
429             reportIntegrityMetadataError(m_resource->url());
430             return;
431         }
432
433         auto response = m_resource->response();
434
435         if (options().filteringPolicy == ResponseFilteringPolicy::Disable) {
436             m_client->didReceiveResponse(identifier, response);
437             m_client->didReceiveData(m_resource->resourceBuffer()->data(), m_resource->resourceBuffer()->size());
438         } else {
439             ASSERT(response.type() == ResourceResponse::Type::Default);
440
441             m_client->didReceiveResponse(identifier, ResourceResponseBase::filter(response));
442             m_client->didReceiveData(m_resource->resourceBuffer()->data(), m_resource->resourceBuffer()->size());
443         }
444     }
445
446     m_client->didFinishLoading(identifier);
447 }
448
449 void DocumentThreadableLoader::didFail(unsigned long, const ResourceError& error)
450 {
451     ASSERT(m_client);
452 #if ENABLE(SERVICE_WORKER)
453     if (m_bypassingPreflightForServiceWorkerRequest && error.isCancellation()) {
454         clearResource();
455
456         m_options.serviceWorkersMode = ServiceWorkersMode::None;
457         makeCrossOriginAccessRequestWithPreflight(WTFMove(m_bypassingPreflightForServiceWorkerRequest.value()));
458         ASSERT(m_bypassingPreflightForServiceWorkerRequest->isNull());
459         m_bypassingPreflightForServiceWorkerRequest = std::nullopt;
460         return;
461     }
462 #endif
463
464     if (m_shouldLogError == ShouldLogError::Yes)
465         logError(m_document, error, m_options.initiator);
466
467     m_client->didFail(error);
468 }
469
470 void DocumentThreadableLoader::preflightSuccess(ResourceRequest&& request)
471 {
472     ResourceRequest actualRequest(WTFMove(request));
473     updateRequestForAccessControl(actualRequest, securityOrigin(), m_options.storedCredentialsPolicy);
474
475     m_preflightChecker = std::nullopt;
476
477     // It should be ok to skip the security check since we already asked about the preflight request.
478     loadRequest(WTFMove(actualRequest), SecurityCheckPolicy::SkipSecurityCheck);
479 }
480
481 void DocumentThreadableLoader::preflightFailure(unsigned long identifier, const ResourceError& error)
482 {
483     m_preflightChecker = std::nullopt;
484
485     InspectorInstrumentation::didFailLoading(m_document.frame(), m_document.frame()->loader().documentLoader(), identifier, error);
486
487     if (m_shouldLogError == ShouldLogError::Yes)
488         logError(m_document, error, m_options.initiator);
489
490     m_client->didFail(error);
491 }
492
493 void DocumentThreadableLoader::loadRequest(ResourceRequest&& request, SecurityCheckPolicy securityCheck)
494 {
495     Ref<DocumentThreadableLoader> protectedThis(*this);
496
497     // Any credential should have been removed from the cross-site requests.
498     const URL& requestURL = request.url();
499     m_options.securityCheck = securityCheck;
500     ASSERT(m_sameOriginRequest || requestURL.user().isEmpty());
501     ASSERT(m_sameOriginRequest || requestURL.pass().isEmpty());
502
503     if (!m_referrer.isNull())
504         request.setHTTPReferrer(m_referrer);
505
506     if (m_async) {
507         ResourceLoaderOptions options = m_options;
508         options.clientCredentialPolicy = m_sameOriginRequest ? ClientCredentialPolicy::MayAskClientForCredentials : ClientCredentialPolicy::CannotAskClientForCredentials;
509         options.contentSecurityPolicyImposition = ContentSecurityPolicyImposition::SkipPolicyCheck;
510         
511         // If there is integrity metadata to validate, we must buffer.
512         if (!m_options.integrity.isEmpty())
513             options.dataBufferingPolicy = DataBufferingPolicy::BufferData;
514
515         request.setAllowCookies(m_options.storedCredentialsPolicy == StoredCredentialsPolicy::Use);
516         CachedResourceRequest newRequest(WTFMove(request), options);
517         if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled())
518             newRequest.setInitiator(m_options.initiator);
519         newRequest.setOrigin(securityOrigin());
520
521         ASSERT(!m_resource);
522         if (m_resource) {
523             CachedResourceHandle<CachedRawResource> resource = std::exchange(m_resource, nullptr);
524             resource->removeClient(*this);
525         }
526
527         auto cachedResource = m_document.cachedResourceLoader().requestRawResource(WTFMove(newRequest));
528         m_resource = cachedResource.value_or(nullptr);
529         if (m_resource)
530             m_resource->addClient(*this);
531         else
532             logErrorAndFail(cachedResource.error());
533         return;
534     }
535
536     // If credentials mode is 'Omit', we should disable cookie sending.
537     ASSERT(m_options.credentials != FetchOptions::Credentials::Omit);
538
539     LoadTiming loadTiming;
540     loadTiming.markStartTimeAndFetchStart();
541
542     // FIXME: ThreadableLoaderOptions.sniffContent is not supported for synchronous requests.
543     RefPtr<SharedBuffer> data;
544     ResourceError error;
545     ResourceResponse response;
546     unsigned long identifier = std::numeric_limits<unsigned long>::max();
547     if (m_document.frame()) {
548         auto& frameLoader = m_document.frame()->loader();
549         if (!frameLoader.mixedContentChecker().canRunInsecureContent(m_document.securityOrigin(), requestURL))
550             return;
551         identifier = frameLoader.loadResourceSynchronously(request, m_options.clientCredentialPolicy, m_options, *m_originalHeaders, error, response, data);
552     }
553
554     loadTiming.setResponseEnd(MonotonicTime::now());
555
556     if (!error.isNull() && response.httpStatusCode() <= 0) {
557         if (requestURL.isLocalFile()) {
558             // We don't want XMLHttpRequest to raise an exception for file:// resources, see <rdar://problem/4962298>.
559             // FIXME: XMLHttpRequest quirks should be in XMLHttpRequest code, not in DocumentThreadableLoader.cpp.
560             didReceiveResponse(identifier, response);
561             didFinishLoading(identifier);
562             return;
563         }
564         logErrorAndFail(error);
565         return;
566     }
567
568     if (!shouldPerformSecurityChecks()) {
569         // FIXME: FrameLoader::loadSynchronously() does not tell us whether a redirect happened or not, so we guess by comparing the
570         // request and response URLs. This isn't a perfect test though, since a server can serve a redirect to the same URL that was
571         // requested. Also comparing the request and response URLs as strings will fail if the requestURL still has its credentials.
572         bool didRedirect = requestURL != response.url();
573         if (didRedirect) {
574             if (!isAllowedByContentSecurityPolicy(response.url(), ContentSecurityPolicy::RedirectResponseReceived::Yes)) {
575                 reportContentSecurityPolicyError(requestURL);
576                 return;
577             }
578             if (!isAllowedRedirect(response.url())) {
579                 reportCrossOriginResourceSharingError(requestURL);
580                 return;
581             }
582         }
583
584         if (!m_sameOriginRequest) {
585             if (m_options.mode == FetchOptions::Mode::NoCors)
586                 response.setTainting(ResourceResponse::Tainting::Opaque);
587             else {
588                 ASSERT(m_options.mode == FetchOptions::Mode::Cors);
589                 response.setTainting(ResourceResponse::Tainting::Cors);
590                 String accessControlErrorDescription;
591                 if (!passesAccessControlCheck(response, m_options.storedCredentialsPolicy, securityOrigin(), accessControlErrorDescription)) {
592                     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, response.url(), accessControlErrorDescription, ResourceError::Type::AccessControl));
593                     return;
594                 }
595             }
596         }
597     }
598     didReceiveResponse(identifier, response);
599
600     if (data)
601         didReceiveData(identifier, data->data(), data->size());
602
603     if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled()) {
604         auto resourceTiming = ResourceTiming::fromSynchronousLoad(requestURL, m_options.initiator, loadTiming, response.deprecatedNetworkLoadMetrics(), response, securityOrigin());
605         if (options().initiatorContext == InitiatorContext::Worker)
606             finishedTimingForWorkerLoad(resourceTiming);
607         else {
608             if (document().domWindow() && document().domWindow()->performance())
609                 document().domWindow()->performance()->addResourceTiming(WTFMove(resourceTiming));
610         }
611     }
612
613     didFinishLoading(identifier);
614 }
615
616 bool DocumentThreadableLoader::isAllowedByContentSecurityPolicy(const URL& url, ContentSecurityPolicy::RedirectResponseReceived redirectResponseReceived)
617 {
618     switch (m_options.contentSecurityPolicyEnforcement) {
619     case ContentSecurityPolicyEnforcement::DoNotEnforce:
620         return true;
621     case ContentSecurityPolicyEnforcement::EnforceChildSrcDirective:
622         return contentSecurityPolicy().allowChildContextFromSource(url, redirectResponseReceived);
623     case ContentSecurityPolicyEnforcement::EnforceConnectSrcDirective:
624         return contentSecurityPolicy().allowConnectToSource(url, redirectResponseReceived);
625     case ContentSecurityPolicyEnforcement::EnforceScriptSrcDirective:
626         return contentSecurityPolicy().allowScriptFromSource(url, redirectResponseReceived);
627     }
628     ASSERT_NOT_REACHED();
629     return false;
630 }
631
632 bool DocumentThreadableLoader::isAllowedRedirect(const URL& url)
633 {
634     if (m_options.mode == FetchOptions::Mode::NoCors)
635         return true;
636
637     return m_sameOriginRequest && securityOrigin().canRequest(url);
638 }
639
640 bool DocumentThreadableLoader::isXMLHttpRequest() const
641 {
642     return m_options.initiator == cachedResourceRequestInitiators().xmlhttprequest;
643 }
644
645 SecurityOrigin& DocumentThreadableLoader::securityOrigin() const
646 {
647     return m_origin ? *m_origin : m_document.securityOrigin();
648 }
649
650 const ContentSecurityPolicy& DocumentThreadableLoader::contentSecurityPolicy() const
651 {
652     if (m_contentSecurityPolicy)
653         return *m_contentSecurityPolicy.get();
654     ASSERT(m_document.contentSecurityPolicy());
655     return *m_document.contentSecurityPolicy();
656 }
657
658 void DocumentThreadableLoader::reportRedirectionWithBadScheme(const URL& url)
659 {
660     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Redirection to URL with a scheme that is not HTTP(S)."_s, ResourceError::Type::AccessControl));
661 }
662
663 void DocumentThreadableLoader::reportContentSecurityPolicyError(const URL& url)
664 {
665     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Blocked by Content Security Policy."_s, ResourceError::Type::AccessControl));
666 }
667
668 void DocumentThreadableLoader::reportCrossOriginResourceSharingError(const URL& url)
669 {
670     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Cross-origin redirection denied by Cross-Origin Resource Sharing policy."_s, ResourceError::Type::AccessControl));
671 }
672
673 void DocumentThreadableLoader::reportIntegrityMetadataError(const URL& url)
674 {
675     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Failed integrity metadata check."_s, ResourceError::Type::General));
676 }
677
678 void DocumentThreadableLoader::logErrorAndFail(const ResourceError& error)
679 {
680     if (m_shouldLogError == ShouldLogError::Yes) {
681         if (error.isAccessControl() && !error.localizedDescription().isEmpty())
682             m_document.addConsoleMessage(MessageSource::Security, MessageLevel::Error, error.localizedDescription());
683         logError(m_document, error, m_options.initiator);
684     }
685     ASSERT(m_client);
686     m_client->didFail(error);
687 }
688
689 } // namespace WebCore