BasePrincipal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. #ifndef mozilla_BasePrincipal_h
  6. #define mozilla_BasePrincipal_h
  7. #include "nsIPrincipal.h"
  8. #include "nsIScriptSecurityManager.h"
  9. #include "nsJSPrincipals.h"
  10. #include "mozilla/Attributes.h"
  11. #include "mozilla/dom/ChromeUtilsBinding.h"
  12. class nsIContentSecurityPolicy;
  13. class nsIObjectOutputStream;
  14. class nsIObjectInputStream;
  15. class nsIURI;
  16. class nsExpandedPrincipal;
  17. namespace mozilla {
  18. class GenericOriginAttributes;
  19. // Base OriginAttributes class. This has several subclass flavors, and is not
  20. // directly constructable itself.
  21. class OriginAttributes : public dom::OriginAttributesDictionary
  22. {
  23. public:
  24. bool operator==(const OriginAttributes& aOther) const
  25. {
  26. return mAppId == aOther.mAppId &&
  27. mInIsolatedMozBrowser == aOther.mInIsolatedMozBrowser &&
  28. mAddonId == aOther.mAddonId &&
  29. mUserContextId == aOther.mUserContextId &&
  30. mPrivateBrowsingId == aOther.mPrivateBrowsingId &&
  31. mFirstPartyDomain == aOther.mFirstPartyDomain;
  32. }
  33. bool operator!=(const OriginAttributes& aOther) const
  34. {
  35. return !(*this == aOther);
  36. }
  37. // Serializes/Deserializes non-default values into the suffix format, i.e.
  38. // |!key1=value1&key2=value2|. If there are no non-default attributes, this
  39. // returns an empty string.
  40. void CreateSuffix(nsACString& aStr) const;
  41. // Don't use this method for anything else than debugging!
  42. void CreateAnonymizedSuffix(nsACString& aStr) const;
  43. MOZ_MUST_USE bool PopulateFromSuffix(const nsACString& aStr);
  44. // Populates the attributes from a string like
  45. // |uri!key1=value1&key2=value2| and returns the uri without the suffix.
  46. MOZ_MUST_USE bool PopulateFromOrigin(const nsACString& aOrigin,
  47. nsACString& aOriginNoSuffix);
  48. // Helper function to match mIsPrivateBrowsing to existing private browsing
  49. // flags. Once all other flags are removed, this can be removed too.
  50. void SyncAttributesWithPrivateBrowsing(bool aInPrivateBrowsing);
  51. void SetFromGenericAttributes(const GenericOriginAttributes& aAttrs);
  52. // check if "privacy.firstparty.isolate" is enabled.
  53. static bool IsFirstPartyEnabled();
  54. protected:
  55. OriginAttributes() {}
  56. explicit OriginAttributes(const OriginAttributesDictionary& aOther)
  57. : OriginAttributesDictionary(aOther) {}
  58. };
  59. class PrincipalOriginAttributes;
  60. class DocShellOriginAttributes;
  61. class NeckoOriginAttributes;
  62. // Various classes in Gecko contain OriginAttributes members, and those
  63. // OriginAttributes get propagated to other classes according to certain rules.
  64. // For example, the OriginAttributes on the docshell affect the OriginAttributes
  65. // for the principal of a document loaded inside it, whose OriginAttributes in
  66. // turn affect those of network loads and child docshells. To codify and
  67. // centralize these rules, we introduce separate subclasses for the different
  68. // flavors, and a variety of InheritFrom* methods to implement the transfer
  69. // behavior.
  70. // For OriginAttributes stored on principals.
  71. class PrincipalOriginAttributes : public OriginAttributes
  72. {
  73. public:
  74. PrincipalOriginAttributes() {}
  75. PrincipalOriginAttributes(uint32_t aAppId, bool aInIsolatedMozBrowser)
  76. {
  77. mAppId = aAppId;
  78. mInIsolatedMozBrowser = aInIsolatedMozBrowser;
  79. }
  80. // Inheriting OriginAttributes from docshell to document when user navigates.
  81. //
  82. // @param aAttrs Origin Attributes of the docshell.
  83. // @param aURI The URI of the document.
  84. void InheritFromDocShellToDoc(const DocShellOriginAttributes& aAttrs,
  85. const nsIURI* aURI);
  86. // Inherit OriginAttributes from Necko.
  87. void InheritFromNecko(const NeckoOriginAttributes& aAttrs);
  88. void StripUserContextIdAndFirstPartyDomain();
  89. };
  90. // For OriginAttributes stored on docshells / loadcontexts / browsing contexts.
  91. class DocShellOriginAttributes : public OriginAttributes
  92. {
  93. public:
  94. DocShellOriginAttributes() {}
  95. DocShellOriginAttributes(uint32_t aAppId, bool aInIsolatedMozBrowser)
  96. {
  97. mAppId = aAppId;
  98. mInIsolatedMozBrowser = aInIsolatedMozBrowser;
  99. }
  100. // Inheriting OriginAttributes from document to child docshell when an
  101. // <iframe> is created.
  102. //
  103. // @param aAttrs Origin Attributes of the document.
  104. void
  105. InheritFromDocToChildDocShell(const PrincipalOriginAttributes& aAttrs);
  106. };
  107. // For OriginAttributes stored on Necko.
  108. class NeckoOriginAttributes : public OriginAttributes
  109. {
  110. public:
  111. NeckoOriginAttributes() {}
  112. NeckoOriginAttributes(uint32_t aAppId, bool aInIsolatedMozBrowser)
  113. {
  114. mAppId = aAppId;
  115. mInIsolatedMozBrowser = aInIsolatedMozBrowser;
  116. }
  117. // Inheriting OriginAttributes from document to necko when a network request
  118. // is made.
  119. void InheritFromDocToNecko(const PrincipalOriginAttributes& aAttrs);
  120. // Inheriting OriginAttributes from a docshell when loading a top-level
  121. // document.
  122. void InheritFromDocShellToNecko(const DocShellOriginAttributes& aAttrs,
  123. const bool aIsTopLevelDocument = false,
  124. nsIURI* aURI = nullptr);
  125. };
  126. // For operating on OriginAttributes not associated with any data structure.
  127. class GenericOriginAttributes : public OriginAttributes
  128. {
  129. public:
  130. GenericOriginAttributes() {}
  131. explicit GenericOriginAttributes(const OriginAttributesDictionary& aOther)
  132. : OriginAttributes(aOther) {}
  133. };
  134. class OriginAttributesPattern : public dom::OriginAttributesPatternDictionary
  135. {
  136. public:
  137. // To convert a JSON string to an OriginAttributesPattern, do the following:
  138. //
  139. // OriginAttributesPattern pattern;
  140. // if (!pattern.Init(aJSONString)) {
  141. // ... // handle failure.
  142. // }
  143. OriginAttributesPattern() {}
  144. explicit OriginAttributesPattern(const OriginAttributesPatternDictionary& aOther)
  145. : OriginAttributesPatternDictionary(aOther) {}
  146. // Performs a match of |aAttrs| against this pattern.
  147. bool Matches(const OriginAttributes& aAttrs) const
  148. {
  149. if (mAppId.WasPassed() && mAppId.Value() != aAttrs.mAppId) {
  150. return false;
  151. }
  152. if (mInIsolatedMozBrowser.WasPassed() && mInIsolatedMozBrowser.Value() != aAttrs.mInIsolatedMozBrowser) {
  153. return false;
  154. }
  155. if (mAddonId.WasPassed() && mAddonId.Value() != aAttrs.mAddonId) {
  156. return false;
  157. }
  158. if (mUserContextId.WasPassed() && mUserContextId.Value() != aAttrs.mUserContextId) {
  159. return false;
  160. }
  161. if (mPrivateBrowsingId.WasPassed() && mPrivateBrowsingId.Value() != aAttrs.mPrivateBrowsingId) {
  162. return false;
  163. }
  164. if (mFirstPartyDomain.WasPassed() && mFirstPartyDomain.Value() != aAttrs.mFirstPartyDomain) {
  165. return false;
  166. }
  167. return true;
  168. }
  169. bool Overlaps(const OriginAttributesPattern& aOther) const
  170. {
  171. if (mAppId.WasPassed() && aOther.mAppId.WasPassed() &&
  172. mAppId.Value() != aOther.mAppId.Value()) {
  173. return false;
  174. }
  175. if (mInIsolatedMozBrowser.WasPassed() &&
  176. aOther.mInIsolatedMozBrowser.WasPassed() &&
  177. mInIsolatedMozBrowser.Value() != aOther.mInIsolatedMozBrowser.Value()) {
  178. return false;
  179. }
  180. if (mAddonId.WasPassed() && aOther.mAddonId.WasPassed() &&
  181. mAddonId.Value() != aOther.mAddonId.Value()) {
  182. return false;
  183. }
  184. if (mUserContextId.WasPassed() && aOther.mUserContextId.WasPassed() &&
  185. mUserContextId.Value() != aOther.mUserContextId.Value()) {
  186. return false;
  187. }
  188. if (mPrivateBrowsingId.WasPassed() && aOther.mPrivateBrowsingId.WasPassed() &&
  189. mPrivateBrowsingId.Value() != aOther.mPrivateBrowsingId.Value()) {
  190. return false;
  191. }
  192. if (mFirstPartyDomain.WasPassed() && aOther.mFirstPartyDomain.WasPassed() &&
  193. mFirstPartyDomain.Value() != aOther.mFirstPartyDomain.Value()) {
  194. return false;
  195. }
  196. return true;
  197. }
  198. };
  199. /*
  200. * Base class from which all nsIPrincipal implementations inherit. Use this for
  201. * default implementations and other commonalities between principal
  202. * implementations.
  203. *
  204. * We should merge nsJSPrincipals into this class at some point.
  205. */
  206. class BasePrincipal : public nsJSPrincipals
  207. {
  208. public:
  209. BasePrincipal();
  210. enum DocumentDomainConsideration { DontConsiderDocumentDomain, ConsiderDocumentDomain};
  211. bool Subsumes(nsIPrincipal* aOther, DocumentDomainConsideration aConsideration);
  212. NS_IMETHOD GetOrigin(nsACString& aOrigin) final;
  213. NS_IMETHOD GetOriginNoSuffix(nsACString& aOrigin) final;
  214. NS_IMETHOD Equals(nsIPrincipal* other, bool* _retval) final;
  215. NS_IMETHOD EqualsConsideringDomain(nsIPrincipal* other, bool* _retval) final;
  216. NS_IMETHOD Subsumes(nsIPrincipal* other, bool* _retval) final;
  217. NS_IMETHOD SubsumesConsideringDomain(nsIPrincipal* other, bool* _retval) final;
  218. NS_IMETHOD CheckMayLoad(nsIURI* uri, bool report, bool allowIfInheritsPrincipal) final;
  219. NS_IMETHOD GetCsp(nsIContentSecurityPolicy** aCsp) override;
  220. NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp) override;
  221. NS_IMETHOD EnsureCSP(nsIDOMDocument* aDocument, nsIContentSecurityPolicy** aCSP) override;
  222. NS_IMETHOD GetPreloadCsp(nsIContentSecurityPolicy** aPreloadCSP) override;
  223. NS_IMETHOD EnsurePreloadCSP(nsIDOMDocument* aDocument, nsIContentSecurityPolicy** aCSP) override;
  224. NS_IMETHOD GetCspJSON(nsAString& outCSPinJSON) override;
  225. NS_IMETHOD GetIsNullPrincipal(bool* aResult) override;
  226. NS_IMETHOD GetIsCodebasePrincipal(bool* aResult) override;
  227. NS_IMETHOD GetIsExpandedPrincipal(bool* aResult) override;
  228. NS_IMETHOD GetIsSystemPrincipal(bool* aResult) override;
  229. NS_IMETHOD GetOriginAttributes(JSContext* aCx, JS::MutableHandle<JS::Value> aVal) final;
  230. NS_IMETHOD GetOriginSuffix(nsACString& aOriginSuffix) final;
  231. NS_IMETHOD GetAppStatus(uint16_t* aAppStatus) final;
  232. NS_IMETHOD GetAppId(uint32_t* aAppStatus) final;
  233. NS_IMETHOD GetAddonId(nsAString& aAddonId) final;
  234. NS_IMETHOD GetIsInIsolatedMozBrowserElement(bool* aIsInIsolatedMozBrowserElement) final;
  235. NS_IMETHOD GetUnknownAppId(bool* aUnknownAppId) final;
  236. NS_IMETHOD GetUserContextId(uint32_t* aUserContextId) final;
  237. NS_IMETHOD GetPrivateBrowsingId(uint32_t* aPrivateBrowsingId) final;
  238. bool EqualsIgnoringAddonId(nsIPrincipal *aOther);
  239. virtual bool AddonHasPermission(const nsAString& aPerm);
  240. virtual bool IsOnCSSUnprefixingWhitelist() override { return false; }
  241. virtual bool IsCodebasePrincipal() const { return false; };
  242. static BasePrincipal* Cast(nsIPrincipal* aPrin) { return static_cast<BasePrincipal*>(aPrin); }
  243. static already_AddRefed<BasePrincipal>
  244. CreateCodebasePrincipal(nsIURI* aURI, const PrincipalOriginAttributes& aAttrs);
  245. static already_AddRefed<BasePrincipal> CreateCodebasePrincipal(const nsACString& aOrigin);
  246. const PrincipalOriginAttributes& OriginAttributesRef() { return mOriginAttributes; }
  247. uint32_t AppId() const { return mOriginAttributes.mAppId; }
  248. uint32_t UserContextId() const { return mOriginAttributes.mUserContextId; }
  249. uint32_t PrivateBrowsingId() const { return mOriginAttributes.mPrivateBrowsingId; }
  250. bool IsInIsolatedMozBrowserElement() const { return mOriginAttributes.mInIsolatedMozBrowser; }
  251. enum PrincipalKind {
  252. eNullPrincipal,
  253. eCodebasePrincipal,
  254. eExpandedPrincipal,
  255. eSystemPrincipal
  256. };
  257. virtual PrincipalKind Kind() = 0;
  258. already_AddRefed<BasePrincipal> CloneStrippingUserContextIdAndFirstPartyDomain();
  259. protected:
  260. virtual ~BasePrincipal();
  261. virtual nsresult GetOriginInternal(nsACString& aOrigin) = 0;
  262. // Note that this does not check OriginAttributes. Callers that depend on
  263. // those must call Subsumes instead.
  264. virtual bool SubsumesInternal(nsIPrincipal* aOther, DocumentDomainConsideration aConsider) = 0;
  265. // Internal, side-effect-free check to determine whether the concrete
  266. // principal would allow the load ignoring any common behavior implemented in
  267. // BasePrincipal::CheckMayLoad.
  268. virtual bool MayLoadInternal(nsIURI* aURI) = 0;
  269. friend class ::nsExpandedPrincipal;
  270. // Helper to check whether this principal is associated with an addon that
  271. // allows unprivileged code to load aURI.
  272. bool AddonAllowsLoad(nsIURI* aURI);
  273. nsCOMPtr<nsIContentSecurityPolicy> mCSP;
  274. nsCOMPtr<nsIContentSecurityPolicy> mPreloadCSP;
  275. PrincipalOriginAttributes mOriginAttributes;
  276. };
  277. } // namespace mozilla
  278. #endif /* mozilla_BasePrincipal_h */