nsIPrincipal.idl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  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. /* Defines the abstract interface for a principal. */
  6. #include "nsISerializable.idl"
  7. %{C++
  8. struct JSPrincipals;
  9. #include "nsCOMPtr.h"
  10. #include "nsTArray.h"
  11. %}
  12. interface nsIURI;
  13. interface nsIContentSecurityPolicy;
  14. interface nsIDOMDocument;
  15. [ptr] native JSContext(JSContext);
  16. [ptr] native JSPrincipals(JSPrincipals);
  17. [ptr] native PrincipalArray(nsTArray<nsCOMPtr<nsIPrincipal> >);
  18. [scriptable, builtinclass, uuid(3da7b133-f1a0-4de9-a2bc-5c49014c1077)]
  19. interface nsIPrincipal : nsISerializable
  20. {
  21. /**
  22. * Returns whether the other principal is equivalent to this principal.
  23. * Principals are considered equal if they are the same principal, or
  24. * they have the same origin.
  25. */
  26. boolean equals(in nsIPrincipal other);
  27. /**
  28. * Like equals, but takes document.domain changes into account.
  29. */
  30. boolean equalsConsideringDomain(in nsIPrincipal other);
  31. %{C++
  32. inline bool Equals(nsIPrincipal* aOther) {
  33. bool equal = false;
  34. return NS_SUCCEEDED(Equals(aOther, &equal)) && equal;
  35. }
  36. inline bool EqualsConsideringDomain(nsIPrincipal* aOther) {
  37. bool equal = false;
  38. return NS_SUCCEEDED(EqualsConsideringDomain(aOther, &equal)) && equal;
  39. }
  40. %}
  41. /**
  42. * Returns a hash value for the principal.
  43. */
  44. [noscript] readonly attribute unsigned long hashValue;
  45. /**
  46. * The codebase URI to which this principal pertains. This is
  47. * generally the document URI.
  48. */
  49. readonly attribute nsIURI URI;
  50. /**
  51. * The domain URI to which this principal pertains.
  52. * This is null unless script successfully sets document.domain to our URI
  53. * or a superdomain of our URI.
  54. * Setting this has no effect on the URI.
  55. * See https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy#Changing_origin
  56. */
  57. [noscript] attribute nsIURI domain;
  58. /**
  59. * Returns whether the other principal is equal to or weaker than this
  60. * principal. Principals are equal if they are the same object or they
  61. * have the same origin.
  62. *
  63. * Thus a principal always subsumes itself.
  64. *
  65. * The system principal subsumes itself and all other principals.
  66. *
  67. * A null principal (corresponding to an unknown, hence assumed minimally
  68. * privileged, security context) is not equal to any other principal
  69. * (including other null principals), and therefore does not subsume
  70. * anything but itself.
  71. */
  72. boolean subsumes(in nsIPrincipal other);
  73. /**
  74. * Same as the previous method, subsumes(), but takes document.domain into
  75. * account.
  76. */
  77. boolean subsumesConsideringDomain(in nsIPrincipal other);
  78. %{C++
  79. inline bool Subsumes(nsIPrincipal* aOther) {
  80. bool subsumes = false;
  81. return NS_SUCCEEDED(Subsumes(aOther, &subsumes)) && subsumes;
  82. }
  83. inline bool SubsumesConsideringDomain(nsIPrincipal* aOther) {
  84. bool subsumes = false;
  85. return NS_SUCCEEDED(SubsumesConsideringDomain(aOther, &subsumes)) && subsumes;
  86. }
  87. %}
  88. /**
  89. * Checks whether this principal is allowed to load the network resource
  90. * located at the given URI under the same-origin policy. This means that
  91. * codebase principals are only allowed to load resources from the same
  92. * domain, the system principal is allowed to load anything, and null
  93. * principals can only load URIs where they are the principal. This is
  94. * changed by the optional flag allowIfInheritsPrincipal (which defaults to
  95. * false) which allows URIs that inherit their loader's principal.
  96. *
  97. * If the load is allowed this function does nothing. If the load is not
  98. * allowed the function throws NS_ERROR_DOM_BAD_URI.
  99. *
  100. * NOTE: Other policies might override this, such as the Access-Control
  101. * specification.
  102. * NOTE: The 'domain' attribute has no effect on the behaviour of this
  103. * function.
  104. *
  105. *
  106. * @param uri The URI about to be loaded.
  107. * @param report If true, will report a warning to the console service
  108. * if the load is not allowed.
  109. * @param allowIfInheritsPrincipal If true, the load is allowed if the
  110. * loadee inherits the principal of the
  111. * loader.
  112. * @throws NS_ERROR_DOM_BAD_URI if the load is not allowed.
  113. */
  114. void checkMayLoad(in nsIURI uri, in boolean report,
  115. in boolean allowIfInheritsPrincipal);
  116. /**
  117. * A Content Security Policy associated with this principal.
  118. * Use this function to query the associated CSP with this principal.
  119. * Please *only* use this function to *set* a CSP when you know exactly what you are doing.
  120. * Most likely you want to call ensureCSP instead of setCSP.
  121. */
  122. [noscript] attribute nsIContentSecurityPolicy csp;
  123. /*
  124. * Use this function to query a CSP associated with this principal.
  125. * If no CSP is associated with this principal then one is created
  126. * internally and setRequestContext is called on the CSP using aDocument.
  127. *
  128. * Please note if aDocument is null, then setRequestContext on the
  129. * CSP object is called using the current principal.
  130. */
  131. [noscript] nsIContentSecurityPolicy ensureCSP(in nsIDOMDocument aDocument);
  132. /**
  133. * A speculative Content Security Policy associated with this
  134. * principal. Set during speculative loading (preloading) and
  135. * used *only* for preloads.
  136. *
  137. * If you want to query the CSP associated with that principal,
  138. * then this is *not* what you want. Instead query 'csp'.
  139. */
  140. [noscript] readonly attribute nsIContentSecurityPolicy preloadCsp;
  141. /*
  142. * Use this function to query a speculative CSP associated with this
  143. * principal. If no speculative CSP is associated with this principal
  144. * then one is created internally and setRequestContext is called on
  145. * the CSP using aDocument.
  146. *
  147. * Please note if aDocument is null, then setRequestContext on the
  148. * speculative CSP object is called using the current principal.
  149. */
  150. [noscript] nsIContentSecurityPolicy ensurePreloadCSP(in nsIDOMDocument aDocument);
  151. /**
  152. * The CSP of the principal in JSON notation.
  153. * Note, that the CSP itself is not exposed to JS, but script
  154. * should be able to obtain a JSON representation of the CSP.
  155. */
  156. readonly attribute AString cspJSON;
  157. /**
  158. * A dictionary of the non-default origin attributes associated with this
  159. * nsIPrincipal.
  160. *
  161. * Attributes are tokens that are taken into account when determining whether
  162. * two principals are same-origin - if any attributes differ, the principals
  163. * are cross-origin, even if the scheme, host, and port are the same.
  164. * Attributes should also be considered for all security and bucketing decisions,
  165. * even those which make non-standard comparisons (like cookies, which ignore
  166. * scheme, or quotas, which ignore subdomains).
  167. *
  168. * If you're looking for an easy-to-use canonical stringification of the origin
  169. * attributes, see |originSuffix| below.
  170. */
  171. [implicit_jscontext]
  172. readonly attribute jsval originAttributes;
  173. /**
  174. * A canonical representation of the origin for this principal. This
  175. * consists of a base string (which, for codebase principals, is of the
  176. * format scheme://host:port), concatenated with |originAttributes| (see
  177. * below).
  178. *
  179. * We maintain the invariant that principalA.equals(principalB) if and only
  180. * if principalA.origin == principalB.origin.
  181. */
  182. readonly attribute ACString origin;
  183. /**
  184. * The base part of |origin| without the concatenation with |originSuffix|.
  185. * This doesn't have the important invariants described above with |origin|,
  186. * and as such should only be used for legacy situations.
  187. */
  188. readonly attribute ACString originNoSuffix;
  189. /**
  190. * A string of the form !key1=value1&key2=value2, where each pair represents
  191. * an attribute with a non-default value. If all attributes have default
  192. * values, this is the empty string.
  193. *
  194. * The value of .originSuffix is automatically serialized into .origin, so any
  195. * consumers using that are automatically origin-attribute-aware. Consumers with
  196. * special requirements must inspect and compare .originSuffix manually.
  197. */
  198. readonly attribute AUTF8String originSuffix;
  199. /**
  200. * The base domain of the codebase URI to which this principal pertains
  201. * (generally the document URI), handling null principals and
  202. * non-hierarchical schemes correctly.
  203. */
  204. readonly attribute ACString baseDomain;
  205. const short APP_STATUS_NOT_INSTALLED = 0;
  206. const short APP_STATUS_INSTALLED = 1;
  207. const short APP_STATUS_PRIVILEGED = 2;
  208. const short APP_STATUS_CERTIFIED = 3;
  209. /**
  210. * Gets the principal's app status, which indicates whether the principal
  211. * corresponds to "app code", and if it does, how privileged that code is.
  212. * This method returns one of the APP_STATUS constants above.
  213. *
  214. * Note that a principal may have
  215. *
  216. * appId != nsIScriptSecurityManager::NO_APP_ID &&
  217. * appId != nsIScriptSecurityManager::UNKNOWN_APP_ID
  218. *
  219. * and still have appStatus == APP_STATUS_NOT_INSTALLED. That's because
  220. * appId identifies the app that contains this principal, but a window
  221. * might be contained in an app and not be running code that the app has
  222. * vouched for. For example, the window might be inside an <iframe
  223. * mozbrowser>, or the window's origin might not match the app's origin.
  224. *
  225. * If you're doing a check to determine "does this principal correspond to
  226. * app code?", you must check appStatus; checking appId != NO_APP_ID is not
  227. * sufficient.
  228. */
  229. [infallible] readonly attribute unsigned short appStatus;
  230. /**
  231. * Gets the id of the app this principal is inside. If this principal is
  232. * not inside an app, returns nsIScriptSecurityManager::NO_APP_ID.
  233. *
  234. * Note that this principal does not necessarily have the permissions of
  235. * the app identified by appId. For example, this principal might
  236. * correspond to an iframe whose origin differs from that of the app frame
  237. * containing it. In this case, the iframe will have the appId of its
  238. * containing app frame, but the iframe must not run with the app's
  239. * permissions.
  240. *
  241. * Similarly, this principal might correspond to an <iframe mozbrowser>
  242. * inside an app frame; in this case, the content inside the iframe should
  243. * not have any of the app's permissions, even if the iframe is at the same
  244. * origin as the app.
  245. *
  246. * If you're doing a security check based on appId, you must check
  247. * appStatus as well.
  248. */
  249. [infallible] readonly attribute unsigned long appId;
  250. /**
  251. * Gets the ID of the add-on this principal belongs to.
  252. */
  253. readonly attribute AString addonId;
  254. /**
  255. * Gets the id of the user context this principal is inside. If this
  256. * principal is inside the default userContext, this returns
  257. * nsIScriptSecurityManager::DEFAULT_USER_CONTEXT_ID.
  258. */
  259. [infallible] readonly attribute unsigned long userContextId;
  260. /**
  261. * Gets the id of the private browsing state of the context containing
  262. * this principal. If the principal has a private browsing value of 0, it
  263. * is not in private browsing.
  264. */
  265. [infallible] readonly attribute unsigned long privateBrowsingId;
  266. /**
  267. * Returns true iff the principal is inside an isolated mozbrowser element.
  268. * <iframe mozbrowser mozapp> and <xul:browser> are not considered to be
  269. * mozbrowser elements. <iframe mozbrowser noisolation> does not count as
  270. * isolated since isolation is disabled. Isolation can only be disabled if
  271. * the containing document is chrome.
  272. */
  273. [infallible] readonly attribute boolean isInIsolatedMozBrowserElement;
  274. /**
  275. * Returns true if this principal has an unknown appId. This shouldn't
  276. * generally be used. We only expose it due to not providing the correct
  277. * appId everywhere where we construct principals.
  278. */
  279. [infallible] readonly attribute boolean unknownAppId;
  280. /**
  281. * Returns true iff this is a null principal (corresponding to an
  282. * unknown, hence assumed minimally privileged, security context).
  283. */
  284. [infallible] readonly attribute boolean isNullPrincipal;
  285. /**
  286. * Returns true iff this principal corresponds to a codebase origin.
  287. */
  288. [infallible] readonly attribute boolean isCodebasePrincipal;
  289. /**
  290. * Returns true iff this is an expanded principal.
  291. */
  292. [infallible] readonly attribute boolean isExpandedPrincipal;
  293. /**
  294. * Returns true iff this is the system principal.
  295. */
  296. [infallible] readonly attribute boolean isSystemPrincipal;
  297. /**
  298. * Returns true if this principal's origin is recognized as being on the
  299. * whitelist of sites that can use the CSS Unprefixing Service.
  300. *
  301. * (This interface provides a trivial implementation, just returning false;
  302. * subclasses can implement something more complex as-needed.)
  303. */
  304. [noscript,notxpcom,nostdcall] bool IsOnCSSUnprefixingWhitelist();
  305. };
  306. /**
  307. * If nsSystemPrincipal is too risky to use, but we want a principal to access
  308. * more than one origin, nsExpandedPrincipals letting us define an array of
  309. * principals it subsumes. So script with an nsExpandedPrincipals will gain
  310. * same origin access when at least one of its principals it contains gained
  311. * sameorigin acccess. An nsExpandedPrincipal will be subsumed by the system
  312. * principal, and by another nsExpandedPrincipal that has all its principals.
  313. * It is added for jetpack content-scripts to let them interact with the
  314. * content and a well defined set of other domains, without the risk of
  315. * leaking out a system principal to the content. See: Bug 734891
  316. */
  317. [uuid(f3e177Df-6a5e-489f-80a7-2dd1481471d8)]
  318. interface nsIExpandedPrincipal : nsISupports
  319. {
  320. /**
  321. * An array of principals that the expanded principal subsumes.
  322. * Note: this list is not reference counted, it is shared, so
  323. * should not be changed and should only be used ephemerally.
  324. */
  325. [noscript] readonly attribute PrincipalArray whiteList;
  326. };