2015-05-11 21:25:59 +00: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 "nsIPrincipal.h"
|
2015-05-14 01:25:40 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2015-05-11 21:25:59 +00:00
|
|
|
#include "nsJSPrincipals.h"
|
|
|
|
|
2015-06-03 06:35:09 +00:00
|
|
|
#include "mozilla/dom/ChromeUtilsBinding.h"
|
2015-05-14 23:24:25 +00:00
|
|
|
|
2015-06-04 18:51:57 +00:00
|
|
|
class nsIContentSecurityPolicy;
|
2015-09-23 08:10:21 +00:00
|
|
|
class nsILoadContext;
|
2015-05-14 20:56:37 +00:00
|
|
|
class nsIObjectOutputStream;
|
|
|
|
class nsIObjectInputStream;
|
|
|
|
|
2015-10-01 03:03:36 +00:00
|
|
|
class nsExpandedPrincipal;
|
|
|
|
|
2015-05-11 21:25:59 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2015-05-15 20:43:11 +00:00
|
|
|
class OriginAttributes : public dom::OriginAttributesDictionary
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OriginAttributes() {}
|
|
|
|
OriginAttributes(uint32_t aAppId, bool aInBrowser)
|
|
|
|
{
|
|
|
|
mAppId = aAppId;
|
|
|
|
mInBrowser = aInBrowser;
|
|
|
|
}
|
2015-06-03 06:38:55 +00:00
|
|
|
explicit OriginAttributes(const OriginAttributesDictionary& aOther)
|
|
|
|
: OriginAttributesDictionary(aOther) {}
|
2015-05-15 20:43:11 +00:00
|
|
|
|
|
|
|
bool operator==(const OriginAttributes& aOther) const
|
|
|
|
{
|
|
|
|
return mAppId == aOther.mAppId &&
|
2015-07-07 22:53:15 +00:00
|
|
|
mInBrowser == aOther.mInBrowser &&
|
2015-07-28 21:32:00 +00:00
|
|
|
mAddonId == aOther.mAddonId &&
|
2015-09-18 07:11:58 +00:00
|
|
|
mUserContextId == aOther.mUserContextId &&
|
|
|
|
mSignedPkg == aOther.mSignedPkg;
|
2015-05-15 20:43:11 +00:00
|
|
|
}
|
|
|
|
bool operator!=(const OriginAttributes& aOther) const
|
|
|
|
{
|
|
|
|
return !(*this == aOther);
|
|
|
|
}
|
|
|
|
|
2015-09-23 08:10:21 +00:00
|
|
|
// The docshell often influences the origin attributes of content loaded
|
|
|
|
// inside of it, and in some cases also influences the origin attributes of
|
|
|
|
// content loaded in child docshells. We say that a given attribute "lives on
|
|
|
|
// the docshell" to indicate that this attribute is specified by the docshell
|
|
|
|
// (if any) associated with a given content document.
|
|
|
|
//
|
|
|
|
// In practice, this usually means that we need to store a copy of those
|
|
|
|
// attributes on each docshell, or provide methods on the docshell to compute
|
|
|
|
// them on-demand.
|
|
|
|
// We could track each of these attributes individually, but since the
|
|
|
|
// majority of the existing origin attributes currently live on the docshell,
|
|
|
|
// it's cleaner to simply store an entire OriginAttributes struct on each
|
|
|
|
// docshell, and selectively copy them to child docshells and content
|
|
|
|
// principals in a manner that implements our desired semantics.
|
|
|
|
//
|
|
|
|
// This method is used to propagate attributes from parent to child
|
|
|
|
// docshells.
|
|
|
|
void InheritFromDocShellParent(const OriginAttributes& aParent);
|
|
|
|
|
|
|
|
// Copy from the origin attributes of the nsILoadContext.
|
|
|
|
bool CopyFromLoadContext(nsILoadContext* aLoadContext);
|
|
|
|
|
2015-06-04 18:51:57 +00:00
|
|
|
// Serializes/Deserializes non-default values into the suffix format, i.e.
|
2015-05-15 20:43:11 +00:00
|
|
|
// |!key1=value1&key2=value2|. If there are no non-default attributes, this
|
|
|
|
// returns an empty string.
|
2015-06-03 08:43:43 +00:00
|
|
|
void CreateSuffix(nsACString& aStr) const;
|
2015-06-04 18:51:57 +00:00
|
|
|
bool PopulateFromSuffix(const nsACString& aStr);
|
2015-06-03 06:38:55 +00:00
|
|
|
|
2015-06-05 04:39:34 +00:00
|
|
|
// Populates the attributes from a string like
|
|
|
|
// |uri!key1=value1&key2=value2| and returns the uri without the suffix.
|
|
|
|
bool PopulateFromOrigin(const nsACString& aOrigin,
|
|
|
|
nsACString& aOriginNoSuffix);
|
2015-05-15 20:43:11 +00:00
|
|
|
};
|
|
|
|
|
2015-07-09 18:36:35 +00:00
|
|
|
class OriginAttributesPattern : public dom::OriginAttributesPatternDictionary
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// To convert a JSON string to an OriginAttributesPattern, do the following:
|
|
|
|
//
|
|
|
|
// OriginAttributesPattern pattern;
|
|
|
|
// if (!pattern.Init(aJSONString)) {
|
|
|
|
// ... // handle failure.
|
|
|
|
// }
|
|
|
|
OriginAttributesPattern() {}
|
|
|
|
|
|
|
|
explicit OriginAttributesPattern(const OriginAttributesPatternDictionary& aOther)
|
|
|
|
: OriginAttributesPatternDictionary(aOther) {}
|
|
|
|
|
|
|
|
// Performs a match of |aAttrs| against this pattern.
|
|
|
|
bool Matches(const OriginAttributes& aAttrs) const
|
|
|
|
{
|
|
|
|
if (mAppId.WasPassed() && mAppId.Value() != aAttrs.mAppId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mInBrowser.WasPassed() && mInBrowser.Value() != aAttrs.mInBrowser) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mAddonId.WasPassed() && mAddonId.Value() != aAttrs.mAddonId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-28 21:32:00 +00:00
|
|
|
if (mUserContextId.WasPassed() && mUserContextId.Value() != aAttrs.mUserContextId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-18 07:11:58 +00:00
|
|
|
if (mSignedPkg.WasPassed() && mSignedPkg.Value() != aAttrs.mSignedPkg) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-09 18:36:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-11 21:25:59 +00: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:
|
2015-06-04 18:51:57 +00:00
|
|
|
BasePrincipal();
|
2015-05-14 01:25:40 +00:00
|
|
|
|
2015-05-14 18:15:56 +00:00
|
|
|
enum DocumentDomainConsideration { DontConsiderDocumentDomain, ConsiderDocumentDomain};
|
|
|
|
bool Subsumes(nsIPrincipal* aOther, DocumentDomainConsideration aConsideration);
|
|
|
|
|
2015-05-14 23:50:44 +00:00
|
|
|
NS_IMETHOD GetOrigin(nsACString& aOrigin) final;
|
2015-05-14 23:55:52 +00:00
|
|
|
NS_IMETHOD GetOriginNoSuffix(nsACString& aOrigin) final;
|
2015-05-14 18:15:56 +00:00
|
|
|
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;
|
2015-10-01 03:03:36 +00:00
|
|
|
NS_IMETHOD CheckMayLoad(nsIURI* uri, bool report, bool allowIfInheritsPrincipal) final;
|
2015-05-15 21:54:21 +00:00
|
|
|
NS_IMETHOD GetCsp(nsIContentSecurityPolicy** aCsp) override;
|
|
|
|
NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp) override;
|
2015-05-21 18:16:04 +00:00
|
|
|
NS_IMETHOD GetCspJSON(nsAString& outCSPinJSON) override;
|
2015-10-24 00:58:21 +00: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;
|
2015-05-14 01:25:40 +00:00
|
|
|
NS_IMETHOD GetJarPrefix(nsACString& aJarPrefix) final;
|
2015-05-14 23:24:25 +00:00
|
|
|
NS_IMETHOD GetOriginAttributes(JSContext* aCx, JS::MutableHandle<JS::Value> aVal) final;
|
|
|
|
NS_IMETHOD GetOriginSuffix(nsACString& aOriginSuffix) final;
|
2015-05-14 01:25:40 +00:00
|
|
|
NS_IMETHOD GetAppStatus(uint16_t* aAppStatus) final;
|
|
|
|
NS_IMETHOD GetAppId(uint32_t* aAppStatus) final;
|
|
|
|
NS_IMETHOD GetIsInBrowserElement(bool* aIsInBrowserElement) final;
|
|
|
|
NS_IMETHOD GetUnknownAppId(bool* aUnknownAppId) final;
|
2015-07-30 18:15:00 +00:00
|
|
|
NS_IMETHOD GetUserContextId(uint32_t* aUserContextId) final;
|
2015-05-11 21:36:56 +00:00
|
|
|
|
|
|
|
virtual bool IsOnCSSUnprefixingWhitelist() override { return false; }
|
2015-05-11 21:25:59 +00:00
|
|
|
|
2015-06-03 20:40:46 +00:00
|
|
|
virtual bool IsCodebasePrincipal() const { return false; };
|
2015-06-03 08:43:43 +00:00
|
|
|
|
2015-05-14 18:15:56 +00:00
|
|
|
static BasePrincipal* Cast(nsIPrincipal* aPrin) { return static_cast<BasePrincipal*>(aPrin); }
|
2015-09-23 10:19:06 +00:00
|
|
|
static already_AddRefed<BasePrincipal>
|
|
|
|
CreateCodebasePrincipal(nsIURI* aURI, const OriginAttributes& aAttrs);
|
2015-08-26 11:12:13 +00:00
|
|
|
static already_AddRefed<BasePrincipal> CreateCodebasePrincipal(const nsACString& aOrigin);
|
2015-05-14 19:24:03 +00:00
|
|
|
|
|
|
|
const OriginAttributes& OriginAttributesRef() { return mOriginAttributes; }
|
|
|
|
uint32_t AppId() const { return mOriginAttributes.mAppId; }
|
2015-07-30 18:15:00 +00:00
|
|
|
uint32_t UserContextId() const { return mOriginAttributes.mUserContextId; }
|
2015-05-14 23:24:25 +00:00
|
|
|
bool IsInBrowserElement() const { return mOriginAttributes.mInBrowser; }
|
2015-05-14 19:24:03 +00:00
|
|
|
|
2015-10-24 00:58:21 +00:00
|
|
|
enum PrincipalKind {
|
|
|
|
eNullPrincipal,
|
|
|
|
eCodebasePrincipal,
|
|
|
|
eExpandedPrincipal,
|
|
|
|
eSystemPrincipal
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual PrincipalKind Kind() = 0;
|
|
|
|
|
2015-05-11 21:25:59 +00:00
|
|
|
protected:
|
2015-06-04 18:51:57 +00:00
|
|
|
virtual ~BasePrincipal();
|
2015-05-11 21:25:59 +00:00
|
|
|
|
2015-05-14 23:50:44 +00:00
|
|
|
virtual nsresult GetOriginInternal(nsACString& aOrigin) = 0;
|
2015-05-14 18:15:56 +00:00
|
|
|
virtual bool SubsumesInternal(nsIPrincipal* aOther, DocumentDomainConsideration aConsider) = 0;
|
|
|
|
|
2015-10-01 03:03:36 +00: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;
|
|
|
|
friend class ::nsExpandedPrincipal;
|
|
|
|
|
2015-07-08 00:53:15 +00:00
|
|
|
// Helper to check whether this principal is associated with an addon that
|
|
|
|
// allows unprivileged code to load aURI.
|
|
|
|
bool AddonAllowsLoad(nsIURI* aURI);
|
|
|
|
|
2015-05-11 21:25:59 +00:00
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> mCSP;
|
2015-05-14 19:24:03 +00:00
|
|
|
OriginAttributes mOriginAttributes;
|
2015-05-11 21:25:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_BasePrincipal_h */
|