blob: b3df41107ddbefd03e3bd44bfd25a83e9ba81b6d [file] [log] [blame]
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +00001/*
2 * Copyright (C) 2016 Canon 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 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.
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 "CrossOriginPreflightChecker.h"
33
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"
rniwa@webkit.org2c6d4eb2017-07-01 21:26:31 +000041#include "FrameLoader.h"
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +000042#include "InspectorInstrumentation.h"
joepeck@webkit.org7dd950a2017-03-09 23:44:12 +000043#include "NetworkLoadMetrics.h"
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +000044#include "RuntimeEnabledFeatures.h"
annulen@yandex.rudd15e812017-06-23 16:11:24 +000045#include "SharedBuffer.h"
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +000046
47namespace WebCore {
48
49CrossOriginPreflightChecker::CrossOriginPreflightChecker(DocumentThreadableLoader& loader, ResourceRequest&& request)
50 : m_loader(loader)
51 , m_request(WTFMove(request))
52{
53}
54
55CrossOriginPreflightChecker::~CrossOriginPreflightChecker()
56{
57 if (m_resource)
commit-queue@webkit.org0396ac02016-10-06 16:53:58 +000058 m_resource->removeClient(*this);
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +000059}
60
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +000061void CrossOriginPreflightChecker::validatePreflightResponse(DocumentThreadableLoader& loader, ResourceRequest&& request, unsigned long identifier, const ResourceResponse& response)
62{
youenn@apple.com36b74c32018-04-25 17:57:56 +000063 auto* frame = loader.document().frame();
64 ASSERT(frame);
65
cdumez@apple.comed885f92017-08-09 05:15:47 +000066 String errorDescription;
cdumez@apple.com22964df2017-09-25 21:17:43 +000067 if (!WebCore::validatePreflightResponse(request, response, loader.options().storedCredentialsPolicy, loader.securityOrigin(), errorDescription)) {
youenn@apple.com36b74c32018-04-25 17:57:56 +000068 if (auto* document = frame->document())
69 document->addConsoleMessage(MessageSource::Security, MessageLevel::Error, errorDescription);
70
cdumez@apple.comed885f92017-08-09 05:15:47 +000071 loader.preflightFailure(identifier, ResourceError(errorDomainWebKitInternal, 0, request.url(), errorDescription, ResourceError::Type::AccessControl));
72 return;
73 }
74
commit-queue@webkit.org135a5722016-11-30 18:01:36 +000075 // FIXME: <https://webkit.org/b/164889> Web Inspector: Show Preflight Request information in inspector
76 // This is only showing success preflight requests and responses but we should show network events
77 // for preflight failures and distinguish them better from non-preflight requests.
joepeck@webkit.org7dd950a2017-03-09 23:44:12 +000078 NetworkLoadMetrics emptyMetrics;
commit-queue@webkit.orgd9b34d42016-12-11 23:40:15 +000079 InspectorInstrumentation::didReceiveResourceResponse(*frame, identifier, frame->loader().documentLoader(), response, nullptr);
commit-queue@webkit.orge4d56222017-03-14 18:19:47 +000080 InspectorInstrumentation::didFinishLoading(frame, frame->loader().documentLoader(), identifier, emptyMetrics, nullptr);
commit-queue@webkit.org135a5722016-11-30 18:01:36 +000081
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +000082 loader.preflightSuccess(WTFMove(request));
83}
84
commit-queue@webkit.org4c0caf32016-10-07 07:02:02 +000085void CrossOriginPreflightChecker::notifyFinished(CachedResource& resource)
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +000086{
commit-queue@webkit.org4c0caf32016-10-07 07:02:02 +000087 ASSERT_UNUSED(resource, &resource == m_resource);
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +000088 if (m_resource->loadFailedOrCanceled()) {
commit-queue@webkit.orgd7aeace2016-06-28 06:09:05 +000089 ResourceError preflightError = m_resource->resourceError();
commit-queue@webkit.orgebc03322016-09-28 16:37:26 +000090 // If the preflight was cancelled by underlying code, it probably means the request was blocked due to some access control policy.
91 // FIXME:: According fetch, we should just pass the error to the layer above. But this may impact some clients like XHR or EventSource.
92 if (preflightError.isNull() || preflightError.isCancellation() || preflightError.isGeneral())
93 preflightError.setType(ResourceError::Type::AccessControl);
94
youenn@apple.com6c983362018-04-26 18:17:55 +000095 if (!preflightError.isTimeout())
utatane.tea@gmail.com84077632018-06-23 08:39:34 +000096 m_loader.document().addConsoleMessage(MessageSource::Security, MessageLevel::Error, "CORS-preflight request was blocked"_s);
commit-queue@webkit.orgd7aeace2016-06-28 06:09:05 +000097 m_loader.preflightFailure(m_resource->identifier(), preflightError);
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +000098 return;
99 }
100 validatePreflightResponse(m_loader, WTFMove(m_request), m_resource->identifier(), m_resource->response());
101}
102
commit-queue@webkit.orge410df22018-01-20 10:45:25 +0000103void CrossOriginPreflightChecker::redirectReceived(CachedResource& resource, ResourceRequest&&, const ResourceResponse& response, CompletionHandler<void(ResourceRequest&&)>&& completionHandler)
104{
105 ASSERT_UNUSED(resource, &resource == m_resource);
106 validatePreflightResponse(m_loader, WTFMove(m_request), m_resource->identifier(), response);
107 completionHandler(ResourceRequest { });
108}
109
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +0000110void CrossOriginPreflightChecker::startPreflight()
111{
commit-queue@webkit.org0c8dff42016-09-06 11:06:52 +0000112 ResourceLoaderOptions options;
113 options.referrerPolicy = m_loader.options().referrerPolicy;
commit-queue@webkit.org7bf75362016-09-21 08:43:23 +0000114 options.contentSecurityPolicyImposition = ContentSecurityPolicyImposition::SkipPolicyCheck;
commit-queue@webkit.org6974fbf2017-12-06 20:51:59 +0000115 options.serviceWorkersMode = ServiceWorkersMode::None;
commit-queue@webkit.orgcb6b5432018-02-07 21:55:23 +0000116 options.initiatorContext = m_loader.options().initiatorContext;
youenn.fablet@crf.canon.fr2959d282016-06-17 10:34:02 +0000117
commit-queue@webkit.org858242f2016-10-13 10:34:15 +0000118 CachedResourceRequest preflightRequest(createAccessControlPreflightRequest(m_request, m_loader.securityOrigin(), m_loader.referrer()), options);
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +0000119 if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled())
120 preflightRequest.setInitiator(m_loader.options().initiator);
121
122 ASSERT(!m_resource);
jfbastien@apple.combcea9872017-12-04 23:34:57 +0000123 m_resource = m_loader.document().cachedResourceLoader().requestRawResource(WTFMove(preflightRequest)).value_or(nullptr);
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +0000124 if (m_resource)
commit-queue@webkit.org0396ac02016-10-06 16:53:58 +0000125 m_resource->addClient(*this);
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +0000126}
127
128void CrossOriginPreflightChecker::doPreflight(DocumentThreadableLoader& loader, ResourceRequest&& request)
129{
130 if (!loader.document().frame())
131 return;
132
commit-queue@webkit.org858242f2016-10-13 10:34:15 +0000133 ResourceRequest preflightRequest = createAccessControlPreflightRequest(request, loader.securityOrigin(), loader.referrer());
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +0000134 ResourceError error;
135 ResourceResponse response;
136 RefPtr<SharedBuffer> data;
commit-queue@webkit.orgf89b11d2016-08-02 07:20:23 +0000137
youenn@apple.comb9709cc2018-04-16 21:50:26 +0000138 unsigned identifier = loader.document().frame()->loader().loadResourceSynchronously(preflightRequest, ClientCredentialPolicy::CannotAskClientForCredentials, FetchOptions { }, { }, error, response, data);
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +0000139
commit-queue@webkit.orgebc03322016-09-28 16:37:26 +0000140 if (!error.isNull()) {
141 // If the preflight was cancelled by underlying code, it probably means the request was blocked due to some access control policy.
142 // FIXME:: According fetch, we should just pass the error to the layer above. But this may impact some clients like XHR or EventSource.
143 if (error.isCancellation() || error.isGeneral())
144 error.setType(ResourceError::Type::AccessControl);
youenn@apple.com6c983362018-04-26 18:17:55 +0000145
146 if (!error.isTimeout())
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000147 loader.document().addConsoleMessage(MessageSource::Security, MessageLevel::Error, "CORS-preflight request was blocked"_s);
youenn@apple.com6c983362018-04-26 18:17:55 +0000148
commit-queue@webkit.orgf88d4fa2016-06-22 18:11:00 +0000149 loader.preflightFailure(identifier, error);
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +0000150 return;
151 }
commit-queue@webkit.org16c9f4962016-07-30 19:27:09 +0000152
153 // FIXME: Ideally, we should ask platformLoadResourceSynchronously to set ResourceResponse isRedirected and use it here.
154 bool isRedirect = preflightRequest.url().strippedForUseAsReferrer() != response.url().strippedForUseAsReferrer();
155 if (isRedirect || !response.isSuccessful()) {
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000156 auto errorMessage = "Preflight response is not successful"_s;
youenn@apple.com36b74c32018-04-25 17:57:56 +0000157 loader.document().addConsoleMessage(MessageSource::Security, MessageLevel::Error, errorMessage);
158
159 loader.preflightFailure(identifier, ResourceError { errorDomainWebKitInternal, 0, request.url(), errorMessage, ResourceError::Type::AccessControl });
commit-queue@webkit.org16c9f4962016-07-30 19:27:09 +0000160 return;
161 }
162
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +0000163 validatePreflightResponse(loader, WTFMove(request), identifier, response);
164}
165
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +0000166void CrossOriginPreflightChecker::setDefersLoading(bool value)
167{
168 if (m_resource)
169 m_resource->setDefersLoading(value);
170}
171
youenn.fablet@crf.canon.frbba97932016-06-10 13:26:30 +0000172} // namespace WebCore