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"
13 #include "nsStringBuffer.h"
14 #include "mozilla/BasePrincipal.h"
15 #include "mozilla/StaticPtr.h"
16 #include "mozilla/dom/StructuredCloneTags.h"
17 // for mozilla::dom::workerinternals::kJSPrincipalsDebugToken
18 #include "mozilla/dom/workerinternals/JSSettings.h"
19 // for mozilla::dom::worklet::kJSPrincipalsDebugToken
20 #include "mozilla/dom/WorkletPrincipals.h"
21 #include "mozilla/ipc/BackgroundUtils.h"
22 #include "mozilla/ipc/PBackgroundSharedTypes.h"
24 using namespace mozilla
;
25 using namespace mozilla::dom
;
26 using namespace mozilla::ipc
;
28 NS_IMETHODIMP_(MozExternalRefCountType
)
29 nsJSPrincipals::AddRef() {
30 MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
31 MOZ_ASSERT(int32_t(refcount
) >= 0, "illegal refcnt");
32 nsrefcnt count
= ++refcount
;
33 NS_LOG_ADDREF(this, count
, "nsJSPrincipals", sizeof(*this));
37 NS_IMETHODIMP_(MozExternalRefCountType
)
38 nsJSPrincipals::Release() {
39 MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
40 MOZ_ASSERT(0 != refcount
, "dup release");
41 nsrefcnt count
= --refcount
;
42 NS_LOG_RELEASE(this, count
, "nsJSPrincipals");
51 bool nsJSPrincipals::Subsume(JSPrincipals
* jsprin
, JSPrincipals
* other
) {
53 nsresult rv
= nsJSPrincipals::get(jsprin
)->Subsumes(
54 nsJSPrincipals::get(other
), &result
);
55 return NS_SUCCEEDED(rv
) && result
;
59 void nsJSPrincipals::Destroy(JSPrincipals
* jsprin
) {
60 // The JS runtime can call this method during the last GC when
61 // nsScriptSecurityManager is destroyed. So we must not assume here that
62 // the security manager still exists.
64 nsJSPrincipals
* nsjsprin
= nsJSPrincipals::get(jsprin
);
66 // We need to destroy the nsIPrincipal. We'll do this by adding
67 // to the refcount and calling release
69 #ifdef NS_BUILD_REFCNT_LOGGING
70 // The refcount logging considers AddRef-to-1 to indicate creation,
71 // so trick it into thinking it's otherwise, but balance the
72 // Release() we do below.
84 // Defined here so one can do principals->dump() in the debugger
85 JS_PUBLIC_API
void JSPrincipals::dump() {
86 if (debugToken
== nsJSPrincipals::DEBUG_TOKEN
) {
88 nsresult rv
= static_cast<nsJSPrincipals
*>(this)->GetScriptLocation(str
);
89 fprintf(stderr
, "nsIPrincipal (%p) = %s\n", static_cast<void*>(this),
90 NS_SUCCEEDED(rv
) ? str
.get() : "(unknown)");
91 } else if (debugToken
== dom::workerinternals::kJSPrincipalsDebugToken
) {
92 fprintf(stderr
, "Web Worker principal singleton (%p)\n", this);
93 } else if (debugToken
== dom::WorkletPrincipals::kJSPrincipalsDebugToken
) {
94 fprintf(stderr
, "Web Worklet principal (%p)\n", this);
97 "!!! JSPrincipals (%p) is not nsJSPrincipals instance - bad token: "
98 "actual=0x%x expected=0x%x\n",
99 this, unsigned(debugToken
), unsigned(nsJSPrincipals::DEBUG_TOKEN
));
106 bool nsJSPrincipals::ReadPrincipals(JSContext
* aCx
,
107 JSStructuredCloneReader
* aReader
,
108 JSPrincipals
** aOutPrincipals
) {
111 if (!JS_ReadUint32Pair(aReader
, &tag
, &unused
)) {
115 if (!(tag
== SCTAG_DOM_NULL_PRINCIPAL
|| tag
== SCTAG_DOM_SYSTEM_PRINCIPAL
||
116 tag
== SCTAG_DOM_CONTENT_PRINCIPAL
||
117 tag
== SCTAG_DOM_EXPANDED_PRINCIPAL
||
118 tag
== SCTAG_DOM_WORKER_PRINCIPAL
)) {
119 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
123 return ReadKnownPrincipalType(aCx
, aReader
, tag
, aOutPrincipals
);
126 static bool ReadPrincipalInfo(JSStructuredCloneReader
* aReader
,
127 OriginAttributes
& aAttrs
, nsACString
& aSpec
,
128 nsACString
& aOriginNoSuffix
,
129 nsACString
& aBaseDomain
) {
130 uint32_t suffixLength
, specLength
;
131 if (!JS_ReadUint32Pair(aReader
, &suffixLength
, &specLength
)) {
135 nsAutoCString suffix
;
136 if (!suffix
.SetLength(suffixLength
, fallible
)) {
140 if (!JS_ReadBytes(aReader
, suffix
.BeginWriting(), suffixLength
)) {
144 if (!aAttrs
.PopulateFromSuffix(suffix
)) {
148 if (!aSpec
.SetLength(specLength
, fallible
)) {
152 if (!JS_ReadBytes(aReader
, aSpec
.BeginWriting(), specLength
)) {
156 uint32_t originNoSuffixLength
, dummy
;
157 if (!JS_ReadUint32Pair(aReader
, &originNoSuffixLength
, &dummy
)) {
161 MOZ_ASSERT(dummy
== 0);
166 if (!aOriginNoSuffix
.SetLength(originNoSuffixLength
, fallible
)) {
170 if (!JS_ReadBytes(aReader
, aOriginNoSuffix
.BeginWriting(),
171 originNoSuffixLength
)) {
175 uint32_t baseDomainIsVoid
, baseDomainLength
;
176 if (!JS_ReadUint32Pair(aReader
, &baseDomainIsVoid
, &baseDomainLength
)) {
180 if (baseDomainIsVoid
!= 0 && baseDomainIsVoid
!= 1) {
184 if (baseDomainIsVoid
) {
185 if (baseDomainLength
!= 0) {
189 aBaseDomain
.SetIsVoid(true);
193 if (!aBaseDomain
.SetLength(baseDomainLength
, fallible
)) {
197 if (!JS_ReadBytes(aReader
, aBaseDomain
.BeginWriting(), baseDomainLength
)) {
204 static bool ReadPrincipalInfo(JSStructuredCloneReader
* aReader
, uint32_t aTag
,
205 PrincipalInfo
& aInfo
) {
206 if (aTag
== SCTAG_DOM_SYSTEM_PRINCIPAL
) {
207 aInfo
= SystemPrincipalInfo();
208 } else if (aTag
== SCTAG_DOM_NULL_PRINCIPAL
) {
209 OriginAttributes attrs
;
211 nsAutoCString originNoSuffix
;
212 nsAutoCString baseDomain
;
213 if (!ReadPrincipalInfo(aReader
, attrs
, spec
, originNoSuffix
, baseDomain
)) {
216 aInfo
= NullPrincipalInfo(attrs
, spec
);
217 } else if (aTag
== SCTAG_DOM_EXPANDED_PRINCIPAL
) {
218 uint32_t length
, unused
;
219 if (!JS_ReadUint32Pair(aReader
, &length
, &unused
)) {
223 ExpandedPrincipalInfo expanded
;
225 for (uint32_t i
= 0; i
< length
; i
++) {
227 if (!JS_ReadUint32Pair(aReader
, &tag
, &unused
)) {
232 if (!ReadPrincipalInfo(aReader
, tag
, sub
)) {
235 expanded
.allowlist().AppendElement(sub
);
239 } else if (aTag
== SCTAG_DOM_CONTENT_PRINCIPAL
) {
240 OriginAttributes attrs
;
242 nsAutoCString originNoSuffix
;
243 nsAutoCString baseDomain
;
244 if (!ReadPrincipalInfo(aReader
, attrs
, spec
, originNoSuffix
, baseDomain
)) {
249 if (originNoSuffix
.IsEmpty()) {
254 MOZ_DIAGNOSTIC_ASSERT(!originNoSuffix
.IsEmpty());
256 // XXX: Do we care about mDomain for structured clone?
257 aInfo
= ContentPrincipalInfo(attrs
, originNoSuffix
, spec
, Nothing(),
263 MOZ_CRASH("unexpected principal structured clone tag");
270 static StaticRefPtr
<nsIPrincipal
> sActiveWorkerPrincipal
;
272 nsJSPrincipals::AutoSetActiveWorkerPrincipal::AutoSetActiveWorkerPrincipal(
273 nsIPrincipal
* aPrincipal
) {
274 MOZ_ASSERT(NS_IsMainThread());
275 MOZ_RELEASE_ASSERT(!sActiveWorkerPrincipal
);
276 sActiveWorkerPrincipal
= aPrincipal
;
279 nsJSPrincipals::AutoSetActiveWorkerPrincipal::~AutoSetActiveWorkerPrincipal() {
280 sActiveWorkerPrincipal
= nullptr;
284 bool nsJSPrincipals::ReadKnownPrincipalType(JSContext
* aCx
,
285 JSStructuredCloneReader
* aReader
,
287 JSPrincipals
** aOutPrincipals
) {
288 MOZ_ASSERT(aTag
== SCTAG_DOM_NULL_PRINCIPAL
||
289 aTag
== SCTAG_DOM_SYSTEM_PRINCIPAL
||
290 aTag
== SCTAG_DOM_CONTENT_PRINCIPAL
||
291 aTag
== SCTAG_DOM_EXPANDED_PRINCIPAL
||
292 aTag
== SCTAG_DOM_WORKER_PRINCIPAL
);
294 if (NS_WARN_IF(!NS_IsMainThread())) {
295 xpc::Throw(aCx
, NS_ERROR_UNCATCHABLE_EXCEPTION
);
299 if (aTag
== SCTAG_DOM_WORKER_PRINCIPAL
) {
300 // When reading principals which were written on a worker thread, we need to
301 // know the principal of the worker which did the write.
302 if (!sActiveWorkerPrincipal
) {
303 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
306 RefPtr
<nsJSPrincipals
> retval
= get(sActiveWorkerPrincipal
);
307 retval
.forget(aOutPrincipals
);
312 if (!ReadPrincipalInfo(aReader
, aTag
, info
)) {
316 auto principalOrErr
= PrincipalInfoToPrincipal(info
);
317 if (NS_WARN_IF(principalOrErr
.isErr())) {
318 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
322 nsCOMPtr
<nsIPrincipal
> principal
= principalOrErr
.unwrap();
324 *aOutPrincipals
= get(principal
.forget().take());
328 static bool WritePrincipalInfo(JSStructuredCloneWriter
* aWriter
,
329 const OriginAttributes
& aAttrs
,
330 const nsCString
& aSpec
,
331 const nsCString
& aOriginNoSuffix
,
332 const nsCString
& aBaseDomain
) {
333 nsAutoCString suffix
;
334 aAttrs
.CreateSuffix(suffix
);
336 if (!(JS_WriteUint32Pair(aWriter
, suffix
.Length(), aSpec
.Length()) &&
337 JS_WriteBytes(aWriter
, suffix
.get(), suffix
.Length()) &&
338 JS_WriteBytes(aWriter
, aSpec
.get(), aSpec
.Length()) &&
339 JS_WriteUint32Pair(aWriter
, aOriginNoSuffix
.Length(), 0) &&
340 JS_WriteBytes(aWriter
, aOriginNoSuffix
.get(),
341 aOriginNoSuffix
.Length()))) {
345 if (aBaseDomain
.IsVoid()) {
346 return JS_WriteUint32Pair(aWriter
, 1, 0);
349 return JS_WriteUint32Pair(aWriter
, 0, aBaseDomain
.Length()) &&
350 JS_WriteBytes(aWriter
, aBaseDomain
.get(), aBaseDomain
.Length());
354 bool nsJSPrincipals::WritePrincipalInfo(JSStructuredCloneWriter
* aWriter
,
355 const PrincipalInfo
& aInfo
) {
356 if (aInfo
.type() == PrincipalInfo::TNullPrincipalInfo
) {
357 const NullPrincipalInfo
& nullInfo
= aInfo
;
358 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_NULL_PRINCIPAL
, 0) &&
359 ::WritePrincipalInfo(aWriter
, nullInfo
.attrs(), nullInfo
.spec(),
362 if (aInfo
.type() == PrincipalInfo::TSystemPrincipalInfo
) {
363 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_SYSTEM_PRINCIPAL
, 0);
365 if (aInfo
.type() == PrincipalInfo::TExpandedPrincipalInfo
) {
366 const ExpandedPrincipalInfo
& expanded
= aInfo
;
367 if (!JS_WriteUint32Pair(aWriter
, SCTAG_DOM_EXPANDED_PRINCIPAL
, 0) ||
368 !JS_WriteUint32Pair(aWriter
, expanded
.allowlist().Length(), 0)) {
372 for (uint32_t i
= 0; i
< expanded
.allowlist().Length(); i
++) {
373 if (!WritePrincipalInfo(aWriter
, expanded
.allowlist()[i
])) {
380 MOZ_ASSERT(aInfo
.type() == PrincipalInfo::TContentPrincipalInfo
);
381 const ContentPrincipalInfo
& cInfo
= aInfo
;
382 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_CONTENT_PRINCIPAL
, 0) &&
383 ::WritePrincipalInfo(aWriter
, cInfo
.attrs(), cInfo
.spec(),
384 cInfo
.originNoSuffix(), cInfo
.baseDomain());
387 bool nsJSPrincipals::write(JSContext
* aCx
, JSStructuredCloneWriter
* aWriter
) {
389 if (NS_WARN_IF(NS_FAILED(PrincipalToPrincipalInfo(this, &info
)))) {
390 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
394 return WritePrincipalInfo(aWriter
, info
);
397 bool nsJSPrincipals::isSystemOrAddonPrincipal() {
398 JS::AutoSuppressGCAnalysis suppress
;
399 return this->IsSystemPrincipal() ||
400 this->GetIsAddonOrExpandedAddonPrincipal();