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/dom/StructuredCloneTags.h"
16 // for mozilla::dom::workerinternals::kJSPrincipalsDebugToken
17 #include "mozilla/dom/workerinternals/JSSettings.h"
18 // for mozilla::dom::worklet::kJSPrincipalsDebugToken
19 #include "mozilla/dom/WorkletPrincipals.h"
20 #include "mozilla/ipc/BackgroundUtils.h"
22 using namespace mozilla
;
23 using namespace mozilla::ipc
;
25 NS_IMETHODIMP_(MozExternalRefCountType
)
26 nsJSPrincipals::AddRef() {
27 MOZ_ASSERT(NS_IsMainThread());
28 MOZ_ASSERT(int32_t(refcount
) >= 0, "illegal refcnt");
29 nsrefcnt count
= ++refcount
;
30 NS_LOG_ADDREF(this, count
, "nsJSPrincipals", sizeof(*this));
34 NS_IMETHODIMP_(MozExternalRefCountType
)
35 nsJSPrincipals::Release() {
36 MOZ_ASSERT(NS_IsMainThread());
37 MOZ_ASSERT(0 != refcount
, "dup release");
38 nsrefcnt count
= --refcount
;
39 NS_LOG_RELEASE(this, count
, "nsJSPrincipals");
48 bool nsJSPrincipals::Subsume(JSPrincipals
* jsprin
, JSPrincipals
* other
) {
50 nsresult rv
= nsJSPrincipals::get(jsprin
)->Subsumes(
51 nsJSPrincipals::get(other
), &result
);
52 return NS_SUCCEEDED(rv
) && result
;
56 void nsJSPrincipals::Destroy(JSPrincipals
* jsprin
) {
57 // The JS runtime can call this method during the last GC when
58 // nsScriptSecurityManager is destroyed. So we must not assume here that
59 // the security manager still exists.
61 nsJSPrincipals
* nsjsprin
= nsJSPrincipals::get(jsprin
);
63 // We need to destroy the nsIPrincipal. We'll do this by adding
64 // to the refcount and calling release
66 #ifdef NS_BUILD_REFCNT_LOGGING
67 // The refcount logging considers AddRef-to-1 to indicate creation,
68 // so trick it into thinking it's otherwise, but balance the
69 // Release() we do below.
81 // Defined here so one can do principals->dump() in the debugger
82 JS_PUBLIC_API
void JSPrincipals::dump() {
83 if (debugToken
== nsJSPrincipals::DEBUG_TOKEN
) {
85 nsresult rv
= static_cast<nsJSPrincipals
*>(this)->GetScriptLocation(str
);
86 fprintf(stderr
, "nsIPrincipal (%p) = %s\n", static_cast<void*>(this),
87 NS_SUCCEEDED(rv
) ? str
.get() : "(unknown)");
88 } else if (debugToken
== dom::workerinternals::kJSPrincipalsDebugToken
) {
89 fprintf(stderr
, "Web Worker principal singleton (%p)\n", this);
90 } else if (debugToken
== dom::WorkletPrincipals::kJSPrincipalsDebugToken
) {
91 fprintf(stderr
, "Web Worklet principal (%p)\n", this);
94 "!!! JSPrincipals (%p) is not nsJSPrincipals instance - bad token: "
95 "actual=0x%x expected=0x%x\n",
96 this, unsigned(debugToken
), unsigned(nsJSPrincipals::DEBUG_TOKEN
));
103 bool nsJSPrincipals::ReadPrincipals(JSContext
* aCx
,
104 JSStructuredCloneReader
* aReader
,
105 JSPrincipals
** aOutPrincipals
) {
108 if (!JS_ReadUint32Pair(aReader
, &tag
, &unused
)) {
112 if (!(tag
== SCTAG_DOM_NULL_PRINCIPAL
|| tag
== SCTAG_DOM_SYSTEM_PRINCIPAL
||
113 tag
== SCTAG_DOM_CONTENT_PRINCIPAL
||
114 tag
== SCTAG_DOM_EXPANDED_PRINCIPAL
||
115 tag
== SCTAG_DOM_WORKER_PRINCIPAL
)) {
116 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
120 return ReadKnownPrincipalType(aCx
, aReader
, tag
, aOutPrincipals
);
123 static bool ReadPrincipalInfo(JSStructuredCloneReader
* aReader
,
124 OriginAttributes
& aAttrs
, nsACString
& aSpec
,
125 nsACString
& aOriginNoSuffix
,
126 nsACString
& aBaseDomain
) {
127 uint32_t suffixLength
, specLength
;
128 if (!JS_ReadUint32Pair(aReader
, &suffixLength
, &specLength
)) {
132 nsAutoCString suffix
;
133 if (!suffix
.SetLength(suffixLength
, fallible
)) {
137 if (!JS_ReadBytes(aReader
, suffix
.BeginWriting(), suffixLength
)) {
141 if (!aAttrs
.PopulateFromSuffix(suffix
)) {
145 if (!aSpec
.SetLength(specLength
, fallible
)) {
149 if (!JS_ReadBytes(aReader
, aSpec
.BeginWriting(), specLength
)) {
153 uint32_t originNoSuffixLength
, dummy
;
154 if (!JS_ReadUint32Pair(aReader
, &originNoSuffixLength
, &dummy
)) {
158 MOZ_ASSERT(dummy
== 0);
163 if (!aOriginNoSuffix
.SetLength(originNoSuffixLength
, fallible
)) {
167 if (!JS_ReadBytes(aReader
, aOriginNoSuffix
.BeginWriting(),
168 originNoSuffixLength
)) {
172 uint32_t baseDomainIsVoid
, baseDomainLength
;
173 if (!JS_ReadUint32Pair(aReader
, &baseDomainIsVoid
, &baseDomainLength
)) {
177 MOZ_ASSERT(baseDomainIsVoid
== 0 || baseDomainIsVoid
== 1);
179 if (baseDomainIsVoid
) {
180 MOZ_ASSERT(baseDomainLength
== 0);
182 aBaseDomain
.SetIsVoid(true);
186 if (!aBaseDomain
.SetLength(baseDomainLength
, fallible
)) {
190 if (!JS_ReadBytes(aReader
, aBaseDomain
.BeginWriting(), baseDomainLength
)) {
197 static bool ReadPrincipalInfo(JSStructuredCloneReader
* aReader
, uint32_t aTag
,
198 PrincipalInfo
& aInfo
) {
199 if (aTag
== SCTAG_DOM_SYSTEM_PRINCIPAL
) {
200 aInfo
= SystemPrincipalInfo();
201 } else if (aTag
== SCTAG_DOM_NULL_PRINCIPAL
) {
202 OriginAttributes attrs
;
204 nsAutoCString originNoSuffix
;
205 nsAutoCString baseDomain
;
206 if (!ReadPrincipalInfo(aReader
, attrs
, spec
, originNoSuffix
, baseDomain
)) {
209 aInfo
= NullPrincipalInfo(attrs
, spec
);
210 } else if (aTag
== SCTAG_DOM_EXPANDED_PRINCIPAL
) {
211 uint32_t length
, unused
;
212 if (!JS_ReadUint32Pair(aReader
, &length
, &unused
)) {
216 ExpandedPrincipalInfo expanded
;
218 for (uint32_t i
= 0; i
< length
; i
++) {
220 if (!JS_ReadUint32Pair(aReader
, &tag
, &unused
)) {
225 if (!ReadPrincipalInfo(aReader
, tag
, sub
)) {
228 expanded
.allowlist().AppendElement(sub
);
232 } else if (aTag
== SCTAG_DOM_CONTENT_PRINCIPAL
) {
233 OriginAttributes attrs
;
235 nsAutoCString originNoSuffix
;
236 nsAutoCString baseDomain
;
237 if (!ReadPrincipalInfo(aReader
, attrs
, spec
, originNoSuffix
, baseDomain
)) {
242 if (originNoSuffix
.IsEmpty()) {
247 MOZ_DIAGNOSTIC_ASSERT(!originNoSuffix
.IsEmpty());
249 // XXX: Do we care about mDomain for structured clone?
250 aInfo
= ContentPrincipalInfo(attrs
, originNoSuffix
, spec
, Nothing(),
256 MOZ_CRASH("unexpected principal structured clone tag");
263 static StaticRefPtr
<nsIPrincipal
> sActiveWorkerPrincipal
;
265 nsJSPrincipals::AutoSetActiveWorkerPrincipal::AutoSetActiveWorkerPrincipal(
266 nsIPrincipal
* aPrincipal
) {
267 MOZ_ASSERT(NS_IsMainThread());
268 MOZ_RELEASE_ASSERT(!sActiveWorkerPrincipal
);
269 sActiveWorkerPrincipal
= aPrincipal
;
272 nsJSPrincipals::AutoSetActiveWorkerPrincipal::~AutoSetActiveWorkerPrincipal() {
273 sActiveWorkerPrincipal
= nullptr;
277 bool nsJSPrincipals::ReadKnownPrincipalType(JSContext
* aCx
,
278 JSStructuredCloneReader
* aReader
,
280 JSPrincipals
** aOutPrincipals
) {
281 MOZ_ASSERT(aTag
== SCTAG_DOM_NULL_PRINCIPAL
||
282 aTag
== SCTAG_DOM_SYSTEM_PRINCIPAL
||
283 aTag
== SCTAG_DOM_CONTENT_PRINCIPAL
||
284 aTag
== SCTAG_DOM_EXPANDED_PRINCIPAL
||
285 aTag
== SCTAG_DOM_WORKER_PRINCIPAL
);
287 if (NS_WARN_IF(!NS_IsMainThread())) {
288 xpc::Throw(aCx
, NS_ERROR_UNCATCHABLE_EXCEPTION
);
292 if (aTag
== SCTAG_DOM_WORKER_PRINCIPAL
) {
293 // When reading principals which were written on a worker thread, we need to
294 // know the principal of the worker which did the write.
295 if (!sActiveWorkerPrincipal
) {
296 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
299 RefPtr
<nsJSPrincipals
> retval
= get(sActiveWorkerPrincipal
);
300 retval
.forget(aOutPrincipals
);
305 if (!ReadPrincipalInfo(aReader
, aTag
, info
)) {
309 auto principalOrErr
= PrincipalInfoToPrincipal(info
);
310 if (NS_WARN_IF(principalOrErr
.isErr())) {
311 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
315 nsCOMPtr
<nsIPrincipal
> principal
= principalOrErr
.unwrap();
317 *aOutPrincipals
= get(principal
.forget().take());
321 static bool WritePrincipalInfo(JSStructuredCloneWriter
* aWriter
,
322 const OriginAttributes
& aAttrs
,
323 const nsCString
& aSpec
,
324 const nsCString
& aOriginNoSuffix
,
325 const nsCString
& aBaseDomain
) {
326 nsAutoCString suffix
;
327 aAttrs
.CreateSuffix(suffix
);
329 if (!(JS_WriteUint32Pair(aWriter
, suffix
.Length(), aSpec
.Length()) &&
330 JS_WriteBytes(aWriter
, suffix
.get(), suffix
.Length()) &&
331 JS_WriteBytes(aWriter
, aSpec
.get(), aSpec
.Length()) &&
332 JS_WriteUint32Pair(aWriter
, aOriginNoSuffix
.Length(), 0) &&
333 JS_WriteBytes(aWriter
, aOriginNoSuffix
.get(),
334 aOriginNoSuffix
.Length()))) {
338 if (aBaseDomain
.IsVoid()) {
339 return JS_WriteUint32Pair(aWriter
, 1, 0);
342 return JS_WriteUint32Pair(aWriter
, 0, aBaseDomain
.Length()) &&
343 JS_WriteBytes(aWriter
, aBaseDomain
.get(), aBaseDomain
.Length());
347 bool nsJSPrincipals::WritePrincipalInfo(JSStructuredCloneWriter
* aWriter
,
348 const PrincipalInfo
& aInfo
) {
349 if (aInfo
.type() == PrincipalInfo::TNullPrincipalInfo
) {
350 const NullPrincipalInfo
& nullInfo
= aInfo
;
351 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_NULL_PRINCIPAL
, 0) &&
352 ::WritePrincipalInfo(aWriter
, nullInfo
.attrs(), nullInfo
.spec(),
355 if (aInfo
.type() == PrincipalInfo::TSystemPrincipalInfo
) {
356 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_SYSTEM_PRINCIPAL
, 0);
358 if (aInfo
.type() == PrincipalInfo::TExpandedPrincipalInfo
) {
359 const ExpandedPrincipalInfo
& expanded
= aInfo
;
360 if (!JS_WriteUint32Pair(aWriter
, SCTAG_DOM_EXPANDED_PRINCIPAL
, 0) ||
361 !JS_WriteUint32Pair(aWriter
, expanded
.allowlist().Length(), 0)) {
365 for (uint32_t i
= 0; i
< expanded
.allowlist().Length(); i
++) {
366 if (!WritePrincipalInfo(aWriter
, expanded
.allowlist()[i
])) {
373 MOZ_ASSERT(aInfo
.type() == PrincipalInfo::TContentPrincipalInfo
);
374 const ContentPrincipalInfo
& cInfo
= aInfo
;
375 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_CONTENT_PRINCIPAL
, 0) &&
376 ::WritePrincipalInfo(aWriter
, cInfo
.attrs(), cInfo
.spec(),
377 cInfo
.originNoSuffix(), cInfo
.baseDomain());
380 bool nsJSPrincipals::write(JSContext
* aCx
, JSStructuredCloneWriter
* aWriter
) {
382 if (NS_WARN_IF(NS_FAILED(PrincipalToPrincipalInfo(this, &info
)))) {
383 xpc::Throw(aCx
, NS_ERROR_DOM_DATA_CLONE_ERR
);
387 return WritePrincipalInfo(aWriter
, info
);
390 bool nsJSPrincipals::isSystemOrAddonPrincipal() {
391 JS::AutoSuppressGCAnalysis suppress
;
392 return this->IsSystemPrincipal() ||
393 this->GetIsAddonOrExpandedAddonPrincipal();