2018-11-30 19:52:05 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2003-10-21 22:11:49 +00:00
|
|
|
|
2020-05-12 19:02:05 +00:00
|
|
|
#include "nsIPrincipal.h"
|
2016-08-04 18:19:24 +00:00
|
|
|
#include "xpcpublic.h"
|
2003-10-21 22:11:49 +00:00
|
|
|
#include "nsString.h"
|
1999-08-20 09:51:02 +00:00
|
|
|
#include "nsJSPrincipals.h"
|
|
|
|
#include "plstr.h"
|
2001-07-31 19:05:34 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsMemory.h"
|
2007-09-28 14:31:04 +00:00
|
|
|
#include "nsStringBuffer.h"
|
2019-12-05 04:44:32 +00:00
|
|
|
#include "mozilla/BasePrincipal.h"
|
2015-10-02 14:44:00 +00:00
|
|
|
#include "mozilla/dom/StructuredCloneTags.h"
|
2018-02-05 18:55:07 +00:00
|
|
|
// for mozilla::dom::workerinternals::kJSPrincipalsDebugToken
|
2018-01-31 07:24:59 +00:00
|
|
|
#include "mozilla/dom/workerinternals/JSSettings.h"
|
2018-04-12 03:14:48 +00:00
|
|
|
// for mozilla::dom::worklet::kJSPrincipalsDebugToken
|
2019-09-06 02:01:44 +00:00
|
|
|
#include "mozilla/dom/WorkletPrincipals.h"
|
2015-10-02 14:44:00 +00:00
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::ipc;
|
2005-07-08 23:26:36 +00:00
|
|
|
|
2015-05-11 20:16:36 +00:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsJSPrincipals::AddRef() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-04-28 19:50:58 +00:00
|
|
|
MOZ_ASSERT(int32_t(refcount) >= 0, "illegal refcnt");
|
2015-05-11 20:16:36 +00:00
|
|
|
nsrefcnt count = ++refcount;
|
|
|
|
NS_LOG_ADDREF(this, count, "nsJSPrincipals", sizeof(*this));
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsJSPrincipals::Release() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-04-28 19:50:58 +00:00
|
|
|
MOZ_ASSERT(0 != refcount, "dup release");
|
2015-05-11 20:16:36 +00:00
|
|
|
nsrefcnt count = --refcount;
|
|
|
|
NS_LOG_RELEASE(this, count, "nsJSPrincipals");
|
|
|
|
if (count == 0) {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:03:35 +00:00
|
|
|
/* static */
|
|
|
|
bool nsJSPrincipals::Subsume(JSPrincipals* jsprin, JSPrincipals* other) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool result;
|
2012-03-09 09:48:50 +00:00
|
|
|
nsresult rv = nsJSPrincipals::get(jsprin)->Subsumes(
|
|
|
|
nsJSPrincipals::get(other), &result);
|
2005-07-08 23:26:36 +00:00
|
|
|
return NS_SUCCEEDED(rv) && result;
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:03:35 +00:00
|
|
|
/* static */
|
|
|
|
void nsJSPrincipals::Destroy(JSPrincipals* jsprin) {
|
2012-03-09 09:48:50 +00:00
|
|
|
// The JS runtime can call this method during the last GC when
|
|
|
|
// nsScriptSecurityManager is destroyed. So we must not assume here that
|
|
|
|
// the security manager still exists.
|
|
|
|
|
|
|
|
nsJSPrincipals* nsjsprin = nsJSPrincipals::get(jsprin);
|
2001-07-31 19:05:34 +00:00
|
|
|
|
1999-10-06 21:12:21 +00:00
|
|
|
// We need to destroy the nsIPrincipal. We'll do this by adding
|
|
|
|
// to the refcount and calling release
|
2000-09-14 08:48:53 +00:00
|
|
|
|
2000-11-08 03:06:57 +00:00
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
|
|
|
// The refcount logging considers AddRef-to-1 to indicate creation,
|
|
|
|
// so trick it into thinking it's otherwise, but balance the
|
|
|
|
// Release() we do below.
|
1999-10-06 21:12:21 +00:00
|
|
|
nsjsprin->refcount++;
|
2012-03-09 09:48:50 +00:00
|
|
|
nsjsprin->AddRef();
|
2000-11-08 03:06:57 +00:00
|
|
|
nsjsprin->refcount--;
|
|
|
|
#else
|
|
|
|
nsjsprin->refcount++;
|
|
|
|
#endif
|
2012-03-09 09:48:50 +00:00
|
|
|
nsjsprin->Release();
|
1999-08-20 09:51:02 +00:00
|
|
|
}
|
|
|
|
|
2012-03-09 09:48:50 +00:00
|
|
|
#ifdef DEBUG
|
1999-08-20 09:51:02 +00:00
|
|
|
|
2012-03-09 09:48:50 +00:00
|
|
|
// Defined here so one can do principals->dump() in the debugger
|
|
|
|
JS_PUBLIC_API void JSPrincipals::dump() {
|
|
|
|
if (debugToken == nsJSPrincipals::DEBUG_TOKEN) {
|
2015-05-11 21:17:07 +00:00
|
|
|
nsAutoCString str;
|
2016-08-30 04:22:04 +00:00
|
|
|
nsresult rv = static_cast<nsJSPrincipals*>(this)->GetScriptLocation(str);
|
|
|
|
fprintf(stderr, "nsIPrincipal (%p) = %s\n", static_cast<void*>(this),
|
|
|
|
NS_SUCCEEDED(rv) ? str.get() : "(unknown)");
|
2018-01-31 07:24:59 +00:00
|
|
|
} else if (debugToken == dom::workerinternals::kJSPrincipalsDebugToken) {
|
2012-03-09 09:48:50 +00:00
|
|
|
fprintf(stderr, "Web Worker principal singleton (%p)\n", this);
|
2019-09-06 02:01:44 +00:00
|
|
|
} else if (debugToken == dom::WorkletPrincipals::kJSPrincipalsDebugToken) {
|
|
|
|
fprintf(stderr, "Web Worklet principal (%p)\n", this);
|
2007-09-28 14:31:04 +00:00
|
|
|
} else {
|
2012-03-09 09:48:50 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"!!! JSPrincipals (%p) is not nsJSPrincipals instance - bad token: "
|
|
|
|
"actual=0x%x expected=0x%x\n",
|
|
|
|
this, unsigned(debugToken), unsigned(nsJSPrincipals::DEBUG_TOKEN));
|
2007-09-28 14:31:04 +00:00
|
|
|
}
|
1999-08-20 09:51:02 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 14:44:00 +00:00
|
|
|
#endif
|
|
|
|
|
2019-02-25 22:03:35 +00:00
|
|
|
/* static */
|
|
|
|
bool nsJSPrincipals::ReadPrincipals(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
JSPrincipals** aOutPrincipals) {
|
2015-10-02 14:44:00 +00:00
|
|
|
uint32_t tag;
|
|
|
|
uint32_t unused;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &tag, &unused)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(tag == SCTAG_DOM_NULL_PRINCIPAL || tag == SCTAG_DOM_SYSTEM_PRINCIPAL ||
|
2016-02-04 23:12:52 +00:00
|
|
|
tag == SCTAG_DOM_CONTENT_PRINCIPAL ||
|
2019-04-15 23:46:05 +00:00
|
|
|
tag == SCTAG_DOM_EXPANDED_PRINCIPAL ||
|
|
|
|
tag == SCTAG_DOM_WORKER_PRINCIPAL)) {
|
2015-10-02 14:44:00 +00:00
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ReadKnownPrincipalType(aCx, aReader, tag, aOutPrincipals);
|
|
|
|
}
|
|
|
|
|
2019-05-21 23:14:27 +00:00
|
|
|
static bool ReadPrincipalInfo(JSStructuredCloneReader* aReader,
|
|
|
|
OriginAttributes& aAttrs, nsACString& aSpec,
|
|
|
|
nsACString& aOriginNoSuffix,
|
|
|
|
nsACString& aBaseDomain) {
|
2016-04-21 08:30:03 +00:00
|
|
|
uint32_t suffixLength, specLength;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &suffixLength, &specLength)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString suffix;
|
2017-03-20 15:03:45 +00:00
|
|
|
if (!suffix.SetLength(suffixLength, fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-21 08:30:03 +00:00
|
|
|
if (!JS_ReadBytes(aReader, suffix.BeginWriting(), suffixLength)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-15 10:22:44 +00:00
|
|
|
if (!aAttrs.PopulateFromSuffix(suffix)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-21 08:30:03 +00:00
|
|
|
|
2017-03-20 15:03:45 +00:00
|
|
|
if (!aSpec.SetLength(specLength, fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-21 08:30:03 +00:00
|
|
|
if (!JS_ReadBytes(aReader, aSpec.BeginWriting(), specLength)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-21 23:14:27 +00:00
|
|
|
uint32_t originNoSuffixLength, dummy;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &originNoSuffixLength, &dummy)) {
|
2017-11-15 10:19:26 +00:00
|
|
|
return false;
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 23:14:27 +00:00
|
|
|
MOZ_ASSERT(dummy == 0);
|
|
|
|
if (dummy != 0) {
|
|
|
|
return false;
|
2018-04-11 19:52:47 +00:00
|
|
|
}
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2017-11-15 10:19:26 +00:00
|
|
|
if (!aOriginNoSuffix.SetLength(originNoSuffixLength, fallible)) {
|
|
|
|
return false;
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 10:19:26 +00:00
|
|
|
if (!JS_ReadBytes(aReader, aOriginNoSuffix.BeginWriting(),
|
|
|
|
originNoSuffixLength)) {
|
|
|
|
return false;
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 20:01:50 +00:00
|
|
|
uint32_t baseDomainIsVoid, baseDomainLength;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &baseDomainIsVoid, &baseDomainLength)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(baseDomainIsVoid == 0 || baseDomainIsVoid == 1);
|
|
|
|
|
|
|
|
if (baseDomainIsVoid) {
|
|
|
|
MOZ_ASSERT(baseDomainLength == 0);
|
|
|
|
|
|
|
|
aBaseDomain.SetIsVoid(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aBaseDomain.SetLength(baseDomainLength, fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_ReadBytes(aReader, aBaseDomain.BeginWriting(), baseDomainLength)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-21 08:30:03 +00:00
|
|
|
return true;
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:12:52 +00:00
|
|
|
static bool ReadPrincipalInfo(JSStructuredCloneReader* aReader, uint32_t aTag,
|
|
|
|
PrincipalInfo& aInfo) {
|
2015-10-02 14:44:00 +00:00
|
|
|
if (aTag == SCTAG_DOM_SYSTEM_PRINCIPAL) {
|
2016-02-04 23:12:52 +00:00
|
|
|
aInfo = SystemPrincipalInfo();
|
2015-10-02 14:44:00 +00:00
|
|
|
} else if (aTag == SCTAG_DOM_NULL_PRINCIPAL) {
|
2017-01-12 16:38:48 +00:00
|
|
|
OriginAttributes attrs;
|
2016-11-30 14:31:09 +00:00
|
|
|
nsAutoCString spec;
|
2017-11-15 10:19:26 +00:00
|
|
|
nsAutoCString originNoSuffix;
|
2019-02-08 20:01:50 +00:00
|
|
|
nsAutoCString baseDomain;
|
|
|
|
if (!ReadPrincipalInfo(aReader, attrs, spec, originNoSuffix, baseDomain)) {
|
2017-11-15 10:19:26 +00:00
|
|
|
return false;
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
2016-11-30 14:31:09 +00:00
|
|
|
aInfo = NullPrincipalInfo(attrs, spec);
|
2016-02-04 23:12:52 +00:00
|
|
|
} else if (aTag == SCTAG_DOM_EXPANDED_PRINCIPAL) {
|
|
|
|
uint32_t length, unused;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &length, &unused)) {
|
2016-04-21 08:30:03 +00:00
|
|
|
return false;
|
2017-11-15 10:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ExpandedPrincipalInfo expanded;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
2016-02-04 23:12:52 +00:00
|
|
|
uint32_t tag;
|
2017-11-15 10:19:26 +00:00
|
|
|
if (!JS_ReadUint32Pair(aReader, &tag, &unused)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrincipalInfo sub;
|
|
|
|
if (!ReadPrincipalInfo(aReader, tag, sub)) {
|
|
|
|
return false;
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
2018-10-31 17:56:43 +00:00
|
|
|
expanded.allowlist().AppendElement(sub);
|
2017-11-15 10:19:26 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:12:52 +00:00
|
|
|
aInfo = expanded;
|
2015-10-02 14:44:00 +00:00
|
|
|
} else if (aTag == SCTAG_DOM_CONTENT_PRINCIPAL) {
|
2017-01-12 16:38:48 +00:00
|
|
|
OriginAttributes attrs;
|
2016-11-30 14:31:09 +00:00
|
|
|
nsAutoCString spec;
|
|
|
|
nsAutoCString originNoSuffix;
|
2019-02-08 20:01:50 +00:00
|
|
|
nsAutoCString baseDomain;
|
2019-05-21 23:14:27 +00:00
|
|
|
if (!ReadPrincipalInfo(aReader, attrs, spec, originNoSuffix, baseDomain)) {
|
2016-02-04 23:12:52 +00:00
|
|
|
return false;
|
2015-10-02 14:44:00 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 18:48:49 +00:00
|
|
|
#ifdef FUZZING
|
|
|
|
if (originNoSuffix.IsEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-11-15 10:19:26 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!originNoSuffix.IsEmpty());
|
|
|
|
|
2019-02-28 16:50:28 +00:00
|
|
|
// XXX: Do we care about mDomain for structured clone?
|
|
|
|
aInfo = ContentPrincipalInfo(attrs, originNoSuffix, spec, Nothing(),
|
2019-05-21 23:14:27 +00:00
|
|
|
baseDomain);
|
2016-02-04 23:12:52 +00:00
|
|
|
} else {
|
2018-03-02 18:01:00 +00:00
|
|
|
#ifdef FUZZING
|
|
|
|
return false;
|
|
|
|
#else
|
2016-02-04 23:12:52 +00:00
|
|
|
MOZ_CRASH("unexpected principal structured clone tag");
|
2018-03-02 18:01:00 +00:00
|
|
|
#endif
|
2016-02-04 23:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-15 23:46:05 +00:00
|
|
|
static StaticRefPtr<nsIPrincipal> sActiveWorkerPrincipal;
|
|
|
|
|
|
|
|
nsJSPrincipals::AutoSetActiveWorkerPrincipal::AutoSetActiveWorkerPrincipal(
|
|
|
|
nsIPrincipal* aPrincipal) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_RELEASE_ASSERT(!sActiveWorkerPrincipal);
|
|
|
|
sActiveWorkerPrincipal = aPrincipal;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsJSPrincipals::AutoSetActiveWorkerPrincipal::~AutoSetActiveWorkerPrincipal() {
|
|
|
|
sActiveWorkerPrincipal = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:03:35 +00:00
|
|
|
/* static */
|
|
|
|
bool nsJSPrincipals::ReadKnownPrincipalType(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
JSPrincipals** aOutPrincipals) {
|
2016-02-04 23:12:52 +00:00
|
|
|
MOZ_ASSERT(aTag == SCTAG_DOM_NULL_PRINCIPAL ||
|
|
|
|
aTag == SCTAG_DOM_SYSTEM_PRINCIPAL ||
|
|
|
|
aTag == SCTAG_DOM_CONTENT_PRINCIPAL ||
|
2019-04-15 23:46:05 +00:00
|
|
|
aTag == SCTAG_DOM_EXPANDED_PRINCIPAL ||
|
|
|
|
aTag == SCTAG_DOM_WORKER_PRINCIPAL);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2016-02-04 23:12:52 +00:00
|
|
|
if (NS_WARN_IF(!NS_IsMainThread())) {
|
|
|
|
xpc::Throw(aCx, NS_ERROR_UNCATCHABLE_EXCEPTION);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-15 23:46:05 +00:00
|
|
|
if (aTag == SCTAG_DOM_WORKER_PRINCIPAL) {
|
|
|
|
// When reading principals which were written on a worker thread, we need to
|
|
|
|
// know the principal of the worker which did the write.
|
|
|
|
if (!sActiveWorkerPrincipal) {
|
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
RefPtr<nsJSPrincipals> retval = get(sActiveWorkerPrincipal);
|
|
|
|
retval.forget(aOutPrincipals);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:12:52 +00:00
|
|
|
PrincipalInfo info;
|
|
|
|
if (!ReadPrincipalInfo(aReader, aTag, info)) {
|
|
|
|
return false;
|
2015-10-02 14:44:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 19:02:05 +00:00
|
|
|
auto principalOrErr = PrincipalInfoToPrincipal(info);
|
|
|
|
if (NS_WARN_IF(principalOrErr.isErr())) {
|
2015-10-02 14:44:00 +00:00
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-12 19:02:05 +00:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = principalOrErr.unwrap();
|
|
|
|
|
|
|
|
*aOutPrincipals = get(principal.forget().take());
|
2015-10-02 14:44:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-21 23:14:27 +00:00
|
|
|
static bool WritePrincipalInfo(JSStructuredCloneWriter* aWriter,
|
|
|
|
const OriginAttributes& aAttrs,
|
|
|
|
const nsCString& aSpec,
|
|
|
|
const nsCString& aOriginNoSuffix,
|
|
|
|
const nsCString& aBaseDomain) {
|
2016-04-21 08:30:03 +00:00
|
|
|
nsAutoCString suffix;
|
|
|
|
aAttrs.CreateSuffix(suffix);
|
2018-04-11 19:52:47 +00:00
|
|
|
|
|
|
|
if (!(JS_WriteUint32Pair(aWriter, suffix.Length(), aSpec.Length()) &&
|
|
|
|
JS_WriteBytes(aWriter, suffix.get(), suffix.Length()) &&
|
|
|
|
JS_WriteBytes(aWriter, aSpec.get(), aSpec.Length()) &&
|
2019-05-21 23:14:27 +00:00
|
|
|
JS_WriteUint32Pair(aWriter, aOriginNoSuffix.Length(), 0) &&
|
2018-04-11 19:52:47 +00:00
|
|
|
JS_WriteBytes(aWriter, aOriginNoSuffix.get(),
|
|
|
|
aOriginNoSuffix.Length()))) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-21 08:30:03 +00:00
|
|
|
|
2019-02-08 20:01:50 +00:00
|
|
|
if (aBaseDomain.IsVoid()) {
|
|
|
|
return JS_WriteUint32Pair(aWriter, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_WriteUint32Pair(aWriter, 0, aBaseDomain.Length()) &&
|
|
|
|
JS_WriteBytes(aWriter, aBaseDomain.get(), aBaseDomain.Length());
|
2016-04-21 08:30:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 00:41:50 +00:00
|
|
|
/* static */
|
|
|
|
bool nsJSPrincipals::WritePrincipalInfo(JSStructuredCloneWriter* aWriter,
|
|
|
|
const PrincipalInfo& aInfo) {
|
2016-02-04 23:12:52 +00:00
|
|
|
if (aInfo.type() == PrincipalInfo::TNullPrincipalInfo) {
|
2016-04-21 08:30:03 +00:00
|
|
|
const NullPrincipalInfo& nullInfo = aInfo;
|
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_NULL_PRINCIPAL, 0) &&
|
2019-09-09 00:41:50 +00:00
|
|
|
::WritePrincipalInfo(aWriter, nullInfo.attrs(), nullInfo.spec(),
|
2020-09-23 15:17:15 +00:00
|
|
|
""_ns, ""_ns);
|
2015-10-02 14:44:00 +00:00
|
|
|
}
|
2016-02-04 23:12:52 +00:00
|
|
|
if (aInfo.type() == PrincipalInfo::TSystemPrincipalInfo) {
|
2015-10-02 14:44:00 +00:00
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_SYSTEM_PRINCIPAL, 0);
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
2016-02-04 23:12:52 +00:00
|
|
|
if (aInfo.type() == PrincipalInfo::TExpandedPrincipalInfo) {
|
|
|
|
const ExpandedPrincipalInfo& expanded = aInfo;
|
|
|
|
if (!JS_WriteUint32Pair(aWriter, SCTAG_DOM_EXPANDED_PRINCIPAL, 0) ||
|
2018-10-31 17:56:43 +00:00
|
|
|
!JS_WriteUint32Pair(aWriter, expanded.allowlist().Length(), 0)) {
|
2016-02-04 23:12:52 +00:00
|
|
|
return false;
|
2015-10-02 14:44:00 +00:00
|
|
|
}
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2018-10-31 17:56:43 +00:00
|
|
|
for (uint32_t i = 0; i < expanded.allowlist().Length(); i++) {
|
|
|
|
if (!WritePrincipalInfo(aWriter, expanded.allowlist()[i])) {
|
2016-02-04 23:12:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
2016-02-04 23:12:52 +00:00
|
|
|
|
|
|
|
MOZ_ASSERT(aInfo.type() == PrincipalInfo::TContentPrincipalInfo);
|
|
|
|
const ContentPrincipalInfo& cInfo = aInfo;
|
2015-10-02 14:44:00 +00:00
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_CONTENT_PRINCIPAL, 0) &&
|
2019-09-09 00:41:50 +00:00
|
|
|
::WritePrincipalInfo(aWriter, cInfo.attrs(), cInfo.spec(),
|
|
|
|
cInfo.originNoSuffix(), cInfo.baseDomain());
|
2015-10-02 14:44:00 +00:00
|
|
|
}
|
2016-02-04 23:12:52 +00:00
|
|
|
|
|
|
|
bool nsJSPrincipals::write(JSContext* aCx, JSStructuredCloneWriter* aWriter) {
|
|
|
|
PrincipalInfo info;
|
|
|
|
if (NS_WARN_IF(NS_FAILED(PrincipalToPrincipalInfo(this, &info)))) {
|
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WritePrincipalInfo(aWriter, info);
|
|
|
|
}
|
2019-10-04 17:37:36 +00:00
|
|
|
|
|
|
|
bool nsJSPrincipals::isSystemOrAddonPrincipal() {
|
2020-02-13 19:10:14 +00:00
|
|
|
JS::AutoSuppressGCAnalysis suppress;
|
2019-10-04 17:37:36 +00:00
|
|
|
return this->IsSystemPrincipal() ||
|
|
|
|
this->GetIsAddonOrExpandedAddonPrincipal();
|
|
|
|
}
|