2 * Copyright (C) 2016 Canon Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
14 * * Neither the name of Canon 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.
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.
32 #include "CrossOriginPreflightChecker.h"
34 #include "CachedRawResource.h"
35 #include "CachedResourceLoader.h"
36 #include "CachedResourceRequest.h"
37 #include "ContentSecurityPolicy.h"
38 #include "CrossOriginAccessControl.h"
39 #include "CrossOriginPreflightResultCache.h"
40 #include "DocumentThreadableLoader.h"
41 #include "FrameLoader.h"
42 #include "InspectorInstrumentation.h"
43 #include "NetworkLoadMetrics.h"
44 #include "RuntimeEnabledFeatures.h"
45 #include "SharedBuffer.h"
49 CrossOriginPreflightChecker::CrossOriginPreflightChecker(DocumentThreadableLoader& loader, ResourceRequest&& request)
51 , m_request(WTFMove(request))
55 CrossOriginPreflightChecker::~CrossOriginPreflightChecker()
58 m_resource->removeClient(*this);
61 void CrossOriginPreflightChecker::validatePreflightResponse(DocumentThreadableLoader& loader, ResourceRequest&& request, unsigned long identifier, const ResourceResponse& response)
63 String errorDescription;
64 if (!WebCore::validatePreflightResponse(request, response, loader.options().storedCredentialsPolicy, loader.securityOrigin(), errorDescription)) {
65 loader.preflightFailure(identifier, ResourceError(errorDomainWebKitInternal, 0, request.url(), errorDescription, ResourceError::Type::AccessControl));
69 Frame* frame = loader.document().frame();
72 // FIXME: <https://webkit.org/b/164889> Web Inspector: Show Preflight Request information in inspector
73 // This is only showing success preflight requests and responses but we should show network events
74 // for preflight failures and distinguish them better from non-preflight requests.
75 NetworkLoadMetrics emptyMetrics;
76 InspectorInstrumentation::didReceiveResourceResponse(*frame, identifier, frame->loader().documentLoader(), response, nullptr);
77 InspectorInstrumentation::didFinishLoading(frame, frame->loader().documentLoader(), identifier, emptyMetrics, nullptr);
79 loader.preflightSuccess(WTFMove(request));
82 void CrossOriginPreflightChecker::notifyFinished(CachedResource& resource)
84 ASSERT_UNUSED(resource, &resource == m_resource);
85 if (m_resource->loadFailedOrCanceled()) {
86 ResourceError preflightError = m_resource->resourceError();
87 // If the preflight was cancelled by underlying code, it probably means the request was blocked due to some access control policy.
88 // FIXME:: According fetch, we should just pass the error to the layer above. But this may impact some clients like XHR or EventSource.
89 if (preflightError.isNull() || preflightError.isCancellation() || preflightError.isGeneral())
90 preflightError.setType(ResourceError::Type::AccessControl);
92 m_loader.preflightFailure(m_resource->identifier(), preflightError);
95 validatePreflightResponse(m_loader, WTFMove(m_request), m_resource->identifier(), m_resource->response());
98 void CrossOriginPreflightChecker::startPreflight()
100 ResourceLoaderOptions options;
101 options.referrerPolicy = m_loader.options().referrerPolicy;
102 options.redirect = FetchOptions::Redirect::Manual;
103 options.contentSecurityPolicyImposition = ContentSecurityPolicyImposition::SkipPolicyCheck;
104 options.serviceWorkersMode = ServiceWorkersMode::None;
106 CachedResourceRequest preflightRequest(createAccessControlPreflightRequest(m_request, m_loader.securityOrigin(), m_loader.referrer()), options);
107 if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled())
108 preflightRequest.setInitiator(m_loader.options().initiator);
111 m_resource = m_loader.document().cachedResourceLoader().requestRawResource(WTFMove(preflightRequest)).value_or(nullptr);
113 m_resource->addClient(*this);
116 void CrossOriginPreflightChecker::doPreflight(DocumentThreadableLoader& loader, ResourceRequest&& request)
118 if (!loader.document().frame())
121 ResourceRequest preflightRequest = createAccessControlPreflightRequest(request, loader.securityOrigin(), loader.referrer());
123 ResourceResponse response;
124 RefPtr<SharedBuffer> data;
126 unsigned identifier = loader.document().frame()->loader().loadResourceSynchronously(preflightRequest, StoredCredentialsPolicy::DoNotUse, ClientCredentialPolicy::CannotAskClientForCredentials, error, response, data);
128 if (!error.isNull()) {
129 // If the preflight was cancelled by underlying code, it probably means the request was blocked due to some access control policy.
130 // FIXME:: According fetch, we should just pass the error to the layer above. But this may impact some clients like XHR or EventSource.
131 if (error.isCancellation() || error.isGeneral())
132 error.setType(ResourceError::Type::AccessControl);
133 loader.preflightFailure(identifier, error);
137 // FIXME: Ideally, we should ask platformLoadResourceSynchronously to set ResourceResponse isRedirected and use it here.
138 bool isRedirect = preflightRequest.url().strippedForUseAsReferrer() != response.url().strippedForUseAsReferrer();
139 if (isRedirect || !response.isSuccessful()) {
140 loader.preflightFailure(identifier, ResourceError(errorDomainWebKitInternal, 0, request.url(), ASCIILiteral("Preflight response is not successful"), ResourceError::Type::AccessControl));
144 validatePreflightResponse(loader, WTFMove(request), identifier, response);
147 void CrossOriginPreflightChecker::setDefersLoading(bool value)
150 m_resource->setDefersLoading(value);
153 bool CrossOriginPreflightChecker::isXMLHttpRequest() const
155 return m_loader.isXMLHttpRequest();
158 } // namespace WebCore