LoadInfo.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/LoadInfo.h"
  6. #include "mozilla/Assertions.h"
  7. #include "mozilla/dom/TabChild.h"
  8. #include "mozilla/dom/ToJSValue.h"
  9. #include "mozIThirdPartyUtil.h"
  10. #include "nsFrameLoader.h"
  11. #include "nsIContentSecurityPolicy.h"
  12. #include "nsIDocShell.h"
  13. #include "nsIDocument.h"
  14. #include "nsIDOMDocument.h"
  15. #include "nsIFrameLoader.h"
  16. #include "nsIInterfaceRequestorUtils.h"
  17. #include "nsISupportsImpl.h"
  18. #include "nsISupportsUtils.h"
  19. #include "nsContentUtils.h"
  20. #include "nsDocShell.h"
  21. #include "nsGlobalWindow.h"
  22. #include "nsNullPrincipal.h"
  23. using namespace mozilla::dom;
  24. namespace mozilla {
  25. namespace net {
  26. static void
  27. InheritOriginAttributes(nsIPrincipal* aLoadingPrincipal, NeckoOriginAttributes& aAttrs)
  28. {
  29. const PrincipalOriginAttributes attrs =
  30. BasePrincipal::Cast(aLoadingPrincipal)->OriginAttributesRef();
  31. aAttrs.InheritFromDocToNecko(attrs);
  32. }
  33. LoadInfo::LoadInfo(nsIPrincipal* aLoadingPrincipal,
  34. nsIPrincipal* aTriggeringPrincipal,
  35. nsINode* aLoadingContext,
  36. nsSecurityFlags aSecurityFlags,
  37. nsContentPolicyType aContentPolicyType)
  38. : mLoadingPrincipal(aLoadingContext ?
  39. aLoadingContext->NodePrincipal() : aLoadingPrincipal)
  40. , mTriggeringPrincipal(aTriggeringPrincipal ?
  41. aTriggeringPrincipal : mLoadingPrincipal.get())
  42. , mPrincipalToInherit(nullptr)
  43. , mLoadingContext(do_GetWeakReference(aLoadingContext))
  44. , mContextForTopLevelLoad(nullptr)
  45. , mSecurityFlags(aSecurityFlags)
  46. , mInternalContentPolicyType(aContentPolicyType)
  47. , mTainting(LoadTainting::Basic)
  48. , mUpgradeInsecureRequests(false)
  49. , mVerifySignedContent(false)
  50. , mEnforceSRI(false)
  51. , mForceAllowDataURI(false)
  52. , mForceInheritPrincipalDropped(false)
  53. , mInnerWindowID(0)
  54. , mOuterWindowID(0)
  55. , mParentOuterWindowID(0)
  56. , mFrameOuterWindowID(0)
  57. , mEnforceSecurity(false)
  58. , mInitialSecurityCheckDone(false)
  59. , mIsThirdPartyContext(false)
  60. , mForcePreflight(false)
  61. , mIsPreflight(false)
  62. , mLoadTriggeredFromExternal(false)
  63. , mIsFromProcessingFrameAttributes(false)
  64. {
  65. MOZ_ASSERT(mLoadingPrincipal);
  66. MOZ_ASSERT(mTriggeringPrincipal);
  67. #ifdef DEBUG
  68. // TYPE_DOCUMENT loads initiated by javascript tests will go through
  69. // nsIOService and use the wrong constructor. Don't enforce the
  70. // !TYPE_DOCUMENT check in those cases
  71. bool skipContentTypeCheck = false;
  72. skipContentTypeCheck = Preferences::GetBool("network.loadinfo.skip_type_assertion");
  73. #endif
  74. // This constructor shouldn't be used for TYPE_DOCUMENT loads that don't
  75. // have a loadingPrincipal
  76. MOZ_ASSERT(skipContentTypeCheck || mLoadingPrincipal ||
  77. mInternalContentPolicyType != nsIContentPolicy::TYPE_DOCUMENT);
  78. // TODO(bug 1259873): Above, we initialize mIsThirdPartyContext to false meaning
  79. // that consumers of LoadInfo that don't pass a context or pass a context from
  80. // which we can't find a window will default to assuming that they're 1st
  81. // party. It would be nice if we could default "safe" and assume that we are
  82. // 3rd party until proven otherwise.
  83. // if consumers pass both, aLoadingContext and aLoadingPrincipal
  84. // then the loadingPrincipal must be the same as the node's principal
  85. MOZ_ASSERT(!aLoadingContext || !aLoadingPrincipal ||
  86. aLoadingContext->NodePrincipal() == aLoadingPrincipal);
  87. // if the load is sandboxed, we can not also inherit the principal
  88. if (mSecurityFlags & nsILoadInfo::SEC_SANDBOXED) {
  89. mSecurityFlags ^= nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL;
  90. mForceInheritPrincipalDropped = true;
  91. }
  92. if (aLoadingContext) {
  93. nsCOMPtr<nsPIDOMWindowOuter> contextOuter = aLoadingContext->OwnerDoc()->GetWindow();
  94. if (contextOuter) {
  95. ComputeIsThirdPartyContext(contextOuter);
  96. mOuterWindowID = contextOuter->WindowID();
  97. nsCOMPtr<nsPIDOMWindowOuter> parent = contextOuter->GetScriptableParent();
  98. mParentOuterWindowID = parent ? parent->WindowID() : mOuterWindowID;
  99. }
  100. mInnerWindowID = aLoadingContext->OwnerDoc()->InnerWindowID();
  101. // When the element being loaded is a frame, we choose the frame's window
  102. // for the window ID and the frame element's window as the parent
  103. // window. This is the behavior that Chrome exposes to add-ons.
  104. // NB: If the frameLoaderOwner doesn't have a frame loader, then the load
  105. // must be coming from an object (such as a plugin) that's loaded into it
  106. // instead of a document being loaded. In that case, treat this object like
  107. // any other non-document-loading element.
  108. nsCOMPtr<nsIFrameLoaderOwner> frameLoaderOwner =
  109. do_QueryInterface(aLoadingContext);
  110. nsCOMPtr<nsIFrameLoader> fl = frameLoaderOwner ?
  111. frameLoaderOwner->GetFrameLoader() : nullptr;
  112. if (fl) {
  113. nsCOMPtr<nsIDocShell> docShell;
  114. if (NS_SUCCEEDED(fl->GetDocShell(getter_AddRefs(docShell))) && docShell) {
  115. nsCOMPtr<nsPIDOMWindowOuter> outerWindow = do_GetInterface(docShell);
  116. if (outerWindow) {
  117. mFrameOuterWindowID = outerWindow->WindowID();
  118. }
  119. }
  120. }
  121. // if the document forces all requests to be upgraded from http to https, then
  122. // we should do that for all requests. If it only forces preloads to be upgraded
  123. // then we should enforce upgrade insecure requests only for preloads.
  124. mUpgradeInsecureRequests =
  125. aLoadingContext->OwnerDoc()->GetUpgradeInsecureRequests(false) ||
  126. (nsContentUtils::IsPreloadType(mInternalContentPolicyType) &&
  127. aLoadingContext->OwnerDoc()->GetUpgradeInsecureRequests(true));
  128. // if owner doc has content signature, we enforce SRI
  129. nsCOMPtr<nsIChannel> channel = aLoadingContext->OwnerDoc()->GetChannel();
  130. if (channel) {
  131. nsCOMPtr<nsILoadInfo> loadInfo = channel->GetLoadInfo();
  132. if (loadInfo) {
  133. mEnforceSRI = loadInfo->GetVerifySignedContent();
  134. }
  135. }
  136. }
  137. // If CSP requires SRI (require-sri-for), then store that information
  138. // in the loadInfo so we can enforce SRI before loading the subresource.
  139. if (!mEnforceSRI) {
  140. // do not look into the CSP if already true:
  141. // a CSP saying that SRI isn't needed should not
  142. // overrule GetVerifySignedContent
  143. if (aLoadingPrincipal) {
  144. nsCOMPtr<nsIContentSecurityPolicy> csp;
  145. aLoadingPrincipal->GetCsp(getter_AddRefs(csp));
  146. uint32_t externalType =
  147. nsContentUtils::InternalContentPolicyTypeToExternal(aContentPolicyType);
  148. // csp could be null if loading principal is system principal
  149. if (csp) {
  150. csp->RequireSRIForType(externalType, &mEnforceSRI);
  151. }
  152. // if CSP is delivered via a meta tag, it's speculatively available
  153. // as 'preloadCSP'. If we are preloading a script or style, we have
  154. // to apply that speculative 'preloadCSP' for such loads.
  155. if (!mEnforceSRI && nsContentUtils::IsPreloadType(aContentPolicyType)) {
  156. nsCOMPtr<nsIContentSecurityPolicy> preloadCSP;
  157. aLoadingPrincipal->GetPreloadCsp(getter_AddRefs(preloadCSP));
  158. if (preloadCSP) {
  159. preloadCSP->RequireSRIForType(externalType, &mEnforceSRI);
  160. }
  161. }
  162. }
  163. }
  164. InheritOriginAttributes(mLoadingPrincipal, mOriginAttributes);
  165. // We need to do this after inheriting the document's origin attributes
  166. // above, in case the loading principal ends up being the system principal.
  167. if (aLoadingContext) {
  168. nsCOMPtr<nsILoadContext> loadContext =
  169. aLoadingContext->OwnerDoc()->GetLoadContext();
  170. nsCOMPtr<nsIDocShell> docShell = aLoadingContext->OwnerDoc()->GetDocShell();
  171. if (loadContext && docShell &&
  172. docShell->ItemType() == nsIDocShellTreeItem::typeContent) {
  173. bool usePrivateBrowsing;
  174. nsresult rv = loadContext->GetUsePrivateBrowsing(&usePrivateBrowsing);
  175. if (NS_SUCCEEDED(rv)) {
  176. mOriginAttributes.SyncAttributesWithPrivateBrowsing(usePrivateBrowsing);
  177. }
  178. }
  179. }
  180. // For chrome docshell, the mPrivateBrowsingId remains 0 even its
  181. // UsePrivateBrowsing() is true, so we only update the mPrivateBrowsingId in
  182. // origin attributes if the type of the docshell is content.
  183. if (aLoadingContext) {
  184. nsCOMPtr<nsIDocShell> docShell = aLoadingContext->OwnerDoc()->GetDocShell();
  185. if (docShell) {
  186. if (docShell->ItemType() == nsIDocShellTreeItem::typeChrome) {
  187. MOZ_ASSERT(mOriginAttributes.mPrivateBrowsingId == 0,
  188. "chrome docshell shouldn't have mPrivateBrowsingId set.");
  189. }
  190. }
  191. }
  192. }
  193. /* Constructor takes an outer window, but no loadingNode or loadingPrincipal.
  194. * This constructor should only be used for TYPE_DOCUMENT loads, since they
  195. * have a null loadingNode and loadingPrincipal.
  196. */
  197. LoadInfo::LoadInfo(nsPIDOMWindowOuter* aOuterWindow,
  198. nsIPrincipal* aTriggeringPrincipal,
  199. nsISupports* aContextForTopLevelLoad,
  200. nsSecurityFlags aSecurityFlags)
  201. : mLoadingPrincipal(nullptr)
  202. , mTriggeringPrincipal(aTriggeringPrincipal)
  203. , mPrincipalToInherit(nullptr)
  204. , mContextForTopLevelLoad(do_GetWeakReference(aContextForTopLevelLoad))
  205. , mSecurityFlags(aSecurityFlags)
  206. , mInternalContentPolicyType(nsIContentPolicy::TYPE_DOCUMENT)
  207. , mTainting(LoadTainting::Basic)
  208. , mUpgradeInsecureRequests(false)
  209. , mVerifySignedContent(false)
  210. , mEnforceSRI(false)
  211. , mForceAllowDataURI(false)
  212. , mForceInheritPrincipalDropped(false)
  213. , mInnerWindowID(0)
  214. , mOuterWindowID(0)
  215. , mParentOuterWindowID(0)
  216. , mFrameOuterWindowID(0)
  217. , mEnforceSecurity(false)
  218. , mInitialSecurityCheckDone(false)
  219. , mIsThirdPartyContext(false) // NB: TYPE_DOCUMENT implies not third-party.
  220. , mForcePreflight(false)
  221. , mIsPreflight(false)
  222. , mLoadTriggeredFromExternal(false)
  223. , mIsFromProcessingFrameAttributes(false)
  224. {
  225. // Top-level loads are never third-party
  226. // Grab the information we can out of the window.
  227. MOZ_ASSERT(aOuterWindow);
  228. MOZ_ASSERT(mTriggeringPrincipal);
  229. // if the load is sandboxed, we can not also inherit the principal
  230. if (mSecurityFlags & nsILoadInfo::SEC_SANDBOXED) {
  231. mSecurityFlags ^= nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL;
  232. mForceInheritPrincipalDropped = true;
  233. }
  234. // NB: Ignore the current inner window since we're navigating away from it.
  235. mOuterWindowID = aOuterWindow->WindowID();
  236. // TODO We can have a parent without a frame element in some cases dealing
  237. // with the hidden window.
  238. nsCOMPtr<nsPIDOMWindowOuter> parent = aOuterWindow->GetScriptableParent();
  239. mParentOuterWindowID = parent ? parent->WindowID() : 0;
  240. // get the docshell from the outerwindow, and then get the originattributes
  241. nsCOMPtr<nsIDocShell> docShell = aOuterWindow->GetDocShell();
  242. MOZ_ASSERT(docShell);
  243. const DocShellOriginAttributes attrs =
  244. nsDocShell::Cast(docShell)->GetOriginAttributes();
  245. if (docShell->ItemType() == nsIDocShellTreeItem::typeChrome) {
  246. MOZ_ASSERT(attrs.mPrivateBrowsingId == 0,
  247. "chrome docshell shouldn't have mPrivateBrowsingId set.");
  248. }
  249. mOriginAttributes.InheritFromDocShellToNecko(attrs);
  250. }
  251. LoadInfo::LoadInfo(const LoadInfo& rhs)
  252. : mLoadingPrincipal(rhs.mLoadingPrincipal)
  253. , mTriggeringPrincipal(rhs.mTriggeringPrincipal)
  254. , mPrincipalToInherit(rhs.mPrincipalToInherit)
  255. , mLoadingContext(rhs.mLoadingContext)
  256. , mContextForTopLevelLoad(rhs.mContextForTopLevelLoad)
  257. , mSecurityFlags(rhs.mSecurityFlags)
  258. , mInternalContentPolicyType(rhs.mInternalContentPolicyType)
  259. , mTainting(rhs.mTainting)
  260. , mUpgradeInsecureRequests(rhs.mUpgradeInsecureRequests)
  261. , mVerifySignedContent(rhs.mVerifySignedContent)
  262. , mEnforceSRI(rhs.mEnforceSRI)
  263. , mForceAllowDataURI(rhs.mForceAllowDataURI)
  264. , mForceInheritPrincipalDropped(rhs.mForceInheritPrincipalDropped)
  265. , mInnerWindowID(rhs.mInnerWindowID)
  266. , mOuterWindowID(rhs.mOuterWindowID)
  267. , mParentOuterWindowID(rhs.mParentOuterWindowID)
  268. , mFrameOuterWindowID(rhs.mFrameOuterWindowID)
  269. , mEnforceSecurity(rhs.mEnforceSecurity)
  270. , mInitialSecurityCheckDone(rhs.mInitialSecurityCheckDone)
  271. , mIsThirdPartyContext(rhs.mIsThirdPartyContext)
  272. , mOriginAttributes(rhs.mOriginAttributes)
  273. , mRedirectChainIncludingInternalRedirects(
  274. rhs.mRedirectChainIncludingInternalRedirects)
  275. , mRedirectChain(rhs.mRedirectChain)
  276. , mCorsUnsafeHeaders(rhs.mCorsUnsafeHeaders)
  277. , mForcePreflight(rhs.mForcePreflight)
  278. , mIsPreflight(rhs.mIsPreflight)
  279. , mLoadTriggeredFromExternal(rhs.mLoadTriggeredFromExternal)
  280. , mIsFromProcessingFrameAttributes(rhs.mIsFromProcessingFrameAttributes)
  281. {
  282. }
  283. LoadInfo::LoadInfo(nsIPrincipal* aLoadingPrincipal,
  284. nsIPrincipal* aTriggeringPrincipal,
  285. nsIPrincipal* aPrincipalToInherit,
  286. nsSecurityFlags aSecurityFlags,
  287. nsContentPolicyType aContentPolicyType,
  288. LoadTainting aTainting,
  289. bool aUpgradeInsecureRequests,
  290. bool aVerifySignedContent,
  291. bool aEnforceSRI,
  292. bool aForceAllowDataURI,
  293. bool aForceInheritPrincipalDropped,
  294. uint64_t aInnerWindowID,
  295. uint64_t aOuterWindowID,
  296. uint64_t aParentOuterWindowID,
  297. uint64_t aFrameOuterWindowID,
  298. bool aEnforceSecurity,
  299. bool aInitialSecurityCheckDone,
  300. bool aIsThirdPartyContext,
  301. const NeckoOriginAttributes& aOriginAttributes,
  302. nsTArray<nsCOMPtr<nsIPrincipal>>& aRedirectChainIncludingInternalRedirects,
  303. nsTArray<nsCOMPtr<nsIPrincipal>>& aRedirectChain,
  304. const nsTArray<nsCString>& aCorsUnsafeHeaders,
  305. bool aForcePreflight,
  306. bool aIsPreflight,
  307. bool aLoadTriggeredFromExternal)
  308. : mLoadingPrincipal(aLoadingPrincipal)
  309. , mTriggeringPrincipal(aTriggeringPrincipal)
  310. , mPrincipalToInherit(aPrincipalToInherit)
  311. , mSecurityFlags(aSecurityFlags)
  312. , mInternalContentPolicyType(aContentPolicyType)
  313. , mTainting(aTainting)
  314. , mUpgradeInsecureRequests(aUpgradeInsecureRequests)
  315. , mVerifySignedContent(aVerifySignedContent)
  316. , mEnforceSRI(aEnforceSRI)
  317. , mForceAllowDataURI(aForceAllowDataURI)
  318. , mForceInheritPrincipalDropped(aForceInheritPrincipalDropped)
  319. , mInnerWindowID(aInnerWindowID)
  320. , mOuterWindowID(aOuterWindowID)
  321. , mParentOuterWindowID(aParentOuterWindowID)
  322. , mFrameOuterWindowID(aFrameOuterWindowID)
  323. , mEnforceSecurity(aEnforceSecurity)
  324. , mInitialSecurityCheckDone(aInitialSecurityCheckDone)
  325. , mIsThirdPartyContext(aIsThirdPartyContext)
  326. , mOriginAttributes(aOriginAttributes)
  327. , mCorsUnsafeHeaders(aCorsUnsafeHeaders)
  328. , mForcePreflight(aForcePreflight)
  329. , mIsPreflight(aIsPreflight)
  330. , mLoadTriggeredFromExternal(aLoadTriggeredFromExternal)
  331. , mIsFromProcessingFrameAttributes(false)
  332. {
  333. // Only top level TYPE_DOCUMENT loads can have a null loadingPrincipal
  334. MOZ_ASSERT(mLoadingPrincipal || aContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT);
  335. MOZ_ASSERT(mTriggeringPrincipal);
  336. mRedirectChainIncludingInternalRedirects.SwapElements(
  337. aRedirectChainIncludingInternalRedirects);
  338. mRedirectChain.SwapElements(aRedirectChain);
  339. }
  340. LoadInfo::~LoadInfo()
  341. {
  342. }
  343. void
  344. LoadInfo::ComputeIsThirdPartyContext(nsPIDOMWindowOuter* aOuterWindow)
  345. {
  346. nsContentPolicyType type =
  347. nsContentUtils::InternalContentPolicyTypeToExternal(mInternalContentPolicyType);
  348. if (type == nsIContentPolicy::TYPE_DOCUMENT) {
  349. // Top-level loads are never third-party.
  350. mIsThirdPartyContext = false;
  351. return;
  352. }
  353. nsCOMPtr<mozIThirdPartyUtil> util(do_GetService(THIRDPARTYUTIL_CONTRACTID));
  354. if (NS_WARN_IF(!util)) {
  355. return;
  356. }
  357. util->IsThirdPartyWindow(aOuterWindow, nullptr, &mIsThirdPartyContext);
  358. }
  359. NS_IMPL_ISUPPORTS(LoadInfo, nsILoadInfo)
  360. already_AddRefed<nsILoadInfo>
  361. LoadInfo::Clone() const
  362. {
  363. RefPtr<LoadInfo> copy(new LoadInfo(*this));
  364. return copy.forget();
  365. }
  366. already_AddRefed<nsILoadInfo>
  367. LoadInfo::CloneWithNewSecFlags(nsSecurityFlags aSecurityFlags) const
  368. {
  369. RefPtr<LoadInfo> copy(new LoadInfo(*this));
  370. copy->mSecurityFlags = aSecurityFlags;
  371. return copy.forget();
  372. }
  373. already_AddRefed<nsILoadInfo>
  374. LoadInfo::CloneForNewRequest() const
  375. {
  376. RefPtr<LoadInfo> copy(new LoadInfo(*this));
  377. copy->mEnforceSecurity = false;
  378. copy->mInitialSecurityCheckDone = false;
  379. copy->mRedirectChainIncludingInternalRedirects.Clear();
  380. copy->mRedirectChain.Clear();
  381. return copy.forget();
  382. }
  383. NS_IMETHODIMP
  384. LoadInfo::GetLoadingPrincipal(nsIPrincipal** aLoadingPrincipal)
  385. {
  386. NS_IF_ADDREF(*aLoadingPrincipal = mLoadingPrincipal);
  387. return NS_OK;
  388. }
  389. nsIPrincipal*
  390. LoadInfo::LoadingPrincipal()
  391. {
  392. return mLoadingPrincipal;
  393. }
  394. NS_IMETHODIMP
  395. LoadInfo::GetTriggeringPrincipal(nsIPrincipal** aTriggeringPrincipal)
  396. {
  397. NS_ADDREF(*aTriggeringPrincipal = mTriggeringPrincipal);
  398. return NS_OK;
  399. }
  400. nsIPrincipal*
  401. LoadInfo::TriggeringPrincipal()
  402. {
  403. return mTriggeringPrincipal;
  404. }
  405. NS_IMETHODIMP
  406. LoadInfo::GetPrincipalToInherit(nsIPrincipal** aPrincipalToInherit)
  407. {
  408. NS_IF_ADDREF(*aPrincipalToInherit = mPrincipalToInherit);
  409. return NS_OK;
  410. }
  411. NS_IMETHODIMP
  412. LoadInfo::SetPrincipalToInherit(nsIPrincipal* aPrincipalToInherit)
  413. {
  414. MOZ_ASSERT(aPrincipalToInherit, "must be a valid principal to inherit");
  415. mPrincipalToInherit = aPrincipalToInherit;
  416. return NS_OK;
  417. }
  418. nsIPrincipal*
  419. LoadInfo::PrincipalToInherit()
  420. {
  421. return mPrincipalToInherit;
  422. }
  423. NS_IMETHODIMP
  424. LoadInfo::GetLoadingDocument(nsIDOMDocument** aResult)
  425. {
  426. nsCOMPtr<nsINode> node = do_QueryReferent(mLoadingContext);
  427. if (node) {
  428. nsCOMPtr<nsIDOMDocument> context = do_QueryInterface(node->OwnerDoc());
  429. context.forget(aResult);
  430. }
  431. return NS_OK;
  432. }
  433. nsINode*
  434. LoadInfo::LoadingNode()
  435. {
  436. nsCOMPtr<nsINode> node = do_QueryReferent(mLoadingContext);
  437. return node;
  438. }
  439. nsISupports*
  440. LoadInfo::ContextForTopLevelLoad()
  441. {
  442. // Most likely you want to query LoadingNode() instead of
  443. // ContextForTopLevelLoad() if this assertion fires.
  444. MOZ_ASSERT(mInternalContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT,
  445. "should only query this context for top level document loads");
  446. nsCOMPtr<nsISupports> context = do_QueryReferent(mContextForTopLevelLoad);
  447. return context;
  448. }
  449. already_AddRefed<nsISupports>
  450. LoadInfo::GetLoadingContext()
  451. {
  452. nsCOMPtr<nsISupports> context;
  453. if (mInternalContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT) {
  454. context = ContextForTopLevelLoad();
  455. }
  456. else {
  457. context = LoadingNode();
  458. }
  459. return context.forget();
  460. }
  461. NS_IMETHODIMP
  462. LoadInfo::GetLoadingContextXPCOM(nsISupports** aResult)
  463. {
  464. nsCOMPtr<nsISupports> context = GetLoadingContext();
  465. context.forget(aResult);
  466. return NS_OK;
  467. }
  468. NS_IMETHODIMP
  469. LoadInfo::GetSecurityFlags(nsSecurityFlags* aResult)
  470. {
  471. *aResult = mSecurityFlags;
  472. return NS_OK;
  473. }
  474. NS_IMETHODIMP
  475. LoadInfo::GetSecurityMode(uint32_t* aFlags)
  476. {
  477. *aFlags = (mSecurityFlags &
  478. (nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_INHERITS |
  479. nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED |
  480. nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS |
  481. nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL |
  482. nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS));
  483. return NS_OK;
  484. }
  485. NS_IMETHODIMP
  486. LoadInfo::GetIsInThirdPartyContext(bool* aIsInThirdPartyContext)
  487. {
  488. *aIsInThirdPartyContext = mIsThirdPartyContext;
  489. return NS_OK;
  490. }
  491. static const uint32_t sCookiePolicyMask =
  492. nsILoadInfo::SEC_COOKIES_DEFAULT |
  493. nsILoadInfo::SEC_COOKIES_INCLUDE |
  494. nsILoadInfo::SEC_COOKIES_SAME_ORIGIN |
  495. nsILoadInfo::SEC_COOKIES_OMIT;
  496. NS_IMETHODIMP
  497. LoadInfo::GetCookiePolicy(uint32_t *aResult)
  498. {
  499. uint32_t policy = mSecurityFlags & sCookiePolicyMask;
  500. if (policy == nsILoadInfo::SEC_COOKIES_DEFAULT) {
  501. policy = (mSecurityFlags & SEC_REQUIRE_CORS_DATA_INHERITS) ?
  502. nsILoadInfo::SEC_COOKIES_SAME_ORIGIN : nsILoadInfo::SEC_COOKIES_INCLUDE;
  503. }
  504. *aResult = policy;
  505. return NS_OK;
  506. }
  507. void
  508. LoadInfo::SetIncludeCookiesSecFlag()
  509. {
  510. MOZ_ASSERT(!mEnforceSecurity,
  511. "Request should not have been opened yet");
  512. MOZ_ASSERT((mSecurityFlags & sCookiePolicyMask) ==
  513. nsILoadInfo::SEC_COOKIES_DEFAULT);
  514. mSecurityFlags = (mSecurityFlags & ~sCookiePolicyMask) |
  515. nsILoadInfo::SEC_COOKIES_INCLUDE;
  516. }
  517. NS_IMETHODIMP
  518. LoadInfo::GetForceInheritPrincipal(bool* aInheritPrincipal)
  519. {
  520. *aInheritPrincipal =
  521. (mSecurityFlags & nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL);
  522. return NS_OK;
  523. }
  524. NS_IMETHODIMP
  525. LoadInfo::GetForceInheritPrincipalOverruleOwner(bool* aInheritPrincipal)
  526. {
  527. *aInheritPrincipal =
  528. (mSecurityFlags & nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL_OVERRULE_OWNER);
  529. return NS_OK;
  530. }
  531. NS_IMETHODIMP
  532. LoadInfo::GetLoadingSandboxed(bool* aLoadingSandboxed)
  533. {
  534. *aLoadingSandboxed = (mSecurityFlags & nsILoadInfo::SEC_SANDBOXED);
  535. return NS_OK;
  536. }
  537. NS_IMETHODIMP
  538. LoadInfo::GetAboutBlankInherits(bool* aResult)
  539. {
  540. *aResult =
  541. (mSecurityFlags & nsILoadInfo::SEC_ABOUT_BLANK_INHERITS);
  542. return NS_OK;
  543. }
  544. NS_IMETHODIMP
  545. LoadInfo::GetAllowChrome(bool* aResult)
  546. {
  547. *aResult =
  548. (mSecurityFlags & nsILoadInfo::SEC_ALLOW_CHROME);
  549. return NS_OK;
  550. }
  551. NS_IMETHODIMP
  552. LoadInfo::GetDisallowScript(bool* aResult)
  553. {
  554. *aResult =
  555. (mSecurityFlags & nsILoadInfo::SEC_DISALLOW_SCRIPT);
  556. return NS_OK;
  557. }
  558. NS_IMETHODIMP
  559. LoadInfo::GetDontFollowRedirects(bool* aResult)
  560. {
  561. *aResult =
  562. (mSecurityFlags & nsILoadInfo::SEC_DONT_FOLLOW_REDIRECTS);
  563. return NS_OK;
  564. }
  565. NS_IMETHODIMP
  566. LoadInfo::GetLoadErrorPage(bool* aResult)
  567. {
  568. *aResult =
  569. (mSecurityFlags & nsILoadInfo::SEC_LOAD_ERROR_PAGE);
  570. return NS_OK;
  571. }
  572. NS_IMETHODIMP
  573. LoadInfo::GetExternalContentPolicyType(nsContentPolicyType* aResult)
  574. {
  575. *aResult = nsContentUtils::InternalContentPolicyTypeToExternal(mInternalContentPolicyType);
  576. return NS_OK;
  577. }
  578. nsContentPolicyType
  579. LoadInfo::InternalContentPolicyType()
  580. {
  581. return mInternalContentPolicyType;
  582. }
  583. NS_IMETHODIMP
  584. LoadInfo::GetUpgradeInsecureRequests(bool* aResult)
  585. {
  586. *aResult = mUpgradeInsecureRequests;
  587. return NS_OK;
  588. }
  589. NS_IMETHODIMP
  590. LoadInfo::SetVerifySignedContent(bool aVerifySignedContent)
  591. {
  592. MOZ_ASSERT(mInternalContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT,
  593. "can only verify content for TYPE_DOCUMENT");
  594. mVerifySignedContent = aVerifySignedContent;
  595. return NS_OK;
  596. }
  597. NS_IMETHODIMP
  598. LoadInfo::GetVerifySignedContent(bool* aResult)
  599. {
  600. *aResult = mVerifySignedContent;
  601. return NS_OK;
  602. }
  603. NS_IMETHODIMP
  604. LoadInfo::SetEnforceSRI(bool aEnforceSRI)
  605. {
  606. mEnforceSRI = aEnforceSRI;
  607. return NS_OK;
  608. }
  609. NS_IMETHODIMP
  610. LoadInfo::GetEnforceSRI(bool* aResult)
  611. {
  612. *aResult = mEnforceSRI;
  613. return NS_OK;
  614. }
  615. NS_IMETHODIMP
  616. LoadInfo::SetForceAllowDataURI(bool aForceAllowDataURI)
  617. {
  618. MOZ_ASSERT(!mForceAllowDataURI ||
  619. mInternalContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT,
  620. "can only allow data URI navigation for TYPE_DOCUMENT");
  621. mForceAllowDataURI = aForceAllowDataURI;
  622. return NS_OK;
  623. }
  624. NS_IMETHODIMP
  625. LoadInfo::GetForceAllowDataURI(bool* aForceAllowDataURI)
  626. {
  627. *aForceAllowDataURI = mForceAllowDataURI;
  628. return NS_OK;
  629. }
  630. NS_IMETHODIMP
  631. LoadInfo::GetForceInheritPrincipalDropped(bool* aResult)
  632. {
  633. *aResult = mForceInheritPrincipalDropped;
  634. return NS_OK;
  635. }
  636. NS_IMETHODIMP
  637. LoadInfo::GetInnerWindowID(uint64_t* aResult)
  638. {
  639. *aResult = mInnerWindowID;
  640. return NS_OK;
  641. }
  642. NS_IMETHODIMP
  643. LoadInfo::GetOuterWindowID(uint64_t* aResult)
  644. {
  645. *aResult = mOuterWindowID;
  646. return NS_OK;
  647. }
  648. NS_IMETHODIMP
  649. LoadInfo::GetParentOuterWindowID(uint64_t* aResult)
  650. {
  651. *aResult = mParentOuterWindowID;
  652. return NS_OK;
  653. }
  654. NS_IMETHODIMP
  655. LoadInfo::GetFrameOuterWindowID(uint64_t* aResult)
  656. {
  657. *aResult = mFrameOuterWindowID;
  658. return NS_OK;
  659. }
  660. NS_IMETHODIMP
  661. LoadInfo::GetScriptableOriginAttributes(JSContext* aCx,
  662. JS::MutableHandle<JS::Value> aOriginAttributes)
  663. {
  664. if (NS_WARN_IF(!ToJSValue(aCx, mOriginAttributes, aOriginAttributes))) {
  665. return NS_ERROR_FAILURE;
  666. }
  667. return NS_OK;
  668. }
  669. NS_IMETHODIMP
  670. LoadInfo::ResetPrincipalsToNullPrincipal()
  671. {
  672. // take the originAttributes from the LoadInfo and create
  673. // a new NullPrincipal using those origin attributes.
  674. PrincipalOriginAttributes pAttrs;
  675. pAttrs.InheritFromNecko(mOriginAttributes);
  676. nsCOMPtr<nsIPrincipal> newNullPrincipal = nsNullPrincipal::Create(pAttrs);
  677. MOZ_ASSERT(mInternalContentPolicyType != nsIContentPolicy::TYPE_DOCUMENT ||
  678. !mLoadingPrincipal,
  679. "LoadingPrincipal should be null for toplevel loads");
  680. // the loadingPrincipal for toplevel loads is always a nullptr;
  681. if (mInternalContentPolicyType != nsIContentPolicy::TYPE_DOCUMENT) {
  682. mLoadingPrincipal = newNullPrincipal;
  683. }
  684. mTriggeringPrincipal = newNullPrincipal;
  685. mPrincipalToInherit = newNullPrincipal;
  686. // setting SEC_FORCE_INHERIT_PRINCIPAL_OVERRULE_OWNER will overrule
  687. // any non null owner set on the channel and will return the principal
  688. // form the loadinfo instead.
  689. mSecurityFlags |= SEC_FORCE_INHERIT_PRINCIPAL_OVERRULE_OWNER;
  690. return NS_OK;
  691. }
  692. NS_IMETHODIMP
  693. LoadInfo::SetScriptableOriginAttributes(JSContext* aCx,
  694. JS::Handle<JS::Value> aOriginAttributes)
  695. {
  696. NeckoOriginAttributes attrs;
  697. if (!aOriginAttributes.isObject() || !attrs.Init(aCx, aOriginAttributes)) {
  698. return NS_ERROR_INVALID_ARG;
  699. }
  700. mOriginAttributes = attrs;
  701. return NS_OK;
  702. }
  703. nsresult
  704. LoadInfo::GetOriginAttributes(mozilla::NeckoOriginAttributes* aOriginAttributes)
  705. {
  706. NS_ENSURE_ARG(aOriginAttributes);
  707. *aOriginAttributes = mOriginAttributes;
  708. return NS_OK;
  709. }
  710. nsresult
  711. LoadInfo::SetOriginAttributes(const mozilla::NeckoOriginAttributes& aOriginAttributes)
  712. {
  713. mOriginAttributes = aOriginAttributes;
  714. return NS_OK;
  715. }
  716. NS_IMETHODIMP
  717. LoadInfo::SetEnforceSecurity(bool aEnforceSecurity)
  718. {
  719. // Indicates whether the channel was openend using AsyncOpen2. Once set
  720. // to true, it must remain true throughout the lifetime of the channel.
  721. // Setting it to anything else than true will be discarded.
  722. MOZ_ASSERT(aEnforceSecurity, "aEnforceSecurity must be true");
  723. mEnforceSecurity = mEnforceSecurity || aEnforceSecurity;
  724. return NS_OK;
  725. }
  726. NS_IMETHODIMP
  727. LoadInfo::GetEnforceSecurity(bool* aResult)
  728. {
  729. *aResult = mEnforceSecurity;
  730. return NS_OK;
  731. }
  732. NS_IMETHODIMP
  733. LoadInfo::SetInitialSecurityCheckDone(bool aInitialSecurityCheckDone)
  734. {
  735. // Indicates whether the channel was ever evaluated by the
  736. // ContentSecurityManager. Once set to true, this flag must
  737. // remain true throughout the lifetime of the channel.
  738. // Setting it to anything else than true will be discarded.
  739. MOZ_ASSERT(aInitialSecurityCheckDone, "aInitialSecurityCheckDone must be true");
  740. mInitialSecurityCheckDone = mInitialSecurityCheckDone || aInitialSecurityCheckDone;
  741. return NS_OK;
  742. }
  743. NS_IMETHODIMP
  744. LoadInfo::GetInitialSecurityCheckDone(bool* aResult)
  745. {
  746. *aResult = mInitialSecurityCheckDone;
  747. return NS_OK;
  748. }
  749. NS_IMETHODIMP
  750. LoadInfo::AppendRedirectedPrincipal(nsIPrincipal* aPrincipal, bool aIsInternalRedirect)
  751. {
  752. NS_ENSURE_ARG(aPrincipal);
  753. MOZ_ASSERT(NS_IsMainThread());
  754. mRedirectChainIncludingInternalRedirects.AppendElement(aPrincipal);
  755. if (!aIsInternalRedirect) {
  756. mRedirectChain.AppendElement(aPrincipal);
  757. }
  758. return NS_OK;
  759. }
  760. NS_IMETHODIMP
  761. LoadInfo::GetRedirectChainIncludingInternalRedirects(JSContext* aCx, JS::MutableHandle<JS::Value> aChain)
  762. {
  763. if (!ToJSValue(aCx, mRedirectChainIncludingInternalRedirects, aChain)) {
  764. return NS_ERROR_OUT_OF_MEMORY;
  765. }
  766. return NS_OK;
  767. }
  768. const nsTArray<nsCOMPtr<nsIPrincipal>>&
  769. LoadInfo::RedirectChainIncludingInternalRedirects()
  770. {
  771. return mRedirectChainIncludingInternalRedirects;
  772. }
  773. NS_IMETHODIMP
  774. LoadInfo::GetRedirectChain(JSContext* aCx, JS::MutableHandle<JS::Value> aChain)
  775. {
  776. if (!ToJSValue(aCx, mRedirectChain, aChain)) {
  777. return NS_ERROR_OUT_OF_MEMORY;
  778. }
  779. return NS_OK;
  780. }
  781. const nsTArray<nsCOMPtr<nsIPrincipal>>&
  782. LoadInfo::RedirectChain()
  783. {
  784. return mRedirectChain;
  785. }
  786. void
  787. LoadInfo::SetCorsPreflightInfo(const nsTArray<nsCString>& aHeaders,
  788. bool aForcePreflight)
  789. {
  790. MOZ_ASSERT(GetSecurityMode() == nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS);
  791. MOZ_ASSERT(!mInitialSecurityCheckDone);
  792. mCorsUnsafeHeaders = aHeaders;
  793. mForcePreflight = aForcePreflight;
  794. }
  795. const nsTArray<nsCString>&
  796. LoadInfo::CorsUnsafeHeaders()
  797. {
  798. return mCorsUnsafeHeaders;
  799. }
  800. NS_IMETHODIMP
  801. LoadInfo::GetForcePreflight(bool* aForcePreflight)
  802. {
  803. *aForcePreflight = mForcePreflight;
  804. return NS_OK;
  805. }
  806. void
  807. LoadInfo::SetIsPreflight()
  808. {
  809. MOZ_ASSERT(GetSecurityMode() == nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS);
  810. MOZ_ASSERT(!mInitialSecurityCheckDone);
  811. mIsPreflight = true;
  812. }
  813. void
  814. LoadInfo::SetUpgradeInsecureRequests()
  815. {
  816. mUpgradeInsecureRequests = true;
  817. }
  818. NS_IMETHODIMP
  819. LoadInfo::GetIsPreflight(bool* aIsPreflight)
  820. {
  821. *aIsPreflight = mIsPreflight;
  822. return NS_OK;
  823. }
  824. NS_IMETHODIMP
  825. LoadInfo::SetLoadTriggeredFromExternal(bool aLoadTriggeredFromExternal)
  826. {
  827. MOZ_ASSERT(!aLoadTriggeredFromExternal ||
  828. mInternalContentPolicyType == nsIContentPolicy::TYPE_DOCUMENT,
  829. "can only set load triggered from external for TYPE_DOCUMENT");
  830. mLoadTriggeredFromExternal = aLoadTriggeredFromExternal;
  831. return NS_OK;
  832. }
  833. NS_IMETHODIMP
  834. LoadInfo::GetLoadTriggeredFromExternal(bool* aLoadTriggeredFromExternal)
  835. {
  836. *aLoadTriggeredFromExternal = mLoadTriggeredFromExternal;
  837. return NS_OK;
  838. }
  839. NS_IMETHODIMP
  840. LoadInfo::GetTainting(uint32_t* aTaintingOut)
  841. {
  842. MOZ_ASSERT(aTaintingOut);
  843. *aTaintingOut = static_cast<uint32_t>(mTainting);
  844. return NS_OK;
  845. }
  846. NS_IMETHODIMP
  847. LoadInfo::MaybeIncreaseTainting(uint32_t aTainting)
  848. {
  849. NS_ENSURE_ARG(aTainting <= TAINTING_OPAQUE);
  850. LoadTainting tainting = static_cast<LoadTainting>(aTainting);
  851. if (tainting > mTainting) {
  852. mTainting = tainting;
  853. }
  854. return NS_OK;
  855. }
  856. NS_IMETHODIMP
  857. LoadInfo::GetIsTopLevelLoad(bool *aResult)
  858. {
  859. *aResult = mFrameOuterWindowID ? mFrameOuterWindowID == mOuterWindowID
  860. : mParentOuterWindowID == mOuterWindowID;
  861. return NS_OK;
  862. }
  863. void
  864. LoadInfo::SetIsFromProcessingFrameAttributes()
  865. {
  866. mIsFromProcessingFrameAttributes = true;
  867. }
  868. NS_IMETHODIMP
  869. LoadInfo::GetIsFromProcessingFrameAttributes(bool *aIsFromProcessingFrameAttributes)
  870. {
  871. MOZ_ASSERT(aIsFromProcessingFrameAttributes);
  872. *aIsFromProcessingFrameAttributes = mIsFromProcessingFrameAttributes;
  873. return NS_OK;
  874. }
  875. } // namespace net
  876. } // namespace mozilla