1 | /*
|
---|
2 | * Copyright (C) 2021 Apple 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
|
---|
6 | * are met:
|
---|
7 | * 1. Redistributions of source code must retain the above copyright
|
---|
8 | * notice, this list of conditions and the following disclaimer.
|
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer in the
|
---|
11 | * documentation and/or other materials provided with the distribution.
|
---|
12 | *
|
---|
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
|
---|
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
---|
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
|
---|
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
---|
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
---|
23 | * THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "config.h"
|
---|
27 | #include "CrossOriginOpenerPolicy.h"
|
---|
28 |
|
---|
29 | #include "ContentSecurityPolicy.h"
|
---|
30 | #include "CrossOriginEmbedderPolicy.h"
|
---|
31 | #include "FormData.h"
|
---|
32 | #include "Frame.h"
|
---|
33 | #include "HTTPHeaderNames.h"
|
---|
34 | #include "HTTPParsers.h"
|
---|
35 | #include "NavigationRequester.h"
|
---|
36 | #include "Page.h"
|
---|
37 | #include "PingLoader.h"
|
---|
38 | #include "ResourceResponse.h"
|
---|
39 | #include "ScriptExecutionContext.h"
|
---|
40 | #include "SecurityPolicy.h"
|
---|
41 | #include <wtf/JSONValues.h>
|
---|
42 |
|
---|
43 | namespace WebCore {
|
---|
44 |
|
---|
45 | static ASCIILiteral crossOriginOpenerPolicyToString(const CrossOriginOpenerPolicyValue& coop)
|
---|
46 | {
|
---|
47 | switch (coop) {
|
---|
48 | case CrossOriginOpenerPolicyValue::SameOrigin:
|
---|
49 | case CrossOriginOpenerPolicyValue::SameOriginPlusCOEP:
|
---|
50 | return "same-origin"_s;
|
---|
51 | case CrossOriginOpenerPolicyValue::SameOriginAllowPopups:
|
---|
52 | return "same-origin-allow-popups"_s;
|
---|
53 | case CrossOriginOpenerPolicyValue::UnsafeNone:
|
---|
54 | break;
|
---|
55 | }
|
---|
56 | return "unsafe-none"_s;
|
---|
57 | }
|
---|
58 |
|
---|
59 | // https://html.spec.whatwg.org/multipage/origin.html#check-browsing-context-group-switch-coop-value
|
---|
60 | static bool checkIfCOOPValuesRequireBrowsingContextGroupSwitch(bool isInitialAboutBlank, CrossOriginOpenerPolicyValue activeDocumentCOOPValue, const SecurityOrigin& activeDocumentNavigationOrigin, CrossOriginOpenerPolicyValue responseCOOPValue, const SecurityOrigin& responseOrigin)
|
---|
61 | {
|
---|
62 | // If the result of matching activeDocumentCOOPValue, activeDocumentNavigationOrigin, responseCOOPValue, and responseOrigin is true, return false.
|
---|
63 | // https://html.spec.whatwg.org/multipage/origin.html#matching-coop
|
---|
64 | if (activeDocumentCOOPValue == CrossOriginOpenerPolicyValue::UnsafeNone && responseCOOPValue == CrossOriginOpenerPolicyValue::UnsafeNone)
|
---|
65 | return false;
|
---|
66 | if (activeDocumentCOOPValue == responseCOOPValue && activeDocumentNavigationOrigin.isSameOriginAs(responseOrigin))
|
---|
67 | return false;
|
---|
68 |
|
---|
69 | // If all of the following are true:
|
---|
70 | // - isInitialAboutBlank,
|
---|
71 | // - activeDocumentCOOPValue's value is "same-origin-allow-popups".
|
---|
72 | // - responseCOOPValue is "unsafe-none",
|
---|
73 | // then return false.
|
---|
74 | if (isInitialAboutBlank && activeDocumentCOOPValue == CrossOriginOpenerPolicyValue::SameOriginAllowPopups && responseCOOPValue == CrossOriginOpenerPolicyValue::UnsafeNone)
|
---|
75 | return false;
|
---|
76 |
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | // https://html.spec.whatwg.org/multipage/origin.html#check-bcg-switch-navigation-report-only
|
---|
81 | static bool checkIfEnforcingReportOnlyCOOPWouldRequireBrowsingContextGroupSwitch(bool isInitialAboutBlank, const CrossOriginOpenerPolicy& activeDocumentCOOP, const SecurityOrigin& activeDocumentNavigationOrigin, const CrossOriginOpenerPolicy& responseCOOP, const SecurityOrigin& responseOrigin)
|
---|
82 | {
|
---|
83 | if (!checkIfCOOPValuesRequireBrowsingContextGroupSwitch(isInitialAboutBlank, activeDocumentCOOP.reportOnlyValue, activeDocumentNavigationOrigin, responseCOOP.reportOnlyValue, responseOrigin))
|
---|
84 | return false;
|
---|
85 |
|
---|
86 | if (checkIfCOOPValuesRequireBrowsingContextGroupSwitch(isInitialAboutBlank, activeDocumentCOOP.reportOnlyValue, activeDocumentNavigationOrigin, responseCOOP.value, responseOrigin))
|
---|
87 | return true;
|
---|
88 |
|
---|
89 | if (checkIfCOOPValuesRequireBrowsingContextGroupSwitch(isInitialAboutBlank, activeDocumentCOOP.value, activeDocumentNavigationOrigin, responseCOOP.reportOnlyValue, responseOrigin))
|
---|
90 | return true;
|
---|
91 |
|
---|
92 | return false;
|
---|
93 | }
|
---|
94 |
|
---|
95 | static std::tuple<Ref<SecurityOrigin>, CrossOriginOpenerPolicy> computeResponseOriginAndCOOP(const ResourceResponse& response, const std::optional<NavigationRequester>& requester, ContentSecurityPolicy* responseCSP)
|
---|
96 | {
|
---|
97 | // Non-initial empty documents (about:blank) should inherit their cross-origin-opener-policy from the navigation's initiator top level document,
|
---|
98 | // if the initiator and its top level document are same-origin, or default (unsafe-none) otherwise.
|
---|
99 | // https://github.com/whatwg/html/issues/6913
|
---|
100 | if (SecurityPolicy::shouldInheritSecurityOriginFromOwner(response.url()) && requester)
|
---|
101 | return std::make_tuple(requester->securityOrigin, requester->securityOrigin->isSameOriginAs(requester->topOrigin) ? requester->crossOriginOpenerPolicy : CrossOriginOpenerPolicy { });
|
---|
102 |
|
---|
103 | // If the HTTP response contains a CSP header, it may set sandbox flags, which would cause the origin to become unique.
|
---|
104 | auto responseOrigin = responseCSP && responseCSP->sandboxFlags() != SandboxNone ? SecurityOrigin::createUnique() : SecurityOrigin::create(response.url());
|
---|
105 | return std::make_tuple(WTFMove(responseOrigin), obtainCrossOriginOpenerPolicy(response));
|
---|
106 | }
|
---|
107 |
|
---|
108 | // https://html.spec.whatwg.org/multipage/origin.html#coop-enforce
|
---|
109 | static CrossOriginOpenerPolicyEnforcementResult enforceResponseCrossOriginOpenerPolicy(const CrossOriginOpenerPolicyEnforcementResult& currentCoopEnforcementResult, const URL& responseURL, SecurityOrigin& responseOrigin, const CrossOriginOpenerPolicy& responseCOOP, bool isDisplayingInitialEmptyDocument)
|
---|
110 | {
|
---|
111 | CrossOriginOpenerPolicyEnforcementResult newCOOPEnforcementResult = {
|
---|
112 | responseURL,
|
---|
113 | responseOrigin,
|
---|
114 | responseCOOP,
|
---|
115 | true /* isCurrentContextNavigationSource */,
|
---|
116 | currentCoopEnforcementResult.needsBrowsingContextGroupSwitch,
|
---|
117 | currentCoopEnforcementResult.needsBrowsingContextGroupSwitchDueToReportOnly
|
---|
118 | };
|
---|
119 |
|
---|
120 | if (checkIfCOOPValuesRequireBrowsingContextGroupSwitch(isDisplayingInitialEmptyDocument, currentCoopEnforcementResult.crossOriginOpenerPolicy.value, currentCoopEnforcementResult.currentOrigin, responseCOOP.value, responseOrigin))
|
---|
121 | newCOOPEnforcementResult.needsBrowsingContextGroupSwitch = true;
|
---|
122 |
|
---|
123 | if (checkIfEnforcingReportOnlyCOOPWouldRequireBrowsingContextGroupSwitch(isDisplayingInitialEmptyDocument, currentCoopEnforcementResult.crossOriginOpenerPolicy, currentCoopEnforcementResult.currentOrigin, responseCOOP, responseOrigin))
|
---|
124 | newCOOPEnforcementResult.needsBrowsingContextGroupSwitchDueToReportOnly = true;
|
---|
125 |
|
---|
126 | return newCOOPEnforcementResult;
|
---|
127 | }
|
---|
128 |
|
---|
129 | // https://html.spec.whatwg.org/multipage/origin.html#obtain-coop
|
---|
130 | CrossOriginOpenerPolicy obtainCrossOriginOpenerPolicy(const ResourceResponse& response)
|
---|
131 | {
|
---|
132 | std::optional<CrossOriginEmbedderPolicy> coep;
|
---|
133 | auto ensureCOEP = [&coep, &response]() -> CrossOriginEmbedderPolicy& {
|
---|
134 | if (!coep)
|
---|
135 | coep = obtainCrossOriginEmbedderPolicy(response, nullptr);
|
---|
136 | return *coep;
|
---|
137 | };
|
---|
138 | auto parseCOOP = [&response, &ensureCOEP](HTTPHeaderName headerName, auto& value, auto& reportingEndpoint) {
|
---|
139 | auto coopParsingResult = parseStructuredFieldValue(response.httpHeaderField(headerName));
|
---|
140 | if (!coopParsingResult)
|
---|
141 | return;
|
---|
142 |
|
---|
143 | if (coopParsingResult->first == "same-origin"_s) {
|
---|
144 | auto& coep = ensureCOEP();
|
---|
145 | if (coep.value == CrossOriginEmbedderPolicyValue::RequireCORP || (headerName == HTTPHeaderName::CrossOriginOpenerPolicyReportOnly && coep.reportOnlyValue == CrossOriginEmbedderPolicyValue::RequireCORP))
|
---|
146 | value = CrossOriginOpenerPolicyValue::SameOriginPlusCOEP;
|
---|
147 | else
|
---|
148 | value = CrossOriginOpenerPolicyValue::SameOrigin;
|
---|
149 | } else if (coopParsingResult->first == "same-origin-allow-popups"_s)
|
---|
150 | value = CrossOriginOpenerPolicyValue::SameOriginAllowPopups;
|
---|
151 |
|
---|
152 | reportingEndpoint = coopParsingResult->second.get<HashTranslatorASCIILiteral>("report-to"_s);
|
---|
153 | };
|
---|
154 |
|
---|
155 | CrossOriginOpenerPolicy policy;
|
---|
156 | if (!SecurityOrigin::create(response.url())->isPotentiallyTrustworthy())
|
---|
157 | return policy;
|
---|
158 |
|
---|
159 | parseCOOP(HTTPHeaderName::CrossOriginOpenerPolicy, policy.value, policy.reportingEndpoint);
|
---|
160 | parseCOOP(HTTPHeaderName::CrossOriginOpenerPolicyReportOnly, policy.reportOnlyValue, policy.reportOnlyReportingEndpoint);
|
---|
161 | return policy;
|
---|
162 | }
|
---|
163 |
|
---|
164 | CrossOriginOpenerPolicy CrossOriginOpenerPolicy::isolatedCopy() const &
|
---|
165 | {
|
---|
166 | return { value, reportingEndpoint.isolatedCopy(), reportOnlyValue, reportOnlyReportingEndpoint.isolatedCopy() };
|
---|
167 | }
|
---|
168 |
|
---|
169 | CrossOriginOpenerPolicy CrossOriginOpenerPolicy::isolatedCopy() &&
|
---|
170 | {
|
---|
171 | return { value, WTFMove(reportingEndpoint).isolatedCopy(), reportOnlyValue, WTFMove(reportOnlyReportingEndpoint).isolatedCopy() };
|
---|
172 | }
|
---|
173 |
|
---|
174 | void addCrossOriginOpenerPolicyHeaders(ResourceResponse& response, const CrossOriginOpenerPolicy& coop)
|
---|
175 | {
|
---|
176 | if (coop.value != CrossOriginOpenerPolicyValue::UnsafeNone) {
|
---|
177 | if (coop.reportingEndpoint.isEmpty())
|
---|
178 | response.setHTTPHeaderField(HTTPHeaderName::CrossOriginOpenerPolicy, crossOriginOpenerPolicyToString(coop.value));
|
---|
179 | else
|
---|
180 | response.setHTTPHeaderField(HTTPHeaderName::CrossOriginOpenerPolicy, makeString(crossOriginOpenerPolicyToString(coop.value), "; report-to=\"", coop.reportingEndpoint, '\"'));
|
---|
181 | }
|
---|
182 | if (coop.reportOnlyValue != CrossOriginOpenerPolicyValue::UnsafeNone) {
|
---|
183 | if (coop.reportOnlyReportingEndpoint.isEmpty())
|
---|
184 | response.setHTTPHeaderField(HTTPHeaderName::CrossOriginOpenerPolicyReportOnly, crossOriginOpenerPolicyToString(coop.reportOnlyValue));
|
---|
185 | else
|
---|
186 | response.setHTTPHeaderField(HTTPHeaderName::CrossOriginOpenerPolicyReportOnly, makeString(crossOriginOpenerPolicyToString(coop.reportOnlyValue), "; report-to=\"", coop.reportOnlyReportingEndpoint, '\"'));
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#process-a-navigate-fetch (Step 13.5.6)
|
---|
191 | std::optional<CrossOriginOpenerPolicyEnforcementResult> doCrossOriginOpenerHandlingOfResponse(const ResourceResponse& response, const std::optional<NavigationRequester>& requester, ContentSecurityPolicy* responseCSP, SandboxFlags effectiveSandboxFlags, bool isDisplayingInitialEmptyDocument, const CrossOriginOpenerPolicyEnforcementResult& currentCoopEnforcementResult)
|
---|
192 | {
|
---|
193 | auto [responseOrigin, responseCOOP] = computeResponseOriginAndCOOP(response, requester, responseCSP);
|
---|
194 |
|
---|
195 | // https://html.spec.whatwg.org/multipage/browsing-the-web.html#process-a-navigate-fetch (Step 13.5.6.2)
|
---|
196 | // If sandboxFlags is not empty and responseCOOP's value is not "unsafe-none", then set response to an appropriate network error and break.
|
---|
197 | if (responseCOOP.value != CrossOriginOpenerPolicyValue::UnsafeNone && effectiveSandboxFlags != SandboxNone)
|
---|
198 | return std::nullopt;
|
---|
199 |
|
---|
200 | return enforceResponseCrossOriginOpenerPolicy(currentCoopEnforcementResult, response.url(), responseOrigin, responseCOOP, isDisplayingInitialEmptyDocument);
|
---|
201 | }
|
---|
202 |
|
---|
203 | CrossOriginOpenerPolicyEnforcementResult CrossOriginOpenerPolicyEnforcementResult::from(const URL& currentURL, Ref<SecurityOrigin>&& currentOrigin, const CrossOriginOpenerPolicy& crossOriginOpenerPolicy, std::optional<NavigationRequester> requester, const URL& openerURL)
|
---|
204 | {
|
---|
205 | CrossOriginOpenerPolicyEnforcementResult result { currentURL, WTFMove(currentOrigin), crossOriginOpenerPolicy };
|
---|
206 | result.isCurrentContextNavigationSource = requester && result.currentOrigin->isSameOriginAs(requester->securityOrigin);
|
---|
207 | if (SecurityPolicy::shouldInheritSecurityOriginFromOwner(currentURL) && openerURL.isValid())
|
---|
208 | result.url = openerURL;
|
---|
209 | return result;
|
---|
210 | }
|
---|
211 |
|
---|
212 | } // namespace WebCore
|
---|