From d0b50071c86de1429c1c93f3cf6b1bf633e851ea Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Thu, 23 Feb 2017 14:30:23 +0100 Subject: [PATCH] Bug 1341218 - nsNullPrincipal should serialize its URL, r=qdot --- caps/nsNullPrincipal.cpp | 30 ++++++++++++++++++++++++------ dom/base/nsContentUtils.cpp | 9 +++++---- dom/base/nsDocument.cpp | 3 ++- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/caps/nsNullPrincipal.cpp b/caps/nsNullPrincipal.cpp index e1555e4794e1..20d713890c18 100644 --- a/caps/nsNullPrincipal.cpp +++ b/caps/nsNullPrincipal.cpp @@ -166,25 +166,43 @@ nsNullPrincipal::Read(nsIObjectInputStream* aStream) // that the Init() method has already been invoked by the time we deserialize. // This is in contrast to nsPrincipal, which uses NS_GENERIC_FACTORY_CONSTRUCTOR, // in which case ::Read needs to invoke Init(). - nsAutoCString suffix; - nsresult rv = aStream->ReadCString(suffix); + + nsAutoCString spec; + nsresult rv = aStream->ReadCString(spec); NS_ENSURE_SUCCESS(rv, rv); - bool ok = mOriginAttributes.PopulateFromSuffix(suffix); + nsCOMPtr uri; + rv = NS_NewURI(getter_AddRefs(uri), spec); + NS_ENSURE_SUCCESS(rv, rv); + + nsAutoCString suffix; + rv = aStream->ReadCString(suffix); + NS_ENSURE_SUCCESS(rv, rv); + + OriginAttributes attrs; + bool ok = attrs.PopulateFromSuffix(suffix); NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE); - return NS_OK; + return Init(attrs, uri); } NS_IMETHODIMP nsNullPrincipal::Write(nsIObjectOutputStream* aStream) { + NS_ENSURE_STATE(mURI); + + nsAutoCString spec; + nsresult rv = mURI->GetSpec(spec); + NS_ENSURE_SUCCESS(rv, rv); + + rv = aStream->WriteStringZ(spec.get()); + NS_ENSURE_SUCCESS(rv, rv); + nsAutoCString suffix; OriginAttributesRef().CreateSuffix(suffix); - nsresult rv = aStream->WriteStringZ(suffix.get()); + rv = aStream->WriteStringZ(suffix.get()); NS_ENSURE_SUCCESS(rv, rv); return NS_OK; } - diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index c09827e9497b..a0db9a2b3d3c 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -512,10 +512,11 @@ nsContentUtils::Init() sSecurityManager->GetSystemPrincipal(&sSystemPrincipal); MOZ_ASSERT(sSystemPrincipal); - // We use the constructor here because we want infallible initialization; we - // apparently don't care whether sNullSubjectPrincipal has a sane URI or not. - RefPtr nullPrincipal = new nsNullPrincipal(); - nullPrincipal->Init(); + RefPtr nullPrincipal = nsNullPrincipal::Create(); + if (!nullPrincipal) { + return NS_ERROR_FAILURE; + } + nullPrincipal.forget(&sNullSubjectPrincipal); nsresult rv = CallGetService(NS_IOSERVICE_CONTRACTID, &sIOService); diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp index 33395f6399c2..e7a06e5d0cf5 100644 --- a/dom/base/nsDocument.cpp +++ b/dom/base/nsDocument.cpp @@ -104,6 +104,7 @@ #include "nsIScriptSecurityManager.h" #include "nsIPermissionManager.h" #include "nsIPrincipal.h" +#include "nsNullPrincipal.h" #include "nsIDOMWindow.h" #include "nsPIDOMWindow.h" @@ -2738,7 +2739,7 @@ nsDocument::InitCSP(nsIChannel* aChannel) if (cspSandboxFlags & SANDBOXED_ORIGIN) { // If the new CSP sandbox flags do not have the allow-same-origin flag // reset the document principal to a null principal - principal = do_CreateInstance("@mozilla.org/nullprincipal;1"); + principal = nsNullPrincipal::Create(); SetPrincipal(principal); }