1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 sts=2 ts=2 et tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /**
8  * This is the principal that has no rights and can't be accessed by
9  * anything other than itself and chrome; null principals are not
10  * same-origin with anything but themselves.
11  */
12 
13 #include "mozilla/ArrayUtils.h"
14 
15 #include "nsDocShell.h"
16 #include "NullPrincipal.h"
17 #include "NullPrincipalURI.h"
18 #include "nsMemory.h"
19 #include "nsIClassInfoImpl.h"
20 #include "nsNetCID.h"
21 #include "nsError.h"
22 #include "ContentPrincipal.h"
23 #include "nsScriptSecurityManager.h"
24 #include "pratom.h"
25 
26 #include "json/json.h"
27 
28 using namespace mozilla;
29 
NS_IMPL_CLASSINFO(NullPrincipal,nullptr,nsIClassInfo::MAIN_THREAD_ONLY,NS_NULLPRINCIPAL_CID)30 NS_IMPL_CLASSINFO(NullPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
31                   NS_NULLPRINCIPAL_CID)
32 NS_IMPL_QUERY_INTERFACE_CI(NullPrincipal, nsIPrincipal, nsISerializable)
33 NS_IMPL_CI_INTERFACE_GETTER(NullPrincipal, nsIPrincipal, nsISerializable)
34 
35 /* static */
36 already_AddRefed<NullPrincipal> NullPrincipal::CreateWithInheritedAttributes(
37     nsIPrincipal* aInheritFrom) {
38   MOZ_ASSERT(aInheritFrom);
39   return CreateWithInheritedAttributes(
40       Cast(aInheritFrom)->OriginAttributesRef(), false);
41 }
42 
43 /* static */
CreateWithInheritedAttributes(nsIDocShell * aDocShell,bool aIsFirstParty)44 already_AddRefed<NullPrincipal> NullPrincipal::CreateWithInheritedAttributes(
45     nsIDocShell* aDocShell, bool aIsFirstParty) {
46   MOZ_ASSERT(aDocShell);
47 
48   OriginAttributes attrs = nsDocShell::Cast(aDocShell)->GetOriginAttributes();
49   return CreateWithInheritedAttributes(attrs, aIsFirstParty);
50 }
51 
52 /* static */
CreateWithInheritedAttributes(const OriginAttributes & aOriginAttributes,bool aIsFirstParty)53 already_AddRefed<NullPrincipal> NullPrincipal::CreateWithInheritedAttributes(
54     const OriginAttributes& aOriginAttributes, bool aIsFirstParty) {
55   RefPtr<NullPrincipal> nullPrin = new NullPrincipal();
56   nsresult rv = nullPrin->Init(aOriginAttributes, aIsFirstParty);
57   MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
58 
59   return nullPrin.forget();
60 }
61 
62 /* static */
Create(const OriginAttributes & aOriginAttributes,nsIURI * aURI)63 already_AddRefed<NullPrincipal> NullPrincipal::Create(
64     const OriginAttributes& aOriginAttributes, nsIURI* aURI) {
65   RefPtr<NullPrincipal> nullPrin = new NullPrincipal();
66   nsresult rv = nullPrin->Init(aOriginAttributes, aURI);
67   MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
68 
69   return nullPrin.forget();
70 }
71 
72 /* static */
CreateWithoutOriginAttributes()73 already_AddRefed<NullPrincipal> NullPrincipal::CreateWithoutOriginAttributes() {
74   return NullPrincipal::Create(OriginAttributes(), nullptr);
75 }
76 
Init(const OriginAttributes & aOriginAttributes,nsIURI * aURI)77 nsresult NullPrincipal::Init(const OriginAttributes& aOriginAttributes,
78                              nsIURI* aURI) {
79   if (aURI) {
80     nsAutoCString scheme;
81     nsresult rv = aURI->GetScheme(scheme);
82     NS_ENSURE_SUCCESS(rv, rv);
83 
84     NS_ENSURE_TRUE(scheme.EqualsLiteral(NS_NULLPRINCIPAL_SCHEME),
85                    NS_ERROR_NOT_AVAILABLE);
86 
87     mURI = aURI;
88   } else {
89     mURI = new NullPrincipalURI();
90   }
91 
92   nsAutoCString originNoSuffix;
93   DebugOnly<nsresult> rv = mURI->GetSpec(originNoSuffix);
94   MOZ_ASSERT(NS_SUCCEEDED(rv));
95 
96   FinishInit(originNoSuffix, aOriginAttributes);
97 
98   return NS_OK;
99 }
100 
Init(const OriginAttributes & aOriginAttributes,bool aIsFirstParty,nsIURI * aURI)101 nsresult NullPrincipal::Init(const OriginAttributes& aOriginAttributes,
102                              bool aIsFirstParty, nsIURI* aURI) {
103   if (aURI) {
104     nsAutoCString scheme;
105     nsresult rv = aURI->GetScheme(scheme);
106     NS_ENSURE_SUCCESS(rv, rv);
107 
108     NS_ENSURE_TRUE(scheme.EqualsLiteral(NS_NULLPRINCIPAL_SCHEME),
109                    NS_ERROR_NOT_AVAILABLE);
110 
111     mURI = aURI;
112   } else {
113     mURI = new NullPrincipalURI();
114   }
115 
116   nsAutoCString originNoSuffix;
117   DebugOnly<nsresult> rv = mURI->GetSpec(originNoSuffix);
118   MOZ_ASSERT(NS_SUCCEEDED(rv));
119 
120   nsAutoCString path;
121   rv = mURI->GetPathQueryRef(path);
122   MOZ_ASSERT(NS_SUCCEEDED(rv));
123 
124   OriginAttributes attrs(aOriginAttributes);
125   if (aIsFirstParty) {
126     // remove the '{}' characters from both ends.
127     path.Mid(path, 1, path.Length() - 2);
128     path.AppendLiteral(".mozilla");
129     attrs.SetFirstPartyDomain(true, path);
130   }
131 
132   FinishInit(originNoSuffix, attrs);
133 
134   return NS_OK;
135 }
136 
GetScriptLocation(nsACString & aStr)137 nsresult NullPrincipal::GetScriptLocation(nsACString& aStr) {
138   return mURI->GetSpec(aStr);
139 }
140 
141 /**
142  * nsIPrincipal implementation
143  */
144 
GetHashValue()145 uint32_t NullPrincipal::GetHashValue() { return (NS_PTR_TO_INT32(this) >> 2); }
146 
147 NS_IMETHODIMP
GetURI(nsIURI ** aURI)148 NullPrincipal::GetURI(nsIURI** aURI) {
149   nsCOMPtr<nsIURI> uri = mURI;
150   uri.forget(aURI);
151   return NS_OK;
152 }
153 NS_IMETHODIMP
GetIsOriginPotentiallyTrustworthy(bool * aResult)154 NullPrincipal::GetIsOriginPotentiallyTrustworthy(bool* aResult) {
155   *aResult = false;
156   return NS_OK;
157 }
158 
159 NS_IMETHODIMP
GetDomain(nsIURI ** aDomain)160 NullPrincipal::GetDomain(nsIURI** aDomain) {
161   nsCOMPtr<nsIURI> uri = mURI;
162   uri.forget(aDomain);
163   return NS_OK;
164 }
165 
166 NS_IMETHODIMP
SetDomain(nsIURI * aDomain)167 NullPrincipal::SetDomain(nsIURI* aDomain) {
168   // I think the right thing to do here is to just throw...  Silently failing
169   // seems counterproductive.
170   return NS_ERROR_NOT_AVAILABLE;
171 }
172 
MayLoadInternal(nsIURI * aURI)173 bool NullPrincipal::MayLoadInternal(nsIURI* aURI) {
174   // Also allow the load if we are the principal of the URI being checked.
175   nsCOMPtr<nsIPrincipal> blobPrincipal;
176   if (dom::BlobURLProtocolHandler::GetBlobURLPrincipal(
177           aURI, getter_AddRefs(blobPrincipal))) {
178     MOZ_ASSERT(blobPrincipal);
179     return SubsumesInternal(blobPrincipal,
180                             BasePrincipal::ConsiderDocumentDomain);
181   }
182 
183   return false;
184 }
185 
186 NS_IMETHODIMP
GetBaseDomain(nsACString & aBaseDomain)187 NullPrincipal::GetBaseDomain(nsACString& aBaseDomain) {
188   // For a null principal, we use our unique uuid as the base domain.
189   return mURI->GetPathQueryRef(aBaseDomain);
190 }
191 
192 NS_IMETHODIMP
GetAddonId(nsAString & aAddonId)193 NullPrincipal::GetAddonId(nsAString& aAddonId) {
194   aAddonId.Truncate();
195   return NS_OK;
196 };
197 
198 /**
199  * nsISerializable implementation
200  */
201 NS_IMETHODIMP
Read(nsIObjectInputStream * aStream)202 NullPrincipal::Read(nsIObjectInputStream* aStream) {
203   // Note - NullPrincipal use NS_GENERIC_FACTORY_CONSTRUCTOR_INIT, which means
204   // that the Init() method has already been invoked by the time we deserialize.
205   // This is in contrast to ContentPrincipal, which uses
206   // NS_GENERIC_FACTORY_CONSTRUCTOR, in which case ::Read needs to invoke
207   // Init().
208 
209   nsAutoCString spec;
210   nsresult rv = aStream->ReadCString(spec);
211   NS_ENSURE_SUCCESS(rv, rv);
212 
213   nsCOMPtr<nsIURI> uri;
214   rv = NS_NewURI(getter_AddRefs(uri), spec);
215   NS_ENSURE_SUCCESS(rv, rv);
216 
217   nsAutoCString suffix;
218   rv = aStream->ReadCString(suffix);
219   NS_ENSURE_SUCCESS(rv, rv);
220 
221   OriginAttributes attrs;
222   bool ok = attrs.PopulateFromSuffix(suffix);
223   NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
224 
225   return Init(attrs, uri);
226 }
227 
228 NS_IMETHODIMP
Write(nsIObjectOutputStream * aStream)229 NullPrincipal::Write(nsIObjectOutputStream* aStream) {
230   // Read is used still for legacy principals
231   MOZ_RELEASE_ASSERT(false, "Old style serialization is removed");
232   return NS_OK;
233 }
234 
PopulateJSONObject(Json::Value & aObject)235 nsresult NullPrincipal::PopulateJSONObject(Json::Value& aObject) {
236   nsAutoCString principalURI;
237   nsresult rv = mURI->GetSpec(principalURI);
238   NS_ENSURE_SUCCESS(rv, rv);
239   MOZ_ASSERT(principalURI.Length() ==
240                  NS_LITERAL_CSTRING(NS_NULLPRINCIPAL_SCHEME ":").Length() +
241                      NSID_LENGTH - 1,
242              "Length of the URI should be: (scheme, uuid, - nullptr)");
243   aObject[std::to_string(eSpec)] = principalURI.get();
244 
245   nsAutoCString suffix;
246   OriginAttributesRef().CreateSuffix(suffix);
247   if (suffix.Length() > 0) {
248     aObject[std::to_string(eSuffix)] = suffix.get();
249   }
250 
251   return NS_OK;
252 }
253 
FromProperties(nsTArray<NullPrincipal::KeyVal> & aFields)254 already_AddRefed<BasePrincipal> NullPrincipal::FromProperties(
255     nsTArray<NullPrincipal::KeyVal>& aFields) {
256   MOZ_ASSERT(aFields.Length() == eMax + 1, "Must have all the keys");
257   nsresult rv;
258   nsCOMPtr<nsIURI> uri;
259   OriginAttributes attrs;
260 
261   // The odd structure here is to make the code to not compile
262   // if all the switch enum cases haven't been codified
263   for (const auto& field : aFields) {
264     switch (field.key) {
265       case NullPrincipal::eSpec:
266         if (!field.valueWasSerialized) {
267           MOZ_ASSERT(false,
268                      "Null principals require a spec URI in serialized JSON");
269           return nullptr;
270         }
271         rv = NS_NewURI(getter_AddRefs(uri), field.value);
272         NS_ENSURE_SUCCESS(rv, nullptr);
273         break;
274       case NullPrincipal::eSuffix:
275         bool ok = attrs.PopulateFromSuffix(field.value);
276         if (!ok) {
277           return nullptr;
278         }
279         break;
280     }
281   }
282 
283   if (!uri) {
284     MOZ_ASSERT(false, "No URI deserialized");
285     return nullptr;
286   }
287 
288   RefPtr<NullPrincipal> nullPrincipal = new NullPrincipal();
289   rv = nullPrincipal->Init(attrs, uri);
290   if (NS_FAILED(rv)) {
291     return nullptr;
292   }
293   return nullPrincipal.forget();
294 }
295