youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 2c6d4eb | 2017-07-01 21:26:31 +0000 | [diff] [blame] | 41 | #include "FrameLoader.h" |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 42 | #include "InspectorInstrumentation.h" |
joepeck@webkit.org | 7dd950a | 2017-03-09 23:44:12 +0000 | [diff] [blame] | 43 | #include "NetworkLoadMetrics.h" |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 44 | #include "RuntimeEnabledFeatures.h" |
annulen@yandex.ru | dd15e81 | 2017-06-23 16:11:24 +0000 | [diff] [blame] | 45 | #include "SharedBuffer.h" |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 46 | |
| 47 | namespace WebCore { |
| 48 | |
| 49 | CrossOriginPreflightChecker::CrossOriginPreflightChecker(DocumentThreadableLoader& loader, ResourceRequest&& request) |
| 50 | : m_loader(loader) |
| 51 | , m_request(WTFMove(request)) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | CrossOriginPreflightChecker::~CrossOriginPreflightChecker() |
| 56 | { |
| 57 | if (m_resource) |
commit-queue@webkit.org | 0396ac0 | 2016-10-06 16:53:58 +0000 | [diff] [blame] | 58 | m_resource->removeClient(*this); |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 59 | } |
| 60 | |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 61 | void CrossOriginPreflightChecker::validatePreflightResponse(DocumentThreadableLoader& loader, ResourceRequest&& request, unsigned long identifier, const ResourceResponse& response) |
| 62 | { |
youenn@apple.com | 36b74c3 | 2018-04-25 17:57:56 +0000 | [diff] [blame] | 63 | auto* frame = loader.document().frame(); |
| 64 | ASSERT(frame); |
| 65 | |
cdumez@apple.com | ed885f9 | 2017-08-09 05:15:47 +0000 | [diff] [blame] | 66 | String errorDescription; |
cdumez@apple.com | 22964df | 2017-09-25 21:17:43 +0000 | [diff] [blame] | 67 | if (!WebCore::validatePreflightResponse(request, response, loader.options().storedCredentialsPolicy, loader.securityOrigin(), errorDescription)) { |
youenn@apple.com | 36b74c3 | 2018-04-25 17:57:56 +0000 | [diff] [blame] | 68 | if (auto* document = frame->document()) |
| 69 | document->addConsoleMessage(MessageSource::Security, MessageLevel::Error, errorDescription); |
| 70 | |
cdumez@apple.com | ed885f9 | 2017-08-09 05:15:47 +0000 | [diff] [blame] | 71 | loader.preflightFailure(identifier, ResourceError(errorDomainWebKitInternal, 0, request.url(), errorDescription, ResourceError::Type::AccessControl)); |
| 72 | return; |
| 73 | } |
| 74 | |
commit-queue@webkit.org | 135a572 | 2016-11-30 18:01:36 +0000 | [diff] [blame] | 75 | // 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.org | 7dd950a | 2017-03-09 23:44:12 +0000 | [diff] [blame] | 78 | NetworkLoadMetrics emptyMetrics; |
commit-queue@webkit.org | d9b34d4 | 2016-12-11 23:40:15 +0000 | [diff] [blame] | 79 | InspectorInstrumentation::didReceiveResourceResponse(*frame, identifier, frame->loader().documentLoader(), response, nullptr); |
commit-queue@webkit.org | e4d5622 | 2017-03-14 18:19:47 +0000 | [diff] [blame] | 80 | InspectorInstrumentation::didFinishLoading(frame, frame->loader().documentLoader(), identifier, emptyMetrics, nullptr); |
commit-queue@webkit.org | 135a572 | 2016-11-30 18:01:36 +0000 | [diff] [blame] | 81 | |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 82 | loader.preflightSuccess(WTFMove(request)); |
| 83 | } |
| 84 | |
commit-queue@webkit.org | 4c0caf3 | 2016-10-07 07:02:02 +0000 | [diff] [blame] | 85 | void CrossOriginPreflightChecker::notifyFinished(CachedResource& resource) |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 86 | { |
commit-queue@webkit.org | 4c0caf3 | 2016-10-07 07:02:02 +0000 | [diff] [blame] | 87 | ASSERT_UNUSED(resource, &resource == m_resource); |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 88 | if (m_resource->loadFailedOrCanceled()) { |
commit-queue@webkit.org | d7aeace | 2016-06-28 06:09:05 +0000 | [diff] [blame] | 89 | ResourceError preflightError = m_resource->resourceError(); |
commit-queue@webkit.org | ebc0332 | 2016-09-28 16:37:26 +0000 | [diff] [blame] | 90 | // 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.com | 6c98336 | 2018-04-26 18:17:55 +0000 | [diff] [blame] | 95 | if (!preflightError.isTimeout()) |
utatane.tea@gmail.com | 8407763 | 2018-06-23 08:39:34 +0000 | [diff] [blame] | 96 | m_loader.document().addConsoleMessage(MessageSource::Security, MessageLevel::Error, "CORS-preflight request was blocked"_s); |
commit-queue@webkit.org | d7aeace | 2016-06-28 06:09:05 +0000 | [diff] [blame] | 97 | m_loader.preflightFailure(m_resource->identifier(), preflightError); |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | validatePreflightResponse(m_loader, WTFMove(m_request), m_resource->identifier(), m_resource->response()); |
| 101 | } |
| 102 | |
commit-queue@webkit.org | e410df2 | 2018-01-20 10:45:25 +0000 | [diff] [blame] | 103 | void 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.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 110 | void CrossOriginPreflightChecker::startPreflight() |
| 111 | { |
commit-queue@webkit.org | 0c8dff4 | 2016-09-06 11:06:52 +0000 | [diff] [blame] | 112 | ResourceLoaderOptions options; |
| 113 | options.referrerPolicy = m_loader.options().referrerPolicy; |
commit-queue@webkit.org | 7bf7536 | 2016-09-21 08:43:23 +0000 | [diff] [blame] | 114 | options.contentSecurityPolicyImposition = ContentSecurityPolicyImposition::SkipPolicyCheck; |
commit-queue@webkit.org | 6974fbf | 2017-12-06 20:51:59 +0000 | [diff] [blame] | 115 | options.serviceWorkersMode = ServiceWorkersMode::None; |
commit-queue@webkit.org | cb6b543 | 2018-02-07 21:55:23 +0000 | [diff] [blame] | 116 | options.initiatorContext = m_loader.options().initiatorContext; |
youenn.fablet@crf.canon.fr | 2959d28 | 2016-06-17 10:34:02 +0000 | [diff] [blame] | 117 | |
commit-queue@webkit.org | 858242f | 2016-10-13 10:34:15 +0000 | [diff] [blame] | 118 | CachedResourceRequest preflightRequest(createAccessControlPreflightRequest(m_request, m_loader.securityOrigin(), m_loader.referrer()), options); |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 119 | if (RuntimeEnabledFeatures::sharedFeatures().resourceTimingEnabled()) |
| 120 | preflightRequest.setInitiator(m_loader.options().initiator); |
| 121 | |
| 122 | ASSERT(!m_resource); |
jfbastien@apple.com | bcea987 | 2017-12-04 23:34:57 +0000 | [diff] [blame] | 123 | m_resource = m_loader.document().cachedResourceLoader().requestRawResource(WTFMove(preflightRequest)).value_or(nullptr); |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 124 | if (m_resource) |
commit-queue@webkit.org | 0396ac0 | 2016-10-06 16:53:58 +0000 | [diff] [blame] | 125 | m_resource->addClient(*this); |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void CrossOriginPreflightChecker::doPreflight(DocumentThreadableLoader& loader, ResourceRequest&& request) |
| 129 | { |
| 130 | if (!loader.document().frame()) |
| 131 | return; |
| 132 | |
commit-queue@webkit.org | 858242f | 2016-10-13 10:34:15 +0000 | [diff] [blame] | 133 | ResourceRequest preflightRequest = createAccessControlPreflightRequest(request, loader.securityOrigin(), loader.referrer()); |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 134 | ResourceError error; |
| 135 | ResourceResponse response; |
| 136 | RefPtr<SharedBuffer> data; |
commit-queue@webkit.org | f89b11d | 2016-08-02 07:20:23 +0000 | [diff] [blame] | 137 | |
youenn@apple.com | b9709cc | 2018-04-16 21:50:26 +0000 | [diff] [blame] | 138 | unsigned identifier = loader.document().frame()->loader().loadResourceSynchronously(preflightRequest, ClientCredentialPolicy::CannotAskClientForCredentials, FetchOptions { }, { }, error, response, data); |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 139 | |
commit-queue@webkit.org | ebc0332 | 2016-09-28 16:37:26 +0000 | [diff] [blame] | 140 | 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.com | 6c98336 | 2018-04-26 18:17:55 +0000 | [diff] [blame] | 145 | |
| 146 | if (!error.isTimeout()) |
utatane.tea@gmail.com | 8407763 | 2018-06-23 08:39:34 +0000 | [diff] [blame] | 147 | loader.document().addConsoleMessage(MessageSource::Security, MessageLevel::Error, "CORS-preflight request was blocked"_s); |
youenn@apple.com | 6c98336 | 2018-04-26 18:17:55 +0000 | [diff] [blame] | 148 | |
commit-queue@webkit.org | f88d4fa | 2016-06-22 18:11:00 +0000 | [diff] [blame] | 149 | loader.preflightFailure(identifier, error); |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 150 | return; |
| 151 | } |
commit-queue@webkit.org | 16c9f496 | 2016-07-30 19:27:09 +0000 | [diff] [blame] | 152 | |
| 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.com | 8407763 | 2018-06-23 08:39:34 +0000 | [diff] [blame] | 156 | auto errorMessage = "Preflight response is not successful"_s; |
youenn@apple.com | 36b74c3 | 2018-04-25 17:57:56 +0000 | [diff] [blame] | 157 | 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.org | 16c9f496 | 2016-07-30 19:27:09 +0000 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 163 | validatePreflightResponse(loader, WTFMove(request), identifier, response); |
| 164 | } |
| 165 | |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 166 | void CrossOriginPreflightChecker::setDefersLoading(bool value) |
| 167 | { |
| 168 | if (m_resource) |
| 169 | m_resource->setDefersLoading(value); |
| 170 | } |
| 171 | |
youenn.fablet@crf.canon.fr | bba9793 | 2016-06-10 13:26:30 +0000 | [diff] [blame] | 172 | } // namespace WebCore |