1 /* -*- Mode: C++; tab-width: 2; 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/. */
7 * This is the principal that has no rights and can't be accessed by
8 * anything other than itself and chrome; null principals are not
9 * same-origin with anything but themselves.
12 #ifndef mozilla_NullPrincipal_h
13 #define mozilla_NullPrincipal_h
15 #include "nsIPrincipal.h"
16 #include "nsJSPrincipals.h"
19 #include "mozilla/BasePrincipal.h"
20 #include "gtest/MozGtestFriend.h"
28 #define NS_NULLPRINCIPAL_CID \
30 0xbd066e5f, 0x146f, 0x4472, { \
31 0x83, 0x31, 0x7b, 0xfd, 0x05, 0xb1, 0xed, 0x90 \
35 #define NS_NULLPRINCIPAL_SCHEME "moz-nullprincipal"
39 class NullPrincipal final
: public BasePrincipal
{
41 static PrincipalKind
Kind() { return eNullPrincipal
; }
43 NS_IMETHOD
QueryInterface(REFNSIID aIID
, void** aInstancePtr
) override
;
44 uint32_t GetHashValue() override
;
45 NS_IMETHOD
GetURI(nsIURI
** aURI
) override
;
46 NS_IMETHOD
GetIsOriginPotentiallyTrustworthy(bool* aResult
) override
;
47 NS_IMETHOD
GetDomain(nsIURI
** aDomain
) override
;
48 NS_IMETHOD
SetDomain(nsIURI
* aDomain
) override
;
49 NS_IMETHOD
GetBaseDomain(nsACString
& aBaseDomain
) override
;
50 NS_IMETHOD
GetAddonId(nsAString
& aAddonId
) override
;
51 NS_IMETHOD
GetPrecursorPrincipal(nsIPrincipal
** aPrecursor
) override
;
53 static already_AddRefed
<NullPrincipal
> CreateWithInheritedAttributes(
54 nsIPrincipal
* aInheritFrom
);
56 // Create NullPrincipal with origin attributes from docshell.
57 // If aIsFirstParty is true, and the pref 'privacy.firstparty.isolate' is also
58 // enabled, the mFirstPartyDomain value of the origin attributes will be set
59 // to an unique value.
60 static already_AddRefed
<NullPrincipal
> CreateWithInheritedAttributes(
61 nsIDocShell
* aDocShell
, bool aIsFirstParty
= false);
62 static already_AddRefed
<NullPrincipal
> CreateWithInheritedAttributes(
63 const OriginAttributes
& aOriginAttributes
, bool aIsFirstParty
= false);
65 static already_AddRefed
<NullPrincipal
> Create(
66 const OriginAttributes
& aOriginAttributes
, nsIURI
* aURI
= nullptr);
68 static already_AddRefed
<NullPrincipal
> CreateWithoutOriginAttributes();
70 // Generates a new unique `moz-nullprincipal:` URI. If `aPrecursor` is
71 // specified, it will be included in the generated URI as the null principal's
74 // The `aPrincipalID` attribute is used to force the creation of a
75 // deterministic NullPrincipal in situations where that is required. Avoid
76 // using this parameter unless absolutely necessary.
77 static already_AddRefed
<nsIURI
> CreateURI(nsIPrincipal
* aPrecursor
= nullptr,
78 const nsID
* aPrincipalID
= nullptr);
80 virtual nsresult
GetScriptLocation(nsACString
& aStr
) override
;
82 nsresult
GetSiteIdentifier(SiteIdentifier
& aSite
) override
{
87 virtual nsresult
PopulateJSONObject(Json::Value
& aObject
) override
;
89 // Serializable keys are the valid enum fields the serialization supports
90 enum SerializableKeys
: uint8_t { eSpec
= 0, eSuffix
, eMax
= eSuffix
};
91 typedef mozilla::BasePrincipal::KeyValT
<SerializableKeys
> KeyVal
;
93 static already_AddRefed
<BasePrincipal
> FromProperties(
94 nsTArray
<NullPrincipal::KeyVal
>& aFields
);
96 class Deserializer
: public BasePrincipal::Deserializer
{
98 NS_IMETHOD
Read(nsIObjectInputStream
* aStream
) override
;
102 NullPrincipal(nsIURI
* aURI
, const nsACString
& aOriginNoSuffix
,
103 const OriginAttributes
& aOriginAttributes
);
105 virtual ~NullPrincipal() = default;
107 bool SubsumesInternal(nsIPrincipal
* aOther
,
108 DocumentDomainConsideration aConsideration
) override
{
110 return FastEquals(aOther
);
113 bool MayLoadInternal(nsIURI
* aURI
) override
;
115 const nsCOMPtr
<nsIURI
> mURI
;
118 FRIEND_TEST(OriginAttributes
, NullPrincipal
);
119 FRIEND_TEST(NullPrincipalPrecursor
, EscapingRoundTrips
);
121 static void EscapePrecursorQuery(nsACString
& aPrecursorQuery
);
122 static void UnescapePrecursorQuery(nsACString
& aPrecursorQuery
);
124 // If aIsFirstParty is true, this NullPrincipal will be initialized based on
125 // the aOriginAttributes with FirstPartyDomain set to a unique value. This
126 // value is generated from mURI.filePath, with ".mozilla" appended at the end.
127 // aURI is used for testing purpose to assign a specific UUID rather than a
128 // randomly generated one.
129 static already_AddRefed
<NullPrincipal
> CreateInternal(
130 const OriginAttributes
& aOriginAttributes
, bool aIsFirstParty
,
131 nsIURI
* aURI
= nullptr, nsIPrincipal
* aPrecursor
= nullptr);
134 } // namespace mozilla
136 #endif // mozilla_NullPrincipal_h