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"
11 #include "nsStringBuffer.h"
12 #include "mozilla/BasePrincipal.h"
13 #include "mozilla/StaticPtr.h"
14 #include "mozilla/dom/StructuredCloneTags.h"
15 #include "mozilla/ipc/BackgroundUtils.h"
16 #include "mozilla/ipc/PBackgroundSharedTypes.h"
18 using namespace mozilla
;
19 using namespace mozilla::dom
;
20 using namespace mozilla::ipc
;
22 NS_IMETHODIMP_(MozExternalRefCountType
)
23 nsJSPrincipals::AddRef() {
24 MOZ_ASSERT(int32_t(refcount
) >= 0, "illegal refcnt");
25 nsrefcnt count
= ++refcount
;
26 NS_LOG_ADDREF(this, count
, "nsJSPrincipals", sizeof(*this));
30 NS_IMETHODIMP_(MozExternalRefCountType
)
31 nsJSPrincipals::Release() {
32 MOZ_ASSERT(0 != refcount
, "dup release");
33 nsrefcnt count
= --refcount
;
34 NS_LOG_RELEASE(this, count
, "nsJSPrincipals");
43 bool nsJSPrincipals::Subsume(JSPrincipals
* jsprin
, JSPrincipals
* other
) {
45 nsresult rv
= nsJSPrincipals::get(jsprin
)->Subsumes(
46 nsJSPrincipals::get(other
), &result
);
47 return NS_SUCCEEDED(rv
) && result
;
51 void nsJSPrincipals::Destroy(JSPrincipals
* jsprin
) {
52 // The JS runtime can call this method during the last GC when
53 // nsScriptSecurityManager is destroyed. So we must not assume here that
54 // the security manager still exists.
56 nsJSPrincipals
* nsjsprin
= nsJSPrincipals::get(jsprin
);
58 // We need to destroy the nsIPrincipal. We'll do this by adding
59 // to the refcount and calling release
61 #ifdef NS_BUILD_REFCNT_LOGGING
62 // The refcount logging considers AddRef-to-1 to indicate creation,
63 // so trick it into thinking it's otherwise, but balance the
64 // Release() we do below.
76 // Defined here so one can do principals->dump() in the debugger
77 JS_PUBLIC_API
void JSPrincipals::dump() {
78 if (debugToken
== nsJSPrincipals::DEBUG_TOKEN
) {
80 nsresult rv
= static_cast<nsJSPrincipals
*>(this)->GetScriptLocation(str
);
81 fprintf(stderr
, "nsIPrincipal (%p) = %s\n", static_cast<void*>(this),
82 NS_SUCCEEDED(rv
) ? str
.get() : "(unknown)");
85 "!!! JSPrincipals (%p) is not nsJSPrincipals instance - bad token: "
86 "actual=0x%x expected=0x%x\n",
87 this, unsigned(debugToken
), unsigned(nsJSPrincipals::DEBUG_TOKEN
));
94 bool nsJSPrincipals::ReadPrincipals(JSContext
* aCx
,
95 JSStructuredCloneReader
* aReader
,
96 JSPrincipals
** aOutPrincipals
) {
99 if (!JS_ReadUint32Pair(aReader
, &tag
, &unused
)) {
103 if (tag
!= SCTAG_DOM_NULL_PRINCIPAL
&& tag
!= SCTAG_DOM_SYSTEM_PRINCIPAL
&&
104 tag
!= SCTAG_DOM_CONTENT_PRINCIPAL
&&
105 tag
!= SCTAG_DOM_EXPANDED_PRINCIPAL
) {
106 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
110 return ReadKnownPrincipalType(aCx
, aReader
, tag
, aOutPrincipals
);
113 static bool ReadPrincipalInfo(JSStructuredCloneReader
* aReader
,
114 OriginAttributes
& aAttrs
, nsACString
& aSpec
,
115 nsACString
& aOriginNoSuffix
,
116 nsACString
& aBaseDomain
) {
117 uint32_t suffixLength
, specLength
;
118 if (!JS_ReadUint32Pair(aReader
, &suffixLength
, &specLength
)) {
122 nsAutoCString suffix
;
123 if (!suffix
.SetLength(suffixLength
, fallible
)) {
127 if (!JS_ReadBytes(aReader
, suffix
.BeginWriting(), suffixLength
)) {
131 if (!aAttrs
.PopulateFromSuffix(suffix
)) {
135 if (!aSpec
.SetLength(specLength
, fallible
)) {
139 if (!JS_ReadBytes(aReader
, aSpec
.BeginWriting(), specLength
)) {
143 uint32_t originNoSuffixLength
, dummy
;
144 if (!JS_ReadUint32Pair(aReader
, &originNoSuffixLength
, &dummy
)) {
148 MOZ_ASSERT(dummy
== 0);
153 if (!aOriginNoSuffix
.SetLength(originNoSuffixLength
, fallible
)) {
157 if (!JS_ReadBytes(aReader
, aOriginNoSuffix
.BeginWriting(),
158 originNoSuffixLength
)) {
162 uint32_t baseDomainIsVoid
, baseDomainLength
;
163 if (!JS_ReadUint32Pair(aReader
, &baseDomainIsVoid
, &baseDomainLength
)) {
167 if (baseDomainIsVoid
!= 0 && baseDomainIsVoid
!= 1) {
171 if (baseDomainIsVoid
) {
172 if (baseDomainLength
!= 0) {
176 aBaseDomain
.SetIsVoid(true);
180 if (!aBaseDomain
.SetLength(baseDomainLength
, fallible
)) {
184 if (!JS_ReadBytes(aReader
, aBaseDomain
.BeginWriting(), baseDomainLength
)) {
191 static bool ReadPrincipalInfo(JSStructuredCloneReader
* aReader
, uint32_t aTag
,
192 PrincipalInfo
& aInfo
) {
193 if (aTag
== SCTAG_DOM_SYSTEM_PRINCIPAL
) {
194 aInfo
= SystemPrincipalInfo();
195 } else if (aTag
== SCTAG_DOM_NULL_PRINCIPAL
) {
196 OriginAttributes attrs
;
198 nsAutoCString originNoSuffix
;
199 nsAutoCString baseDomain
;
200 if (!::ReadPrincipalInfo(aReader
, attrs
, spec
, originNoSuffix
,
204 aInfo
= NullPrincipalInfo(attrs
, spec
);
205 } else if (aTag
== SCTAG_DOM_EXPANDED_PRINCIPAL
) {
206 uint32_t length
, unused
;
207 if (!JS_ReadUint32Pair(aReader
, &length
, &unused
)) {
211 ExpandedPrincipalInfo expanded
;
213 for (uint32_t i
= 0; i
< length
; i
++) {
215 if (!JS_ReadUint32Pair(aReader
, &tag
, &unused
)) {
220 if (!ReadPrincipalInfo(aReader
, tag
, sub
)) {
223 expanded
.allowlist().AppendElement(sub
);
227 } else if (aTag
== SCTAG_DOM_CONTENT_PRINCIPAL
) {
228 OriginAttributes attrs
;
230 nsAutoCString originNoSuffix
;
231 nsAutoCString baseDomain
;
232 if (!::ReadPrincipalInfo(aReader
, attrs
, spec
, originNoSuffix
,
238 if (originNoSuffix
.IsEmpty()) {
243 MOZ_DIAGNOSTIC_ASSERT(!originNoSuffix
.IsEmpty());
245 // XXX: Do we care about mDomain for structured clone?
246 aInfo
= ContentPrincipalInfo(attrs
, originNoSuffix
, spec
, Nothing(),
252 MOZ_CRASH("unexpected principal structured clone tag");
260 bool nsJSPrincipals::ReadPrincipalInfo(JSStructuredCloneReader
* aReader
,
261 PrincipalInfo
& aInfo
) {
262 uint32_t tag
, unused
;
263 if (!JS_ReadUint32Pair(aReader
, &tag
, &unused
)) {
266 return ::ReadPrincipalInfo(aReader
, tag
, aInfo
);
270 bool nsJSPrincipals::ReadKnownPrincipalType(JSContext
* aCx
,
271 JSStructuredCloneReader
* aReader
,
273 JSPrincipals
** aOutPrincipals
) {
274 MOZ_ASSERT(aTag
== SCTAG_DOM_NULL_PRINCIPAL
||
275 aTag
== SCTAG_DOM_SYSTEM_PRINCIPAL
||
276 aTag
== SCTAG_DOM_CONTENT_PRINCIPAL
||
277 aTag
== SCTAG_DOM_EXPANDED_PRINCIPAL
);
280 if (!::ReadPrincipalInfo(aReader
, aTag
, info
)) {
284 auto principalOrErr
= PrincipalInfoToPrincipal(info
);
285 if (NS_WARN_IF(principalOrErr
.isErr())) {
286 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
290 nsCOMPtr
<nsIPrincipal
> principal
= principalOrErr
.unwrap();
292 *aOutPrincipals
= get(principal
.forget().take());
296 static bool WritePrincipalInfo(JSStructuredCloneWriter
* aWriter
,
297 const OriginAttributes
& aAttrs
,
298 const nsCString
& aSpec
,
299 const nsCString
& aOriginNoSuffix
,
300 const nsCString
& aBaseDomain
) {
301 nsAutoCString suffix
;
302 aAttrs
.CreateSuffix(suffix
);
304 if (!(JS_WriteUint32Pair(aWriter
, suffix
.Length(), aSpec
.Length()) &&
305 JS_WriteBytes(aWriter
, suffix
.get(), suffix
.Length()) &&
306 JS_WriteBytes(aWriter
, aSpec
.get(), aSpec
.Length()) &&
307 JS_WriteUint32Pair(aWriter
, aOriginNoSuffix
.Length(), 0) &&
308 JS_WriteBytes(aWriter
, aOriginNoSuffix
.get(),
309 aOriginNoSuffix
.Length()))) {
313 if (aBaseDomain
.IsVoid()) {
314 return JS_WriteUint32Pair(aWriter
, 1, 0);
317 return JS_WriteUint32Pair(aWriter
, 0, aBaseDomain
.Length()) &&
318 JS_WriteBytes(aWriter
, aBaseDomain
.get(), aBaseDomain
.Length());
322 bool nsJSPrincipals::WritePrincipalInfo(JSStructuredCloneWriter
* aWriter
,
323 const PrincipalInfo
& aInfo
) {
324 if (aInfo
.type() == PrincipalInfo::TNullPrincipalInfo
) {
325 const NullPrincipalInfo
& nullInfo
= aInfo
;
326 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_NULL_PRINCIPAL
, 0) &&
327 ::WritePrincipalInfo(aWriter
, nullInfo
.attrs(), nullInfo
.spec(),
330 if (aInfo
.type() == PrincipalInfo::TSystemPrincipalInfo
) {
331 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_SYSTEM_PRINCIPAL
, 0);
333 if (aInfo
.type() == PrincipalInfo::TExpandedPrincipalInfo
) {
334 const ExpandedPrincipalInfo
& expanded
= aInfo
;
335 if (!JS_WriteUint32Pair(aWriter
, SCTAG_DOM_EXPANDED_PRINCIPAL
, 0) ||
336 !JS_WriteUint32Pair(aWriter
, expanded
.allowlist().Length(), 0)) {
340 for (uint32_t i
= 0; i
< expanded
.allowlist().Length(); i
++) {
341 if (!WritePrincipalInfo(aWriter
, expanded
.allowlist()[i
])) {
348 MOZ_ASSERT(aInfo
.type() == PrincipalInfo::TContentPrincipalInfo
);
349 const ContentPrincipalInfo
& cInfo
= aInfo
;
350 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_CONTENT_PRINCIPAL
, 0) &&
351 ::WritePrincipalInfo(aWriter
, cInfo
.attrs(), cInfo
.spec(),
352 cInfo
.originNoSuffix(), cInfo
.baseDomain());
355 bool nsJSPrincipals::write(JSContext
* aCx
, JSStructuredCloneWriter
* aWriter
) {
357 if (NS_WARN_IF(NS_FAILED(PrincipalToPrincipalInfo(this, &info
)))) {
358 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
362 return WritePrincipalInfo(aWriter
, info
);
365 bool nsJSPrincipals::isSystemOrAddonPrincipal() {
366 JS::AutoSuppressGCAnalysis suppress
;
367 return this->IsSystemPrincipal() ||
368 this->GetIsAddonOrExpandedAddonPrincipal();