2015-05-12 00:25:59 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_BasePrincipal_h
|
|
|
|
#define mozilla_BasePrincipal_h
|
|
|
|
|
|
|
|
#include "nsJSPrincipals.h"
|
|
|
|
|
2016-08-15 13:22:44 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2017-03-22 20:45:40 +03:00
|
|
|
#include "mozilla/OriginAttributes.h"
|
2015-05-15 02:24:25 +03:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
class nsAtom;
|
2015-06-04 21:51:57 +03:00
|
|
|
class nsIContentSecurityPolicy;
|
2015-05-14 23:56:37 +03:00
|
|
|
class nsIObjectOutputStream;
|
|
|
|
class nsIObjectInputStream;
|
2015-11-03 04:50:54 +03:00
|
|
|
class nsIURI;
|
2015-05-14 23:56:37 +03:00
|
|
|
|
2017-03-22 13:38:17 +03:00
|
|
|
class ExpandedPrincipal;
|
2015-10-01 06:03:36 +03:00
|
|
|
|
2015-05-12 00:25:59 +03:00
|
|
|
namespace mozilla {
|
2017-09-05 21:04:43 +03:00
|
|
|
namespace extensions {
|
|
|
|
class WebExtensionPolicy;
|
|
|
|
}
|
2015-05-12 00:25:59 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Base class from which all nsIPrincipal implementations inherit. Use this for
|
|
|
|
* default implementations and other commonalities between principal
|
|
|
|
* implementations.
|
|
|
|
*
|
|
|
|
* We should merge nsJSPrincipals into this class at some point.
|
|
|
|
*/
|
|
|
|
class BasePrincipal : public nsJSPrincipals
|
|
|
|
{
|
|
|
|
public:
|
2017-02-24 05:33:40 +03:00
|
|
|
enum PrincipalKind {
|
|
|
|
eNullPrincipal,
|
|
|
|
eCodebasePrincipal,
|
|
|
|
eExpandedPrincipal,
|
|
|
|
eSystemPrincipal
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit BasePrincipal(PrincipalKind aKind);
|
2015-05-14 04:25:40 +03:00
|
|
|
|
2017-04-16 23:32:42 +03:00
|
|
|
template<typename T>
|
|
|
|
bool Is() const
|
|
|
|
{
|
|
|
|
return mKind == T::Kind();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T* As()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(Is<T>());
|
|
|
|
return static_cast<T*>(this);
|
|
|
|
}
|
|
|
|
|
2015-05-14 21:15:56 +03:00
|
|
|
enum DocumentDomainConsideration { DontConsiderDocumentDomain, ConsiderDocumentDomain};
|
|
|
|
bool Subsumes(nsIPrincipal* aOther, DocumentDomainConsideration aConsideration);
|
|
|
|
|
2018-02-06 09:46:57 +03:00
|
|
|
NS_IMETHOD GetOrigin(nsACString& aOrigin) final;
|
|
|
|
NS_IMETHOD GetOriginNoSuffix(nsACString& aOrigin) final;
|
|
|
|
NS_IMETHOD Equals(nsIPrincipal* other, bool* _retval) final;
|
|
|
|
NS_IMETHOD EqualsConsideringDomain(nsIPrincipal* other, bool* _retval) final;
|
|
|
|
NS_IMETHOD Subsumes(nsIPrincipal* other, bool* _retval) final;
|
|
|
|
NS_IMETHOD SubsumesConsideringDomain(nsIPrincipal* other, bool* _retval) final;
|
|
|
|
NS_IMETHOD SubsumesConsideringDomainIgnoringFPD(nsIPrincipal* other, bool* _retval) final;
|
|
|
|
NS_IMETHOD CheckMayLoad(nsIURI* uri, bool report, bool allowIfInheritsPrincipal) final;
|
|
|
|
NS_IMETHOD GetAddonPolicy(nsISupports** aResult) final;
|
2015-05-16 00:54:21 +03:00
|
|
|
NS_IMETHOD GetCsp(nsIContentSecurityPolicy** aCsp) override;
|
2017-01-30 16:09:37 +03:00
|
|
|
NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp) override;
|
2016-01-15 00:21:31 +03:00
|
|
|
NS_IMETHOD EnsureCSP(nsIDOMDocument* aDocument, nsIContentSecurityPolicy** aCSP) override;
|
2015-11-15 06:28:23 +03:00
|
|
|
NS_IMETHOD GetPreloadCsp(nsIContentSecurityPolicy** aPreloadCSP) override;
|
2016-01-15 00:21:31 +03:00
|
|
|
NS_IMETHOD EnsurePreloadCSP(nsIDOMDocument* aDocument, nsIContentSecurityPolicy** aCSP) override;
|
2015-05-21 21:16:04 +03:00
|
|
|
NS_IMETHOD GetCspJSON(nsAString& outCSPinJSON) override;
|
2015-10-24 03:58:21 +03:00
|
|
|
NS_IMETHOD GetIsNullPrincipal(bool* aResult) override;
|
|
|
|
NS_IMETHOD GetIsCodebasePrincipal(bool* aResult) override;
|
|
|
|
NS_IMETHOD GetIsExpandedPrincipal(bool* aResult) override;
|
|
|
|
NS_IMETHOD GetIsSystemPrincipal(bool* aResult) override;
|
2018-02-06 09:46:57 +03:00
|
|
|
NS_IMETHOD GetOriginAttributes(JSContext* aCx, JS::MutableHandle<JS::Value> aVal) final;
|
|
|
|
NS_IMETHOD GetOriginSuffix(nsACString& aOriginSuffix) final;
|
|
|
|
NS_IMETHOD GetAppId(uint32_t* aAppId) final;
|
|
|
|
NS_IMETHOD GetIsInIsolatedMozBrowserElement(bool* aIsInIsolatedMozBrowserElement) final;
|
|
|
|
NS_IMETHOD GetUserContextId(uint32_t* aUserContextId) final;
|
|
|
|
NS_IMETHOD GetPrivateBrowsingId(uint32_t* aPrivateBrowsingId) final;
|
2015-05-12 00:36:56 +03:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
virtual bool AddonHasPermission(const nsAtom* aPerm);
|
2016-07-09 03:19:17 +03:00
|
|
|
|
2015-06-03 23:40:46 +03:00
|
|
|
virtual bool IsCodebasePrincipal() const { return false; };
|
2015-06-03 11:43:43 +03:00
|
|
|
|
2015-05-14 21:15:56 +03:00
|
|
|
static BasePrincipal* Cast(nsIPrincipal* aPrin) { return static_cast<BasePrincipal*>(aPrin); }
|
2017-03-29 09:24:01 +03:00
|
|
|
|
|
|
|
static already_AddRefed<BasePrincipal>
|
|
|
|
CreateCodebasePrincipal(const nsACString& aOrigin);
|
|
|
|
|
|
|
|
// These following method may not create a codebase principal in case it's
|
|
|
|
// not possible to generate a correct origin from the passed URI. If this
|
|
|
|
// happens, a NullPrincipal is returned.
|
|
|
|
|
2015-09-23 13:19:06 +03:00
|
|
|
static already_AddRefed<BasePrincipal>
|
2017-01-12 19:38:48 +03:00
|
|
|
CreateCodebasePrincipal(nsIURI* aURI, const OriginAttributes& aAttrs);
|
2015-05-14 22:24:03 +03:00
|
|
|
|
2018-02-06 09:46:57 +03:00
|
|
|
const OriginAttributes& OriginAttributesRef() final { return mOriginAttributes; }
|
2015-05-14 22:24:03 +03:00
|
|
|
uint32_t AppId() const { return mOriginAttributes.mAppId; }
|
2017-09-05 21:04:43 +03:00
|
|
|
extensions::WebExtensionPolicy* AddonPolicy();
|
2015-07-30 21:15:00 +03:00
|
|
|
uint32_t UserContextId() const { return mOriginAttributes.mUserContextId; }
|
2016-06-29 15:01:00 +03:00
|
|
|
uint32_t PrivateBrowsingId() const { return mOriginAttributes.mPrivateBrowsingId; }
|
2016-02-05 04:42:44 +03:00
|
|
|
bool IsInIsolatedMozBrowserElement() const { return mOriginAttributes.mInIsolatedMozBrowser; }
|
2015-05-14 22:24:03 +03:00
|
|
|
|
2017-02-24 05:33:40 +03:00
|
|
|
PrincipalKind Kind() const { return mKind; }
|
2015-10-24 03:58:21 +03:00
|
|
|
|
2016-09-20 11:35:21 +03:00
|
|
|
already_AddRefed<BasePrincipal> CloneStrippingUserContextIdAndFirstPartyDomain();
|
|
|
|
|
2017-02-10 08:08:06 +03:00
|
|
|
// Helper to check whether this principal is associated with an addon that
|
|
|
|
// allows unprivileged code to load aURI. aExplicit == true will prevent
|
|
|
|
// use of all_urls permission, requiring the domain in its permissions.
|
|
|
|
bool AddonAllowsLoad(nsIURI* aURI, bool aExplicit = false);
|
|
|
|
|
2017-03-07 06:50:10 +03:00
|
|
|
// Call these to avoid the cost of virtual dispatch.
|
2017-03-07 07:04:50 +03:00
|
|
|
inline bool FastEquals(nsIPrincipal* aOther);
|
|
|
|
inline bool FastEqualsConsideringDomain(nsIPrincipal* aOther);
|
|
|
|
inline bool FastSubsumes(nsIPrincipal* aOther);
|
|
|
|
inline bool FastSubsumesConsideringDomain(nsIPrincipal* aOther);
|
|
|
|
inline bool FastSubsumesConsideringDomainIgnoringFPD(nsIPrincipal* aOther);
|
2017-02-25 02:05:44 +03:00
|
|
|
|
2017-11-03 05:56:27 +03:00
|
|
|
// Returns the principal to inherit when a caller with this principal loads
|
|
|
|
// the given URI.
|
|
|
|
//
|
|
|
|
// For most principal types, this returns the principal itself. For expanded
|
|
|
|
// principals, it returns the first sub-principal which subsumes the given URI
|
|
|
|
// (or, if no URI is given, the last whitelist principal).
|
2017-11-23 01:20:26 +03:00
|
|
|
nsIPrincipal* PrincipalToInherit(nsIURI* aRequestedURI = nullptr);
|
2017-11-03 05:56:27 +03:00
|
|
|
|
2017-10-08 00:53:30 +03:00
|
|
|
/**
|
|
|
|
* Returns true if this principal's CSP should override a document's CSP for
|
2018-02-01 16:05:05 +03:00
|
|
|
* loads that it triggers. Currently true for system principal, for expanded
|
|
|
|
* principals which subsume the document principal, and add-on codebase
|
|
|
|
* principals regardless of whether they subsume the document principal.
|
2017-10-08 00:53:30 +03:00
|
|
|
*/
|
|
|
|
bool OverridesCSP(nsIPrincipal* aDocumentPrincipal)
|
|
|
|
{
|
2018-02-01 16:05:05 +03:00
|
|
|
// SystemPrincipal can override the page's CSP by definition.
|
|
|
|
if (mKind == eSystemPrincipal) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-08 01:25:59 +03:00
|
|
|
// Expanded principals override CSP if and only if they subsume the document
|
|
|
|
// principal.
|
|
|
|
if (mKind == eExpandedPrincipal) {
|
|
|
|
return FastSubsumes(aDocumentPrincipal);
|
|
|
|
}
|
|
|
|
// Extension principals always override the CSP non-extension principals.
|
|
|
|
// This is primarily for the sake of their stylesheets, which are usually
|
|
|
|
// loaded from channels and cannot have expanded principals.
|
|
|
|
return (AddonPolicy() &&
|
|
|
|
!BasePrincipal::Cast(aDocumentPrincipal)->AddonPolicy());
|
2017-10-08 00:53:30 +03:00
|
|
|
}
|
|
|
|
|
2015-05-12 00:25:59 +03:00
|
|
|
protected:
|
2015-06-04 21:51:57 +03:00
|
|
|
virtual ~BasePrincipal();
|
2015-05-12 00:25:59 +03:00
|
|
|
|
2016-11-02 20:04:13 +03:00
|
|
|
// Note that this does not check OriginAttributes. Callers that depend on
|
|
|
|
// those must call Subsumes instead.
|
2015-05-14 21:15:56 +03:00
|
|
|
virtual bool SubsumesInternal(nsIPrincipal* aOther, DocumentDomainConsideration aConsider) = 0;
|
|
|
|
|
2015-10-01 06:03:36 +03:00
|
|
|
// Internal, side-effect-free check to determine whether the concrete
|
|
|
|
// principal would allow the load ignoring any common behavior implemented in
|
|
|
|
// BasePrincipal::CheckMayLoad.
|
|
|
|
virtual bool MayLoadInternal(nsIURI* aURI) = 0;
|
2017-03-22 13:38:17 +03:00
|
|
|
friend class ::ExpandedPrincipal;
|
2015-10-01 06:03:36 +03:00
|
|
|
|
2017-03-29 09:21:03 +03:00
|
|
|
void
|
|
|
|
SetHasExplicitDomain()
|
|
|
|
{
|
|
|
|
mHasExplicitDomain = true;
|
|
|
|
}
|
|
|
|
|
2017-02-25 01:02:24 +03:00
|
|
|
// This function should be called as the last step of the initialization of the
|
|
|
|
// principal objects. It's typically called as the last step from the Init()
|
|
|
|
// method of the child classes.
|
2017-03-29 09:22:26 +03:00
|
|
|
void FinishInit(const nsACString& aOriginNoSuffix,
|
|
|
|
const OriginAttributes& aOriginAttributes);
|
2017-02-25 01:02:24 +03:00
|
|
|
|
2015-05-12 00:25:59 +03:00
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> mCSP;
|
2015-11-15 06:28:23 +03:00
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> mPreloadCSP;
|
2017-03-29 09:22:26 +03:00
|
|
|
|
|
|
|
private:
|
2017-03-29 09:24:01 +03:00
|
|
|
static already_AddRefed<BasePrincipal>
|
|
|
|
CreateCodebasePrincipal(nsIURI* aURI, const OriginAttributes& aAttrs,
|
|
|
|
const nsACString& aOriginNoSuffix);
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> mOriginNoSuffix;
|
|
|
|
RefPtr<nsAtom> mOriginSuffix;
|
2017-03-29 09:21:03 +03:00
|
|
|
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes mOriginAttributes;
|
2017-02-24 05:33:40 +03:00
|
|
|
PrincipalKind mKind;
|
2017-03-29 09:21:03 +03:00
|
|
|
bool mHasExplicitDomain;
|
|
|
|
bool mInitialized;
|
2015-05-12 00:25:59 +03:00
|
|
|
};
|
|
|
|
|
2017-03-07 07:04:50 +03:00
|
|
|
inline bool
|
|
|
|
BasePrincipal::FastEquals(nsIPrincipal* aOther)
|
|
|
|
{
|
|
|
|
auto other = Cast(aOther);
|
|
|
|
if (Kind() != other->Kind()) {
|
|
|
|
// Principals of different kinds can't be equal.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Two principals are considered to be equal if their origins are the same.
|
|
|
|
// If the two principals are codebase principals, their origin attributes
|
|
|
|
// (aka the origin suffix) must also match.
|
|
|
|
// If the two principals are null principals, they're only equal if they're
|
|
|
|
// the same object.
|
|
|
|
if (Kind() == eNullPrincipal || Kind() == eSystemPrincipal) {
|
|
|
|
return this == other;
|
|
|
|
}
|
|
|
|
|
2017-03-29 09:24:01 +03:00
|
|
|
if (Kind() == eCodebasePrincipal) {
|
|
|
|
return mOriginNoSuffix == other->mOriginNoSuffix &&
|
|
|
|
mOriginSuffix == other->mOriginSuffix;
|
2017-03-07 07:04:50 +03:00
|
|
|
}
|
|
|
|
|
2017-03-29 09:24:01 +03:00
|
|
|
MOZ_ASSERT(Kind() == eExpandedPrincipal);
|
|
|
|
return mOriginNoSuffix == other->mOriginNoSuffix;
|
2017-03-07 07:04:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
BasePrincipal::FastEqualsConsideringDomain(nsIPrincipal* aOther)
|
|
|
|
{
|
|
|
|
// If neither of the principals have document.domain set, we use the fast path
|
|
|
|
// in Equals(). Otherwise, we fall back to the slow path below.
|
|
|
|
auto other = Cast(aOther);
|
2017-03-29 09:21:03 +03:00
|
|
|
if (!mHasExplicitDomain && !other->mHasExplicitDomain) {
|
2017-03-07 07:04:50 +03:00
|
|
|
return FastEquals(aOther);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Subsumes(aOther, ConsiderDocumentDomain) &&
|
|
|
|
other->Subsumes(this, ConsiderDocumentDomain);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
BasePrincipal::FastSubsumes(nsIPrincipal* aOther)
|
|
|
|
{
|
|
|
|
// If two principals are equal, then they both subsume each other.
|
|
|
|
// We deal with two special cases first:
|
|
|
|
// Null principals only subsume each other if they are equal, and are only
|
|
|
|
// equal if they're the same object.
|
|
|
|
auto other = Cast(aOther);
|
|
|
|
if (Kind() == eNullPrincipal && other->Kind() == eNullPrincipal) {
|
|
|
|
return this == other;
|
|
|
|
}
|
2017-03-29 09:24:01 +03:00
|
|
|
if (FastEquals(aOther)) {
|
2017-03-07 07:04:50 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, fall back to the slow path.
|
|
|
|
return Subsumes(aOther, DontConsiderDocumentDomain);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
BasePrincipal::FastSubsumesConsideringDomain(nsIPrincipal* aOther)
|
|
|
|
{
|
|
|
|
// If neither of the principals have document.domain set, we hand off to
|
|
|
|
// FastSubsumes() which has fast paths for some special cases. Otherwise, we fall
|
|
|
|
// back to the slow path below.
|
2017-03-29 09:21:03 +03:00
|
|
|
if (!mHasExplicitDomain && !Cast(aOther)->mHasExplicitDomain) {
|
2017-03-07 07:04:50 +03:00
|
|
|
return FastSubsumes(aOther);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Subsumes(aOther, ConsiderDocumentDomain);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
BasePrincipal::FastSubsumesConsideringDomainIgnoringFPD(nsIPrincipal* aOther)
|
|
|
|
{
|
|
|
|
if (Kind() == eCodebasePrincipal &&
|
|
|
|
!dom::ChromeUtils::IsOriginAttributesEqualIgnoringFPD(
|
|
|
|
mOriginAttributes, Cast(aOther)->mOriginAttributes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SubsumesInternal(aOther, ConsiderDocumentDomain);
|
|
|
|
}
|
|
|
|
|
2015-05-12 00:25:59 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_BasePrincipal_h */
|