NullPrincipal.h (4284B)
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/. */ 5 6 /** 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. 10 */ 11 12 #ifndef mozilla_NullPrincipal_h 13 #define mozilla_NullPrincipal_h 14 15 #include "nsIPrincipal.h" 16 #include "nsJSPrincipals.h" 17 #include "nsCOMPtr.h" 18 19 #include "mozilla/BasePrincipal.h" 20 #include "gtest/MozGtestFriend.h" 21 22 class nsIDocShell; 23 class nsIURI; 24 25 #define NS_NULLPRINCIPAL_CID \ 26 {0xbd066e5f, 0x146f, 0x4472, {0x83, 0x31, 0x7b, 0xfd, 0x05, 0xb1, 0xed, 0x90}} 27 28 #define NS_NULLPRINCIPAL_SCHEME "moz-nullprincipal" 29 30 namespace mozilla { 31 32 class JSONWriter; 33 34 class NullPrincipal final : public BasePrincipal { 35 public: 36 static PrincipalKind Kind() { return eNullPrincipal; } 37 38 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override; 39 NS_IMETHOD GetURI(nsIURI** aURI) override; 40 NS_IMETHOD GetIsOriginPotentiallyTrustworthy(bool* aResult) override; 41 NS_IMETHOD GetDomain(nsIURI** aDomain) override; 42 NS_IMETHOD SetDomain(nsIURI* aDomain) override; 43 NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override; 44 NS_IMETHOD GetAddonId(nsAString& aAddonId) override; 45 NS_IMETHOD GetPrecursorPrincipal(nsIPrincipal** aPrecursor) override; 46 47 // Create a NullPrincipal, inheriting origin attributes from the given 48 // principal. 49 // If aInheritFrom is a content principal, or has a content principal 50 // precursor, it will be used as the precursor for this principal. 51 static already_AddRefed<NullPrincipal> CreateWithInheritedAttributes( 52 nsIPrincipal* aInheritFrom); 53 54 // Create a new NullPrincipal with the specified OriginAttributes. 55 // 56 // If `aNullPrincipalURI` is specified, it must be a NS_NULLPRINCIPAL_SCHEME 57 // URI previously created using `NullPrincipal::CreateURI`, and will be used 58 // as the origin URI for this principal. 59 static already_AddRefed<NullPrincipal> Create( 60 const OriginAttributes& aOriginAttributes, 61 nsIURI* aNullPrincipalURI = nullptr); 62 63 static already_AddRefed<NullPrincipal> CreateWithoutOriginAttributes(); 64 65 // Generates a new unique `moz-nullprincipal:` URI. If `aPrecursor` is 66 // specified, it will be included in the generated URI as the null principal's 67 // precursor. 68 // 69 // The `aPrincipalID` attribute is used to force the creation of a 70 // deterministic NullPrincipal in situations where that is required. Avoid 71 // using this parameter unless absolutely necessary. 72 static already_AddRefed<nsIURI> CreateURI(nsIPrincipal* aPrecursor = nullptr, 73 const nsID* aPrincipalID = nullptr); 74 75 virtual nsresult GetScriptLocation(nsACString& aStr) override; 76 77 nsresult GetSiteIdentifier(SiteIdentifier& aSite) override { 78 aSite.Init(this); 79 return NS_OK; 80 } 81 82 virtual nsresult WriteJSONInnerProperties(JSONWriter& aWriter) override; 83 84 // Serializable keys are the valid enum fields the serialization supports 85 enum SerializableKeys : uint8_t { eSpec = 0, eSuffix, eMax = eSuffix }; 86 87 static constexpr char SpecKey = '0'; 88 static_assert(eSpec == 0); 89 static constexpr char SuffixKey = '1'; 90 static_assert(eSuffix == 1); 91 92 class Deserializer : public BasePrincipal::Deserializer { 93 public: 94 NS_IMETHOD Read(nsIObjectInputStream* aStream) override; 95 }; 96 97 protected: 98 NullPrincipal(nsIURI* aURI, const nsACString& aOriginNoSuffix, 99 const OriginAttributes& aOriginAttributes); 100 101 virtual ~NullPrincipal() = default; 102 103 bool SubsumesInternal(nsIPrincipal* aOther, 104 DocumentDomainConsideration aConsideration) override { 105 MOZ_ASSERT(aOther); 106 return FastEquals(aOther); 107 } 108 109 bool MayLoadInternal(nsIURI* aURI) override; 110 111 const nsCOMPtr<nsIURI> mURI; 112 113 private: 114 FRIEND_TEST(NullPrincipalPrecursor, EscapingRoundTrips); 115 116 static void EscapePrecursorQuery(nsACString& aPrecursorQuery); 117 static void UnescapePrecursorQuery(nsACString& aPrecursorQuery); 118 }; 119 120 } // namespace mozilla 121 122 #endif // mozilla_NullPrincipal_h