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"
25 #define NS_NULLPRINCIPAL_CID \
27 0xbd066e5f, 0x146f, 0x4472, { \
28 0x83, 0x31, 0x7b, 0xfd, 0x05, 0xb1, 0xed, 0x90 \
32 #define NS_NULLPRINCIPAL_SCHEME "moz-nullprincipal"
38 class NullPrincipal final
: public BasePrincipal
{
40 static PrincipalKind
Kind() { return eNullPrincipal
; }
42 NS_IMETHOD
QueryInterface(REFNSIID aIID
, void** aInstancePtr
) override
;
43 uint32_t GetHashValue() override
;
44 NS_IMETHOD
GetURI(nsIURI
** aURI
) override
;
45 NS_IMETHOD
GetIsOriginPotentiallyTrustworthy(bool* aResult
) override
;
46 NS_IMETHOD
GetDomain(nsIURI
** aDomain
) override
;
47 NS_IMETHOD
SetDomain(nsIURI
* aDomain
) override
;
48 NS_IMETHOD
GetBaseDomain(nsACString
& aBaseDomain
) override
;
49 NS_IMETHOD
GetAddonId(nsAString
& aAddonId
) override
;
50 NS_IMETHOD
GetPrecursorPrincipal(nsIPrincipal
** aPrecursor
) override
;
52 // Create a NullPrincipal, inheriting origin attributes from the given
54 // If aInheritFrom is a content principal, or has a content principal
55 // precursor, it will be used as the precursor for this principal.
56 static already_AddRefed
<NullPrincipal
> CreateWithInheritedAttributes(
57 nsIPrincipal
* aInheritFrom
);
59 // Create a new NullPrincipal with the specified OriginAttributes.
61 // If `aNullPrincipalURI` is specified, it must be a NS_NULLPRINCIPAL_SCHEME
62 // URI previously created using `NullPrincipal::CreateURI`, and will be used
63 // as the origin URI for this principal.
64 static already_AddRefed
<NullPrincipal
> Create(
65 const OriginAttributes
& aOriginAttributes
,
66 nsIURI
* aNullPrincipalURI
= 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
WriteJSONInnerProperties(JSONWriter
& aWriter
) override
;
89 // Serializable keys are the valid enum fields the serialization supports
90 enum SerializableKeys
: uint8_t { eSpec
= 0, eSuffix
, eMax
= eSuffix
};
92 static constexpr char SpecKey
= '0';
93 static_assert(eSpec
== 0);
94 static constexpr char SuffixKey
= '1';
95 static_assert(eSuffix
== 1);
97 class Deserializer
: public BasePrincipal::Deserializer
{
99 NS_IMETHOD
Read(nsIObjectInputStream
* aStream
) override
;
103 NullPrincipal(nsIURI
* aURI
, const nsACString
& aOriginNoSuffix
,
104 const OriginAttributes
& aOriginAttributes
);
106 virtual ~NullPrincipal() = default;
108 bool SubsumesInternal(nsIPrincipal
* aOther
,
109 DocumentDomainConsideration aConsideration
) override
{
111 return FastEquals(aOther
);
114 bool MayLoadInternal(nsIURI
* aURI
) override
;
116 const nsCOMPtr
<nsIURI
> mURI
;
119 FRIEND_TEST(NullPrincipalPrecursor
, EscapingRoundTrips
);
121 static void EscapePrecursorQuery(nsACString
& aPrecursorQuery
);
122 static void UnescapePrecursorQuery(nsACString
& aPrecursorQuery
);
125 } // namespace mozilla
127 #endif // mozilla_NullPrincipal_h