Make fetch() use "same-origin" credentials by default
[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     m_referrer = request.httpReferrer();
324     if (m_referrer.isNull())
325         m_options.referrerPolicy = ReferrerPolicy::NoReferrer;
326
327     // Let's fetch the request with the original headers (equivalent to request cloning specified by fetch algorithm).
328     // Do not copy the Authorization header if removed by the network layer.
329     if (!request.httpHeaderFields().contains(HTTPHeaderName::Authorization))
330         m_originalHeaders->remove(HTTPHeaderName::Authorization);
331     request.setHTTPHeaderFields(*m_originalHeaders);
332
333 #if ENABLE(SERVICE_WORKER)
334     if (redirectResponse.source() != ResourceResponse::Source::ServiceWorker && redirectResponse.source() != ResourceResponse::Source::MemoryCache)
335         m_options.serviceWorkersMode = ServiceWorkersMode::None;
336 #endif
337     makeCrossOriginAccessRequest(ResourceRequest(request));
338     completionHandler(WTFMove(request));
339 }
340
341 void DocumentThreadableLoader::dataSent(CachedResource& resource, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
342 {
343     ASSERT(m_client);
344     ASSERT_UNUSED(resource, &resource == m_resource);
345     m_client->didSendData(bytesSent, totalBytesToBeSent);
346 }
347
348 void DocumentThreadableLoader::responseReceived(CachedResource& resource, const ResourceResponse& response, CompletionHandler<void()>&& completionHandler)
349 {
350     ASSERT_UNUSED(resource, &resource == m_resource);
351     didReceiveResponse(m_resource->identifier(), response);
352
353     if (completionHandler)
354         completionHandler();
355 }
356
357 void DocumentThreadableLoader::didReceiveResponse(unsigned long identifier, const ResourceResponse& response)
358 {
359     ASSERT(m_client);
360     ASSERT(response.type() != ResourceResponse::Type::Error);
361
362     InspectorInstrumentation::didReceiveThreadableLoaderResponse(*this, identifier);
363
364     if (m_delayCallbacksForIntegrityCheck)
365         return;
366
367     if (options().filteringPolicy == ResponseFilteringPolicy::Disable) {
368         m_client->didReceiveResponse(identifier, response);
369         return;
370     }
371
372     if (response.type() == ResourceResponse::Type::Default) {
373         m_client->didReceiveResponse(identifier, ResourceResponseBase::filter(response));
374         if (response.tainting() == ResourceResponse::Tainting::Opaque) {
375             clearResource();
376             if (m_client)
377                 m_client->didFinishLoading(identifier);
378         }
379         return;
380     }
381     ASSERT(response.type() == ResourceResponse::Type::Opaqueredirect || response.source() == ResourceResponse::Source::ServiceWorker || response.source() == ResourceResponse::Source::MemoryCache);
382     m_client->didReceiveResponse(identifier, response);
383 }
384
385 void DocumentThreadableLoader::dataReceived(CachedResource& resource, const char* data, int dataLength)
386 {
387     ASSERT_UNUSED(resource, &resource == m_resource);
388     didReceiveData(m_resource->identifier(), data, dataLength);
389 }
390
391 void DocumentThreadableLoader::didReceiveData(unsigned long, const char* data, int dataLength)
392 {
393     ASSERT(m_client);
394
395     if (m_delayCallbacksForIntegrityCheck)
396         return;
397
398     m_client->didReceiveData(data, dataLength);
399 }
400
401 void DocumentThreadableLoader::finishedTimingForWorkerLoad(CachedResource& resource, const ResourceTiming& resourceTiming)
402 {
403     ASSERT(m_client);
404     ASSERT_UNUSED(resource, &resource == m_resource);
405
406     finishedTimingForWorkerLoad(resourceTiming);
407 }
408
409 void DocumentThreadableLoader::finishedTimingForWorkerLoad(const ResourceTiming& resourceTiming)
410 {
411     ASSERT(m_options.initiatorContext == InitiatorContext::Worker);
412
413     m_client->didFinishTiming(resourceTiming);
414 }
415
416 void DocumentThreadableLoader::notifyFinished(CachedResource& resource)
417 {
418     ASSERT(m_client);
419     ASSERT_UNUSED(resource, &resource == m_resource);
420
421     if (m_resource->errorOccurred())
422         didFail(m_resource->identifier(), m_resource->resourceError());
423     else
424         didFinishLoading(m_resource->identifier());
425 }
426
427 void DocumentThreadableLoader::didFinishLoading(unsigned long identifier)
428 {
429     ASSERT(m_client);
430
431     if (m_delayCallbacksForIntegrityCheck) {
432         if (!matchIntegrityMetadata(*m_resource, m_options.integrity)) {
433             reportIntegrityMetadataError(m_resource->url());
434             return;
435         }
436
437         auto response = m_resource->response();
438
439         if (options().filteringPolicy == ResponseFilteringPolicy::Disable) {
440             m_client->didReceiveResponse(identifier, response);
441             m_client->didReceiveData(m_resource->resourceBuffer()->data(), m_resource->resourceBuffer()->size());
442         } else {
443             ASSERT(response.type() == ResourceResponse::Type::Default);
444
445             m_client->didReceiveResponse(identifier, ResourceResponseBase::filter(response));
446             m_client->didReceiveData(m_resource->resourceBuffer()->data(), m_resource->resourceBuffer()->size());
447         }
448     }
449
450     m_client->didFinishLoading(identifier);
451 }
452
453 void DocumentThreadableLoader::didFail(unsigned long, const ResourceError& error)
454 {
455     ASSERT(m_client);
456 #if ENABLE(SERVICE_WORKER)
457     if (m_bypassingPreflightForServiceWorkerRequest && error.isCancellation()) {
458         clearResource();
459
460         m_options.serviceWorkersMode = ServiceWorkersMode::None;
461         makeCrossOriginAccessRequestWithPreflight(WTFMove(m_bypassingPreflightForServiceWorkerRequest.value()));
462         ASSERT(m_bypassingPreflightForServiceWorkerRequest->isNull());
463         m_bypassingPreflightForServiceWorkerRequest = std::nullopt;
464         return;
465     }
466 #endif
467
468     if (m_shouldLogError == ShouldLogError::Yes)
469         logError(m_document, error, m_options.initiator);
470
471     m_client->didFail(error);
472 }
473
474 void DocumentThreadableLoader::preflightSuccess(ResourceRequest&& request)
475 {
476     ResourceRequest actualRequest(WTFMove(request));
477     updateRequestForAccessControl(actualRequest, securityOrigin(), m_options.storedCredentialsPolicy);
478
479     m_preflightChecker = std::nullopt;
480
481     // It should be ok to skip the security check since we already asked about the preflight request.
482     loadRequest(WTFMove(actualRequest), SecurityCheckPolicy::SkipSecurityCheck);
483 }
484
485 void DocumentThreadableLoader::preflightFailure(unsigned long identifier, const ResourceError& error)
486 {
487     m_preflightChecker = std::nullopt;
488
489     InspectorInstrumentation::didFailLoading(m_document.frame(), m_document.frame()->loader().documentLoader(), identifier, error);
490
491     if (m_shouldLogError == ShouldLogError::Yes)
492         logError(m_document, error, m_options.initiator);
493
494     m_client->didFail(error);
495 }
496
497 void DocumentThreadableLoader::loadRequest(ResourceRequest&& request, SecurityCheckPolicy securityCheck)
498 {
499     Ref<DocumentThreadableLoader> protectedThis(*this);
500
501     // Any credential should have been removed from the cross-site requests.
502     const URL& requestURL = request.url();
503     m_options.securityCheck = securityCheck;
504     ASSERT(m_sameOriginRequest || requestURL.user().isEmpty());
505     ASSERT(m_sameOriginRequest || requestURL.pass().isEmpty());
506
507     if (!m_referrer.isNull())
508         request.setHTTPReferrer(m_referrer);
509
510     if (m_async) {
511         ResourceLoaderOptions options = m_options;
512         options.clientCredentialPolicy = m_sameOriginRequest ? ClientCredentialPolicy::MayAskClientForCredentials : ClientCredentialPolicy::CannotAskClientForCredentials;
513         options.contentSecurityPolicyImposition = ContentSecurityPolicyImposition::SkipPolicyCheck;
514         
515         // If there is integrity metadata to validate, we must buffer.
516         if (!m_options.integrity.isEmpty())
517             options.dataBufferingPolicy = DataBufferingPolicy::BufferData;
518
519         request.setAllowCookies(m_options.storedCredentialsPolicy == StoredCredentialsPolicy::Use);
520         CachedResourceRequest newRequest(WTFMove(request), options);
521         if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled())
522             newRequest.setInitiator(m_options.initiator);
523         newRequest.setOrigin(securityOrigin());
524
525         ASSERT(!m_resource);
526         if (m_resource) {
527             CachedResourceHandle<CachedRawResource> resource = std::exchange(m_resource, nullptr);
528             resource->removeClient(*this);
529         }
530
531         auto cachedResource = m_document.cachedResourceLoader().requestRawResource(WTFMove(newRequest));
532         m_resource = cachedResource.value_or(nullptr);
533         if (m_resource)
534             m_resource->addClient(*this);
535         else
536             logErrorAndFail(cachedResource.error());
537         return;
538     }
539
540     // If credentials mode is 'Omit', we should disable cookie sending.
541     ASSERT(m_options.credentials != FetchOptions::Credentials::Omit);
542
543     LoadTiming loadTiming;
544     loadTiming.markStartTimeAndFetchStart();
545
546     // FIXME: ThreadableLoaderOptions.sniffContent is not supported for synchronous requests.
547     RefPtr<SharedBuffer> data;
548     ResourceError error;
549     ResourceResponse response;
550     unsigned long identifier = std::numeric_limits<unsigned long>::max();
551     if (m_document.frame()) {
552         auto& frameLoader = m_document.frame()->loader();
553         if (!frameLoader.mixedContentChecker().canRunInsecureContent(m_document.securityOrigin(), requestURL))
554             return;
555         identifier = frameLoader.loadResourceSynchronously(request, m_options.clientCredentialPolicy, m_options, *m_originalHeaders, error, response, data);
556     }
557
558     loadTiming.setResponseEnd(MonotonicTime::now());
559
560     if (!error.isNull() && response.httpStatusCode() <= 0) {
561         if (requestURL.isLocalFile()) {
562             // We don't want XMLHttpRequest to raise an exception for file:// resources, see <rdar://problem/4962298>.
563             // FIXME: XMLHttpRequest quirks should be in XMLHttpRequest code, not in DocumentThreadableLoader.cpp.
564             didReceiveResponse(identifier, response);
565             didFinishLoading(identifier);
566             return;
567         }
568         logErrorAndFail(error);
569         return;
570     }
571
572     if (!shouldPerformSecurityChecks()) {
573         // FIXME: FrameLoader::loadSynchronously() does not tell us whether a redirect happened or not, so we guess by comparing the
574         // request and response URLs. This isn't a perfect test though, since a server can serve a redirect to the same URL that was
575         // requested. Also comparing the request and response URLs as strings will fail if the requestURL still has its credentials.
576         bool didRedirect = requestURL != response.url();
577         if (didRedirect) {
578             if (!isAllowedByContentSecurityPolicy(response.url(), ContentSecurityPolicy::RedirectResponseReceived::Yes)) {
579                 reportContentSecurityPolicyError(requestURL);
580                 return;
581             }
582             if (!isAllowedRedirect(response.url())) {
583                 reportCrossOriginResourceSharingError(requestURL);
584                 return;
585             }
586         }
587
588         if (!m_sameOriginRequest) {
589             if (m_options.mode == FetchOptions::Mode::NoCors)
590                 response.setTainting(ResourceResponse::Tainting::Opaque);
591             else {
592                 ASSERT(m_options.mode == FetchOptions::Mode::Cors);
593                 response.setTainting(ResourceResponse::Tainting::Cors);
594                 String accessControlErrorDescription;
595                 if (!passesAccessControlCheck(response, m_options.storedCredentialsPolicy, securityOrigin(), accessControlErrorDescription)) {
596                     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, response.url(), accessControlErrorDescription, ResourceError::Type::AccessControl));
597                     return;
598                 }
599             }
600         }
601     }
602     didReceiveResponse(identifier, response);
603
604     if (data)
605         didReceiveData(identifier, data->data(), data->size());
606
607     if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled()) {
608         auto resourceTiming = ResourceTiming::fromSynchronousLoad(requestURL, m_options.initiator, loadTiming, response.deprecatedNetworkLoadMetrics(), response, securityOrigin());
609         if (options().initiatorContext == InitiatorContext::Worker)
610             finishedTimingForWorkerLoad(resourceTiming);
611         else {
612             if (document().domWindow() && document().domWindow()->performance())
613                 document().domWindow()->performance()->addResourceTiming(WTFMove(resourceTiming));
614         }
615     }
616
617     didFinishLoading(identifier);
618 }
619
620 bool DocumentThreadableLoader::isAllowedByContentSecurityPolicy(const URL& url, ContentSecurityPolicy::RedirectResponseReceived redirectResponseReceived)
621 {
622     switch (m_options.contentSecurityPolicyEnforcement) {
623     case ContentSecurityPolicyEnforcement::DoNotEnforce:
624         return true;
625     case ContentSecurityPolicyEnforcement::EnforceChildSrcDirective:
626         return contentSecurityPolicy().allowChildContextFromSource(url, redirectResponseReceived);
627     case ContentSecurityPolicyEnforcement::EnforceConnectSrcDirective:
628         return contentSecurityPolicy().allowConnectToSource(url, redirectResponseReceived);
629     case ContentSecurityPolicyEnforcement::EnforceScriptSrcDirective:
630         return contentSecurityPolicy().allowScriptFromSource(url, redirectResponseReceived);
631     }
632     ASSERT_NOT_REACHED();
633     return false;
634 }
635
636 bool DocumentThreadableLoader::isAllowedRedirect(const URL& url)
637 {
638     if (m_options.mode == FetchOptions::Mode::NoCors)
639         return true;
640
641     return m_sameOriginRequest && securityOrigin().canRequest(url);
642 }
643
644 bool DocumentThreadableLoader::isXMLHttpRequest() const
645 {
646     return m_options.initiator == cachedResourceRequestInitiators().xmlhttprequest;
647 }
648
649 SecurityOrigin& DocumentThreadableLoader::securityOrigin() const
650 {
651     return m_origin ? *m_origin : m_document.securityOrigin();
652 }
653
654 const ContentSecurityPolicy& DocumentThreadableLoader::contentSecurityPolicy() const
655 {
656     if (m_contentSecurityPolicy)
657         return *m_contentSecurityPolicy.get();
658     ASSERT(m_document.contentSecurityPolicy());
659     return *m_document.contentSecurityPolicy();
660 }
661
662 void DocumentThreadableLoader::reportRedirectionWithBadScheme(const URL& url)
663 {
664     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Redirection to URL with a scheme that is not HTTP(S)."_s, ResourceError::Type::AccessControl));
665 }
666
667 void DocumentThreadableLoader::reportContentSecurityPolicyError(const URL& url)
668 {
669     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Blocked by Content Security Policy."_s, ResourceError::Type::AccessControl));
670 }
671
672 void DocumentThreadableLoader::reportCrossOriginResourceSharingError(const URL& url)
673 {
674     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Cross-origin redirection denied by Cross-Origin Resource Sharing policy."_s, ResourceError::Type::AccessControl));
675 }
676
677 void DocumentThreadableLoader::reportIntegrityMetadataError(const URL& url)
678 {
679     logErrorAndFail(ResourceError(errorDomainWebKitInternal, 0, url, "Failed integrity metadata check."_s, ResourceError::Type::General));
680 }
681
682 void DocumentThreadableLoader::logErrorAndFail(const ResourceError& error)
683 {
684     if (m_shouldLogError == ShouldLogError::Yes) {
685         if (error.isAccessControl() && !error.localizedDescription().isEmpty())
686             m_document.addConsoleMessage(MessageSource::Security, MessageLevel::Error, error.localizedDescription());
687         logError(m_document, error, m_options.initiator);
688     }
689     ASSERT(m_client);
690     m_client->didFail(error);
691 }
692
693 } // namespace WebCore