1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "nsIHttpChannel.h"
9 #include "nsContentUtils.h"
10 #include "nsIRedirectHistoryEntry.h"
11 #include "nsIReferrerInfo.h"
12 #include "mozIThirdPartyUtil.h"
13 #include "nsMixedContentBlocker.h"
14 #include "nsNetUtil.h"
15 #include "mozilla/BasePrincipal.h"
16 #include "mozilla/StaticPrefs_dom.h"
18 // Helper function which maps an internal content policy type
19 // to the corresponding destination for the context of SecFetch.
20 nsCString
MapInternalContentPolicyTypeToDest(nsContentPolicyType aType
) {
22 case nsIContentPolicy::TYPE_OTHER
:
24 case nsIContentPolicy::TYPE_INTERNAL_SCRIPT
:
25 case nsIContentPolicy::TYPE_INTERNAL_SCRIPT_PRELOAD
:
26 case nsIContentPolicy::TYPE_INTERNAL_MODULE
:
27 case nsIContentPolicy::TYPE_INTERNAL_MODULE_PRELOAD
:
28 case nsIContentPolicy::TYPE_INTERNAL_WORKER_IMPORT_SCRIPTS
:
29 case nsIContentPolicy::TYPE_INTERNAL_CHROMEUTILS_COMPILED_SCRIPT
:
30 case nsIContentPolicy::TYPE_INTERNAL_FRAME_MESSAGEMANAGER_SCRIPT
:
31 case nsIContentPolicy::TYPE_SCRIPT
:
33 case nsIContentPolicy::TYPE_INTERNAL_WORKER
:
35 case nsIContentPolicy::TYPE_INTERNAL_SHARED_WORKER
:
36 return "sharedworker"_ns
;
37 case nsIContentPolicy::TYPE_INTERNAL_SERVICE_WORKER
:
38 return "serviceworker"_ns
;
39 case nsIContentPolicy::TYPE_INTERNAL_AUDIOWORKLET
:
40 return "audioworklet"_ns
;
41 case nsIContentPolicy::TYPE_INTERNAL_PAINTWORKLET
:
42 return "paintworklet"_ns
;
43 case nsIContentPolicy::TYPE_IMAGESET
:
44 case nsIContentPolicy::TYPE_INTERNAL_IMAGE
:
45 case nsIContentPolicy::TYPE_INTERNAL_IMAGE_PRELOAD
:
46 case nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON
:
47 case nsIContentPolicy::TYPE_IMAGE
:
49 case nsIContentPolicy::TYPE_STYLESHEET
:
50 case nsIContentPolicy::TYPE_INTERNAL_STYLESHEET
:
51 case nsIContentPolicy::TYPE_INTERNAL_STYLESHEET_PRELOAD
:
53 case nsIContentPolicy::TYPE_OBJECT
:
54 case nsIContentPolicy::TYPE_INTERNAL_OBJECT
:
56 case nsIContentPolicy::TYPE_INTERNAL_EMBED
:
58 case nsIContentPolicy::TYPE_DOCUMENT
:
60 case nsIContentPolicy::TYPE_SUBDOCUMENT
:
61 case nsIContentPolicy::TYPE_INTERNAL_IFRAME
:
63 case nsIContentPolicy::TYPE_INTERNAL_FRAME
:
65 case nsIContentPolicy::TYPE_PING
:
67 case nsIContentPolicy::TYPE_XMLHTTPREQUEST
:
68 case nsIContentPolicy::TYPE_INTERNAL_XMLHTTPREQUEST
:
70 case nsIContentPolicy::TYPE_INTERNAL_EVENTSOURCE
:
72 case nsIContentPolicy::TYPE_OBJECT_SUBREQUEST
:
74 case nsIContentPolicy::TYPE_DTD
:
75 case nsIContentPolicy::TYPE_INTERNAL_DTD
:
76 case nsIContentPolicy::TYPE_INTERNAL_FORCE_ALLOWED_DTD
:
78 case nsIContentPolicy::TYPE_FONT
:
79 case nsIContentPolicy::TYPE_INTERNAL_FONT_PRELOAD
:
80 case nsIContentPolicy::TYPE_UA_FONT
:
82 case nsIContentPolicy::TYPE_MEDIA
:
84 case nsIContentPolicy::TYPE_INTERNAL_AUDIO
:
86 case nsIContentPolicy::TYPE_INTERNAL_VIDEO
:
88 case nsIContentPolicy::TYPE_INTERNAL_TRACK
:
90 case nsIContentPolicy::TYPE_WEBSOCKET
:
91 return "websocket"_ns
;
92 case nsIContentPolicy::TYPE_CSP_REPORT
:
94 case nsIContentPolicy::TYPE_XSLT
:
96 case nsIContentPolicy::TYPE_BEACON
:
98 case nsIContentPolicy::TYPE_FETCH
:
99 case nsIContentPolicy::TYPE_INTERNAL_FETCH_PRELOAD
:
101 case nsIContentPolicy::TYPE_WEB_MANIFEST
:
102 return "manifest"_ns
;
103 case nsIContentPolicy::TYPE_SAVEAS_DOWNLOAD
:
105 case nsIContentPolicy::TYPE_SPECULATIVE
:
107 case nsIContentPolicy::TYPE_PROXIED_WEBRTC_MEDIA
:
109 case nsIContentPolicy::TYPE_INVALID
:
111 // Do not add default: so that compilers can catch the missing case.
114 MOZ_CRASH("Unhandled nsContentPolicyType value");
117 // Helper function to determine if a ExpandedPrincipal is of the same-origin as
118 // a URI in the sec-fetch context.
119 void IsExpandedPrincipalSameOrigin(
120 nsCOMPtr
<nsIExpandedPrincipal
> aExpandedPrincipal
, nsIURI
* aURI
,
121 bool aIsPrivateWin
, bool* aRes
) {
123 for (const auto& principal
: aExpandedPrincipal
->AllowList()) {
124 // Ignore extension principals to continue treating
125 // "moz-extension:"-requests as not "same-origin".
126 if (!mozilla::BasePrincipal::Cast(principal
)->AddonPolicy()) {
127 // A ExpandedPrincipal usually has at most one ContentPrincipal, so we can
128 // check IsSameOrigin on it here and return early.
129 mozilla::BasePrincipal::Cast(principal
)->IsSameOrigin(aURI
, aIsPrivateWin
,
136 // Helper function to determine whether a request (including involved
137 // redirects) is same-origin in the context of SecFetch.
138 bool IsSameOrigin(nsIHttpChannel
* aHTTPChannel
) {
139 nsCOMPtr
<nsIURI
> channelURI
;
140 NS_GetFinalChannelURI(aHTTPChannel
, getter_AddRefs(channelURI
));
142 nsCOMPtr
<nsILoadInfo
> loadInfo
= aHTTPChannel
->LoadInfo();
144 if (mozilla::BasePrincipal::Cast(loadInfo
->TriggeringPrincipal())
146 // If an extension triggered the load that has access to the URI then the
147 // load is considered as same-origin.
148 return mozilla::BasePrincipal::Cast(loadInfo
->TriggeringPrincipal())
149 ->AddonAllowsLoad(channelURI
);
152 bool isPrivateWin
= loadInfo
->GetOriginAttributes().mPrivateBrowsingId
> 0;
153 bool isSameOrigin
= false;
154 if (nsContentUtils::IsExpandedPrincipal(loadInfo
->TriggeringPrincipal())) {
155 nsCOMPtr
<nsIExpandedPrincipal
> ep
=
156 do_QueryInterface(loadInfo
->TriggeringPrincipal());
157 IsExpandedPrincipalSameOrigin(ep
, channelURI
, isPrivateWin
, &isSameOrigin
);
159 nsresult rv
= loadInfo
->TriggeringPrincipal()->IsSameOrigin(
160 channelURI
, isPrivateWin
, &isSameOrigin
);
161 mozilla::Unused
<< NS_WARN_IF(NS_FAILED(rv
));
164 // if the initial request is not same-origin, we can return here
165 // because we already know it's not a same-origin request
170 // let's further check all the hoops in the redirectChain to
171 // ensure all involved redirects are same-origin
172 nsCOMPtr
<nsIPrincipal
> redirectPrincipal
;
173 for (nsIRedirectHistoryEntry
* entry
: loadInfo
->RedirectChain()) {
174 entry
->GetPrincipal(getter_AddRefs(redirectPrincipal
));
175 if (redirectPrincipal
) {
176 nsresult rv
= redirectPrincipal
->IsSameOrigin(channelURI
, isPrivateWin
,
178 mozilla::Unused
<< NS_WARN_IF(NS_FAILED(rv
));
185 // must be a same-origin request
189 // Helper function to determine whether a request (including involved
190 // redirects) is same-site in the context of SecFetch.
191 bool IsSameSite(nsIChannel
* aHTTPChannel
) {
192 nsCOMPtr
<mozIThirdPartyUtil
> thirdPartyUtil
=
193 do_GetService(THIRDPARTYUTIL_CONTRACTID
);
194 if (!thirdPartyUtil
) {
198 nsAutoCString hostDomain
;
199 nsCOMPtr
<nsILoadInfo
> loadInfo
= aHTTPChannel
->LoadInfo();
200 nsresult rv
= loadInfo
->TriggeringPrincipal()->GetBaseDomain(hostDomain
);
201 mozilla::Unused
<< NS_WARN_IF(NS_FAILED(rv
));
203 nsAutoCString channelDomain
;
204 nsCOMPtr
<nsIURI
> channelURI
;
205 NS_GetFinalChannelURI(aHTTPChannel
, getter_AddRefs(channelURI
));
206 rv
= thirdPartyUtil
->GetBaseDomain(channelURI
, channelDomain
);
207 mozilla::Unused
<< NS_WARN_IF(NS_FAILED(rv
));
209 // if the initial request is not same-site, or not https, we can
210 // return here because we already know it's not a same-site request
211 if (!hostDomain
.Equals(channelDomain
) ||
212 !loadInfo
->TriggeringPrincipal()->SchemeIs("https")) {
216 // let's further check all the hoops in the redirectChain to
217 // ensure all involved redirects are same-site and https
218 nsCOMPtr
<nsIPrincipal
> redirectPrincipal
;
219 for (nsIRedirectHistoryEntry
* entry
: loadInfo
->RedirectChain()) {
220 entry
->GetPrincipal(getter_AddRefs(redirectPrincipal
));
221 if (redirectPrincipal
) {
222 redirectPrincipal
->GetBaseDomain(hostDomain
);
223 if (!hostDomain
.Equals(channelDomain
) ||
224 !redirectPrincipal
->SchemeIs("https")) {
230 // must be a same-site request
234 // Helper function to determine whether a request was triggered
235 // by the end user in the context of SecFetch.
236 bool IsUserTriggeredForSecFetchSite(nsIHttpChannel
* aHTTPChannel
) {
238 * The goal is to distinguish between "webby" navigations that are controlled
239 * by a given website (e.g. links, the window.location setter,form
240 * submissions, etc.), and those that are not (e.g. user interaction with a
241 * user agent’s address bar, bookmarks, etc).
243 nsCOMPtr
<nsILoadInfo
> loadInfo
= aHTTPChannel
->LoadInfo();
244 ExtContentPolicyType contentType
= loadInfo
->GetExternalContentPolicyType();
246 // A request issued by the browser is always user initiated.
247 if (loadInfo
->TriggeringPrincipal()->IsSystemPrincipal() &&
248 contentType
== ExtContentPolicy::TYPE_OTHER
) {
252 // only requests wich result in type "document" are subject to
253 // user initiated actions in the context of SecFetch.
254 if (contentType
!= ExtContentPolicy::TYPE_DOCUMENT
&&
255 contentType
!= ExtContentPolicy::TYPE_SUBDOCUMENT
) {
259 // The load is considered user triggered if it was triggered by an external
261 if (loadInfo
->GetLoadTriggeredFromExternal()) {
265 // sec-fetch-site can only be user triggered if the load was user triggered.
266 if (!loadInfo
->GetHasValidUserGestureActivation()) {
270 // We can assert that the navigation must be "webby" if the load was triggered
271 // by a meta refresh. See also Bug 1647128.
272 if (loadInfo
->GetIsMetaRefresh()) {
276 // All web requests have a valid "original" referrer set in the
277 // ReferrerInfo which we can use to determine whether a request
278 // was triggered by a user or not.
279 nsCOMPtr
<nsIReferrerInfo
> referrerInfo
= aHTTPChannel
->GetReferrerInfo();
281 nsCOMPtr
<nsIURI
> originalReferrer
;
282 referrerInfo
->GetOriginalReferrer(getter_AddRefs(originalReferrer
));
283 if (originalReferrer
) {
291 void mozilla::dom::SecFetch::AddSecFetchDest(nsIHttpChannel
* aHTTPChannel
) {
292 nsCOMPtr
<nsILoadInfo
> loadInfo
= aHTTPChannel
->LoadInfo();
293 nsContentPolicyType contentType
= loadInfo
->InternalContentPolicyType();
294 nsCString dest
= MapInternalContentPolicyTypeToDest(contentType
);
297 aHTTPChannel
->SetRequestHeader("Sec-Fetch-Dest"_ns
, dest
, false);
298 mozilla::Unused
<< NS_WARN_IF(NS_FAILED(rv
));
301 void mozilla::dom::SecFetch::AddSecFetchMode(nsIHttpChannel
* aHTTPChannel
) {
302 nsAutoCString
mode("no-cors");
304 nsCOMPtr
<nsILoadInfo
> loadInfo
= aHTTPChannel
->LoadInfo();
305 uint32_t securityMode
= loadInfo
->GetSecurityMode();
306 ExtContentPolicyType externalType
= loadInfo
->GetExternalContentPolicyType();
309 nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_INHERITS_SEC_CONTEXT
||
310 securityMode
== nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED
) {
311 mode
= "same-origin"_ns
;
312 } else if (securityMode
==
313 nsILoadInfo::SEC_REQUIRE_CORS_INHERITS_SEC_CONTEXT
) {
316 // If it's not one of the security modes above, then we ensure it's
317 // at least one of the others defined in nsILoadInfo
320 nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT
||
322 nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL
,
323 "unhandled security mode");
326 if (externalType
== ExtContentPolicy::TYPE_DOCUMENT
||
327 externalType
== ExtContentPolicy::TYPE_SUBDOCUMENT
||
328 externalType
== ExtContentPolicy::TYPE_OBJECT
) {
329 mode
= "navigate"_ns
;
330 } else if (externalType
== ExtContentPolicy::TYPE_WEBSOCKET
) {
331 mode
= "websocket"_ns
;
335 aHTTPChannel
->SetRequestHeader("Sec-Fetch-Mode"_ns
, mode
, false);
336 mozilla::Unused
<< NS_WARN_IF(NS_FAILED(rv
));
339 void mozilla::dom::SecFetch::AddSecFetchSite(nsIHttpChannel
* aHTTPChannel
) {
340 nsAutoCString
site("same-origin");
342 bool isSameOrigin
= IsSameOrigin(aHTTPChannel
);
344 bool isSameSite
= IsSameSite(aHTTPChannel
);
346 site
= "same-site"_ns
;
348 site
= "cross-site"_ns
;
352 if (IsUserTriggeredForSecFetchSite(aHTTPChannel
)) {
357 aHTTPChannel
->SetRequestHeader("Sec-Fetch-Site"_ns
, site
, false);
358 mozilla::Unused
<< NS_WARN_IF(NS_FAILED(rv
));
361 void mozilla::dom::SecFetch::AddSecFetchUser(nsIHttpChannel
* aHTTPChannel
) {
362 nsCOMPtr
<nsILoadInfo
> loadInfo
= aHTTPChannel
->LoadInfo();
363 ExtContentPolicyType externalType
= loadInfo
->GetExternalContentPolicyType();
365 // sec-fetch-user only applies to loads of type document or subdocument
366 if (externalType
!= ExtContentPolicy::TYPE_DOCUMENT
&&
367 externalType
!= ExtContentPolicy::TYPE_SUBDOCUMENT
) {
371 // sec-fetch-user only applies if the request is user triggered.
372 // requests triggered by an external application are considerd user triggered.
373 if (!loadInfo
->GetLoadTriggeredFromExternal() &&
374 !loadInfo
->GetHasValidUserGestureActivation()) {
378 nsAutoCString
user("?1");
380 aHTTPChannel
->SetRequestHeader("Sec-Fetch-User"_ns
, user
, false);
381 mozilla::Unused
<< NS_WARN_IF(NS_FAILED(rv
));
384 void mozilla::dom::SecFetch::AddSecFetchHeader(nsIHttpChannel
* aHTTPChannel
) {
385 // if sec-fetch-* is prefed off, then there is nothing to do
386 if (!StaticPrefs::dom_security_secFetch_enabled()) {
390 nsCOMPtr
<nsIURI
> uri
;
391 nsresult rv
= aHTTPChannel
->GetURI(getter_AddRefs(uri
));
392 if (NS_WARN_IF(NS_FAILED(rv
))) {
396 // if we are not dealing with a potentially trustworthy URL, then
397 // there is nothing to do here
398 if (!nsMixedContentBlocker::IsPotentiallyTrustworthyOrigin(uri
)) {
402 AddSecFetchDest(aHTTPChannel
);
403 AddSecFetchMode(aHTTPChannel
);
404 AddSecFetchSite(aHTTPChannel
);
405 AddSecFetchUser(aHTTPChannel
);