1 /* -*- Mode: C++; tab-width: 4; 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/. */
6 #include "nsIPrincipal.h"
9 #include "nsJSPrincipals.h"
12 #include "nsStringBuffer.h"
13 #include "mozilla/BasePrincipal.h"
14 #include "mozilla/StaticPtr.h"
15 #include "mozilla/dom/StructuredCloneTags.h"
16 #include "mozilla/ipc/BackgroundUtils.h"
17 #include "mozilla/ipc/PBackgroundSharedTypes.h"
19 using namespace mozilla
;
20 using namespace mozilla::dom
;
21 using namespace mozilla::ipc
;
23 NS_IMETHODIMP_(MozExternalRefCountType
)
24 nsJSPrincipals::AddRef() {
25 MOZ_ASSERT(int32_t(refcount
) >= 0, "illegal refcnt");
26 nsrefcnt count
= ++refcount
;
27 NS_LOG_ADDREF(this, count
, "nsJSPrincipals", sizeof(*this));
31 NS_IMETHODIMP_(MozExternalRefCountType
)
32 nsJSPrincipals::Release() {
33 MOZ_ASSERT(0 != refcount
, "dup release");
34 nsrefcnt count
= --refcount
;
35 NS_LOG_RELEASE(this, count
, "nsJSPrincipals");
44 bool nsJSPrincipals::Subsume(JSPrincipals
* jsprin
, JSPrincipals
* other
) {
46 nsresult rv
= nsJSPrincipals::get(jsprin
)->Subsumes(
47 nsJSPrincipals::get(other
), &result
);
48 return NS_SUCCEEDED(rv
) && result
;
52 void nsJSPrincipals::Destroy(JSPrincipals
* jsprin
) {
53 // The JS runtime can call this method during the last GC when
54 // nsScriptSecurityManager is destroyed. So we must not assume here that
55 // the security manager still exists.
57 nsJSPrincipals
* nsjsprin
= nsJSPrincipals::get(jsprin
);
59 // We need to destroy the nsIPrincipal. We'll do this by adding
60 // to the refcount and calling release
62 #ifdef NS_BUILD_REFCNT_LOGGING
63 // The refcount logging considers AddRef-to-1 to indicate creation,
64 // so trick it into thinking it's otherwise, but balance the
65 // Release() we do below.
77 // Defined here so one can do principals->dump() in the debugger
78 JS_PUBLIC_API
void JSPrincipals::dump() {
79 if (debugToken
== nsJSPrincipals::DEBUG_TOKEN
) {
81 nsresult rv
= static_cast<nsJSPrincipals
*>(this)->GetScriptLocation(str
);
82 fprintf(stderr
, "nsIPrincipal (%p) = %s\n", static_cast<void*>(this),
83 NS_SUCCEEDED(rv
) ? str
.get() : "(unknown)");
86 "!!! JSPrincipals (%p) is not nsJSPrincipals instance - bad token: "
87 "actual=0x%x expected=0x%x\n",
88 this, unsigned(debugToken
), unsigned(nsJSPrincipals::DEBUG_TOKEN
));
95 bool nsJSPrincipals::ReadPrincipals(JSContext
* aCx
,
96 JSStructuredCloneReader
* aReader
,
97 JSPrincipals
** aOutPrincipals
) {
100 if (!JS_ReadUint32Pair(aReader
, &tag
, &unused
)) {
104 if (tag
!= SCTAG_DOM_NULL_PRINCIPAL
&& tag
!= SCTAG_DOM_SYSTEM_PRINCIPAL
&&
105 tag
!= SCTAG_DOM_CONTENT_PRINCIPAL
&&
106 tag
!= SCTAG_DOM_EXPANDED_PRINCIPAL
) {
107 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
111 return ReadKnownPrincipalType(aCx
, aReader
, tag
, aOutPrincipals
);
114 static bool ReadPrincipalInfo(JSStructuredCloneReader
* aReader
,
115 OriginAttributes
& aAttrs
, nsACString
& aSpec
,
116 nsACString
& aOriginNoSuffix
,
117 nsACString
& aBaseDomain
) {
118 uint32_t suffixLength
, specLength
;
119 if (!JS_ReadUint32Pair(aReader
, &suffixLength
, &specLength
)) {
123 nsAutoCString suffix
;
124 if (!suffix
.SetLength(suffixLength
, fallible
)) {
128 if (!JS_ReadBytes(aReader
, suffix
.BeginWriting(), suffixLength
)) {
132 if (!aAttrs
.PopulateFromSuffix(suffix
)) {
136 if (!aSpec
.SetLength(specLength
, fallible
)) {
140 if (!JS_ReadBytes(aReader
, aSpec
.BeginWriting(), specLength
)) {
144 uint32_t originNoSuffixLength
, dummy
;
145 if (!JS_ReadUint32Pair(aReader
, &originNoSuffixLength
, &dummy
)) {
149 MOZ_ASSERT(dummy
== 0);
154 if (!aOriginNoSuffix
.SetLength(originNoSuffixLength
, fallible
)) {
158 if (!JS_ReadBytes(aReader
, aOriginNoSuffix
.BeginWriting(),
159 originNoSuffixLength
)) {
163 uint32_t baseDomainIsVoid
, baseDomainLength
;
164 if (!JS_ReadUint32Pair(aReader
, &baseDomainIsVoid
, &baseDomainLength
)) {
168 if (baseDomainIsVoid
!= 0 && baseDomainIsVoid
!= 1) {
172 if (baseDomainIsVoid
) {
173 if (baseDomainLength
!= 0) {
177 aBaseDomain
.SetIsVoid(true);
181 if (!aBaseDomain
.SetLength(baseDomainLength
, fallible
)) {
185 if (!JS_ReadBytes(aReader
, aBaseDomain
.BeginWriting(), baseDomainLength
)) {
192 static bool ReadPrincipalInfo(JSStructuredCloneReader
* aReader
, uint32_t aTag
,
193 PrincipalInfo
& aInfo
) {
194 if (aTag
== SCTAG_DOM_SYSTEM_PRINCIPAL
) {
195 aInfo
= SystemPrincipalInfo();
196 } else if (aTag
== SCTAG_DOM_NULL_PRINCIPAL
) {
197 OriginAttributes attrs
;
199 nsAutoCString originNoSuffix
;
200 nsAutoCString baseDomain
;
201 if (!::ReadPrincipalInfo(aReader
, attrs
, spec
, originNoSuffix
,
205 aInfo
= NullPrincipalInfo(attrs
, spec
);
206 } else if (aTag
== SCTAG_DOM_EXPANDED_PRINCIPAL
) {
207 uint32_t length
, unused
;
208 if (!JS_ReadUint32Pair(aReader
, &length
, &unused
)) {
212 ExpandedPrincipalInfo expanded
;
214 for (uint32_t i
= 0; i
< length
; i
++) {
216 if (!JS_ReadUint32Pair(aReader
, &tag
, &unused
)) {
221 if (!ReadPrincipalInfo(aReader
, tag
, sub
)) {
224 expanded
.allowlist().AppendElement(sub
);
228 } else if (aTag
== SCTAG_DOM_CONTENT_PRINCIPAL
) {
229 OriginAttributes attrs
;
231 nsAutoCString originNoSuffix
;
232 nsAutoCString baseDomain
;
233 if (!::ReadPrincipalInfo(aReader
, attrs
, spec
, originNoSuffix
,
239 if (originNoSuffix
.IsEmpty()) {
244 MOZ_DIAGNOSTIC_ASSERT(!originNoSuffix
.IsEmpty());
246 // XXX: Do we care about mDomain for structured clone?
247 aInfo
= ContentPrincipalInfo(attrs
, originNoSuffix
, spec
, Nothing(),
253 MOZ_CRASH("unexpected principal structured clone tag");
261 bool nsJSPrincipals::ReadPrincipalInfo(JSStructuredCloneReader
* aReader
,
262 PrincipalInfo
& aInfo
) {
263 uint32_t tag
, unused
;
264 if (!JS_ReadUint32Pair(aReader
, &tag
, &unused
)) {
267 return ::ReadPrincipalInfo(aReader
, tag
, aInfo
);
271 bool nsJSPrincipals::ReadKnownPrincipalType(JSContext
* aCx
,
272 JSStructuredCloneReader
* aReader
,
274 JSPrincipals
** aOutPrincipals
) {
275 MOZ_ASSERT(aTag
== SCTAG_DOM_NULL_PRINCIPAL
||
276 aTag
== SCTAG_DOM_SYSTEM_PRINCIPAL
||
277 aTag
== SCTAG_DOM_CONTENT_PRINCIPAL
||
278 aTag
== SCTAG_DOM_EXPANDED_PRINCIPAL
);
281 if (!::ReadPrincipalInfo(aReader
, aTag
, info
)) {
285 auto principalOrErr
= PrincipalInfoToPrincipal(info
);
286 if (NS_WARN_IF(principalOrErr
.isErr())) {
287 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
291 nsCOMPtr
<nsIPrincipal
> principal
= principalOrErr
.unwrap();
293 *aOutPrincipals
= get(principal
.forget().take());
297 static bool WritePrincipalInfo(JSStructuredCloneWriter
* aWriter
,
298 const OriginAttributes
& aAttrs
,
299 const nsCString
& aSpec
,
300 const nsCString
& aOriginNoSuffix
,
301 const nsCString
& aBaseDomain
) {
302 nsAutoCString suffix
;
303 aAttrs
.CreateSuffix(suffix
);
305 if (!(JS_WriteUint32Pair(aWriter
, suffix
.Length(), aSpec
.Length()) &&
306 JS_WriteBytes(aWriter
, suffix
.get(), suffix
.Length()) &&
307 JS_WriteBytes(aWriter
, aSpec
.get(), aSpec
.Length()) &&
308 JS_WriteUint32Pair(aWriter
, aOriginNoSuffix
.Length(), 0) &&
309 JS_WriteBytes(aWriter
, aOriginNoSuffix
.get(),
310 aOriginNoSuffix
.Length()))) {
314 if (aBaseDomain
.IsVoid()) {
315 return JS_WriteUint32Pair(aWriter
, 1, 0);
318 return JS_WriteUint32Pair(aWriter
, 0, aBaseDomain
.Length()) &&
319 JS_WriteBytes(aWriter
, aBaseDomain
.get(), aBaseDomain
.Length());
323 bool nsJSPrincipals::WritePrincipalInfo(JSStructuredCloneWriter
* aWriter
,
324 const PrincipalInfo
& aInfo
) {
325 if (aInfo
.type() == PrincipalInfo::TNullPrincipalInfo
) {
326 const NullPrincipalInfo
& nullInfo
= aInfo
;
327 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_NULL_PRINCIPAL
, 0) &&
328 ::WritePrincipalInfo(aWriter
, nullInfo
.attrs(), nullInfo
.spec(),
331 if (aInfo
.type() == PrincipalInfo::TSystemPrincipalInfo
) {
332 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_SYSTEM_PRINCIPAL
, 0);
334 if (aInfo
.type() == PrincipalInfo::TExpandedPrincipalInfo
) {
335 const ExpandedPrincipalInfo
& expanded
= aInfo
;
336 if (!JS_WriteUint32Pair(aWriter
, SCTAG_DOM_EXPANDED_PRINCIPAL
, 0) ||
337 !JS_WriteUint32Pair(aWriter
, expanded
.allowlist().Length(), 0)) {
341 for (uint32_t i
= 0; i
< expanded
.allowlist().Length(); i
++) {
342 if (!WritePrincipalInfo(aWriter
, expanded
.allowlist()[i
])) {
349 MOZ_ASSERT(aInfo
.type() == PrincipalInfo::TContentPrincipalInfo
);
350 const ContentPrincipalInfo
& cInfo
= aInfo
;
351 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_CONTENT_PRINCIPAL
, 0) &&
352 ::WritePrincipalInfo(aWriter
, cInfo
.attrs(), cInfo
.spec(),
353 cInfo
.originNoSuffix(), cInfo
.baseDomain());
356 bool nsJSPrincipals::write(JSContext
* aCx
, JSStructuredCloneWriter
* aWriter
) {
358 if (NS_WARN_IF(NS_FAILED(PrincipalToPrincipalInfo(this, &info
)))) {
359 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
363 return WritePrincipalInfo(aWriter
, info
);
366 bool nsJSPrincipals::isSystemOrAddonPrincipal() {
367 JS::AutoSuppressGCAnalysis suppress
;
368 return this->IsSystemPrincipal() ||
369 this->GetIsAddonOrExpandedAddonPrincipal();