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/. */
6 /* Defines the abstract interface for a principal. */
8 #include
"nsIContentSecurityPolicy.idl"
9 #include
"nsISerializable.idl"
10 #include
"nsIAboutModule.idl"
11 #include
"nsIReferrerInfo.idl"
13 #include
"mozIDOMWindow.idl"
20 #include
"mozilla/DebugOnly.h"
22 class OriginAttributes
;
26 * Some methods have a fast path for the case when we're comparing a principal
27 * to itself. The situation may happen for example with about:blank documents.
30 #define DECL_FAST_INLINE_HELPER
(method_
) \
31 inline bool method_
(nsIPrincipal
* aOther
) \
33 mozilla
::DebugOnly
<bool
> val
= false
; \
34 MOZ_ASSERT_IF
(this
== aOther
, \
35 NS_SUCCEEDED
(method_
(aOther
, &val
)) && val
); \
37 bool
retVal = false
; \
40 (NS_SUCCEEDED
(method_
(aOther
, &retVal)) && retVal); \
47 webidl WebExtensionPolicy
;
49 [ptr] native JSContext
(JSContext
);
50 [ptr] native JSPrincipals
(JSPrincipals
);
51 [ref] native PrincipalArray
(const nsTArray
<nsCOMPtr
<nsIPrincipal
>>);
52 [ref] native const_OriginAttributes
(const mozilla
::OriginAttributes
);
53 native ReferrerPolicy
(mozilla
::dom
::ReferrerPolicy
);
55 [scriptable
, builtinclass
, uuid(f75f502d
-79fd
-48be
-a079
-e5a7b8f80c8b
)]
56 interface nsIPrincipal
: nsISupports
59 * Returns whether the other principal is equivalent to this principal.
60 * Principals are considered equal if they are the same principal, or
61 * they have the same origin.
63 * May be called from any thread.
65 boolean equals
(in nsIPrincipal other
);
68 * Returns whether the other principal is equivalent to this principal
69 * for permission purposes
70 * Matches {originAttributes ,equalsURIForPermission}
72 * May be called from any thread.
75 boolean equalsForPermission
(in nsIPrincipal other
, in bool aExactHost
);
78 * Like equals, but takes document.domain changes into account.
80 * May be called from any thread, though document.domain may racily change
81 * during the comparison when called from off-main-thread.
83 boolean equalsConsideringDomain
(in nsIPrincipal other
);
86 DECL_FAST_INLINE_HELPER
(Equals
)
87 DECL_FAST_INLINE_HELPER
(EqualsConsideringDomain
)
91 * Returns whether the Principals URI is equal to the other URI
93 * May be called from any thread.
95 boolean equalsURI
(in nsIURI aOtherURI
);
98 * Returns a hash value for the principal.
100 * May be called from any thread.
102 [notxpcom
, nostdcall
] readonly attribute
unsigned long hashValue
;
105 * The principal URI to which this principal pertains. This is
106 * generally the document URI.
108 * May be called from any thread.
110 [infallible
] readonly attribute nsIURI URI
;
113 * The domain URI to which this principal pertains.
114 * This is null unless script successfully sets document.domain to our URI
115 * or a superdomain of our URI.
116 * Setting this has no effect on the URI.
117 * See https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy#Changing_origin
119 * The getter may be called from any thread, but may only be set on the main thread.
121 [noscript
] attribute nsIURI domain
;
124 * Returns whether the other principal is equal to or weaker than this
125 * principal. Principals are equal if they are the same object or they
126 * have the same origin.
128 * Thus a principal always subsumes itself.
130 * The system principal subsumes itself and all other principals.
132 * A null principal (corresponding to an unknown, hence assumed minimally
133 * privileged, security context) is not equal to any other principal
134 * (including other null principals), and therefore does not subsume
135 * anything but itself.
137 * May be called from any thread.
139 boolean subsumes
(in nsIPrincipal other
);
142 * Same as the previous method, subsumes(), but takes document.domain into
145 * May be called from any thread, though document.domain may racily change
146 * during the comparison when called from off-main-thread.
148 boolean subsumesConsideringDomain
(in nsIPrincipal other
);
151 * Same as the subsumesConsideringDomain(), but ignores the first party
152 * domain in its originAttributes.
154 * May be called from any thread, though document.domain may racily change
155 * during the comparison when called from off-main-thread.
157 boolean subsumesConsideringDomainIgnoringFPD
(in nsIPrincipal other
);
160 DECL_FAST_INLINE_HELPER
(Subsumes
)
161 DECL_FAST_INLINE_HELPER
(SubsumesConsideringDomain
)
162 DECL_FAST_INLINE_HELPER
(SubsumesConsideringDomainIgnoringFPD
)
163 #undef DECL_FAST_INLINE_HELPER
167 * Checks whether this principal is allowed to load the network resource
168 * located at the given URI under the same-origin policy. This means that
169 * content principals are only allowed to load resources from the same
170 * domain, the system principal is allowed to load anything, and null
171 * principals can only load URIs where they are the principal. This is
172 * changed by the optional flag allowIfInheritsPrincipal (which defaults to
173 * false) which allows URIs that inherit their loader's principal.
175 * If the load is allowed this function does nothing. If the load is not
176 * allowed the function throws NS_ERROR_DOM_BAD_URI.
178 * NOTE: Other policies might override this, such as the Access-Control
180 * NOTE: The 'domain' attribute has no effect on the behaviour of this
182 * NOTE: Main-Thread Only.
185 * @param uri The URI about to be loaded.
186 * @param allowIfInheritsPrincipal If true, the load is allowed if the
187 * loadee inherits the principal of the
189 * @throws NS_ERROR_DOM_BAD_URI if the load is not allowed.
191 void checkMayLoad
(in nsIURI uri
,
192 in boolean allowIfInheritsPrincipal
);
195 * Like checkMayLoad, but if returning an error will also report that error
196 * to the console, using the provided window id. The window id may be 0 to
197 * report to just the browser console, not web consoles.
199 * NOTE: Main-Thread Only.
201 void checkMayLoadWithReporting
(in nsIURI uri
,
202 in boolean allowIfInheritsPrincipal
,
203 in unsigned long long innerWindowID
);
206 * Checks if the provided URI is considered third-party to the
207 * URI of the principal.
208 * Returns true if the URI is third-party.
210 * May be called from any thread.
212 * @param uri - The URI to check
214 boolean isThirdPartyURI
(in nsIURI uri
);
217 * Checks if the provided principal is considered third-party to the
218 * URI of the Principal.
219 * Returns true if the principal is third-party.
221 * May be called from any thread.
223 * @param principal - The principal to check
225 boolean isThirdPartyPrincipal
(in nsIPrincipal principal
);
228 * Checks if the provided channel is considered third-party to the
229 * URI of the principal.
230 * Returns true if the channel is third-party.
231 * Returns false if the Principal is a System Principal
233 * NOTE: Main-Thread Only.
235 * @param channel - The Channel to check
237 boolean isThirdPartyChannel
(in nsIChannel channel
);
240 * A dictionary of the non-default origin attributes associated with this
243 * Attributes are tokens that are taken into account when determining whether
244 * two principals are same-origin - if any attributes differ, the principals
245 * are cross-origin, even if the scheme, host, and port are the same.
246 * Attributes should also be considered for all security and bucketing decisions,
247 * even those which make non-standard comparisons (like cookies, which ignore
248 * scheme, or quotas, which ignore subdomains).
250 * If you're looking for an easy-to-use canonical stringification of the origin
251 * attributes, see |originSuffix| below.
254 readonly attribute jsval originAttributes
;
256 // May be called from any thread.
257 [noscript
, notxpcom
, nostdcall
, binaryname
(OriginAttributesRef
)]
258 const_OriginAttributes OriginAttributesRef
();
261 * A canonical representation of the origin for this principal. This
262 * consists of a base string (which, for content principals, is of the
263 * format scheme://host:port), concatenated with |originAttributes| (see
266 * We maintain the invariant that principalA.equals(principalB) if and only
267 * if principalA.origin == principalB.origin.
269 * May be called from any thread.
271 readonly attribute ACString origin
;
274 * Returns an ASCII compatible representation
275 * of the principals Origin
277 * May be called from any thread.
279 [noscript
] readonly attribute ACString asciiOrigin
;
282 * Returns the "host:port" portion of the
283 * Principals URI, if any.
285 * May be called from any thread.
287 readonly attribute ACString hostPort
;
290 * Returns the "host:port" portion of the
291 * Principals URI, if any.
293 * May be called from any thread.
295 readonly attribute ACString asciiHost
;
298 * Returns the "host" portion of the
299 * Principals URI, if any.
301 * May be called from any thread.
303 readonly attribute ACString host
;
306 * Returns the prePath of the principals uri
307 * follows the format scheme:
308 * "scheme://username:password@hostname:portnumber/"
310 * May be called from any thread.
312 readonly attribute ACString prePath
;
315 * Returns the filePath of the principals uri. See nsIURI.
317 * May be called from any thread.
319 readonly attribute ACString filePath
;
322 * Returns the ASCII Spec from the Principals URI.
323 * Might return the empty string, e.g. for the case of
324 * a SystemPrincipal or an EpxandedPrincipal.
326 * May be called from any thread.
328 * WARNING: DO NOT USE FOR SECURITY CHECKS.
329 * just for logging purposes!
331 readonly attribute ACString asciiSpec
;
334 * Returns the Spec from the Principals URI.
335 * Might return the empty string, e.g. for the case of
336 * a SystemPrincipal or an EpxandedPrincipal.
338 * May be called from any thread.
340 * WARNING: Do not land new Code using, as this will be removed soon
342 readonly attribute ACString spec
;
345 * Returns the Pre Path of the Principals URI with
346 * user:pass stripped for privacy and spoof prevention
348 * May be called from any thread.
350 readonly attribute ACString exposablePrePath
;
353 * Returns the Spec of the Principals URI with
354 * user/pass/ref/query stripped for privacy and spoof prevention
356 * May be called from any thread.
358 readonly attribute ACString exposableSpec
;
361 * Return the scheme of the principals URI
363 * May be called from any thread.
365 readonly attribute ACString scheme
;
368 * Checks if the Principal's URI Scheme matches with the parameter
370 * May be called from any thread.
372 * @param scheme The scheme to be checked
375 boolean schemeIs
(in string scheme
);
378 * Checks if the Principal's URI is contained in the given Pref
380 * NOTE: Main-Thread Only.
382 * @param pref The pref to be checked
385 boolean isURIInPrefList
(in string pref
);
388 * Check if the Principal's URI is contained in the given list
390 * May be called from any thread.
392 * @param list The list to be checked
395 boolean isURIInList
(in ACString list
);
398 * Uses NS_Security Compare to determine if the
399 * other URI is same-origin as the uri of the Principal
401 * May be called from any thread.
404 boolean isSameOrigin
(in nsIURI otherURI
);
407 * Checks if the Principal is allowed to load the Provided file:// URI
408 * using NS_RelaxStrictFileOriginPolicy
410 * May be called from any thread.
412 bool allowsRelaxStrictFileOriginPolicy
(in nsIURI aURI
);
416 * Generates a Cache-Key for the Cors-Preflight Cache
418 * May be called from any thread.
421 ACString getPrefLightCacheKey
(in nsIURI aURI
,in bool aWithCredentials
,
422 in const_OriginAttributes aOriginAttributes
);
426 * Checks if the Principals URI has first party storage access
427 * when loaded inside the provided 3rd party resource window.
428 * See also: ContentBlocking::ShouldAllowAccessFor
430 * NOTE: Main-Thread Only.
432 bool hasFirstpartyStorageAccess
(in mozIDOMWindow aWindow
, out uint32_t rejectedReason
);
436 * Returns a Key for the LocalStorage Manager, used to
437 * check the Principals Origin Storage usage.
439 * May be called from any thread.
441 readonly attribute ACString localStorageQuotaKey
;
445 * https://w3c.github.io/webappsec-secure-contexts/#is-origin-trustworthy
447 * The value returned by this method feeds into the the Secure Context
448 * algorithm that determins the value of Window.isSecureContext and
449 * WorkerGlobalScope.isSecureContext.
451 * This method returns false instead of throwing upon errors.
453 * NOTE: Main-Thread Only.
456 readonly attribute
boolean isOriginPotentiallyTrustworthy
;
459 * NOTE: Main-Thread Only.
462 readonly attribute
boolean isLoopbackHost
;
465 * Returns the Flags of the Principals
466 * associated AboutModule, in case there is one.
468 * NOTE: Main-Thread Only.
470 uint32_t getAboutModuleFlags
();
473 * Returns the Key to access the Principals
474 * Origin Local/Session Storage
476 * May be called from any thread.
478 readonly attribute ACString storageOriginKey
;
481 * Creates and Returns a new ReferrerInfo with the
484 * May be called from any thread.
486 nsIReferrerInfo createReferrerInfo
(in ReferrerPolicy aReferrerPolicy
);
489 * The base part of |origin| without the concatenation with |originSuffix|.
490 * This doesn't have the important invariants described above with |origin|,
491 * and as such should only be used for legacy situations.
493 * May be called from any thread.
495 readonly attribute ACString originNoSuffix
;
498 * A string of the form ^key1=value1&key2=value2, where each pair represents
499 * an attribute with a non-default value. If all attributes have default
500 * values, this is the empty string.
502 * The value of .originSuffix is automatically serialized into .origin, so any
503 * consumers using that are automatically origin-attribute-aware. Consumers with
504 * special requirements must inspect and compare .originSuffix manually.
506 * May be called from any thread.
508 readonly attribute AUTF8String originSuffix
;
511 * A canonical representation of the site-origin for this principal.
512 * This string has the same format as |origin| (see above). Two principals
513 * with differing |siteOrigin| values will never compare equal, even when
514 * considering domain mutations.
516 * For most principals, |siteOrigin| matches |origin| precisely. Only
517 * principals which allow mutating |domain|, such as ContentPrincipal,
518 * override the default implementation in BasePrincipal.
520 * May be called from any thread.
522 readonly attribute ACString siteOrigin
;
525 * The base part of |siteOrigin| without the concatenation with
528 * May be called from any thread.
530 readonly attribute ACString siteOriginNoSuffix
;
533 * The base domain of the principal URI to which this principal pertains
534 * (generally the document URI), handling null principals and
535 * non-hierarchical schemes correctly.
537 * May be called from any thread.
539 readonly attribute ACString baseDomain
;
542 * Gets the ID of the add-on this principal belongs to.
544 * May be called from any thread.
546 readonly attribute AString addonId
;
549 * Gets the WebExtensionPolicy of the add-on this principal belongs to.
551 * NOTE: Main-Thread Only.
553 readonly attribute WebExtensionPolicy addonPolicy
;
556 * Gets the id of the user context this principal is inside. If this
557 * principal is inside the default userContext, this returns
558 * nsIScriptSecurityManager::DEFAULT_USER_CONTEXT_ID.
560 * May be called from any thread.
562 [infallible
] readonly attribute
unsigned long userContextId
;
565 * Gets the id of the private browsing state of the context containing
566 * this principal. If the principal has a private browsing value of 0, it
567 * is not in private browsing.
569 * May be called from any thread.
571 [infallible
] readonly attribute
unsigned long privateBrowsingId
;
574 * Returns true iff the principal is inside an isolated mozbrowser element.
575 * <xul:browser> is not considered to be a mozbrowser element.
576 * <iframe mozbrowser noisolation> does not count as isolated since
577 * isolation is disabled. Isolation can only be disabled if the
578 * containing document is chrome.
580 * May be called from any thread.
582 [infallible
] readonly attribute
boolean isInIsolatedMozBrowserElement
;
585 * Returns true iff this is a null principal (corresponding to an
586 * unknown, hence assumed minimally privileged, security context).
588 * May be called from any thread.
590 [infallible
] readonly attribute
boolean isNullPrincipal
;
593 * Returns true iff this principal corresponds to a principal origin.
595 * May be called from any thread.
597 [infallible
] readonly attribute
boolean isContentPrincipal
;
600 * Returns true iff this is an expanded principal.
602 * May be called from any thread.
604 [infallible
] readonly attribute
boolean isExpandedPrincipal
;
607 * Returns true iff this is the system principal. C++ callers should use
608 * IsSystemPrincipal() instead of this scriptable accessor.
610 * May be called from any thread.
612 readonly attribute
boolean isSystemPrincipal
;
615 * Faster and nicer version callable from C++. Callers must include
616 * BasePrincipal.h, where it's implemented.
618 * May be called from any thread.
621 inline bool IsSystemPrincipal
() const;
625 * Returns true iff the principal is either an addon principal or
626 * an expanded principal, which contains at least one addon principal.
628 * May be called from any thread.
630 [infallible
] readonly attribute
boolean isAddonOrExpandedAddonPrincipal
;
633 // MOZ_DBG support (threadsafe)
634 friend std
::ostream
& operator
<<(std
::ostream
& aOut
, const nsIPrincipal
& aPrincipal
) {
635 nsIPrincipal
* principal
= const_cast
<nsIPrincipal
*>(&aPrincipal
);
636 nsAutoCString origin
;
637 mozilla
::DebugOnly
<nsresult
> rv
= principal
->GetOrigin
(origin
);
638 MOZ_ASSERT
(NS_SUCCEEDED
(rv
));
639 return aOut
<< "nsIPrincipal { " << origin
<< " }";
644 * Returns true if the URI is an Onion URI.
646 * May be called from any thread.
648 [infallible
] readonly attribute
boolean isOnion
;
651 * Returns true if the Domain Policy allows js execution
652 * for the Principal's URI
654 * NOTE: Main-Thread Only.
656 readonly attribute
boolean isScriptAllowedByPolicy
;
660 * Returns true if the Principal can acess l10n
661 * features for the Provided DocumentURI
663 * NOTE: Main-Thread Only.
665 boolean isL10nAllowed
(in nsIURI aDocumentURI
);
668 * Returns a nsIPrincipal, with one less Subdomain Segment
669 * Returns `nullptr` if there are no more segments to remove.
671 * May be called from any thread.
673 [infallible
] readonly attribute nsIPrincipal nextSubDomainPrincipal
;
676 * Returns if the principal is for an IP address.
678 * May be called from any thread.
680 [infallible
] readonly attribute
boolean isIpAddress
;
683 * Returns if the principal is for a local IP address.
685 * May be called from any thread.
687 [infallible
] readonly attribute
boolean isLocalIpAddress
;
690 * If this principal is a null principal, reconstruct the precursor
691 * principal which this null principal was derived from. This may be null,
692 * in which case this is not a null principal, there is no known precursor
693 * to this null principal, it was created by a privileged context, or there
694 * was a bugged origin in the precursor string.
696 * May be called from any thread.
698 * WARNING: Be careful when using this principal, as it is not part of the
699 * security properties of the null principal, and should NOT be used to
700 * grant a resource with a null principal access to resources from its
701 * precursor origin. This is only to be used for places where tracking how
702 * null principals were created is necessary.
704 [infallible
] readonly attribute nsIPrincipal precursorPrincipal
;
708 * If SystemPrincipal is too risky to use, but we want a principal to access
709 * more than one origin, ExpandedPrincipals letting us define an array of
710 * principals it subsumes. So script with an ExpandedPrincipals will gain
711 * same origin access when at least one of its principals it contains gained
712 * sameorigin acccess. An ExpandedPrincipal will be subsumed by the system
713 * principal, and by another ExpandedPrincipal that has all its principals.
714 * It is added for jetpack content-scripts to let them interact with the
715 * content and a well defined set of other domains, without the risk of
716 * leaking out a system principal to the content. See: Bug 734891
718 [uuid(f3e177Df
-6a5e
-489f
-80a7
-2dd1481471d8
)]
719 interface nsIExpandedPrincipal
: nsISupports
722 * An array of principals that the expanded principal subsumes.
724 * When an expanded principal is used as a triggering principal for a
725 * request that inherits a security context, one of its constitutent
726 * principals is inherited rather than the expanded principal itself. The
727 * last principal in the allowlist is the default principal to inherit.
729 * Note: this list is not reference counted, it is shared, so
730 * should not be changed and should only be used ephemerally.
732 * May be called from any thread.
734 [noscript
, notxpcom
, nostdcall
]
735 PrincipalArray AllowList
();
739 * Bug 1548468: Move CSP off ExpandedPrincipal.
741 * A Content Security Policy associated with this principal. Use this function
742 * to query the associated CSP with this principal.
744 * NOTE: Main-Thread Only.
746 readonly attribute nsIContentSecurityPolicy csp
;
749 inline already_AddRefed
<nsIContentSecurityPolicy
> GetCsp
()
751 nsCOMPtr
<nsIContentSecurityPolicy
> result
;
752 mozilla
::DebugOnly
<nsresult
> rv
= GetCsp
(getter_AddRefs
(result
));
753 MOZ_ASSERT
(NS_SUCCEEDED
(rv
));
754 return result.forget
();