Bug 1526591 - Remove devtools.inspector.shapesHighlighter.enabled pref. r=rcaliman
[gecko.git] / netwerk / base / LoadInfo.cpp
blob41211b609b47e003885f1e0da2faed48ba1baea8
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/. */
7 #include "mozilla/LoadInfo.h"
9 #include "mozilla/Assertions.h"
10 #include "mozilla/dom/ClientIPCTypes.h"
11 #include "mozilla/dom/ClientSource.h"
12 #include "mozilla/dom/PerformanceStorage.h"
13 #include "mozilla/dom/TabChild.h"
14 #include "mozilla/dom/ToJSValue.h"
15 #include "mozilla/dom/BrowsingContext.h"
16 #include "mozilla/NullPrincipal.h"
17 #include "mozIThirdPartyUtil.h"
18 #include "nsFrameLoader.h"
19 #include "nsIContentSecurityPolicy.h"
20 #include "nsIDocShell.h"
21 #include "mozilla/dom/Document.h"
22 #include "nsIFrameLoaderOwner.h"
23 #include "nsIInterfaceRequestorUtils.h"
24 #include "nsISupportsImpl.h"
25 #include "nsISupportsUtils.h"
26 #include "nsIXPConnect.h"
27 #include "nsContentUtils.h"
28 #include "nsDocShell.h"
29 #include "nsGlobalWindow.h"
30 #include "nsMixedContentBlocker.h"
31 #include "nsRedirectHistoryEntry.h"
32 #include "nsSandboxFlags.h"
33 #include "LoadInfo.h"
35 using namespace mozilla::dom;
37 namespace mozilla {
38 namespace net {
40 static uint64_t FindTopOuterWindowID(nsPIDOMWindowOuter* aOuter) {
41 nsCOMPtr<nsPIDOMWindowOuter> outer = aOuter;
42 while (nsCOMPtr<nsPIDOMWindowOuter> parent =
43 outer->GetScriptableParentOrNull()) {
44 outer = parent;
46 return outer->WindowID();
49 LoadInfo::LoadInfo(
50 nsIPrincipal* aLoadingPrincipal, nsIPrincipal* aTriggeringPrincipal,
51 nsINode* aLoadingContext, nsSecurityFlags aSecurityFlags,
52 nsContentPolicyType aContentPolicyType,
53 const Maybe<mozilla::dom::ClientInfo>& aLoadingClientInfo,
54 const Maybe<mozilla::dom::ServiceWorkerDescriptor>& aController)
55 : mLoadingPrincipal(aLoadingContext ? aLoadingContext->NodePrincipal()
56 : aLoadingPrincipal),
57 mTriggeringPrincipal(aTriggeringPrincipal ? aTriggeringPrincipal
58 : mLoadingPrincipal.get()),
59 mPrincipalToInherit(nullptr),
60 mClientInfo(aLoadingClientInfo),
61 mController(aController),
62 mLoadingContext(do_GetWeakReference(aLoadingContext)),
63 mContextForTopLevelLoad(nullptr),
64 mSecurityFlags(aSecurityFlags),
65 mInternalContentPolicyType(aContentPolicyType),
66 mTainting(LoadTainting::Basic),
67 mUpgradeInsecureRequests(false),
68 mBrowserUpgradeInsecureRequests(false),
69 mBrowserWouldUpgradeInsecureRequests(false),
70 mVerifySignedContent(false),
71 mEnforceSRI(false),
72 mForceAllowDataURI(false),
73 mAllowInsecureRedirectToDataURI(false),
74 mSkipContentPolicyCheckForWebRequest(false),
75 mOriginalFrameSrcLoad(false),
76 mForceInheritPrincipalDropped(false),
77 mInnerWindowID(0),
78 mOuterWindowID(0),
79 mParentOuterWindowID(0),
80 mTopOuterWindowID(0),
81 mFrameOuterWindowID(0),
82 mBrowsingContextID(0),
83 mEnforceSecurity(false),
84 mInitialSecurityCheckDone(false),
85 mIsThirdPartyContext(false),
86 mIsDocshellReload(false),
87 mSendCSPViolationEvents(true),
88 mForcePreflight(false),
89 mIsPreflight(false),
90 mLoadTriggeredFromExternal(false),
91 mServiceWorkerTaintingSynthesized(false),
92 mDocumentHasUserInteracted(false),
93 mDocumentHasLoaded(false),
94 mIsFromProcessingFrameAttributes(false) {
95 MOZ_ASSERT(mLoadingPrincipal);
96 MOZ_ASSERT(mTriggeringPrincipal);
98 #ifdef DEBUG
99 // TYPE_DOCUMENT loads initiated by javascript tests will go through
100 // nsIOService and use the wrong constructor. Don't enforce the
101 // !TYPE_DOCUMENT check in those cases
102 bool skipContentTypeCheck = false;
103 skipContentTypeCheck =
104 Preferences::GetBool("network.loadinfo.skip_type_assertion");
105 #endif
107 // This constructor shouldn't be used for TYPE_DOCUMENT loads that don't
108 // have a loadingPrincipal
109 MOZ_ASSERT(skipContentTypeCheck || mLoadingPrincipal ||
110 mInternalContentPolicyType != nsIContentPolicy::TYPE_DOCUMENT);
112 // We should only get an explicit controller for subresource requests.
113 MOZ_DIAGNOSTIC_ASSERT(aController.isNothing() ||
114 !nsContentUtils::IsNonSubresourceInternalPolicyType(
115 mInternalContentPolicyType));
117 // TODO(bug 1259873): Above, we initialize mIsThirdPartyContext to false
118 // meaning that consumers of LoadInfo that don't pass a context or pass a
119 // context from which we can't find a window will default to assuming that
120 // they're 1st party. It would be nice if we could default "safe" and assume
121 // that we are 3rd party until proven otherwise.
123 // if consumers pass both, aLoadingContext and aLoadingPrincipal
124 // then the loadingPrincipal must be the same as the node's principal
125 MOZ_ASSERT(!aLoadingContext || !aLoadingPrincipal ||
126 aLoadingContext->NodePrincipal() == aLoadingPrincipal);
128 // if the load is sandboxed, we can not also inherit the principal
129 if (mSecurityFlags & nsILoadInfo::SEC_SANDBOXED) {
130 mForceInheritPrincipalDropped =
131 (mSecurityFlags & nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL);
132 mSecurityFlags &= ~nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL;
135 uint32_t externalType =
136 nsContentUtils::InternalContentPolicyTypeToExternal(aContentPolicyType);
138 if (aLoadingContext) {
139 // Ensure that all network requests for a window client have the ClientInfo
140 // properly set. Workers must currently pass the loading ClientInfo
141 // explicitly. We allow main thread requests to explicitly pass the value as
142 // well.
143 if (mClientInfo.isNothing()) {
144 mClientInfo = aLoadingContext->OwnerDoc()->GetClientInfo();
147 // For subresource loads set the service worker based on the calling
148 // context's controller. Workers must currently pass the controller in
149 // explicitly. We allow main thread requests to explicitly pass the value
150 // as well, but otherwise extract from the loading context here.
151 if (mController.isNothing() &&
152 !nsContentUtils::IsNonSubresourceInternalPolicyType(
153 mInternalContentPolicyType)) {
154 mController = aLoadingContext->OwnerDoc()->GetController();
157 nsCOMPtr<nsPIDOMWindowOuter> contextOuter =
158 aLoadingContext->OwnerDoc()->GetWindow();
159 if (contextOuter) {
160 ComputeIsThirdPartyContext(contextOuter);
161 mOuterWindowID = contextOuter->WindowID();
162 nsCOMPtr<nsPIDOMWindowOuter> parent = contextOuter->GetScriptableParent();
163 mParentOuterWindowID = parent ? parent->WindowID() : mOuterWindowID;
164 mTopOuterWindowID = FindTopOuterWindowID(contextOuter);
165 RefPtr<dom::BrowsingContext> bc = contextOuter->GetBrowsingContext();
166 mBrowsingContextID = bc ? bc->Id() : 0;
168 nsGlobalWindowInner* innerWindow =
169 nsGlobalWindowInner::Cast(contextOuter->GetCurrentInnerWindow());
170 if (innerWindow) {
171 mTopLevelPrincipal = innerWindow->GetTopLevelPrincipal();
173 // The top-level-storage-area-principal is not null only for the first
174 // level of iframes (null for top-level contexts, and null for
175 // sub-iframes). If we are loading a sub-document resource, we must
176 // calculate what the top-level-storage-area-principal will be for the
177 // new context.
178 if (externalType != nsIContentPolicy::TYPE_SUBDOCUMENT) {
179 mTopLevelStorageAreaPrincipal =
180 innerWindow->GetTopLevelStorageAreaPrincipal();
181 } else if (contextOuter->IsTopLevelWindow()) {
182 Document* doc = innerWindow->GetExtantDoc();
183 if (!doc || (!doc->StorageAccessSandboxed() &&
184 !nsContentUtils::IsInPrivateBrowsing(doc))) {
185 mTopLevelStorageAreaPrincipal = innerWindow->GetPrincipal();
189 mDocumentHasLoaded = innerWindow->IsDocumentLoaded();
191 if (innerWindow->IsFrame()) {
192 // For resources within iframes, we actually want the
193 // top-level document's flag, not the iframe document's.
194 mDocumentHasLoaded = false;
195 nsGlobalWindowOuter* topOuter =
196 innerWindow->GetScriptableTopInternal();
197 if (topOuter) {
198 nsGlobalWindowInner* topInner =
199 nsGlobalWindowInner::Cast(topOuter->GetCurrentInnerWindow());
200 if (topInner) {
201 mDocumentHasLoaded = topInner->IsDocumentLoaded();
208 mInnerWindowID = aLoadingContext->OwnerDoc()->InnerWindowID();
209 mAncestorPrincipals = aLoadingContext->OwnerDoc()->AncestorPrincipals();
210 mAncestorOuterWindowIDs =
211 aLoadingContext->OwnerDoc()->AncestorOuterWindowIDs();
212 MOZ_DIAGNOSTIC_ASSERT(mAncestorPrincipals.Length() ==
213 mAncestorOuterWindowIDs.Length());
214 mDocumentHasUserInteracted =
215 aLoadingContext->OwnerDoc()->UserHasInteracted();
217 // When the element being loaded is a frame, we choose the frame's window
218 // for the window ID and the frame element's window as the parent
219 // window. This is the behavior that Chrome exposes to add-ons.
220 // NB: If the frameLoaderOwner doesn't have a frame loader, then the load
221 // must be coming from an object (such as a plugin) that's loaded into it
222 // instead of a document being loaded. In that case, treat this object like
223 // any other non-document-loading element.
224 nsCOMPtr<nsIFrameLoaderOwner> frameLoaderOwner =
225 do_QueryInterface(aLoadingContext);
226 RefPtr<nsFrameLoader> fl =
227 frameLoaderOwner ? frameLoaderOwner->GetFrameLoader() : nullptr;
228 if (fl) {
229 nsCOMPtr<nsIDocShell> docShell = fl->GetDocShell(IgnoreErrors());
230 if (docShell) {
231 nsCOMPtr<nsPIDOMWindowOuter> outerWindow = do_GetInterface(docShell);
232 if (outerWindow) {
233 mFrameOuterWindowID = outerWindow->WindowID();
238 // if the document forces all requests to be upgraded from http to https,
239 // then we should do that for all requests. If it only forces preloads to be
240 // upgraded then we should enforce upgrade insecure requests only for
241 // preloads.
242 mUpgradeInsecureRequests =
243 aLoadingContext->OwnerDoc()->GetUpgradeInsecureRequests(false) ||
244 (nsContentUtils::IsPreloadType(mInternalContentPolicyType) &&
245 aLoadingContext->OwnerDoc()->GetUpgradeInsecureRequests(true));
247 if (nsContentUtils::IsUpgradableDisplayType(externalType)) {
248 nsCOMPtr<nsIURI> uri;
249 mLoadingPrincipal->GetURI(getter_AddRefs(uri));
250 if (uri) {
251 // Checking https not secure context as http://localhost can't be
252 // upgraded
253 bool isHttpsScheme;
254 nsresult rv = uri->SchemeIs("https", &isHttpsScheme);
255 if (NS_SUCCEEDED(rv) && isHttpsScheme) {
256 if (nsMixedContentBlocker::ShouldUpgradeMixedDisplayContent()) {
257 mBrowserUpgradeInsecureRequests = true;
258 } else {
259 mBrowserWouldUpgradeInsecureRequests = true;
264 // if owner doc has content signature, we enforce SRI
265 nsCOMPtr<nsIChannel> channel = aLoadingContext->OwnerDoc()->GetChannel();
266 if (channel) {
267 nsCOMPtr<nsILoadInfo> loadInfo = channel->GetLoadInfo();
268 if (loadInfo) {
269 mEnforceSRI = loadInfo->GetVerifySignedContent();
274 // If CSP requires SRI (require-sri-for), then store that information
275 // in the loadInfo so we can enforce SRI before loading the subresource.
276 if (!mEnforceSRI) {
277 // do not look into the CSP if already true:
278 // a CSP saying that SRI isn't needed should not
279 // overrule GetVerifySignedContent
280 if (aLoadingPrincipal) {
281 nsCOMPtr<nsIContentSecurityPolicy> csp;
282 aLoadingPrincipal->GetCsp(getter_AddRefs(csp));
283 uint32_t externalType =
284 nsContentUtils::InternalContentPolicyTypeToExternal(
285 aContentPolicyType);
286 // csp could be null if loading principal is system principal
287 if (csp) {
288 csp->RequireSRIForType(externalType, &mEnforceSRI);
290 // if CSP is delivered via a meta tag, it's speculatively available
291 // as 'preloadCSP'. If we are preloading a script or style, we have
292 // to apply that speculative 'preloadCSP' for such loads.
293 if (!mEnforceSRI && nsContentUtils::IsPreloadType(aContentPolicyType)) {
294 nsCOMPtr<nsIContentSecurityPolicy> preloadCSP;
295 aLoadingPrincipal->GetPreloadCsp(getter_AddRefs(preloadCSP));
296 if (preloadCSP) {
297 preloadCSP->RequireSRIForType(externalType, &mEnforceSRI);
303 mOriginAttributes = mLoadingPrincipal->OriginAttributesRef();
305 // We need to do this after inheriting the document's origin attributes
306 // above, in case the loading principal ends up being the system principal.
307 if (aLoadingContext) {
308 nsCOMPtr<nsILoadContext> loadContext =
309 aLoadingContext->OwnerDoc()->GetLoadContext();
310 nsCOMPtr<nsIDocShell> docShell = aLoadingContext->OwnerDoc()->GetDocShell();
311 if (loadContext && docShell &&
312 docShell->ItemType() == nsIDocShellTreeItem::typeContent) {
313 bool usePrivateBrowsing;
314 nsresult rv = loadContext->GetUsePrivateBrowsing(&usePrivateBrowsing);
315 if (NS_SUCCEEDED(rv)) {
316 mOriginAttributes.SyncAttributesWithPrivateBrowsing(usePrivateBrowsing);
321 // For chrome docshell, the mPrivateBrowsingId remains 0 even its
322 // UsePrivateBrowsing() is true, so we only update the mPrivateBrowsingId in
323 // origin attributes if the type of the docshell is content.
324 if (aLoadingContext) {
325 nsCOMPtr<nsIDocShell> docShell = aLoadingContext->OwnerDoc()->GetDocShell();
326 if (docShell) {
327 if (docShell->ItemType() == nsIDocShellTreeItem::typeChrome) {
328 MOZ_ASSERT(mOriginAttributes.mPrivateBrowsingId == 0,
329 "chrome docshell shouldn't have mPrivateBrowsingId set.");
335 /* Constructor takes an outer window, but no loadingNode or loadingPrincipal.
336 * This constructor should only be used for TYPE_DOCUMENT loads, since they
337 * have a null loadingNode and loadingPrincipal.
339 LoadInfo::LoadInfo(nsPIDOMWindowOuter* aOuterWindow,
340 nsIPrincipal* aTriggeringPrincipal,
341 nsISupports* aContextForTopLevelLoad,
342 nsSecurityFlags aSecurityFlags)
343 : mLoadingPrincipal(nullptr),
344 mTriggeringPrincipal(aTriggeringPrincipal),
345 mPrincipalToInherit(nullptr),
346 mContextForTopLevelLoad(do_GetWeakReference(aContextForTopLevelLoad)),
347 mSecurityFlags(aSecurityFlags),
348 mInternalContentPolicyType(nsIContentPolicy::TYPE_DOCUMENT),
349 mTainting(LoadTainting::Basic),
350 mUpgradeInsecureRequests(false),
351 mBrowserUpgradeInsecureRequests(false),
352 mBrowserWouldUpgradeInsecureRequests(false),
353 mVerifySignedContent(false),
354 mEnforceSRI(false),
355 mForceAllowDataURI(false),
356 mAllowInsecureRedirectToDataURI(false),
357 mSkipContentPolicyCheckForWebRequest(false),
358 mOriginalFrameSrcLoad(false),
359 mForceInheritPrincipalDropped(false),
360 mInnerWindowID(0),
361 mOuterWindowID(0),
362 mParentOuterWindowID(0),
363 mTopOuterWindowID(0),
364 mFrameOuterWindowID(0),
365 mBrowsingContextID(0),
366 mEnforceSecurity(false),
367 mInitialSecurityCheckDone(false),
368 mIsThirdPartyContext(false), // NB: TYPE_DOCUMENT implies !third-party.
369 mIsDocshellReload(false),
370 mSendCSPViolationEvents(true),
371 mForcePreflight(false),
372 mIsPreflight(false),
373 mLoadTriggeredFromExternal(false),
374 mServiceWorkerTaintingSynthesized(false),
375 mDocumentHasUserInteracted(false),
376 mDocumentHasLoaded(false),
377 mIsFromProcessingFrameAttributes(false) {
378 // Top-level loads are never third-party
379 // Grab the information we can out of the window.
380 MOZ_ASSERT(aOuterWindow);
381 MOZ_ASSERT(mTriggeringPrincipal);
383 // if the load is sandboxed, we can not also inherit the principal
384 if (mSecurityFlags & nsILoadInfo::SEC_SANDBOXED) {
385 mForceInheritPrincipalDropped =
386 (mSecurityFlags & nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL);
387 mSecurityFlags &= ~nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL;
390 // NB: Ignore the current inner window since we're navigating away from it.
391 mOuterWindowID = aOuterWindow->WindowID();
392 RefPtr<BrowsingContext> bc = aOuterWindow->GetBrowsingContext();
393 mBrowsingContextID = bc ? bc->Id() : 0;
395 // TODO We can have a parent without a frame element in some cases dealing
396 // with the hidden window.
397 nsCOMPtr<nsPIDOMWindowOuter> parent = aOuterWindow->GetScriptableParent();
398 mParentOuterWindowID = parent ? parent->WindowID() : 0;
399 mTopOuterWindowID = FindTopOuterWindowID(aOuterWindow);
401 nsGlobalWindowInner* innerWindow =
402 nsGlobalWindowInner::Cast(aOuterWindow->GetCurrentInnerWindow());
403 if (innerWindow) {
404 mTopLevelPrincipal = innerWindow->GetTopLevelPrincipal();
405 // mTopLevelStorageAreaPrincipal is always null for top-level document
406 // loading.
409 // get the docshell from the outerwindow, and then get the originattributes
410 nsCOMPtr<nsIDocShell> docShell = aOuterWindow->GetDocShell();
411 MOZ_ASSERT(docShell);
412 mOriginAttributes = nsDocShell::Cast(docShell)->GetOriginAttributes();
413 mAncestorPrincipals = nsDocShell::Cast(docShell)->AncestorPrincipals();
414 mAncestorOuterWindowIDs =
415 nsDocShell::Cast(docShell)->AncestorOuterWindowIDs();
416 MOZ_DIAGNOSTIC_ASSERT(mAncestorPrincipals.Length() ==
417 mAncestorOuterWindowIDs.Length());
419 #ifdef DEBUG
420 if (docShell->ItemType() == nsIDocShellTreeItem::typeChrome) {
421 MOZ_ASSERT(mOriginAttributes.mPrivateBrowsingId == 0,
422 "chrome docshell shouldn't have mPrivateBrowsingId set.");
424 #endif
427 LoadInfo::LoadInfo(const LoadInfo& rhs)
428 : mLoadingPrincipal(rhs.mLoadingPrincipal),
429 mTriggeringPrincipal(rhs.mTriggeringPrincipal),
430 mPrincipalToInherit(rhs.mPrincipalToInherit),
431 mSandboxedLoadingPrincipal(rhs.mSandboxedLoadingPrincipal),
432 mTopLevelPrincipal(rhs.mTopLevelPrincipal),
433 mTopLevelStorageAreaPrincipal(rhs.mTopLevelStorageAreaPrincipal),
434 mResultPrincipalURI(rhs.mResultPrincipalURI),
435 mClientInfo(rhs.mClientInfo),
436 // mReservedClientSource must be handled specially during redirect
437 // mReservedClientInfo must be handled specially during redirect
438 // mInitialClientInfo must be handled specially during redirect
439 mController(rhs.mController),
440 mPerformanceStorage(rhs.mPerformanceStorage),
441 mLoadingContext(rhs.mLoadingContext),
442 mContextForTopLevelLoad(rhs.mContextForTopLevelLoad),
443 mSecurityFlags(rhs.mSecurityFlags),
444 mInternalContentPolicyType(rhs.mInternalContentPolicyType),
445 mTainting(rhs.mTainting),
446 mUpgradeInsecureRequests(rhs.mUpgradeInsecureRequests),
447 mBrowserUpgradeInsecureRequests(rhs.mBrowserUpgradeInsecureRequests),
448 mBrowserWouldUpgradeInsecureRequests(
449 rhs.mBrowserWouldUpgradeInsecureRequests),
450 mVerifySignedContent(rhs.mVerifySignedContent),
451 mEnforceSRI(rhs.mEnforceSRI),
452 mForceAllowDataURI(rhs.mForceAllowDataURI),
453 mAllowInsecureRedirectToDataURI(rhs.mAllowInsecureRedirectToDataURI),
454 mSkipContentPolicyCheckForWebRequest(
455 rhs.mSkipContentPolicyCheckForWebRequest),
456 mOriginalFrameSrcLoad(rhs.mOriginalFrameSrcLoad),
457 mForceInheritPrincipalDropped(rhs.mForceInheritPrincipalDropped),
458 mInnerWindowID(rhs.mInnerWindowID),
459 mOuterWindowID(rhs.mOuterWindowID),
460 mParentOuterWindowID(rhs.mParentOuterWindowID),
461 mTopOuterWindowID(rhs.mTopOuterWindowID),
462 mFrameOuterWindowID(rhs.mFrameOuterWindowID),
463 mBrowsingContextID(rhs.mBrowsingContextID),
464 mEnforceSecurity(rhs.mEnforceSecurity),
465 mInitialSecurityCheckDone(rhs.mInitialSecurityCheckDone),
466 mIsThirdPartyContext(rhs.mIsThirdPartyContext),
467 mIsDocshellReload(rhs.mIsDocshellReload),
468 mSendCSPViolationEvents(rhs.mSendCSPViolationEvents),
469 mOriginAttributes(rhs.mOriginAttributes),
470 mRedirectChainIncludingInternalRedirects(
471 rhs.mRedirectChainIncludingInternalRedirects),
472 mRedirectChain(rhs.mRedirectChain),
473 mAncestorPrincipals(rhs.mAncestorPrincipals),
474 mAncestorOuterWindowIDs(rhs.mAncestorOuterWindowIDs),
475 mCorsUnsafeHeaders(rhs.mCorsUnsafeHeaders),
476 mForcePreflight(rhs.mForcePreflight),
477 mIsPreflight(rhs.mIsPreflight),
478 mLoadTriggeredFromExternal(rhs.mLoadTriggeredFromExternal),
479 // mServiceWorkerTaintingSynthesized must be handled specially during
480 // redirect
481 mServiceWorkerTaintingSynthesized(false),
482 mDocumentHasUserInteracted(rhs.mDocumentHasUserInteracted),
483 mDocumentHasLoaded(rhs.mDocumentHasLoaded),
484 mIsFromProcessingFrameAttributes(rhs.mIsFromProcessingFrameAttributes) {}
486 LoadInfo::LoadInfo(
487 nsIPrincipal* aLoadingPrincipal, nsIPrincipal* aTriggeringPrincipal,
488 nsIPrincipal* aPrincipalToInherit, nsIPrincipal* aSandboxedLoadingPrincipal,
489 nsIPrincipal* aTopLevelPrincipal,
490 nsIPrincipal* aTopLevelStorageAreaPrincipal, nsIURI* aResultPrincipalURI,
491 const Maybe<ClientInfo>& aClientInfo,
492 const Maybe<ClientInfo>& aReservedClientInfo,
493 const Maybe<ClientInfo>& aInitialClientInfo,
494 const Maybe<ServiceWorkerDescriptor>& aController,
495 nsSecurityFlags aSecurityFlags, nsContentPolicyType aContentPolicyType,
496 LoadTainting aTainting, bool aUpgradeInsecureRequests,
497 bool aBrowserUpgradeInsecureRequests,
498 bool aBrowserWouldUpgradeInsecureRequests, bool aVerifySignedContent,
499 bool aEnforceSRI, bool aForceAllowDataURI,
500 bool aAllowInsecureRedirectToDataURI,
501 bool aSkipContentPolicyCheckForWebRequest,
502 bool aForceInheritPrincipalDropped, uint64_t aInnerWindowID,
503 uint64_t aOuterWindowID, uint64_t aParentOuterWindowID,
504 uint64_t aTopOuterWindowID, uint64_t aFrameOuterWindowID,
505 uint64_t aBrowsingContextID, bool aEnforceSecurity,
506 bool aInitialSecurityCheckDone, bool aIsThirdPartyContext,
507 bool aIsDocshellReload, bool aSendCSPViolationEvents,
508 const OriginAttributes& aOriginAttributes,
509 RedirectHistoryArray& aRedirectChainIncludingInternalRedirects,
510 RedirectHistoryArray& aRedirectChain,
511 nsTArray<nsCOMPtr<nsIPrincipal>>&& aAncestorPrincipals,
512 const nsTArray<uint64_t>& aAncestorOuterWindowIDs,
513 const nsTArray<nsCString>& aCorsUnsafeHeaders, bool aForcePreflight,
514 bool aIsPreflight, bool aLoadTriggeredFromExternal,
515 bool aServiceWorkerTaintingSynthesized, bool aDocumentHasUserInteracted,
516 bool aDocumentHasLoaded)
517 : mLoadingPrincipal(aLoadingPrincipal),
518 mTriggeringPrincipal(aTriggeringPrincipal),
519 mPrincipalToInherit(aPrincipalToInherit),
520 mTopLevelPrincipal(aTopLevelPrincipal),
521 mTopLevelStorageAreaPrincipal(aTopLevelStorageAreaPrincipal),
522 mResultPrincipalURI(aResultPrincipalURI),
523 mClientInfo(aClientInfo),
524 mReservedClientInfo(aReservedClientInfo),
525 mInitialClientInfo(aInitialClientInfo),
526 mController(aController),
527 mSecurityFlags(aSecurityFlags),
528 mInternalContentPolicyType(aContentPolicyType),
529 mTainting(aTainting),
530 mUpgradeInsecureRequests(aUpgradeInsecureRequests),
531 mBrowserUpgradeInsecureRequests(aBrowserUpgradeInsecureRequests),
532 mBrowserWouldUpgradeInsecureRequests(
533 aBrowserWouldUpgradeInsecureRequests),
534 mVerifySignedContent(aVerifySignedContent),
535 mEnforceSRI(aEnforceSRI),
536 mForceAllowDataURI(aForceAllowDataURI),
537 mAllowInsecureRedirectToDataURI(aAllowInsecureRedirectToDataURI),
538 mSkipContentPolicyCheckForWebRequest(
539 aSkipContentPolicyCheckForWebRequest),
540 mOriginalFrameSrcLoad(false),
541 mForceInheritPrincipalDropped(aForceInheritPrincipalDropped),
542 mInnerWindowID(aInnerWindowID),
543 mOuterWindowID(aOuterWindowID),
544 mParentOuterWindowID(aParentOuterWindowID),
545 mTopOuterWindowID(aTopOuterWindowID),
546 mFrameOuterWindowID(aFrameOuterWindowID),
547 mBrowsingContextID(aBrowsingContextID),
548 mEnforceSecurity(aEnforceSecurity),
549 mInitialSecurityCheckDone(aInitialSecurityCheckDone),
550 mIsThirdPartyContext(aIsThirdPartyContext),
551 mIsDocshellReload(aIsDocshellReload),
552 mSendCSPViolationEvents(aSendCSPViolationEvents),
553 mOriginAttributes(aOriginAttributes),
554 mAncestorPrincipals(std::move(aAncestorPrincipals)),
555 mAncestorOuterWindowIDs(aAncestorOuterWindowIDs),
556 mCorsUnsafeHeaders(aCorsUnsafeHeaders),
557 mForcePreflight(aForcePreflight),
558 mIsPreflight(aIsPreflight),
559 mLoadTriggeredFromExternal(aLoadTriggeredFromExternal),
560 mServiceWorkerTaintingSynthesized(aServiceWorkerTaintingSynthesized),
561 mDocumentHasUserInteracted(aDocumentHasUserInteracted),
562 mDocumentHasLoaded(aDocumentHasLoaded),
563 mIsFromProcessingFrameAttributes(false) {
564 // Only top level TYPE_DOCUMENT loads can have a null loadingPrincipal
565 MOZ_ASSERT(mLoadingPrincipal ||
566 aContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT);
567 MOZ_ASSERT(mTriggeringPrincipal);
569 mRedirectChainIncludingInternalRedirects.SwapElements(
570 aRedirectChainIncludingInternalRedirects);
572 mRedirectChain.SwapElements(aRedirectChain);
575 void LoadInfo::ComputeIsThirdPartyContext(nsPIDOMWindowOuter* aOuterWindow) {
576 nsContentPolicyType type =
577 nsContentUtils::InternalContentPolicyTypeToExternal(
578 mInternalContentPolicyType);
579 if (type == nsIContentPolicy::TYPE_DOCUMENT) {
580 // Top-level loads are never third-party.
581 mIsThirdPartyContext = false;
582 return;
585 nsCOMPtr<mozIThirdPartyUtil> util(do_GetService(THIRDPARTYUTIL_CONTRACTID));
586 if (NS_WARN_IF(!util)) {
587 return;
590 util->IsThirdPartyWindow(aOuterWindow, nullptr, &mIsThirdPartyContext);
593 NS_IMPL_ISUPPORTS(LoadInfo, nsILoadInfo)
595 already_AddRefed<nsILoadInfo> LoadInfo::Clone() const {
596 RefPtr<LoadInfo> copy(new LoadInfo(*this));
597 return copy.forget();
600 already_AddRefed<nsILoadInfo> LoadInfo::CloneWithNewSecFlags(
601 nsSecurityFlags aSecurityFlags) const {
602 RefPtr<LoadInfo> copy(new LoadInfo(*this));
603 copy->mSecurityFlags = aSecurityFlags;
604 return copy.forget();
607 already_AddRefed<nsILoadInfo> LoadInfo::CloneForNewRequest() const {
608 RefPtr<LoadInfo> copy(new LoadInfo(*this));
609 copy->mEnforceSecurity = false;
610 copy->mInitialSecurityCheckDone = false;
611 copy->mRedirectChainIncludingInternalRedirects.Clear();
612 copy->mRedirectChain.Clear();
613 copy->mResultPrincipalURI = nullptr;
614 return copy.forget();
617 NS_IMETHODIMP
618 LoadInfo::GetLoadingPrincipal(nsIPrincipal** aLoadingPrincipal) {
619 NS_IF_ADDREF(*aLoadingPrincipal = mLoadingPrincipal);
620 return NS_OK;
623 nsIPrincipal* LoadInfo::LoadingPrincipal() { return mLoadingPrincipal; }
625 NS_IMETHODIMP
626 LoadInfo::GetTriggeringPrincipal(nsIPrincipal** aTriggeringPrincipal) {
627 NS_ADDREF(*aTriggeringPrincipal = mTriggeringPrincipal);
628 return NS_OK;
631 nsIPrincipal* LoadInfo::TriggeringPrincipal() { return mTriggeringPrincipal; }
633 NS_IMETHODIMP
634 LoadInfo::GetPrincipalToInherit(nsIPrincipal** aPrincipalToInherit) {
635 NS_IF_ADDREF(*aPrincipalToInherit = mPrincipalToInherit);
636 return NS_OK;
639 NS_IMETHODIMP
640 LoadInfo::SetPrincipalToInherit(nsIPrincipal* aPrincipalToInherit) {
641 MOZ_ASSERT(aPrincipalToInherit, "must be a valid principal to inherit");
642 mPrincipalToInherit = aPrincipalToInherit;
643 return NS_OK;
646 nsIPrincipal* LoadInfo::PrincipalToInherit() { return mPrincipalToInherit; }
648 nsIPrincipal* LoadInfo::FindPrincipalToInherit(nsIChannel* aChannel) {
649 if (mPrincipalToInherit) {
650 return mPrincipalToInherit;
653 nsCOMPtr<nsIURI> uri = mResultPrincipalURI;
654 if (!uri) {
655 Unused << aChannel->GetOriginalURI(getter_AddRefs(uri));
658 auto prin = BasePrincipal::Cast(mTriggeringPrincipal);
659 return prin->PrincipalToInherit(uri);
662 nsIPrincipal* LoadInfo::GetSandboxedLoadingPrincipal() {
663 if (!(mSecurityFlags & nsILoadInfo::SEC_SANDBOXED)) {
664 return nullptr;
667 if (!mSandboxedLoadingPrincipal) {
668 if (mLoadingPrincipal) {
669 mSandboxedLoadingPrincipal =
670 NullPrincipal::CreateWithInheritedAttributes(mLoadingPrincipal);
671 } else {
672 OriginAttributes attrs(mOriginAttributes);
673 mSandboxedLoadingPrincipal = NullPrincipal::Create(attrs);
676 MOZ_ASSERT(mSandboxedLoadingPrincipal);
678 return mSandboxedLoadingPrincipal;
681 nsIPrincipal* LoadInfo::GetTopLevelPrincipal() { return mTopLevelPrincipal; }
683 nsIPrincipal* LoadInfo::GetTopLevelStorageAreaPrincipal() {
684 return mTopLevelStorageAreaPrincipal;
687 NS_IMETHODIMP
688 LoadInfo::GetLoadingDocument(Document** aResult) {
689 if (nsCOMPtr<nsINode> node = do_QueryReferent(mLoadingContext)) {
690 RefPtr<Document> context = node->OwnerDoc();
691 context.forget(aResult);
693 return NS_OK;
696 nsINode* LoadInfo::LoadingNode() {
697 nsCOMPtr<nsINode> node = do_QueryReferent(mLoadingContext);
698 return node;
701 already_AddRefed<nsISupports> LoadInfo::ContextForTopLevelLoad() {
702 // Most likely you want to query LoadingNode() instead of
703 // ContextForTopLevelLoad() if this assertion fires.
704 MOZ_ASSERT(mInternalContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT,
705 "should only query this context for top level document loads");
706 nsCOMPtr<nsISupports> context = do_QueryReferent(mContextForTopLevelLoad);
707 return context.forget();
710 already_AddRefed<nsISupports> LoadInfo::GetLoadingContext() {
711 nsCOMPtr<nsISupports> context;
712 if (mInternalContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT) {
713 context = ContextForTopLevelLoad();
714 } else {
715 context = LoadingNode();
717 return context.forget();
720 NS_IMETHODIMP
721 LoadInfo::GetLoadingContextXPCOM(nsISupports** aResult) {
722 nsCOMPtr<nsISupports> context = GetLoadingContext();
723 context.forget(aResult);
724 return NS_OK;
727 NS_IMETHODIMP
728 LoadInfo::GetSecurityFlags(nsSecurityFlags* aResult) {
729 *aResult = mSecurityFlags;
730 return NS_OK;
733 NS_IMETHODIMP
734 LoadInfo::GetSecurityMode(uint32_t* aFlags) {
735 *aFlags =
736 (mSecurityFlags & (nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_INHERITS |
737 nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED |
738 nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS |
739 nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL |
740 nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS));
741 return NS_OK;
744 NS_IMETHODIMP
745 LoadInfo::GetIsInThirdPartyContext(bool* aIsInThirdPartyContext) {
746 *aIsInThirdPartyContext = mIsThirdPartyContext;
747 return NS_OK;
750 static const uint32_t sCookiePolicyMask =
751 nsILoadInfo::SEC_COOKIES_DEFAULT | nsILoadInfo::SEC_COOKIES_INCLUDE |
752 nsILoadInfo::SEC_COOKIES_SAME_ORIGIN | nsILoadInfo::SEC_COOKIES_OMIT;
754 NS_IMETHODIMP
755 LoadInfo::GetCookiePolicy(uint32_t* aResult) {
756 uint32_t policy = mSecurityFlags & sCookiePolicyMask;
757 if (policy == nsILoadInfo::SEC_COOKIES_DEFAULT) {
758 policy = (mSecurityFlags & SEC_REQUIRE_CORS_DATA_INHERITS)
759 ? nsILoadInfo::SEC_COOKIES_SAME_ORIGIN
760 : nsILoadInfo::SEC_COOKIES_INCLUDE;
763 *aResult = policy;
764 return NS_OK;
767 void LoadInfo::SetIncludeCookiesSecFlag() {
768 MOZ_ASSERT(!mEnforceSecurity, "Request should not have been opened yet");
769 MOZ_ASSERT((mSecurityFlags & sCookiePolicyMask) ==
770 nsILoadInfo::SEC_COOKIES_DEFAULT);
771 mSecurityFlags =
772 (mSecurityFlags & ~sCookiePolicyMask) | nsILoadInfo::SEC_COOKIES_INCLUDE;
775 NS_IMETHODIMP
776 LoadInfo::GetForceInheritPrincipal(bool* aInheritPrincipal) {
777 *aInheritPrincipal =
778 (mSecurityFlags & nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL);
779 return NS_OK;
782 NS_IMETHODIMP
783 LoadInfo::GetForceInheritPrincipalOverruleOwner(bool* aInheritPrincipal) {
784 *aInheritPrincipal =
785 (mSecurityFlags &
786 nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL_OVERRULE_OWNER);
787 return NS_OK;
790 NS_IMETHODIMP
791 LoadInfo::GetLoadingSandboxed(bool* aLoadingSandboxed) {
792 *aLoadingSandboxed = (mSecurityFlags & nsILoadInfo::SEC_SANDBOXED);
793 return NS_OK;
796 NS_IMETHODIMP
797 LoadInfo::GetAboutBlankInherits(bool* aResult) {
798 *aResult = (mSecurityFlags & nsILoadInfo::SEC_ABOUT_BLANK_INHERITS);
799 return NS_OK;
802 NS_IMETHODIMP
803 LoadInfo::GetAllowChrome(bool* aResult) {
804 *aResult = (mSecurityFlags & nsILoadInfo::SEC_ALLOW_CHROME);
805 return NS_OK;
808 NS_IMETHODIMP
809 LoadInfo::GetDisallowScript(bool* aResult) {
810 *aResult = (mSecurityFlags & nsILoadInfo::SEC_DISALLOW_SCRIPT);
811 return NS_OK;
814 NS_IMETHODIMP
815 LoadInfo::GetDontFollowRedirects(bool* aResult) {
816 *aResult = (mSecurityFlags & nsILoadInfo::SEC_DONT_FOLLOW_REDIRECTS);
817 return NS_OK;
820 NS_IMETHODIMP
821 LoadInfo::GetLoadErrorPage(bool* aResult) {
822 *aResult = (mSecurityFlags & nsILoadInfo::SEC_LOAD_ERROR_PAGE);
823 return NS_OK;
826 NS_IMETHODIMP
827 LoadInfo::GetIsDocshellReload(bool* aResult) {
828 *aResult = mIsDocshellReload;
829 return NS_OK;
832 NS_IMETHODIMP
833 LoadInfo::SetIsDocshellReload(bool aValue) {
834 mIsDocshellReload = aValue;
835 return NS_OK;
838 NS_IMETHODIMP
839 LoadInfo::GetSendCSPViolationEvents(bool* aResult) {
840 *aResult = mSendCSPViolationEvents;
841 return NS_OK;
844 NS_IMETHODIMP
845 LoadInfo::SetSendCSPViolationEvents(bool aValue) {
846 mSendCSPViolationEvents = aValue;
847 return NS_OK;
850 NS_IMETHODIMP
851 LoadInfo::GetExternalContentPolicyType(nsContentPolicyType* aResult) {
852 *aResult = nsContentUtils::InternalContentPolicyTypeToExternal(
853 mInternalContentPolicyType);
854 return NS_OK;
857 nsContentPolicyType LoadInfo::InternalContentPolicyType() {
858 return mInternalContentPolicyType;
861 NS_IMETHODIMP
862 LoadInfo::GetUpgradeInsecureRequests(bool* aResult) {
863 *aResult = mUpgradeInsecureRequests;
864 return NS_OK;
867 NS_IMETHODIMP
868 LoadInfo::GetBrowserUpgradeInsecureRequests(bool* aResult) {
869 *aResult = mBrowserUpgradeInsecureRequests;
870 return NS_OK;
873 NS_IMETHODIMP
874 LoadInfo::GetBrowserWouldUpgradeInsecureRequests(bool* aResult) {
875 *aResult = mBrowserWouldUpgradeInsecureRequests;
876 return NS_OK;
879 NS_IMETHODIMP
880 LoadInfo::SetVerifySignedContent(bool aVerifySignedContent) {
881 MOZ_ASSERT(mInternalContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT,
882 "can only verify content for TYPE_DOCUMENT");
883 mVerifySignedContent = aVerifySignedContent;
884 return NS_OK;
887 NS_IMETHODIMP
888 LoadInfo::GetVerifySignedContent(bool* aResult) {
889 *aResult = mVerifySignedContent;
890 return NS_OK;
893 NS_IMETHODIMP
894 LoadInfo::SetEnforceSRI(bool aEnforceSRI) {
895 mEnforceSRI = aEnforceSRI;
896 return NS_OK;
899 NS_IMETHODIMP
900 LoadInfo::GetEnforceSRI(bool* aResult) {
901 *aResult = mEnforceSRI;
902 return NS_OK;
905 NS_IMETHODIMP
906 LoadInfo::SetForceAllowDataURI(bool aForceAllowDataURI) {
907 MOZ_ASSERT(!mForceAllowDataURI ||
908 mInternalContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT,
909 "can only allow data URI navigation for TYPE_DOCUMENT");
910 mForceAllowDataURI = aForceAllowDataURI;
911 return NS_OK;
914 NS_IMETHODIMP
915 LoadInfo::GetForceAllowDataURI(bool* aForceAllowDataURI) {
916 *aForceAllowDataURI = mForceAllowDataURI;
917 return NS_OK;
920 NS_IMETHODIMP
921 LoadInfo::SetAllowInsecureRedirectToDataURI(
922 bool aAllowInsecureRedirectToDataURI) {
923 mAllowInsecureRedirectToDataURI = aAllowInsecureRedirectToDataURI;
924 return NS_OK;
927 NS_IMETHODIMP
928 LoadInfo::GetAllowInsecureRedirectToDataURI(
929 bool* aAllowInsecureRedirectToDataURI) {
930 *aAllowInsecureRedirectToDataURI = mAllowInsecureRedirectToDataURI;
931 return NS_OK;
934 NS_IMETHODIMP
935 LoadInfo::SetSkipContentPolicyCheckForWebRequest(bool aSkip) {
936 mSkipContentPolicyCheckForWebRequest = aSkip;
937 return NS_OK;
940 NS_IMETHODIMP
941 LoadInfo::GetSkipContentPolicyCheckForWebRequest(bool* aSkip) {
942 *aSkip = mSkipContentPolicyCheckForWebRequest;
943 return NS_OK;
946 NS_IMETHODIMP
947 LoadInfo::SetOriginalFrameSrcLoad(bool aOriginalFrameSrcLoad) {
948 mOriginalFrameSrcLoad = aOriginalFrameSrcLoad;
949 return NS_OK;
952 NS_IMETHODIMP
953 LoadInfo::GetOriginalFrameSrcLoad(bool* aOriginalFrameSrcLoad) {
954 *aOriginalFrameSrcLoad = mOriginalFrameSrcLoad;
955 return NS_OK;
958 NS_IMETHODIMP
959 LoadInfo::GetForceInheritPrincipalDropped(bool* aResult) {
960 *aResult = mForceInheritPrincipalDropped;
961 return NS_OK;
964 NS_IMETHODIMP
965 LoadInfo::GetInnerWindowID(uint64_t* aResult) {
966 *aResult = mInnerWindowID;
967 return NS_OK;
970 NS_IMETHODIMP
971 LoadInfo::GetOuterWindowID(uint64_t* aResult) {
972 *aResult = mOuterWindowID;
973 return NS_OK;
976 NS_IMETHODIMP
977 LoadInfo::GetParentOuterWindowID(uint64_t* aResult) {
978 *aResult = mParentOuterWindowID;
979 return NS_OK;
982 NS_IMETHODIMP
983 LoadInfo::GetTopOuterWindowID(uint64_t* aResult) {
984 *aResult = mTopOuterWindowID;
985 return NS_OK;
988 NS_IMETHODIMP
989 LoadInfo::GetFrameOuterWindowID(uint64_t* aResult) {
990 *aResult = mFrameOuterWindowID;
991 return NS_OK;
994 NS_IMETHODIMP
995 LoadInfo::GetBrowsingContextID(uint64_t* aResult) {
996 *aResult = mBrowsingContextID;
997 return NS_OK;
1000 NS_IMETHODIMP
1001 LoadInfo::GetBrowsingContext(dom::BrowsingContext** aResult) {
1002 *aResult = BrowsingContext::Get(mBrowsingContextID).take();
1003 return NS_OK;
1006 NS_IMETHODIMP
1007 LoadInfo::GetScriptableOriginAttributes(
1008 JSContext* aCx, JS::MutableHandle<JS::Value> aOriginAttributes) {
1009 if (NS_WARN_IF(!ToJSValue(aCx, mOriginAttributes, aOriginAttributes))) {
1010 return NS_ERROR_FAILURE;
1012 return NS_OK;
1015 NS_IMETHODIMP
1016 LoadInfo::ResetPrincipalToInheritToNullPrincipal() {
1017 // take the originAttributes from the LoadInfo and create
1018 // a new NullPrincipal using those origin attributes.
1019 nsCOMPtr<nsIPrincipal> newNullPrincipal =
1020 NullPrincipal::Create(mOriginAttributes);
1022 mPrincipalToInherit = newNullPrincipal;
1024 // setting SEC_FORCE_INHERIT_PRINCIPAL_OVERRULE_OWNER will overrule
1025 // any non null owner set on the channel and will return the principal
1026 // form the loadinfo instead.
1027 mSecurityFlags |= SEC_FORCE_INHERIT_PRINCIPAL_OVERRULE_OWNER;
1029 return NS_OK;
1032 NS_IMETHODIMP
1033 LoadInfo::SetScriptableOriginAttributes(
1034 JSContext* aCx, JS::Handle<JS::Value> aOriginAttributes) {
1035 OriginAttributes attrs;
1036 if (!aOriginAttributes.isObject() || !attrs.Init(aCx, aOriginAttributes)) {
1037 return NS_ERROR_INVALID_ARG;
1040 mOriginAttributes = attrs;
1041 return NS_OK;
1044 nsresult LoadInfo::GetOriginAttributes(
1045 mozilla::OriginAttributes* aOriginAttributes) {
1046 NS_ENSURE_ARG(aOriginAttributes);
1047 *aOriginAttributes = mOriginAttributes;
1048 return NS_OK;
1051 nsresult LoadInfo::SetOriginAttributes(
1052 const mozilla::OriginAttributes& aOriginAttributes) {
1053 mOriginAttributes = aOriginAttributes;
1054 return NS_OK;
1057 NS_IMETHODIMP
1058 LoadInfo::SetEnforceSecurity(bool aEnforceSecurity) {
1059 // Indicates whether the channel was openend using AsyncOpen2. Once set
1060 // to true, it must remain true throughout the lifetime of the channel.
1061 // Setting it to anything else than true will be discarded.
1062 MOZ_ASSERT(aEnforceSecurity, "aEnforceSecurity must be true");
1063 mEnforceSecurity = mEnforceSecurity || aEnforceSecurity;
1064 return NS_OK;
1067 NS_IMETHODIMP
1068 LoadInfo::GetEnforceSecurity(bool* aResult) {
1069 *aResult = mEnforceSecurity;
1070 return NS_OK;
1073 NS_IMETHODIMP
1074 LoadInfo::SetInitialSecurityCheckDone(bool aInitialSecurityCheckDone) {
1075 // Indicates whether the channel was ever evaluated by the
1076 // ContentSecurityManager. Once set to true, this flag must
1077 // remain true throughout the lifetime of the channel.
1078 // Setting it to anything else than true will be discarded.
1079 MOZ_ASSERT(aInitialSecurityCheckDone,
1080 "aInitialSecurityCheckDone must be true");
1081 mInitialSecurityCheckDone =
1082 mInitialSecurityCheckDone || aInitialSecurityCheckDone;
1083 return NS_OK;
1086 NS_IMETHODIMP
1087 LoadInfo::GetInitialSecurityCheckDone(bool* aResult) {
1088 *aResult = mInitialSecurityCheckDone;
1089 return NS_OK;
1092 NS_IMETHODIMP
1093 LoadInfo::AppendRedirectHistoryEntry(nsIRedirectHistoryEntry* aEntry,
1094 bool aIsInternalRedirect) {
1095 NS_ENSURE_ARG(aEntry);
1096 MOZ_ASSERT(NS_IsMainThread());
1098 mRedirectChainIncludingInternalRedirects.AppendElement(aEntry);
1099 if (!aIsInternalRedirect) {
1100 mRedirectChain.AppendElement(aEntry);
1102 return NS_OK;
1105 NS_IMETHODIMP
1106 LoadInfo::GetRedirects(JSContext* aCx, JS::MutableHandle<JS::Value> aRedirects,
1107 const RedirectHistoryArray& aArray) {
1108 JS::Rooted<JSObject*> redirects(aCx, JS_NewArrayObject(aCx, aArray.Length()));
1109 NS_ENSURE_TRUE(redirects, NS_ERROR_OUT_OF_MEMORY);
1111 JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
1112 NS_ENSURE_TRUE(global, NS_ERROR_UNEXPECTED);
1114 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1116 for (size_t idx = 0; idx < aArray.Length(); idx++) {
1117 JS::RootedObject jsobj(aCx);
1118 nsresult rv =
1119 xpc->WrapNative(aCx, global, aArray[idx],
1120 NS_GET_IID(nsIRedirectHistoryEntry), jsobj.address());
1121 NS_ENSURE_SUCCESS(rv, rv);
1122 NS_ENSURE_STATE(jsobj);
1124 bool rc = JS_DefineElement(aCx, redirects, idx, jsobj, JSPROP_ENUMERATE);
1125 NS_ENSURE_TRUE(rc, NS_ERROR_UNEXPECTED);
1128 aRedirects.setObject(*redirects);
1129 return NS_OK;
1132 NS_IMETHODIMP
1133 LoadInfo::GetRedirectChainIncludingInternalRedirects(
1134 JSContext* aCx, JS::MutableHandle<JS::Value> aChain) {
1135 return GetRedirects(aCx, aChain, mRedirectChainIncludingInternalRedirects);
1138 const RedirectHistoryArray&
1139 LoadInfo::RedirectChainIncludingInternalRedirects() {
1140 return mRedirectChainIncludingInternalRedirects;
1143 NS_IMETHODIMP
1144 LoadInfo::GetRedirectChain(JSContext* aCx,
1145 JS::MutableHandle<JS::Value> aChain) {
1146 return GetRedirects(aCx, aChain, mRedirectChain);
1149 const RedirectHistoryArray& LoadInfo::RedirectChain() { return mRedirectChain; }
1151 const nsTArray<nsCOMPtr<nsIPrincipal>>& LoadInfo::AncestorPrincipals() {
1152 return mAncestorPrincipals;
1155 const nsTArray<uint64_t>& LoadInfo::AncestorOuterWindowIDs() {
1156 return mAncestorOuterWindowIDs;
1159 void LoadInfo::SetCorsPreflightInfo(const nsTArray<nsCString>& aHeaders,
1160 bool aForcePreflight) {
1161 MOZ_ASSERT(GetSecurityMode() == nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS);
1162 MOZ_ASSERT(!mInitialSecurityCheckDone);
1163 mCorsUnsafeHeaders = aHeaders;
1164 mForcePreflight = aForcePreflight;
1167 const nsTArray<nsCString>& LoadInfo::CorsUnsafeHeaders() {
1168 return mCorsUnsafeHeaders;
1171 NS_IMETHODIMP
1172 LoadInfo::GetForcePreflight(bool* aForcePreflight) {
1173 *aForcePreflight = mForcePreflight;
1174 return NS_OK;
1177 void LoadInfo::SetIsPreflight() {
1178 MOZ_ASSERT(GetSecurityMode() == nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS);
1179 MOZ_ASSERT(!mInitialSecurityCheckDone);
1180 mIsPreflight = true;
1183 void LoadInfo::SetUpgradeInsecureRequests() { mUpgradeInsecureRequests = true; }
1185 void LoadInfo::SetBrowserUpgradeInsecureRequests() {
1186 mBrowserUpgradeInsecureRequests = true;
1189 void LoadInfo::SetBrowserWouldUpgradeInsecureRequests() {
1190 mBrowserWouldUpgradeInsecureRequests = true;
1193 NS_IMETHODIMP
1194 LoadInfo::GetIsPreflight(bool* aIsPreflight) {
1195 *aIsPreflight = mIsPreflight;
1196 return NS_OK;
1199 NS_IMETHODIMP
1200 LoadInfo::SetLoadTriggeredFromExternal(bool aLoadTriggeredFromExternal) {
1201 MOZ_ASSERT(!aLoadTriggeredFromExternal ||
1202 mInternalContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT,
1203 "can only set load triggered from external for TYPE_DOCUMENT");
1204 mLoadTriggeredFromExternal = aLoadTriggeredFromExternal;
1205 return NS_OK;
1208 NS_IMETHODIMP
1209 LoadInfo::GetLoadTriggeredFromExternal(bool* aLoadTriggeredFromExternal) {
1210 *aLoadTriggeredFromExternal = mLoadTriggeredFromExternal;
1211 return NS_OK;
1214 NS_IMETHODIMP
1215 LoadInfo::GetServiceWorkerTaintingSynthesized(
1216 bool* aServiceWorkerTaintingSynthesized) {
1217 MOZ_ASSERT(aServiceWorkerTaintingSynthesized);
1218 *aServiceWorkerTaintingSynthesized = mServiceWorkerTaintingSynthesized;
1219 return NS_OK;
1222 NS_IMETHODIMP
1223 LoadInfo::GetTainting(uint32_t* aTaintingOut) {
1224 MOZ_ASSERT(aTaintingOut);
1225 *aTaintingOut = static_cast<uint32_t>(mTainting);
1226 return NS_OK;
1229 NS_IMETHODIMP
1230 LoadInfo::MaybeIncreaseTainting(uint32_t aTainting) {
1231 NS_ENSURE_ARG(aTainting <= TAINTING_OPAQUE);
1233 // Skip if the tainting has been set by the service worker.
1234 if (mServiceWorkerTaintingSynthesized) {
1235 return NS_OK;
1238 LoadTainting tainting = static_cast<LoadTainting>(aTainting);
1239 if (tainting > mTainting) {
1240 mTainting = tainting;
1242 return NS_OK;
1245 void LoadInfo::SynthesizeServiceWorkerTainting(LoadTainting aTainting) {
1246 MOZ_DIAGNOSTIC_ASSERT(aTainting <= LoadTainting::Opaque);
1247 mTainting = aTainting;
1249 // Flag to prevent the tainting from being increased.
1250 mServiceWorkerTaintingSynthesized = true;
1253 NS_IMETHODIMP
1254 LoadInfo::GetDocumentHasUserInteracted(bool* aDocumentHasUserInteracted) {
1255 MOZ_ASSERT(aDocumentHasUserInteracted);
1256 *aDocumentHasUserInteracted = mDocumentHasUserInteracted;
1257 return NS_OK;
1260 NS_IMETHODIMP
1261 LoadInfo::SetDocumentHasUserInteracted(bool aDocumentHasUserInteracted) {
1262 mDocumentHasUserInteracted = aDocumentHasUserInteracted;
1263 return NS_OK;
1266 NS_IMETHODIMP
1267 LoadInfo::GetDocumentHasLoaded(bool* aDocumentHasLoaded) {
1268 MOZ_ASSERT(aDocumentHasLoaded);
1269 *aDocumentHasLoaded = mDocumentHasLoaded;
1270 return NS_OK;
1273 NS_IMETHODIMP
1274 LoadInfo::SetDocumentHasLoaded(bool aDocumentHasLoaded) {
1275 mDocumentHasLoaded = aDocumentHasLoaded;
1276 return NS_OK;
1279 NS_IMETHODIMP
1280 LoadInfo::GetIsTopLevelLoad(bool* aResult) {
1281 *aResult = mFrameOuterWindowID ? mFrameOuterWindowID == mOuterWindowID
1282 : mParentOuterWindowID == mOuterWindowID;
1283 return NS_OK;
1286 void LoadInfo::SetIsFromProcessingFrameAttributes() {
1287 mIsFromProcessingFrameAttributes = true;
1290 NS_IMETHODIMP
1291 LoadInfo::GetIsFromProcessingFrameAttributes(
1292 bool* aIsFromProcessingFrameAttributes) {
1293 MOZ_ASSERT(aIsFromProcessingFrameAttributes);
1294 *aIsFromProcessingFrameAttributes = mIsFromProcessingFrameAttributes;
1295 return NS_OK;
1298 NS_IMETHODIMP
1299 LoadInfo::GetResultPrincipalURI(nsIURI** aURI) {
1300 NS_IF_ADDREF(*aURI = mResultPrincipalURI);
1301 return NS_OK;
1304 NS_IMETHODIMP
1305 LoadInfo::SetResultPrincipalURI(nsIURI* aURI) {
1306 mResultPrincipalURI = aURI;
1307 return NS_OK;
1310 void LoadInfo::SetClientInfo(const ClientInfo& aClientInfo) {
1311 mClientInfo.emplace(aClientInfo);
1314 const Maybe<ClientInfo>& LoadInfo::GetClientInfo() { return mClientInfo; }
1316 void LoadInfo::GiveReservedClientSource(
1317 UniquePtr<ClientSource>&& aClientSource) {
1318 MOZ_DIAGNOSTIC_ASSERT(aClientSource);
1319 mReservedClientSource = std::move(aClientSource);
1320 SetReservedClientInfo(mReservedClientSource->Info());
1323 UniquePtr<ClientSource> LoadInfo::TakeReservedClientSource() {
1324 if (mReservedClientSource) {
1325 // If the reserved ClientInfo was set due to a ClientSource being present,
1326 // then clear that info object when the ClientSource is taken.
1327 mReservedClientInfo.reset();
1329 return std::move(mReservedClientSource);
1332 void LoadInfo::SetReservedClientInfo(const ClientInfo& aClientInfo) {
1333 MOZ_DIAGNOSTIC_ASSERT(mInitialClientInfo.isNothing());
1334 // Treat assignments of the same value as a no-op. The emplace below
1335 // will normally assert when overwriting an existing value.
1336 if (mReservedClientInfo.isSome() &&
1337 mReservedClientInfo.ref() == aClientInfo) {
1338 return;
1340 mReservedClientInfo.emplace(aClientInfo);
1343 void LoadInfo::OverrideReservedClientInfoInParent(
1344 const ClientInfo& aClientInfo) {
1345 // This should only be called to handle redirects in the parent process.
1346 MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default);
1348 mInitialClientInfo.reset();
1349 mReservedClientInfo.reset();
1350 mReservedClientInfo.emplace(aClientInfo);
1353 const Maybe<ClientInfo>& LoadInfo::GetReservedClientInfo() {
1354 return mReservedClientInfo;
1357 void LoadInfo::SetInitialClientInfo(const ClientInfo& aClientInfo) {
1358 MOZ_DIAGNOSTIC_ASSERT(!mReservedClientSource);
1359 MOZ_DIAGNOSTIC_ASSERT(mReservedClientInfo.isNothing());
1360 // Treat assignments of the same value as a no-op. The emplace below
1361 // will normally assert when overwriting an existing value.
1362 if (mInitialClientInfo.isSome() && mInitialClientInfo.ref() == aClientInfo) {
1363 return;
1365 mInitialClientInfo.emplace(aClientInfo);
1368 const Maybe<ClientInfo>& LoadInfo::GetInitialClientInfo() {
1369 return mInitialClientInfo;
1372 void LoadInfo::SetController(const ServiceWorkerDescriptor& aServiceWorker) {
1373 mController.emplace(aServiceWorker);
1376 void LoadInfo::ClearController() { mController.reset(); }
1378 const Maybe<ServiceWorkerDescriptor>& LoadInfo::GetController() {
1379 return mController;
1382 void LoadInfo::SetPerformanceStorage(PerformanceStorage* aPerformanceStorage) {
1383 mPerformanceStorage = aPerformanceStorage;
1386 PerformanceStorage* LoadInfo::GetPerformanceStorage() {
1387 return mPerformanceStorage;
1390 NS_IMETHODIMP
1391 LoadInfo::GetCspEventListener(nsICSPEventListener** aCSPEventListener) {
1392 NS_IF_ADDREF(*aCSPEventListener = mCSPEventListener);
1393 return NS_OK;
1396 NS_IMETHODIMP
1397 LoadInfo::SetCspEventListener(nsICSPEventListener* aCSPEventListener) {
1398 mCSPEventListener = aCSPEventListener;
1399 return NS_OK;
1402 } // namespace net
1403 } // namespace mozilla