Requests handled by Service Worker should not go through preflighting
[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 "Performance.h"
48 #include "ProgressTracker.h"
49 #include "ResourceError.h"
50 #include "ResourceRequest.h"
51 #include "ResourceTiming.h"
52 #include "RuntimeEnabledFeatures.h"
53 #include "SchemeRegistry.h"
54 #include "SecurityOrigin.h"
55 #include "SharedBuffer.h"
56 #include "SubresourceIntegrity.h"
57 #include "SubresourceLoader.h"
58 #include "ThreadableLoaderClient.h"
59 #include <wtf/Assertions.h>
60 #include <wtf/Ref.h>
61
62 namespace WebCore {
63
64 void DocumentThreadableLoader::loadResourceSynchronously(Document& document, ResourceRequest&& request, ThreadableLoaderClient& client, const ThreadableLoaderOptions& options, RefPtr<SecurityOrigin>&& origin, std::unique_ptr<ContentSecurityPolicy>&& contentSecurityPolicy)
65 {
66     // The loader will be deleted as soon as this function exits.
67     Ref<DocumentThreadableLoader> loader = adoptRef(*new DocumentThreadableLoader(document, client, LoadSynchronously, WTFMove(request), options, WTFMove(origin), WTFMove(contentSecurityPolicy), String(), ShouldLogError::Yes));
68     ASSERT(loader->hasOneRef());
69 }
70
71 void DocumentThreadableLoader::loadResourceSynchronously(Document& document, ResourceRequest&& request, ThreadableLoaderClient& client, const ThreadableLoaderOptions& options)
72 {
73     loadResourceSynchronously(document, WTFMove(request), client, options, nullptr, nullptr);
74 }
75
76 RefPtr<DocumentThreadableLoader> DocumentThreadableLoader::create(Document& document, ThreadableLoaderClient& client,
77 ResourceRequest&& request, const ThreadableLoaderOptions& options, RefPtr<SecurityOrigin>&& origin,
78 std::unique_ptr<ContentSecurityPolicy>&& contentSecurityPolicy, String&& referrer, ShouldLogError shouldLogError)
79 {
80     RefPtr<DocumentThreadableLoader> loader = adoptRef(new DocumentThreadableLoader(document, client, LoadAsynchronously, WTFMove(request), options, WTFMove(origin), WTFMove(contentSecurityPolicy), WTFMove(referrer), shouldLogError));
81     if (!loader->isLoading())
82         loader = nullptr;
83     return loader;
84 }
85
86 RefPtr<DocumentThreadableLoader> DocumentThreadableLoader::create(Document& document, ThreadableLoaderClient& client, ResourceRequest&& request, const ThreadableLoaderOptions& options, String&& referrer)
87 {
88     return create(document, client, WTFMove(request), options, nullptr, nullptr, WTFMove(referrer), ShouldLogError::Yes);
89 }
90
91 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)
92     : m_client(&client)
93     , m_document(document)
94     , m_options(options)
95     , m_origin(WTFMove(origin))
96     , m_referrer(WTFMove(referrer))
97     , m_sameOriginRequest(securityOrigin().canRequest(request.url()))
98     , m_simpleRequest(true)
99     , m_async(blockingBehavior == LoadAsynchronously)
100     , m_delayCallbacksForIntegrityCheck(!m_options.integrity.isEmpty())
101     , m_contentSecurityPolicy(WTFMove(contentSecurityPolicy))
102     , m_shouldLogError(shouldLogError)
103 {
104     relaxAdoptionRequirement();
105
106     // Setting a referrer header is only supported in the async code path.
107     ASSERT(m_async || m_referrer.isEmpty());
108
109     // Referrer and Origin headers should be set after the preflight if any.
110     ASSERT(!request.hasHTTPReferrer() && !request.hasHTTPOrigin());
111
112     ASSERT_WITH_SECURITY_IMPLICATION(isAllowedByContentSecurityPolicy(request.url(), ContentSecurityPolicy::RedirectResponseReceived::No));
113
114     m_options.storedCredentialsPolicy = (m_options.credentials == FetchOptions::Credentials::Include || (m_options.credentials == FetchOptions::Credentials::SameOrigin && m_sameOriginRequest)) ? StoredCredentialsPolicy::Use : StoredCredentialsPolicy::DoNotUse;
115
116     ASSERT(!request.httpHeaderFields().contains(HTTPHeaderName::Origin));
117
118     // Copy headers if we need to replay the request after a redirection.
119     if (m_async && m_options.mode == FetchOptions::Mode::Cors)
120         m_originalHeaders = request.httpHeaderFields();
121
122     if (document.page() && document.page()->isRunningUserScripts() && SchemeRegistry::isUserExtensionScheme(request.url().protocol().toStringWithoutCopying())) {
123         m_options.mode = FetchOptions::Mode::NoCors;
124         m_options.filteringPolicy = ResponseFilteringPolicy::Disable;
125     }
126
127     // 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.
128     if (request.url().protocolIsData())
129         m_sameOriginRequest = options.sameOriginDataURLFlag == SameOriginDataURLFlag::Set;
130
131     if (m_sameOriginRequest || m_options.mode == FetchOptions::Mode::NoCors) {
132         loadRequest(WTFMove(request), DoSecurityCheck);
133         return;
134     }
135
136     if (m_options.mode == FetchOptions::Mode::SameOrigin) {
137         logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, request.url(), "Cross origin requests are not allowed when using same-origin fetch mode."));
138         return;
139     }
140
141     makeCrossOriginAccessRequest(WTFMove(request));
142 }
143
144 void DocumentThreadableLoader::makeCrossOriginAccessRequest(ResourceRequest&& request)
145 {
146     ASSERT(m_options.mode == FetchOptions::Mode::Cors);
147
148     if ((m_options.preflightPolicy == ConsiderPreflight && isSimpleCrossOriginAccessRequest(request.httpMethod(), request.httpHeaderFields())) || m_options.preflightPolicy == PreventPreflight)
149         makeSimpleCrossOriginAccessRequest(WTFMove(request));
150     else {
151 #if ENABLE(SERVICE_WORKER)
152         if (m_options.serviceWorkersMode == ServiceWorkersMode::All && m_async) {
153             if (m_options.serviceWorkerIdentifier || document().activeServiceWorker()) {
154                 ASSERT(!m_bypassingPreflightForServiceWorkerRequest);
155                 m_bypassingPreflightForServiceWorkerRequest = WTFMove(request);
156                 m_options.serviceWorkersMode = ServiceWorkersMode::Only;
157                 loadRequest(ResourceRequest { m_bypassingPreflightForServiceWorkerRequest.value() }, SkipSecurityCheck);
158                 return;
159             }
160         }
161 #endif
162         m_simpleRequest = false;
163         if (CrossOriginPreflightResultCache::singleton().canSkipPreflight(securityOrigin().toString(), request.url(), m_options.storedCredentialsPolicy, request.httpMethod(), request.httpHeaderFields()))
164             preflightSuccess(WTFMove(request));
165         else
166             makeCrossOriginAccessRequestWithPreflight(WTFMove(request));
167     }
168 }
169
170 void DocumentThreadableLoader::makeSimpleCrossOriginAccessRequest(ResourceRequest&& request)
171 {
172     ASSERT(m_options.preflightPolicy != ForcePreflight);
173     ASSERT(m_options.preflightPolicy == PreventPreflight || isSimpleCrossOriginAccessRequest(request.httpMethod(), request.httpHeaderFields()));
174
175     // 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.
176     if (!SchemeRegistry::shouldTreatURLSchemeAsCORSEnabled(request.url().protocol().toStringWithoutCopying())) {
177         logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, request.url(), "Cross origin requests are only supported for HTTP.", ResourceError::Type::AccessControl));
178         return;
179     }
180
181     updateRequestForAccessControl(request, securityOrigin(), m_options.storedCredentialsPolicy);
182     loadRequest(WTFMove(request), DoSecurityCheck);
183 }
184
185 void DocumentThreadableLoader::makeCrossOriginAccessRequestWithPreflight(ResourceRequest&& request)
186 {
187     if (m_async) {
188         m_preflightChecker.emplace(*this, WTFMove(request));
189         m_preflightChecker->startPreflight();
190         return;
191     }
192     CrossOriginPreflightChecker::doPreflight(*this, WTFMove(request));
193 }
194
195 DocumentThreadableLoader::~DocumentThreadableLoader()
196 {
197     if (m_resource)
198         m_resource->removeClient(*this);
199 }
200
201 void DocumentThreadableLoader::cancel()
202 {
203     Ref<DocumentThreadableLoader> protectedThis(*this);
204
205     // Cancel can re-enter and m_resource might be null here as a result.
206     if (m_client && m_resource) {
207         // FIXME: This error is sent to the client in didFail(), so it should not be an internal one. Use FrameLoaderClient::cancelledError() instead.
208         ResourceError error(errorDomainWebKitInternal, 0, m_resource->url(), "Load cancelled", ResourceError::Type::Cancellation);
209         m_client->didFail(error);
210     }
211     clearResource();
212     m_client = nullptr;
213 }
214
215 void DocumentThreadableLoader::setDefersLoading(bool value)
216 {
217     if (m_resource)
218         m_resource->setDefersLoading(value);
219     if (m_preflightChecker)
220         m_preflightChecker->setDefersLoading(value);
221 }
222
223 void DocumentThreadableLoader::clearResource()
224 {
225     // Script can cancel and restart a request reentrantly within removeClient(),
226     // which could lead to calling CachedResource::removeClient() multiple times for
227     // this DocumentThreadableLoader. Save off a copy of m_resource and clear it to
228     // prevent the reentrancy.
229     if (CachedResourceHandle<CachedRawResource> resource = m_resource) {
230         m_resource = nullptr;
231         resource->removeClient(*this);
232     }
233     if (m_preflightChecker)
234         m_preflightChecker = std::nullopt;
235 }
236
237 void DocumentThreadableLoader::redirectReceived(CachedResource& resource, ResourceRequest& request, const ResourceResponse& redirectResponse)
238 {
239     ASSERT(m_client);
240     ASSERT_UNUSED(resource, &resource == m_resource);
241
242     Ref<DocumentThreadableLoader> protectedThis(*this);
243     --m_options.maxRedirectCount;
244
245     // FIXME: We restrict this check to Fetch API for the moment, as this might disrupt WorkerScriptLoader.
246     // Reassess this check based on https://github.com/whatwg/fetch/issues/393 discussions.
247     // We should also disable that check in navigation mode.
248     if (!request.url().protocolIsInHTTPFamily() && m_options.initiator == cachedResourceRequestInitiators().fetch) {
249         reportRedirectionWithBadScheme(request.url());
250         clearResource();
251         return;
252     }
253
254     if (!isAllowedByContentSecurityPolicy(request.url(), redirectResponse.isNull() ? ContentSecurityPolicy::RedirectResponseReceived::No : ContentSecurityPolicy::RedirectResponseReceived::Yes)) {
255         reportContentSecurityPolicyError(redirectResponse.url());
256         clearResource();
257         return;
258     }
259
260     // Allow same origin requests to continue after allowing clients to audit the redirect.
261     if (isAllowedRedirect(request.url()))
262         return;
263
264     // Force any subsequent request to use these checks.
265     m_sameOriginRequest = false;
266
267     ASSERT(m_resource);
268     ASSERT(m_originalHeaders);
269
270     // Use a unique for subsequent loads if needed.
271     // https://fetch.spec.whatwg.org/#concept-http-redirect-fetch (Step 10).
272     ASSERT(m_options.mode == FetchOptions::Mode::Cors);
273     if (!securityOrigin().canRequest(redirectResponse.url()) && !protocolHostAndPortAreEqual(redirectResponse.url(), request.url()))
274         m_origin = SecurityOrigin::createUnique();
275
276     // Except in case where preflight is needed, loading should be able to continue on its own.
277     // But we also handle credentials here if it is restricted to SameOrigin.
278     if (m_options.credentials != FetchOptions::Credentials::SameOrigin && m_simpleRequest && isSimpleCrossOriginAccessRequest(request.httpMethod(), *m_originalHeaders))
279         return;
280
281     m_options.storedCredentialsPolicy = StoredCredentialsPolicy::DoNotUse;
282
283     clearResource();
284
285     // Let's fetch the request with the original headers (equivalent to request cloning specified by fetch algorithm).
286     // Do not copy the Authorization header if removed by the network layer.
287     if (!request.httpHeaderFields().contains(HTTPHeaderName::Authorization))
288         m_originalHeaders->remove(HTTPHeaderName::Authorization);
289     request.setHTTPHeaderFields(*m_originalHeaders);
290
291     makeCrossOriginAccessRequest(ResourceRequest(request));
292 }
293
294 void DocumentThreadableLoader::dataSent(CachedResource& resource, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
295 {
296     ASSERT(m_client);
297     ASSERT_UNUSED(resource, &resource == m_resource);
298     m_client->didSendData(bytesSent, totalBytesToBeSent);
299 }
300
301 void DocumentThreadableLoader::responseReceived(CachedResource& resource, const ResourceResponse& response)
302 {
303     ASSERT_UNUSED(resource, &resource == m_resource);
304     didReceiveResponse(m_resource->identifier(), response);
305 }
306
307 void DocumentThreadableLoader::didReceiveResponse(unsigned long identifier, const ResourceResponse& response)
308 {
309     ASSERT(m_client);
310     ASSERT(response.type() != ResourceResponse::Type::Error);
311
312     InspectorInstrumentation::didReceiveThreadableLoaderResponse(*this, identifier);
313
314     if (m_delayCallbacksForIntegrityCheck)
315         return;
316
317     if (options().filteringPolicy == ResponseFilteringPolicy::Disable) {
318         m_client->didReceiveResponse(identifier, response);
319         return;
320     }
321
322     if (response.type() == ResourceResponse::Type::Default) {
323         m_client->didReceiveResponse(identifier, ResourceResponseBase::filter(response));
324         if (response.tainting() == ResourceResponse::Tainting::Opaque) {
325             clearResource();
326             if (m_client)
327                 m_client->didFinishLoading(identifier);
328         }
329     } else {
330         ASSERT(response.type() == ResourceResponse::Type::Opaqueredirect);
331         m_client->didReceiveResponse(identifier, response);
332     }
333 }
334
335 void DocumentThreadableLoader::dataReceived(CachedResource& resource, const char* data, int dataLength)
336 {
337     ASSERT_UNUSED(resource, &resource == m_resource);
338     didReceiveData(m_resource->identifier(), data, dataLength);
339 }
340
341 void DocumentThreadableLoader::didReceiveData(unsigned long, const char* data, int dataLength)
342 {
343     ASSERT(m_client);
344
345     if (m_delayCallbacksForIntegrityCheck)
346         return;
347
348     m_client->didReceiveData(data, dataLength);
349 }
350
351 void DocumentThreadableLoader::finishedTimingForWorkerLoad(CachedResource& resource, const ResourceTiming& resourceTiming)
352 {
353     ASSERT(m_client);
354     ASSERT_UNUSED(resource, &resource == m_resource);
355
356     finishedTimingForWorkerLoad(resourceTiming);
357 }
358
359 void DocumentThreadableLoader::finishedTimingForWorkerLoad(const ResourceTiming& resourceTiming)
360 {
361     ASSERT(m_options.initiatorContext == InitiatorContext::Worker);
362
363     m_client->didFinishTiming(resourceTiming);
364 }
365
366 void DocumentThreadableLoader::notifyFinished(CachedResource& resource)
367 {
368     ASSERT(m_client);
369     ASSERT_UNUSED(resource, &resource == m_resource);
370
371     if (m_resource->errorOccurred())
372         didFail(m_resource->identifier(), m_resource->resourceError());
373     else
374         didFinishLoading(m_resource->identifier());
375 }
376
377 void DocumentThreadableLoader::didFinishLoading(unsigned long identifier)
378 {
379     ASSERT(m_client);
380
381     if (m_delayCallbacksForIntegrityCheck) {
382         if (!matchIntegrityMetadata(*m_resource, m_options.integrity)) {
383             reportIntegrityMetadataError(m_resource->url());
384             return;
385         }
386
387         auto response = m_resource->response();
388
389         if (options().filteringPolicy == ResponseFilteringPolicy::Disable) {
390             m_client->didReceiveResponse(identifier, response);
391             m_client->didReceiveData(m_resource->resourceBuffer()->data(), m_resource->resourceBuffer()->size());
392         } else {
393             ASSERT(response.type() == ResourceResponse::Type::Default);
394
395             m_client->didReceiveResponse(identifier, ResourceResponseBase::filter(response));
396             m_client->didReceiveData(m_resource->resourceBuffer()->data(), m_resource->resourceBuffer()->size());
397         }
398     }
399
400     m_client->didFinishLoading(identifier);
401 }
402
403 void DocumentThreadableLoader::didFail(unsigned long, const ResourceError& error)
404 {
405     ASSERT(m_client);
406 #if ENABLE(SERVICE_WORKER)
407     if (m_bypassingPreflightForServiceWorkerRequest) {
408         m_options.serviceWorkersMode = ServiceWorkersMode::None;
409         makeCrossOriginAccessRequest(WTFMove(m_bypassingPreflightForServiceWorkerRequest.value()));
410         return;
411     }
412 #endif
413     logErrorAndFail(error);
414 }
415
416 void DocumentThreadableLoader::preflightSuccess(ResourceRequest&& request)
417 {
418     ResourceRequest actualRequest(WTFMove(request));
419     updateRequestForAccessControl(actualRequest, securityOrigin(), m_options.storedCredentialsPolicy);
420
421     m_preflightChecker = std::nullopt;
422
423     // It should be ok to skip the security check since we already asked about the preflight request.
424     loadRequest(WTFMove(actualRequest), SkipSecurityCheck);
425 }
426
427 void DocumentThreadableLoader::preflightFailure(unsigned long identifier, const ResourceError& error)
428 {
429     m_preflightChecker = std::nullopt;
430
431     InspectorInstrumentation::didFailLoading(m_document.frame(), m_document.frame()->loader().documentLoader(), identifier, error);
432     ASSERT(m_client);
433     logErrorAndFail(error);
434 }
435
436 void DocumentThreadableLoader::loadRequest(ResourceRequest&& request, SecurityCheckPolicy securityCheck)
437 {
438     Ref<DocumentThreadableLoader> protectedThis(*this);
439
440     // Any credential should have been removed from the cross-site requests.
441     const URL& requestURL = request.url();
442     m_options.securityCheck = securityCheck;
443     ASSERT(m_sameOriginRequest || requestURL.user().isEmpty());
444     ASSERT(m_sameOriginRequest || requestURL.pass().isEmpty());
445
446     if (!m_referrer.isNull())
447         request.setHTTPReferrer(m_referrer);
448
449     if (m_async) {
450         ResourceLoaderOptions options = m_options;
451         options.clientCredentialPolicy = m_sameOriginRequest ? ClientCredentialPolicy::MayAskClientForCredentials : ClientCredentialPolicy::CannotAskClientForCredentials;
452         options.contentSecurityPolicyImposition = ContentSecurityPolicyImposition::SkipPolicyCheck;
453         
454         // If there is integrity metadata to validate, we must buffer.
455         if (!m_options.integrity.isEmpty())
456             options.dataBufferingPolicy = BufferData;
457
458         request.setAllowCookies(m_options.storedCredentialsPolicy == StoredCredentialsPolicy::Use);
459         CachedResourceRequest newRequest(WTFMove(request), options);
460         if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled())
461             newRequest.setInitiator(m_options.initiator);
462         newRequest.setOrigin(securityOrigin());
463
464         ASSERT(!m_resource);
465         if (m_resource) {
466             CachedResourceHandle<CachedRawResource> resource = std::exchange(m_resource, nullptr);
467             resource->removeClient(*this);
468         }
469
470         auto cachedResource = m_document.cachedResourceLoader().requestRawResource(WTFMove(newRequest));
471         m_resource = cachedResource.valueOr(nullptr);
472         if (m_resource)
473             m_resource->addClient(*this);
474         else
475             logErrorAndFail(cachedResource.error());
476         return;
477     }
478
479     // If credentials mode is 'Omit', we should disable cookie sending.
480     ASSERT(m_options.credentials != FetchOptions::Credentials::Omit);
481
482     LoadTiming loadTiming;
483     loadTiming.markStartTimeAndFetchStart();
484
485     // FIXME: ThreadableLoaderOptions.sniffContent is not supported for synchronous requests.
486     RefPtr<SharedBuffer> data;
487     ResourceError error;
488     ResourceResponse response;
489     unsigned long identifier = std::numeric_limits<unsigned long>::max();
490     if (m_document.frame()) {
491         auto& frameLoader = m_document.frame()->loader();
492         if (!frameLoader.mixedContentChecker().canRunInsecureContent(m_document.securityOrigin(), requestURL))
493             return;
494         identifier = frameLoader.loadResourceSynchronously(request, m_options.storedCredentialsPolicy, m_options.clientCredentialPolicy, error, response, data);
495     }
496
497     loadTiming.setResponseEnd(MonotonicTime::now());
498
499     if (!error.isNull() && response.httpStatusCode() <= 0) {
500         if (requestURL.isLocalFile()) {
501             // We don't want XMLHttpRequest to raise an exception for file:// resources, see <rdar://problem/4962298>.
502             // FIXME: XMLHttpRequest quirks should be in XMLHttpRequest code, not in DocumentThreadableLoader.cpp.
503             didReceiveResponse(identifier, response);
504             didFinishLoading(identifier);
505             return;
506         }
507         logErrorAndFail(error);
508         return;
509     }
510
511     // FIXME: FrameLoader::loadSynchronously() does not tell us whether a redirect happened or not, so we guess by comparing the
512     // request and response URLs. This isn't a perfect test though, since a server can serve a redirect to the same URL that was
513     // requested. Also comparing the request and response URLs as strings will fail if the requestURL still has its credentials.
514     bool didRedirect = requestURL != response.url();
515     if (didRedirect) {
516         if (!isAllowedByContentSecurityPolicy(response.url(), ContentSecurityPolicy::RedirectResponseReceived::Yes)) {
517             reportContentSecurityPolicyError(requestURL);
518             return;
519         }
520         if (!isAllowedRedirect(response.url())) {
521             reportCrossOriginResourceSharingError(requestURL);
522             return;
523         }
524     }
525
526     if (!m_sameOriginRequest) {
527         if (m_options.mode == FetchOptions::Mode::NoCors)
528             response.setTainting(ResourceResponse::Tainting::Opaque);
529         else {
530             ASSERT(m_options.mode == FetchOptions::Mode::Cors);
531             response.setTainting(ResourceResponse::Tainting::Cors);
532             String accessControlErrorDescription;
533             if (!passesAccessControlCheck(response, m_options.storedCredentialsPolicy, securityOrigin(), accessControlErrorDescription)) {
534                 logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, response.url(), accessControlErrorDescription, ResourceError::Type::AccessControl));
535                 return;
536             }
537         }
538     }
539     didReceiveResponse(identifier, response);
540
541     if (data)
542         didReceiveData(identifier, data->data(), data->size());
543
544     if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled()) {
545         auto resourceTiming = ResourceTiming::fromSynchronousLoad(requestURL, m_options.initiator, loadTiming, response.deprecatedNetworkLoadMetrics(), response, securityOrigin());
546         if (options().initiatorContext == InitiatorContext::Worker)
547             finishedTimingForWorkerLoad(resourceTiming);
548         else {
549             if (document().domWindow() && document().domWindow()->performance())
550                 document().domWindow()->performance()->addResourceTiming(WTFMove(resourceTiming));
551         }
552     }
553
554     didFinishLoading(identifier);
555 }
556
557 bool DocumentThreadableLoader::isAllowedByContentSecurityPolicy(const URL& url, ContentSecurityPolicy::RedirectResponseReceived redirectResponseReceived)
558 {
559     switch (m_options.contentSecurityPolicyEnforcement) {
560     case ContentSecurityPolicyEnforcement::DoNotEnforce:
561         return true;
562     case ContentSecurityPolicyEnforcement::EnforceChildSrcDirective:
563         return contentSecurityPolicy().allowChildContextFromSource(url, redirectResponseReceived);
564     case ContentSecurityPolicyEnforcement::EnforceConnectSrcDirective:
565         return contentSecurityPolicy().allowConnectToSource(url, redirectResponseReceived);
566     case ContentSecurityPolicyEnforcement::EnforceScriptSrcDirective:
567         return contentSecurityPolicy().allowScriptFromSource(url, redirectResponseReceived);
568     }
569     ASSERT_NOT_REACHED();
570     return false;
571 }
572
573 bool DocumentThreadableLoader::isAllowedRedirect(const URL& url)
574 {
575     if (m_options.mode == FetchOptions::Mode::NoCors)
576         return true;
577
578     return m_sameOriginRequest && securityOrigin().canRequest(url);
579 }
580
581 bool DocumentThreadableLoader::isXMLHttpRequest() const
582 {
583     return m_options.initiator == cachedResourceRequestInitiators().xmlhttprequest;
584 }
585
586 SecurityOrigin& DocumentThreadableLoader::securityOrigin() const
587 {
588     return m_origin ? *m_origin : m_document.securityOrigin();
589 }
590
591 const ContentSecurityPolicy& DocumentThreadableLoader::contentSecurityPolicy() const
592 {
593     if (m_contentSecurityPolicy)
594         return *m_contentSecurityPolicy.get();
595     ASSERT(m_document.contentSecurityPolicy());
596     return *m_document.contentSecurityPolicy();
597 }
598
599 void DocumentThreadableLoader::reportRedirectionWithBadScheme(const URL& url)
600 {
601     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Redirection to URL with a scheme that is not HTTP(S).", ResourceError::Type::AccessControl));
602 }
603
604 void DocumentThreadableLoader::reportContentSecurityPolicyError(const URL& url)
605 {
606     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Cross-origin redirection denied by Content Security Policy.", ResourceError::Type::AccessControl));
607 }
608
609 void DocumentThreadableLoader::reportCrossOriginResourceSharingError(const URL& url)
610 {
611     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Cross-origin redirection denied by Cross-Origin Resource Sharing policy.", ResourceError::Type::AccessControl));
612 }
613
614 void DocumentThreadableLoader::reportIntegrityMetadataError(const URL& url)
615 {
616     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Failed integrity metadata check.", ResourceError::Type::General));
617 }
618
619 void DocumentThreadableLoader::logErrorAndFail(const ResourceError& error)
620 {
621     if (m_shouldLogError == ShouldLogError::Yes)
622         logError(m_document, error, m_options.initiator);
623     ASSERT(m_client);
624     m_client->didFail(error);
625 }
626
627 } // namespace WebCore