1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 sw=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/. */
7 #include "ContentPrincipal.h"
9 #include "mozIThirdPartyUtil.h"
10 #include "nsContentUtils.h"
12 #include "nsScriptSecurityManager.h"
14 #include "nsReadableUtils.h"
18 #include "nsIStandardURL.h"
19 #include "nsIURIWithSpecialOrigin.h"
20 #include "nsIURIMutator.h"
21 #include "nsJSPrincipals.h"
22 #include "nsIEffectiveTLDService.h"
23 #include "nsIClassInfoImpl.h"
24 #include "nsIObjectInputStream.h"
25 #include "nsIObjectOutputStream.h"
26 #include "nsIProtocolHandler.h"
28 #include "nsIContentSecurityPolicy.h"
30 #include "js/RealmIterators.h"
31 #include "js/Wrapper.h"
33 #include "mozilla/dom/BlobURLProtocolHandler.h"
34 #include "mozilla/dom/ScriptSettings.h"
35 #include "mozilla/ClearOnShutdown.h"
36 #include "mozilla/ExtensionPolicyService.h"
37 #include "mozilla/Preferences.h"
38 #include "mozilla/HashFunctions.h"
40 #include "nsSerializationHelper.h"
43 #include "ContentPrincipalJSONHandler.h"
45 using namespace mozilla
;
47 NS_IMPL_CLASSINFO(ContentPrincipal
, nullptr, 0, NS_PRINCIPAL_CID
)
48 NS_IMPL_QUERY_INTERFACE_CI(ContentPrincipal
, nsIPrincipal
)
49 NS_IMPL_CI_INTERFACE_GETTER(ContentPrincipal
, nsIPrincipal
)
51 ContentPrincipal::ContentPrincipal(nsIURI
* aURI
,
52 const OriginAttributes
& aOriginAttributes
,
53 const nsACString
& aOriginNoSuffix
,
54 nsIURI
* aInitialDomain
)
55 : BasePrincipal(eContentPrincipal
, aOriginNoSuffix
, aOriginAttributes
),
57 mDomain(aInitialDomain
) {
59 // We're just creating the principal, so no need to re-compute wrappers.
60 SetHasExplicitDomain();
63 #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
64 // Assert that the URI we get here isn't any of the schemes that we know we
65 // should not get here. These schemes always either inherit their principal
66 // or fall back to a null principal. These are schemes which return
67 // URI_INHERITS_SECURITY_CONTEXT from their protocol handler's
68 // GetProtocolFlags function.
70 MOZ_DIAGNOSTIC_ASSERT(
71 NS_SUCCEEDED(NS_URIChainHasFlags(
72 aURI
, nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT
, &hasFlag
)) &&
77 ContentPrincipal::ContentPrincipal(ContentPrincipal
* aOther
,
78 const OriginAttributes
& aOriginAttributes
)
79 : BasePrincipal(aOther
, aOriginAttributes
),
81 mDomain(aOther
->mDomain
),
82 mAddon(aOther
->mAddon
) {}
84 ContentPrincipal::~ContentPrincipal() = default;
86 nsresult
ContentPrincipal::GetScriptLocation(nsACString
& aStr
) {
87 return mURI
->GetSpec(aStr
);
91 nsresult
ContentPrincipal::GenerateOriginNoSuffixFromURI(
92 nsIURI
* aURI
, nsACString
& aOriginNoSuffix
) {
94 return NS_ERROR_FAILURE
;
97 nsCOMPtr
<nsIURI
> origin
= NS_GetInnermostURI(aURI
);
99 return NS_ERROR_FAILURE
;
102 MOZ_ASSERT(!NS_IsAboutBlank(origin
),
103 "The inner URI for about:blank must be moz-safe-about:blank");
105 // Handle non-strict file:// uris.
106 if (!nsScriptSecurityManager::GetStrictFileOriginPolicy() &&
107 NS_URIIsLocalFile(origin
)) {
108 // If strict file origin policy is not in effect, all local files are
109 // considered to be same-origin, so return a known dummy origin here.
110 aOriginNoSuffix
.AssignLiteral("file://UNIVERSAL_FILE_URI_ORIGIN");
115 // NB: This is only compiled for Thunderbird/Suite.
116 #if IS_ORIGIN_IS_FULL_SPEC_DEFINED
117 bool fullSpec
= false;
118 rv
= NS_URIChainHasFlags(origin
, nsIProtocolHandler::ORIGIN_IS_FULL_SPEC
,
120 NS_ENSURE_SUCCESS(rv
, rv
);
122 return origin
->GetAsciiSpec(aOriginNoSuffix
);
126 // We want the invariant that prinA.origin == prinB.origin i.f.f.
127 // prinA.equals(prinB). However, this requires that we impose certain
128 // constraints on the behavior and origin semantics of principals, and in
129 // particular, forbid creating origin strings for principals whose equality
130 // constraints are not expressible as strings (i.e. object equality).
131 // Moreover, we want to forbid URIs containing the magic "^" we use as a
132 // separating character for origin attributes.
134 // These constraints can generally be achieved by restricting .origin to
135 // nsIStandardURL-based URIs, but there are a few other URI schemes that we
137 if (origin
->SchemeIs("about") ||
138 (origin
->SchemeIs("moz-safe-about") &&
139 // We generally consider two about:foo origins to be same-origin, but
140 // about:blank is special since it can be generated from different
141 // sources. We check for moz-safe-about:blank since origin is an
143 !StringBeginsWith(origin
->GetSpecOrDefault(),
144 "moz-safe-about:blank"_ns
))) {
145 rv
= origin
->GetAsciiSpec(aOriginNoSuffix
);
146 NS_ENSURE_SUCCESS(rv
, rv
);
148 int32_t pos
= aOriginNoSuffix
.FindChar('?');
149 int32_t hashPos
= aOriginNoSuffix
.FindChar('#');
151 if (hashPos
!= kNotFound
&& (pos
== kNotFound
|| hashPos
< pos
)) {
155 if (pos
!= kNotFound
) {
156 aOriginNoSuffix
.Truncate(pos
);
159 // These URIs could technically contain a '^', but they never should.
160 if (NS_WARN_IF(aOriginNoSuffix
.FindChar('^', 0) != -1)) {
161 aOriginNoSuffix
.Truncate();
162 return NS_ERROR_FAILURE
;
167 // This URL can be a blobURL. In this case, we should use the 'parent'
168 // principal instead.
169 nsCOMPtr
<nsIPrincipal
> blobPrincipal
;
170 if (dom::BlobURLProtocolHandler::GetBlobURLPrincipal(
171 origin
, getter_AddRefs(blobPrincipal
))) {
172 MOZ_ASSERT(blobPrincipal
);
173 return blobPrincipal
->GetOriginNoSuffix(aOriginNoSuffix
);
176 // If we reached this branch, we can only create an origin if we have a
177 // nsIStandardURL. So, we query to a nsIStandardURL, and fail if we aren't
178 // an instance of an nsIStandardURL nsIStandardURLs have the good property
179 // of escaping the '^' character in their specs, which means that we can be
180 // sure that the caret character (which is reserved for delimiting the end
181 // of the spec, and the beginning of the origin attributes) is not present
182 // in the origin string
183 nsCOMPtr
<nsIStandardURL
> standardURL
= do_QueryInterface(origin
);
185 return NS_ERROR_FAILURE
;
188 // See whether we have a useful hostPort. If we do, use that.
189 nsAutoCString hostPort
;
190 if (!origin
->SchemeIs("chrome")) {
191 rv
= origin
->GetAsciiHostPort(hostPort
);
192 NS_ENSURE_SUCCESS(rv
, rv
);
194 if (!hostPort
.IsEmpty()) {
195 rv
= origin
->GetScheme(aOriginNoSuffix
);
196 NS_ENSURE_SUCCESS(rv
, rv
);
197 aOriginNoSuffix
.AppendLiteral("://");
198 aOriginNoSuffix
.Append(hostPort
);
202 rv
= aURI
->GetAsciiSpec(aOriginNoSuffix
);
203 NS_ENSURE_SUCCESS(rv
, rv
);
205 // The origin, when taken from the spec, should not contain the ref part of
208 int32_t pos
= aOriginNoSuffix
.FindChar('?');
209 int32_t hashPos
= aOriginNoSuffix
.FindChar('#');
211 if (hashPos
!= kNotFound
&& (pos
== kNotFound
|| hashPos
< pos
)) {
215 if (pos
!= kNotFound
) {
216 aOriginNoSuffix
.Truncate(pos
);
222 bool ContentPrincipal::SubsumesInternal(
223 nsIPrincipal
* aOther
,
224 BasePrincipal::DocumentDomainConsideration aConsideration
) {
227 // For ContentPrincipal, Subsumes is equivalent to Equals.
228 if (aOther
== this) {
232 // If either the subject or the object has changed its principal by
233 // explicitly setting document.domain then the other must also have
234 // done so in order to be considered the same origin. This prevents
235 // DNS spoofing based on document.domain (154930)
236 if (aConsideration
== ConsiderDocumentDomain
) {
237 // Get .domain on each principal.
238 nsCOMPtr
<nsIURI
> thisDomain
, otherDomain
;
239 GetDomain(getter_AddRefs(thisDomain
));
240 aOther
->GetDomain(getter_AddRefs(otherDomain
));
242 // If either has .domain set, we have equality i.f.f. the domains match.
243 // Otherwise, we fall through to the non-document-domain-considering case.
244 if (thisDomain
|| otherDomain
) {
246 nsScriptSecurityManager::SecurityCompareURIs(thisDomain
, otherDomain
);
249 nsAutoCString thisSiteOrigin
, otherSiteOrigin
;
250 MOZ_ALWAYS_SUCCEEDS(GetSiteOrigin(thisSiteOrigin
));
251 MOZ_ALWAYS_SUCCEEDS(aOther
->GetSiteOrigin(otherSiteOrigin
));
253 thisSiteOrigin
== otherSiteOrigin
,
254 "SubsumesConsideringDomain passed with mismatched siteOrigin!");
261 // Do a fast check (including origin attributes) or a slow uri comparison.
262 return FastEquals(aOther
) || aOther
->IsSameOrigin(mURI
);
266 ContentPrincipal::GetURI(nsIURI
** aURI
) {
267 *aURI
= do_AddRef(mURI
).take();
271 bool ContentPrincipal::MayLoadInternal(nsIURI
* aURI
) {
274 #if defined(MOZ_THUNDERBIRD) || defined(MOZ_SUITE)
275 nsCOMPtr
<nsIURIWithSpecialOrigin
> uriWithSpecialOrigin
=
276 do_QueryInterface(aURI
);
277 if (uriWithSpecialOrigin
) {
278 nsCOMPtr
<nsIURI
> origin
;
279 nsresult rv
= uriWithSpecialOrigin
->GetOrigin(getter_AddRefs(origin
));
280 if (NS_WARN_IF(NS_FAILED(rv
))) {
284 OriginAttributes attrs
;
285 RefPtr
<BasePrincipal
> principal
=
286 BasePrincipal::CreateContentPrincipal(origin
, attrs
);
287 return nsIPrincipal::Subsumes(principal
);
291 nsCOMPtr
<nsIPrincipal
> blobPrincipal
;
292 if (dom::BlobURLProtocolHandler::GetBlobURLPrincipal(
293 aURI
, getter_AddRefs(blobPrincipal
))) {
294 MOZ_ASSERT(blobPrincipal
);
295 return nsIPrincipal::Subsumes(blobPrincipal
);
298 // If this principal is associated with an addon, check whether that addon
299 // has been given permission to load from this domain.
300 if (AddonAllowsLoad(aURI
)) {
304 if (nsScriptSecurityManager::SecurityCompareURIs(mURI
, aURI
)) {
308 // If strict file origin policy is in effect, local files will always fail
309 // SecurityCompareURIs unless they are identical. Explicitly check file origin
310 // policy, in that case.
311 if (nsScriptSecurityManager::GetStrictFileOriginPolicy() &&
312 NS_URIIsLocalFile(aURI
) && NS_RelaxStrictFileOriginPolicy(aURI
, mURI
)) {
319 uint32_t ContentPrincipal::GetHashValue() {
320 MOZ_ASSERT(mURI
, "Need a principal URI");
322 nsCOMPtr
<nsIURI
> uri
;
323 GetDomain(getter_AddRefs(uri
));
325 GetURI(getter_AddRefs(uri
));
327 return NS_SecurityHashURI(uri
);
331 ContentPrincipal::GetDomain(nsIURI
** aDomain
) {
332 if (!GetHasExplicitDomain()) {
337 mozilla::MutexAutoLock
lock(mMutex
);
338 NS_ADDREF(*aDomain
= mDomain
);
343 ContentPrincipal::SetDomain(nsIURI
* aDomain
) {
344 AssertIsOnMainThread();
348 mozilla::MutexAutoLock
lock(mMutex
);
350 SetHasExplicitDomain();
353 // Set the changed-document-domain flag on compartments containing realms
354 // using this principal.
355 auto cb
= [](JSContext
*, void*, JS::Realm
* aRealm
,
356 const JS::AutoRequireNoGC
& nogc
) {
357 JS::Compartment
* comp
= JS::GetCompartmentForRealm(aRealm
);
358 xpc::SetCompartmentChangedDocumentDomain(comp
);
360 JSPrincipals
* principals
=
361 nsJSPrincipals::get(static_cast<nsIPrincipal
*>(this));
363 dom::AutoJSAPI jsapi
;
365 JS::IterateRealmsWithPrincipals(jsapi
.cx(), principals
, nullptr, cb
);
370 static nsresult
GetSpecialBaseDomain(const nsCOMPtr
<nsIURI
>& aURI
,
371 bool* aHandled
, nsACString
& aBaseDomain
) {
374 // Special handling for a file URI.
375 if (NS_URIIsLocalFile(aURI
)) {
376 // If strict file origin policy is not in effect, all local files are
377 // considered to be same-origin, so return a known dummy domain here.
378 if (!nsScriptSecurityManager::GetStrictFileOriginPolicy()) {
380 aBaseDomain
.AssignLiteral("UNIVERSAL_FILE_URI_ORIGIN");
384 // Otherwise, we return the file path.
385 nsCOMPtr
<nsIURL
> url
= do_QueryInterface(aURI
);
389 return url
->GetFilePath(aBaseDomain
);
393 bool hasNoRelativeFlag
;
394 nsresult rv
= NS_URIChainHasFlags(aURI
, nsIProtocolHandler::URI_NORELATIVE
,
396 if (NS_WARN_IF(NS_FAILED(rv
))) {
400 // In case of FTP we want to get base domain via TLD service even if FTP
401 // protocol handler is disabled and the scheme is handled by external protocol
402 // handler which returns URI_NORELATIVE flag.
403 if (hasNoRelativeFlag
&& !aURI
->SchemeIs("ftp")) {
405 return aURI
->GetSpec(aBaseDomain
);
408 if (aURI
->SchemeIs("indexeddb")) {
410 return aURI
->GetSpec(aBaseDomain
);
417 ContentPrincipal::GetBaseDomain(nsACString
& aBaseDomain
) {
418 // Handle some special URIs first.
420 nsresult rv
= GetSpecialBaseDomain(mURI
, &handled
, aBaseDomain
);
421 NS_ENSURE_SUCCESS(rv
, rv
);
427 // For everything else, we ask the TLD service via the ThirdPartyUtil.
428 nsCOMPtr
<mozIThirdPartyUtil
> thirdPartyUtil
=
429 do_GetService(THIRDPARTYUTIL_CONTRACTID
);
430 if (!thirdPartyUtil
) {
431 return NS_ERROR_FAILURE
;
434 return thirdPartyUtil
->GetBaseDomain(mURI
, aBaseDomain
);
438 ContentPrincipal::GetSiteOriginNoSuffix(nsACString
& aSiteOrigin
) {
439 nsresult rv
= GetOriginNoSuffix(aSiteOrigin
);
440 NS_ENSURE_SUCCESS(rv
, rv
);
442 // It is possible for two principals with the same origin to have different
443 // mURI values. In order to ensure that two principals with matching origins
444 // also have matching siteOrigins, we derive the siteOrigin entirely from the
445 // origin string and do not rely on mURI at all here.
446 nsCOMPtr
<nsIURI
> origin
;
447 rv
= NS_NewURI(getter_AddRefs(origin
), aSiteOrigin
);
449 // We got an error parsing the origin as a URI? siteOrigin == origin
450 // aSiteOrigin was already filled with `OriginNoSuffix`
454 // Handle some special URIs first.
455 nsAutoCString baseDomain
;
457 rv
= GetSpecialBaseDomain(origin
, &handled
, baseDomain
);
458 NS_ENSURE_SUCCESS(rv
, rv
);
461 // This is a special URI ("file:", "about:", "view-source:", etc). Just
462 // return the origin.
466 // For everything else, we ask the TLD service. Note that, unlike in
467 // GetBaseDomain, we don't use ThirdPartyUtil.getBaseDomain because if the
468 // host is an IP address that returns the raw address and we can't use it with
469 // SetHost below because SetHost expects '[' and ']' around IPv6 addresses.
471 nsCOMPtr
<nsIEffectiveTLDService
> tldService
=
472 do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID
);
474 return NS_ERROR_FAILURE
;
477 bool gotBaseDomain
= false;
478 rv
= tldService
->GetBaseDomain(origin
, 0, baseDomain
);
479 if (NS_SUCCEEDED(rv
)) {
480 gotBaseDomain
= true;
482 // If this is an IP address or something like "localhost", we just continue
483 // with gotBaseDomain = false.
484 if (rv
!= NS_ERROR_HOST_IS_IP_ADDRESS
&&
485 rv
!= NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS
&&
486 rv
!= NS_ERROR_INVALID_ARG
) {
491 // NOTE: Calling `SetHostPort` with a portless domain is insufficient to clear
492 // the port, so an extra `SetPort` call has to be made.
493 nsCOMPtr
<nsIURI
> siteUri
;
494 NS_MutateURI
mutator(origin
);
495 mutator
.SetUserPass(""_ns
).SetPort(-1);
497 mutator
.SetHost(baseDomain
);
499 rv
= mutator
.Finalize(siteUri
);
500 MOZ_ASSERT(NS_SUCCEEDED(rv
), "failed to create siteUri");
501 NS_ENSURE_SUCCESS(rv
, rv
);
503 aSiteOrigin
.Truncate();
504 rv
= GenerateOriginNoSuffixFromURI(siteUri
, aSiteOrigin
);
505 MOZ_ASSERT(NS_SUCCEEDED(rv
), "failed to create siteOriginNoSuffix");
509 nsresult
ContentPrincipal::GetSiteIdentifier(SiteIdentifier
& aSite
) {
510 nsCString siteOrigin
;
511 nsresult rv
= GetSiteOrigin(siteOrigin
);
512 NS_ENSURE_SUCCESS(rv
, rv
);
514 RefPtr
<BasePrincipal
> principal
= CreateContentPrincipal(siteOrigin
);
516 NS_WARNING("could not instantiate content principal");
517 return NS_ERROR_FAILURE
;
520 aSite
.Init(principal
);
524 RefPtr
<extensions::WebExtensionPolicyCore
> ContentPrincipal::AddonPolicyCore() {
525 mozilla::MutexAutoLock
lock(mMutex
);
526 if (!mAddon
.isSome()) {
527 NS_ENSURE_TRUE(mURI
, nullptr);
529 RefPtr
<extensions::WebExtensionPolicyCore
> core
;
530 if (mURI
->SchemeIs("moz-extension")) {
532 NS_ENSURE_SUCCESS(mURI
->GetHost(host
), nullptr);
533 core
= ExtensionPolicyService::GetCoreByHost(host
);
536 mAddon
.emplace(core
);
542 ContentPrincipal::GetAddonId(nsAString
& aAddonId
) {
543 if (RefPtr
<extensions::WebExtensionPolicyCore
> policy
= AddonPolicyCore()) {
544 policy
->Id()->ToString(aAddonId
);
552 ContentPrincipal::Deserializer::Read(nsIObjectInputStream
* aStream
) {
553 MOZ_ASSERT(!mPrincipal
);
555 nsCOMPtr
<nsISupports
> supports
;
556 nsCOMPtr
<nsIURI
> principalURI
;
557 nsresult rv
= NS_ReadOptionalObject(aStream
, true, getter_AddRefs(supports
));
562 principalURI
= do_QueryInterface(supports
);
563 // Enforce re-parsing about: URIs so that if they change, we continue to use
564 // their new principals correctly.
565 if (principalURI
->SchemeIs("about")) {
567 principalURI
->GetSpec(spec
);
568 NS_ENSURE_SUCCESS(NS_NewURI(getter_AddRefs(principalURI
), spec
),
572 nsCOMPtr
<nsIURI
> domain
;
573 rv
= NS_ReadOptionalObject(aStream
, true, getter_AddRefs(supports
));
578 domain
= do_QueryInterface(supports
);
580 nsAutoCString suffix
;
581 rv
= aStream
->ReadCString(suffix
);
582 NS_ENSURE_SUCCESS(rv
, rv
);
584 OriginAttributes attrs
;
585 bool ok
= attrs
.PopulateFromSuffix(suffix
);
586 NS_ENSURE_TRUE(ok
, NS_ERROR_FAILURE
);
588 // Since Bug 965637 we do not serialize the CSP within the
589 // Principal anymore. Nevertheless there might still be
590 // serialized Principals that do have a serialized CSP.
591 // For now, we just read the CSP here but do not actually
592 // consume it. Please note that we deliberately ignore
593 // the return value to avoid CSP deserialization problems.
594 // After Bug 1508939 we will have a new serialization for
595 // Principals which allows us to update the code here.
596 // Additionally, the format for serialized CSPs changed
597 // within Bug 965637 which also can cause failures within
598 // the CSP deserialization code.
599 Unused
<< NS_ReadOptionalObject(aStream
, true, getter_AddRefs(supports
));
601 nsAutoCString originNoSuffix
;
602 rv
= GenerateOriginNoSuffixFromURI(principalURI
, originNoSuffix
);
603 NS_ENSURE_SUCCESS(rv
, rv
);
606 new ContentPrincipal(principalURI
, attrs
, originNoSuffix
, domain
);
610 nsresult
ContentPrincipal::WriteJSONInnerProperties(JSONWriter
& aWriter
) {
611 nsAutoCString principalURI
;
612 nsresult rv
= mURI
->GetSpec(principalURI
);
613 NS_ENSURE_SUCCESS(rv
, rv
);
615 // We turn each int enum field into a JSON string key of the object, aWriter
616 // is set up to be inside of the inner object that has stringified enum keys
617 // An example inner object might be:
621 // {"0": "https://mozilla.com", "2": "^privateBrowsingId=1"}
623 // ----------------------------- |
625 // Key ----------------------
628 WriteJSONProperty
<eURI
>(aWriter
, principalURI
);
630 if (GetHasExplicitDomain()) {
631 nsAutoCString domainStr
;
633 MutexAutoLock
lock(mMutex
);
634 rv
= mDomain
->GetSpec(domainStr
);
635 NS_ENSURE_SUCCESS(rv
, rv
);
637 WriteJSONProperty
<eDomain
>(aWriter
, domainStr
);
640 nsAutoCString suffix
;
641 OriginAttributesRef().CreateSuffix(suffix
);
642 if (suffix
.Length() > 0) {
643 WriteJSONProperty
<eSuffix
>(aWriter
, suffix
);
649 bool ContentPrincipalJSONHandler::startObject() {
652 mState
= State::StartObject
;
655 NS_WARNING("Unexpected object value");
656 mState
= State::Error
;
663 bool ContentPrincipalJSONHandler::propertyName(const JS::Latin1Char
* name
,
666 case State::StartObject
:
667 case State::AfterPropertyValue
: {
670 nsPrintfCString("Unexpected property name length: %zu", length
)
672 mState
= State::Error
;
676 char key
= char(name
[0]);
678 case ContentPrincipal::URIKey
:
679 mState
= State::URIKey
;
681 case ContentPrincipal::DomainKey
:
682 mState
= State::DomainKey
;
684 case ContentPrincipal::SuffixKey
:
685 mState
= State::SuffixKey
;
689 nsPrintfCString("Unexpected property name: '%c'", key
).get());
690 mState
= State::Error
;
696 NS_WARNING("Unexpected property name");
697 mState
= State::Error
;
704 bool ContentPrincipalJSONHandler::endObject() {
706 case State::AfterPropertyValue
: {
707 MOZ_ASSERT(mPrincipalURI
);
708 // NOTE: mDomain is optional.
710 nsAutoCString originNoSuffix
;
711 nsresult rv
= ContentPrincipal::GenerateOriginNoSuffixFromURI(
712 mPrincipalURI
, originNoSuffix
);
714 mState
= State::Error
;
719 new ContentPrincipal(mPrincipalURI
, mAttrs
, originNoSuffix
, mDomain
);
720 MOZ_ASSERT(mPrincipal
);
722 mState
= State::EndObject
;
726 NS_WARNING("Unexpected end of object");
727 mState
= State::Error
;
734 bool ContentPrincipalJSONHandler::stringValue(const JS::Latin1Char
* str
,
737 case State::URIKey
: {
738 nsDependentCSubstring
spec(reinterpret_cast<const char*>(str
), length
);
740 nsresult rv
= NS_NewURI(getter_AddRefs(mPrincipalURI
), spec
);
742 mState
= State::Error
;
747 // Enforce re-parsing about: URIs so that if they change, we
748 // continue to use their new principals correctly.
749 if (mPrincipalURI
->SchemeIs("about")) {
751 mPrincipalURI
->GetSpec(spec
);
752 rv
= NS_NewURI(getter_AddRefs(mPrincipalURI
), spec
);
754 mState
= State::Error
;
760 mState
= State::AfterPropertyValue
;
763 case State::DomainKey
: {
764 nsDependentCSubstring
spec(reinterpret_cast<const char*>(str
), length
);
766 nsresult rv
= NS_NewURI(getter_AddRefs(mDomain
), spec
);
768 mState
= State::Error
;
772 mState
= State::AfterPropertyValue
;
775 case State::SuffixKey
: {
776 nsDependentCSubstring
attrs(reinterpret_cast<const char*>(str
), length
);
777 if (!mAttrs
.PopulateFromSuffix(attrs
)) {
778 mState
= State::Error
;
782 mState
= State::AfterPropertyValue
;
786 NS_WARNING("Unexpected string value");
787 mState
= State::Error
;