2001-09-25 05:03:58 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04: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-22 02:11:49 +04:00
|
|
|
|
2016-08-04 21:19:24 +03:00
|
|
|
#include "xpcpublic.h"
|
2003-10-22 02:11:49 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIObjectOutputStream.h"
|
|
|
|
#include "nsIObjectInputStream.h"
|
1999-08-20 13:51:02 +04:00
|
|
|
#include "nsJSPrincipals.h"
|
|
|
|
#include "plstr.h"
|
1999-08-27 12:36:49 +04:00
|
|
|
#include "nsXPIDLString.h"
|
2001-07-31 23:05:34 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsMemory.h"
|
2007-09-28 18:31:04 +04:00
|
|
|
#include "nsStringBuffer.h"
|
1999-08-20 13:51:02 +04:00
|
|
|
|
2015-10-02 17:44:00 +03:00
|
|
|
#include "mozilla/dom/StructuredCloneTags.h"
|
2012-03-09 13:48:50 +04:00
|
|
|
// for mozilla::dom::workers::kJSPrincipalsDebugToken
|
|
|
|
#include "mozilla/dom/workers/Workers.h"
|
2015-10-02 17:44:00 +03:00
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::ipc;
|
2005-07-09 03:26:36 +04:00
|
|
|
|
2015-05-11 23:16:36 +03:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsJSPrincipals::AddRef()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
NS_PRECONDITION(int32_t(refcount) >= 0, "illegal refcnt");
|
|
|
|
nsrefcnt count = ++refcount;
|
|
|
|
NS_LOG_ADDREF(this, count, "nsJSPrincipals", sizeof(*this));
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsJSPrincipals::Release()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
NS_PRECONDITION(0 != refcount, "dup release");
|
|
|
|
nsrefcnt count = --refcount;
|
|
|
|
NS_LOG_RELEASE(this, count, "nsJSPrincipals");
|
|
|
|
if (count == 0) {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
/* static */ bool
|
2012-03-09 13:48:50 +04:00
|
|
|
nsJSPrincipals::Subsume(JSPrincipals *jsprin, JSPrincipals *other)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool result;
|
2012-03-09 13:48:50 +04:00
|
|
|
nsresult rv = nsJSPrincipals::get(jsprin)->Subsumes(nsJSPrincipals::get(other), &result);
|
2005-07-09 03:26:36 +04:00
|
|
|
return NS_SUCCEEDED(rv) && result;
|
|
|
|
}
|
|
|
|
|
2012-03-09 13:48:50 +04:00
|
|
|
/* static */ void
|
|
|
|
nsJSPrincipals::Destroy(JSPrincipals *jsprin)
|
2001-07-31 23:05:34 +04:00
|
|
|
{
|
2012-03-09 13:48:50 +04: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 23:05:34 +04:00
|
|
|
|
1999-10-07 01:12:21 +04:00
|
|
|
// We need to destroy the nsIPrincipal. We'll do this by adding
|
|
|
|
// to the refcount and calling release
|
2000-09-14 12:48:53 +04:00
|
|
|
|
2000-11-08 06:06:57 +03: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-07 01:12:21 +04:00
|
|
|
nsjsprin->refcount++;
|
2012-03-09 13:48:50 +04:00
|
|
|
nsjsprin->AddRef();
|
2000-11-08 06:06:57 +03:00
|
|
|
nsjsprin->refcount--;
|
|
|
|
#else
|
|
|
|
nsjsprin->refcount++;
|
|
|
|
#endif
|
2012-03-09 13:48:50 +04:00
|
|
|
nsjsprin->Release();
|
1999-08-20 13:51:02 +04:00
|
|
|
}
|
|
|
|
|
2012-03-09 13:48:50 +04:00
|
|
|
#ifdef DEBUG
|
1999-08-20 13:51:02 +04:00
|
|
|
|
2012-03-09 13:48:50 +04:00
|
|
|
// Defined here so one can do principals->dump() in the debugger
|
2016-03-10 08:04:11 +03:00
|
|
|
JS_PUBLIC_API(void)
|
2012-03-09 13:48:50 +04:00
|
|
|
JSPrincipals::dump()
|
1999-08-30 01:58:42 +04:00
|
|
|
{
|
2012-03-09 13:48:50 +04:00
|
|
|
if (debugToken == nsJSPrincipals::DEBUG_TOKEN) {
|
2015-05-12 00:17:07 +03:00
|
|
|
nsAutoCString str;
|
2016-08-30 07:22:04 +03: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)");
|
2015-10-02 17:44:00 +03:00
|
|
|
} else if (debugToken == dom::workers::kJSPrincipalsDebugToken) {
|
2012-03-09 13:48:50 +04:00
|
|
|
fprintf(stderr, "Web Worker principal singleton (%p)\n", this);
|
2007-09-28 18:31:04 +04:00
|
|
|
} else {
|
2012-03-09 13:48:50 +04: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 18:31:04 +04:00
|
|
|
}
|
1999-08-20 13:51:02 +04:00
|
|
|
}
|
|
|
|
|
2015-10-02 17:44:00 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* static */ bool
|
|
|
|
nsJSPrincipals::ReadPrincipals(JSContext* aCx, JSStructuredCloneReader* aReader,
|
|
|
|
JSPrincipals** aOutPrincipals)
|
|
|
|
{
|
|
|
|
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-05 02:12:52 +03:00
|
|
|
tag == SCTAG_DOM_CONTENT_PRINCIPAL ||
|
|
|
|
tag == SCTAG_DOM_EXPANDED_PRINCIPAL)) {
|
2015-10-02 17:44:00 +03:00
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ReadKnownPrincipalType(aCx, aReader, tag, aOutPrincipals);
|
|
|
|
}
|
|
|
|
|
2016-04-21 11:30:03 +03:00
|
|
|
static bool
|
|
|
|
ReadSuffixAndSpec(JSStructuredCloneReader* aReader,
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes& aAttrs,
|
2016-04-21 11:30:03 +03:00
|
|
|
nsACString& aSpec)
|
|
|
|
{
|
|
|
|
uint32_t suffixLength, specLength;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &suffixLength, &specLength)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString suffix;
|
2017-03-20 18:03:45 +03:00
|
|
|
if (!suffix.SetLength(suffixLength, fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-21 11:30:03 +03:00
|
|
|
if (!JS_ReadBytes(aReader, suffix.BeginWriting(), suffixLength)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-15 13:22:44 +03:00
|
|
|
if (!aAttrs.PopulateFromSuffix(suffix)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-21 11:30:03 +03:00
|
|
|
|
2017-03-20 18:03:45 +03:00
|
|
|
if (!aSpec.SetLength(specLength, fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-21 11:30:03 +03:00
|
|
|
if (!JS_ReadBytes(aReader, aSpec.BeginWriting(), specLength)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-05 02:12:52 +03:00
|
|
|
static bool
|
|
|
|
ReadPrincipalInfo(JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
PrincipalInfo& aInfo)
|
2015-10-02 17:44:00 +03:00
|
|
|
{
|
|
|
|
if (aTag == SCTAG_DOM_SYSTEM_PRINCIPAL) {
|
2016-02-05 02:12:52 +03:00
|
|
|
aInfo = SystemPrincipalInfo();
|
2015-10-02 17:44:00 +03:00
|
|
|
} else if (aTag == SCTAG_DOM_NULL_PRINCIPAL) {
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes attrs;
|
2016-11-30 17:31:09 +03:00
|
|
|
nsAutoCString spec;
|
|
|
|
if (!ReadSuffixAndSpec(aReader, attrs, spec)) {
|
2016-04-21 11:30:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-11-30 17:31:09 +03:00
|
|
|
aInfo = NullPrincipalInfo(attrs, spec);
|
2016-02-05 02:12:52 +03:00
|
|
|
} else if (aTag == SCTAG_DOM_EXPANDED_PRINCIPAL) {
|
|
|
|
uint32_t length, unused;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &length, &unused)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExpandedPrincipalInfo expanded;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
|
|
|
uint32_t tag;
|
|
|
|
if (!JS_ReadUint32Pair(aReader, &tag, &unused)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrincipalInfo sub;
|
|
|
|
if (!ReadPrincipalInfo(aReader, tag, sub)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
expanded.whitelist().AppendElement(sub);
|
|
|
|
}
|
|
|
|
|
|
|
|
aInfo = expanded;
|
|
|
|
} else if (aTag == SCTAG_DOM_CONTENT_PRINCIPAL) {
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes attrs;
|
2015-10-02 17:44:00 +03:00
|
|
|
nsAutoCString spec;
|
2016-04-21 11:30:03 +03:00
|
|
|
if (!ReadSuffixAndSpec(aReader, attrs, spec)) {
|
2015-10-02 17:44:00 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-20 18:03:45 +03:00
|
|
|
aInfo = ContentPrincipalInfo(attrs, void_t(), spec);
|
2016-02-05 02:12:52 +03:00
|
|
|
} else {
|
|
|
|
MOZ_CRASH("unexpected principal structured clone tag");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool
|
|
|
|
nsJSPrincipals::ReadKnownPrincipalType(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
JSPrincipals** aOutPrincipals)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTag == SCTAG_DOM_NULL_PRINCIPAL ||
|
|
|
|
aTag == SCTAG_DOM_SYSTEM_PRINCIPAL ||
|
|
|
|
aTag == SCTAG_DOM_CONTENT_PRINCIPAL ||
|
|
|
|
aTag == SCTAG_DOM_EXPANDED_PRINCIPAL);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!NS_IsMainThread())) {
|
|
|
|
xpc::Throw(aCx, NS_ERROR_UNCATCHABLE_EXCEPTION);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrincipalInfo info;
|
|
|
|
if (!ReadPrincipalInfo(aReader, aTag, info)) {
|
|
|
|
return false;
|
2015-10-02 17:44:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPrincipal> prin = PrincipalInfoToPrincipal(info, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aOutPrincipals = get(prin.forget().take());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-21 11:30:03 +03:00
|
|
|
static bool
|
|
|
|
WriteSuffixAndSpec(JSStructuredCloneWriter* aWriter,
|
2017-01-12 19:38:48 +03:00
|
|
|
const OriginAttributes& aAttrs,
|
2016-04-21 11:30:03 +03:00
|
|
|
const nsCString& aSpec)
|
|
|
|
{
|
|
|
|
nsAutoCString suffix;
|
|
|
|
aAttrs.CreateSuffix(suffix);
|
|
|
|
|
|
|
|
return JS_WriteUint32Pair(aWriter, suffix.Length(), aSpec.Length()) &&
|
|
|
|
JS_WriteBytes(aWriter, suffix.get(), suffix.Length()) &&
|
|
|
|
JS_WriteBytes(aWriter, aSpec.get(), aSpec.Length());
|
|
|
|
}
|
|
|
|
|
2016-02-05 02:12:52 +03:00
|
|
|
static bool
|
|
|
|
WritePrincipalInfo(JSStructuredCloneWriter* aWriter, const PrincipalInfo& aInfo)
|
2015-10-02 17:44:00 +03:00
|
|
|
{
|
2016-02-05 02:12:52 +03:00
|
|
|
if (aInfo.type() == PrincipalInfo::TNullPrincipalInfo) {
|
2016-04-21 11:30:03 +03:00
|
|
|
const NullPrincipalInfo& nullInfo = aInfo;
|
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_NULL_PRINCIPAL, 0) &&
|
2016-11-30 17:31:09 +03:00
|
|
|
WriteSuffixAndSpec(aWriter, nullInfo.attrs(), nullInfo.spec());
|
2015-10-02 17:44:00 +03:00
|
|
|
}
|
2016-02-05 02:12:52 +03:00
|
|
|
if (aInfo.type() == PrincipalInfo::TSystemPrincipalInfo) {
|
2015-10-02 17:44:00 +03:00
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_SYSTEM_PRINCIPAL, 0);
|
|
|
|
}
|
2016-02-05 02:12:52 +03:00
|
|
|
if (aInfo.type() == PrincipalInfo::TExpandedPrincipalInfo) {
|
|
|
|
const ExpandedPrincipalInfo& expanded = aInfo;
|
|
|
|
if (!JS_WriteUint32Pair(aWriter, SCTAG_DOM_EXPANDED_PRINCIPAL, 0) ||
|
|
|
|
!JS_WriteUint32Pair(aWriter, expanded.whitelist().Length(), 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-02 17:44:00 +03:00
|
|
|
|
2016-02-05 02:12:52 +03:00
|
|
|
for (uint32_t i = 0; i < expanded.whitelist().Length(); i++) {
|
|
|
|
if (!WritePrincipalInfo(aWriter, expanded.whitelist()[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aInfo.type() == PrincipalInfo::TContentPrincipalInfo);
|
|
|
|
const ContentPrincipalInfo& cInfo = aInfo;
|
2015-10-02 17:44:00 +03:00
|
|
|
return JS_WriteUint32Pair(aWriter, SCTAG_DOM_CONTENT_PRINCIPAL, 0) &&
|
2016-04-21 11:30:03 +03:00
|
|
|
WriteSuffixAndSpec(aWriter, cInfo.attrs(), cInfo.spec());
|
2015-10-02 17:44:00 +03:00
|
|
|
}
|
2016-02-05 02:12:52 +03: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);
|
|
|
|
}
|