From cf852bbfb92ee4634ae9442d4d98ddf30451e06a Mon Sep 17 00:00:00 2001 From: ssengupta Date: Tue, 12 May 2020 19:02:05 +0000 Subject: [PATCH] Bug 1635399 - Function PrincipalInfoToPrincipal now returns Result, nsresult> r=ckerschb Differential Revision: https://phabricator.services.mozilla.com/D73868 --- caps/nsJSPrincipals.cpp | 10 +- dom/base/nsGlobalWindowInner.cpp | 5 +- dom/cache/PrincipalVerifier.cpp | 13 +- dom/clients/manager/ClientChannelHelper.cpp | 20 ++- dom/clients/manager/ClientInfo.cpp | 5 +- dom/clients/manager/ClientInfo.h | 5 +- dom/clients/manager/ClientOpenWindowUtils.cpp | 9 +- dom/indexedDB/ActorsChild.cpp | 16 +- dom/indexedDB/ActorsParent.cpp | 11 +- dom/indexedDB/IDBDatabase.cpp | 13 +- dom/indexedDB/IDBFactory.cpp | 7 +- dom/ipc/ContentParent.cpp | 7 +- dom/ipc/PermissionMessageUtils.cpp | 18 +- dom/localstorage/ActorsChild.cpp | 8 +- dom/reporting/ReportingHeader.cpp | 13 +- dom/serviceworkers/ServiceWorkerContainer.cpp | 10 +- .../ServiceWorkerDescriptor.cpp | 6 +- dom/serviceworkers/ServiceWorkerDescriptor.h | 2 +- .../ServiceWorkerInterceptController.cpp | 7 +- dom/serviceworkers/ServiceWorkerManager.cpp | 77 ++++++--- .../ServiceWorkerManagerChild.cpp | 6 +- .../ServiceWorkerManagerService.cpp | 5 +- .../ServiceWorkerRegistrationDescriptor.cpp | 7 +- .../ServiceWorkerRegistrationDescriptor.h | 2 +- .../ServiceWorkerRegistrationImpl.cpp | 25 ++- dom/serviceworkers/ServiceWorkerUtils.cpp | 8 +- dom/simpledb/ActorsParent.cpp | 15 +- dom/storage/StorageActivityService.cpp | 13 +- dom/storage/StorageIPC.cpp | 16 +- .../WebBrowserPersistRemoteDocument.cpp | 9 +- dom/workers/ScriptLoader.cpp | 8 +- .../remoteworkers/RemoteWorkerChild.cpp | 34 ++-- .../remoteworkers/RemoteWorkerManager.cpp | 8 +- .../sharedworkers/SharedWorkerService.cpp | 23 ++- ipc/glue/BackgroundParentImpl.cpp | 8 +- ipc/glue/BackgroundUtils.cpp | 159 ++++++++++-------- ipc/glue/BackgroundUtils.h | 6 +- netwerk/base/nsNetUtil.cpp | 26 +-- netwerk/cookie/CookieJarSettings.cpp | 14 +- netwerk/ipc/DocumentLoadListener.cpp | 9 +- netwerk/ipc/NeckoParent.cpp | 3 +- netwerk/protocol/http/HttpChannelParent.cpp | 6 +- .../prefetch/OfflineCacheUpdateParent.cpp | 15 +- 43 files changed, 416 insertions(+), 271 deletions(-) diff --git a/caps/nsJSPrincipals.cpp b/caps/nsJSPrincipals.cpp index 047b9350b1ab..0c6756cd5bc1 100644 --- a/caps/nsJSPrincipals.cpp +++ b/caps/nsJSPrincipals.cpp @@ -3,6 +3,7 @@ * 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/. */ +#include "nsIPrincipal.h" #include "xpcpublic.h" #include "nsString.h" #include "nsJSPrincipals.h" @@ -305,14 +306,15 @@ bool nsJSPrincipals::ReadKnownPrincipalType(JSContext* aCx, return false; } - nsresult rv; - nsCOMPtr prin = PrincipalInfoToPrincipal(info, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { + auto principalOrErr = PrincipalInfoToPrincipal(info); + if (NS_WARN_IF(principalOrErr.isErr())) { xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR); return false; } - *aOutPrincipals = get(prin.forget().take()); + nsCOMPtr principal = principalOrErr.unwrap(); + + *aOutPrincipals = get(principal.forget().take()); return true; } diff --git a/dom/base/nsGlobalWindowInner.cpp b/dom/base/nsGlobalWindowInner.cpp index d21873461aaa..28d3a7e66310 100644 --- a/dom/base/nsGlobalWindowInner.cpp +++ b/dom/base/nsGlobalWindowInner.cpp @@ -1679,8 +1679,9 @@ nsresult nsGlobalWindowInner::EnsureClientSource() { // an initial content page created that was then immediately replaced. // This is pretty close to what we are actually doing. if (mClientSource) { - nsCOMPtr clientPrincipal( - mClientSource->Info().GetPrincipal()); + auto principalOrErr = mClientSource->Info().GetPrincipal(); + nsCOMPtr clientPrincipal = + principalOrErr.isOk() ? principalOrErr.unwrap() : nullptr; if (!clientPrincipal || !clientPrincipal->Equals(mDoc->NodePrincipal())) { mClientSource.reset(); } diff --git a/dom/cache/PrincipalVerifier.cpp b/dom/cache/PrincipalVerifier.cpp index 78f29dce4113..90de74df31f8 100644 --- a/dom/cache/PrincipalVerifier.cpp +++ b/dom/cache/PrincipalVerifier.cpp @@ -6,12 +6,14 @@ #include "mozilla/dom/cache/PrincipalVerifier.h" +#include "ErrorList.h" #include "mozilla/dom/ContentParent.h" #include "mozilla/dom/cache/ManagerId.h" #include "mozilla/ipc/BackgroundParent.h" #include "mozilla/ipc/PBackgroundParent.h" #include "mozilla/ipc/BackgroundUtils.h" #include "mozilla/BasePrincipal.h" +#include "nsCOMPtr.h" #include "nsContentUtils.h" #include "nsIPrincipal.h" #include "nsNetUtil.h" @@ -104,14 +106,14 @@ void PrincipalVerifier::VerifyOnMainThread() { RefPtr actor; actor.swap(mActor); - nsresult rv; - RefPtr principal = - PrincipalInfoToPrincipal(mPrincipalInfo, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { - DispatchToInitiatingThread(rv); + auto principalOrErr = PrincipalInfoToPrincipal(mPrincipalInfo); + if (NS_WARN_IF(principalOrErr.isErr())) { + DispatchToInitiatingThread(principalOrErr.unwrapErr()); return; } + nsCOMPtr principal = principalOrErr.unwrap(); + // We disallow null principal on the client side, but double-check here. if (NS_WARN_IF(principal->GetIsNullPrincipal())) { DispatchToInitiatingThread(NS_ERROR_FAILURE); @@ -128,6 +130,7 @@ void PrincipalVerifier::VerifyOnMainThread() { actor = nullptr; #ifdef DEBUG + nsresult rv = NS_OK; // Sanity check principal origin by using it to construct a URI and security // checking it. Don't do this for the system principal, though, as its origin // is a synthetic [System Principal] string. diff --git a/dom/clients/manager/ClientChannelHelper.cpp b/dom/clients/manager/ClientChannelHelper.cpp index b6dace1fde29..6b1e56caffce 100644 --- a/dom/clients/manager/ClientChannelHelper.cpp +++ b/dom/clients/manager/ClientChannelHelper.cpp @@ -264,12 +264,13 @@ nsresult AddClientChannelHelperInternal(nsIChannel* aChannel, // Only allow the initial ClientInfo to be set if the current channel // principal matches. if (initialClientInfo.isSome()) { - nsCOMPtr initialPrincipal = PrincipalInfoToPrincipal( - initialClientInfo.ref().PrincipalInfo(), nullptr); + auto initialPrincipalOrErr = + PrincipalInfoToPrincipal(initialClientInfo.ref().PrincipalInfo()); bool equals = false; - rv = initialPrincipal ? initialPrincipal->Equals(channelPrincipal, &equals) - : NS_ERROR_FAILURE; + rv = initialPrincipalOrErr.isErr() ? initialPrincipalOrErr.unwrapErr() + : initialPrincipalOrErr.unwrap()->Equals( + channelPrincipal, &equals); if (NS_FAILED(rv) || !equals) { initialClientInfo.reset(); } @@ -278,13 +279,14 @@ nsresult AddClientChannelHelperInternal(nsIChannel* aChannel, // Only allow the reserved ClientInfo to be set if the current channel // principal matches. if (reservedClientInfo.isSome()) { - nsCOMPtr reservedPrincipal = PrincipalInfoToPrincipal( - reservedClientInfo.ref().PrincipalInfo(), nullptr); + auto reservedPrincipalOrErr = + PrincipalInfoToPrincipal(reservedClientInfo.ref().PrincipalInfo()); bool equals = false; - rv = reservedPrincipal - ? reservedPrincipal->Equals(channelPrincipal, &equals) - : NS_ERROR_FAILURE; + rv = reservedPrincipalOrErr.isErr() + ? reservedPrincipalOrErr.unwrapErr() + : reservedPrincipalOrErr.unwrap()->Equals(channelPrincipal, + &equals); if (NS_FAILED(rv) || !equals) { reservedClientInfo.reset(); } diff --git a/dom/clients/manager/ClientInfo.cpp b/dom/clients/manager/ClientInfo.cpp index 13acd2b75dc2..867f9664cac4 100644 --- a/dom/clients/manager/ClientInfo.cpp +++ b/dom/clients/manager/ClientInfo.cpp @@ -106,10 +106,9 @@ bool ClientInfo::IsPrivateBrowsing() const { } } -nsCOMPtr ClientInfo::GetPrincipal() const { +Result, nsresult> ClientInfo::GetPrincipal() const { MOZ_ASSERT(NS_IsMainThread()); - nsCOMPtr ref = PrincipalInfoToPrincipal(PrincipalInfo()); - return ref; + return PrincipalInfoToPrincipal(PrincipalInfo()); } const Maybe& ClientInfo::GetCspInfo() const { diff --git a/dom/clients/manager/ClientInfo.h b/dom/clients/manager/ClientInfo.h index 7e83edf2e442..62790895a426 100644 --- a/dom/clients/manager/ClientInfo.h +++ b/dom/clients/manager/ClientInfo.h @@ -94,9 +94,8 @@ class ClientInfo final { // Determine if the client is in private browsing mode. bool IsPrivateBrowsing() const; - // Get a main-thread nsIPrincipal for the client. This may return nullptr - // if the PrincipalInfo() fails to deserialize for some reason. - nsCOMPtr GetPrincipal() const; + // Get a main-thread nsIPrincipal for the client. + Result, nsresult> GetPrincipal() const; const Maybe& GetCspInfo() const; void SetCspInfo(const mozilla::ipc::CSPInfo& aCSPInfo); diff --git a/dom/clients/manager/ClientOpenWindowUtils.cpp b/dom/clients/manager/ClientOpenWindowUtils.cpp index ea39c83649db..f709cb38b370 100644 --- a/dom/clients/manager/ClientOpenWindowUtils.cpp +++ b/dom/clients/manager/ClientOpenWindowUtils.cpp @@ -202,8 +202,13 @@ void OpenWindow(const ClientOpenWindowArgs& aArgs, BrowsingContext** aBC, return; } - nsCOMPtr principal = - PrincipalInfoToPrincipal(aArgs.principalInfo()); + auto principalOrErr = PrincipalInfoToPrincipal(aArgs.principalInfo()); + if (NS_WARN_IF(principalOrErr.isErr())) { + nsPrintfCString err("Failed to obtain principal"); + aRv.ThrowTypeError(err); + return; + } + nsCOMPtr principal = principalOrErr.unwrap(); MOZ_DIAGNOSTIC_ASSERT(principal); nsCOMPtr csp; diff --git a/dom/indexedDB/ActorsChild.cpp b/dom/indexedDB/ActorsChild.cpp index 4d5bb87b6bf9..33711d2731b5 100644 --- a/dom/indexedDB/ActorsChild.cpp +++ b/dom/indexedDB/ActorsChild.cpp @@ -919,13 +919,14 @@ class WorkerPermissionChallenge final : public Runnable { return true; } - nsresult rv; - const nsCOMPtr principal = - mozilla::ipc::PrincipalInfoToPrincipal(mPrincipalInfo, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { + auto principalOrErr = + mozilla::ipc::PrincipalInfoToPrincipal(mPrincipalInfo); + if (NS_WARN_IF(principalOrErr.isErr())) { return true; } + const nsCOMPtr principal = principalOrErr.unwrap(); + if (XRE_IsParentProcess()) { const nsCOMPtr ownerElement = do_QueryInterface(window->GetChromeEventHandler()); @@ -1694,12 +1695,11 @@ mozilla::ipc::IPCResult BackgroundFactoryRequestChild::RecvPermissionChallenge( return IPC_OK(); } - nsresult rv; - nsCOMPtr principal = - mozilla::ipc::PrincipalInfoToPrincipal(aPrincipalInfo, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { + auto principalOrErr = mozilla::ipc::PrincipalInfoToPrincipal(aPrincipalInfo); + if (NS_WARN_IF(principalOrErr.isErr())) { return IPC_FAIL_NO_REASON(this); } + nsCOMPtr principal = principalOrErr.unwrap(); if (XRE_IsParentProcess()) { nsCOMPtr global = mFactory->GetParentObject(); diff --git a/dom/indexedDB/ActorsParent.cpp b/dom/indexedDB/ActorsParent.cpp index 5e74ffd2398e..6cf497f8e4e1 100644 --- a/dom/indexedDB/ActorsParent.cpp +++ b/dom/indexedDB/ActorsParent.cpp @@ -20857,16 +20857,17 @@ nsresult FactoryOp::CheckPermission( MOZ_ASSERT(principalInfo.type() == PrincipalInfo::TContentPrincipalInfo); - nsresult rv; - nsCOMPtr principal = - PrincipalInfoToPrincipal(principalInfo, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + auto principalOrErr = PrincipalInfoToPrincipal(principalInfo); + if (NS_WARN_IF(principalOrErr.isErr())) { + return principalOrErr.unwrapErr(); } + nsCOMPtr principal = principalOrErr.unwrap(); + nsCString suffix; nsCString group; nsCString origin; + nsresult rv; rv = QuotaManager::GetInfoFromPrincipal(principal, &suffix, &group, &origin); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; diff --git a/dom/indexedDB/IDBDatabase.cpp b/dom/indexedDB/IDBDatabase.cpp index a35d061b47b9..ead65b5b5195 100644 --- a/dom/indexedDB/IDBDatabase.cpp +++ b/dom/indexedDB/IDBDatabase.cpp @@ -881,15 +881,14 @@ nsresult IDBDatabase::GetQuotaInfo(nsACString& aOrigin, return NS_OK; case PrincipalInfo::TContentPrincipalInfo: { - nsresult rv; - nsCOMPtr principal = - PrincipalInfoToPrincipal(*principalInfo, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + auto principalOrErr = PrincipalInfoToPrincipal(*principalInfo); + if (NS_WARN_IF(principalOrErr.isErr())) { + return principalOrErr.unwrapErr(); } - rv = QuotaManager::GetInfoFromPrincipal(principal, nullptr, nullptr, - &aOrigin); + nsCOMPtr principal = principalOrErr.unwrap(); + nsresult rv = QuotaManager::GetInfoFromPrincipal(principal, nullptr, + nullptr, &aOrigin); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } diff --git a/dom/indexedDB/IDBFactory.cpp b/dom/indexedDB/IDBFactory.cpp index 5610bf5473a3..5c54430a9750 100644 --- a/dom/indexedDB/IDBFactory.cpp +++ b/dom/indexedDB/IDBFactory.cpp @@ -595,10 +595,11 @@ RefPtr IDBFactory::OpenInternal( if (NS_IsMainThread()) { // aPrincipal is passed inconsistently, so even when we are already on // the main thread, we may have been passed a null aPrincipal. - nsCOMPtr principal = PrincipalInfoToPrincipal(principalInfo); - if (principal) { + auto principalOrErr = PrincipalInfoToPrincipal(principalInfo); + if (principalOrErr.isOk()) { nsAutoString addonId; - Unused << NS_WARN_IF(NS_FAILED(principal->GetAddonId(addonId))); + Unused << NS_WARN_IF( + NS_FAILED(principalOrErr.unwrap()->GetAddonId(addonId))); isAddon = !addonId.IsEmpty(); } } diff --git a/dom/ipc/ContentParent.cpp b/dom/ipc/ContentParent.cpp index 028c5f286a36..519f949e747c 100644 --- a/dom/ipc/ContentParent.cpp +++ b/dom/ipc/ContentParent.cpp @@ -14,6 +14,7 @@ #include "BrowserParent.h" #include "chrome/common/process_watcher.h" +#include "mozilla/Result.h" #ifdef ACCESSIBILITY # include "mozilla/a11y/PDocAccessible.h" @@ -2575,9 +2576,9 @@ bool ContentParent::InitInternal(ProcessPriority aInitialPriority) { // Send down to the content process the permissions for each of the // registered service worker scopes. for (auto& registration : registrations) { - nsCOMPtr principal = - PrincipalInfoToPrincipal(registration.principal()); - if (principal) { + auto principalOrErr = PrincipalInfoToPrincipal(registration.principal()); + if (principalOrErr.isOk()) { + nsCOMPtr principal = principalOrErr.unwrap(); TransmitPermissionsForPrincipal(principal); } } diff --git a/dom/ipc/PermissionMessageUtils.cpp b/dom/ipc/PermissionMessageUtils.cpp index 0bd588b47b04..93ab2d44bc69 100644 --- a/dom/ipc/PermissionMessageUtils.cpp +++ b/dom/ipc/PermissionMessageUtils.cpp @@ -7,6 +7,8 @@ #include "mozilla/dom/PermissionMessageUtils.h" #include "mozilla/ipc/BackgroundUtils.h" #include "mozilla/ipc/PBackgroundSharedTypes.h" +#include "nsCOMPtr.h" +#include "nsIPrincipal.h" namespace mozilla { namespace ipc { @@ -35,9 +37,19 @@ bool IPDLParamTraits::Read(const IPC::Message* aMsg, return false; } - nsresult rv = NS_OK; - *aResult = info ? PrincipalInfoToPrincipal(info.ref(), &rv) : nullptr; - return NS_SUCCEEDED(rv); + if (info.isNothing()) { + return true; + } + + auto principalOrErr = PrincipalInfoToPrincipal(info.ref()); + + if (NS_WARN_IF(principalOrErr.isErr())) { + return false; + } + + nsCOMPtr principal = principalOrErr.unwrap(); + *aResult = principal; + return true; } } // namespace ipc diff --git a/dom/localstorage/ActorsChild.cpp b/dom/localstorage/ActorsChild.cpp index 479ae1b2dd04..de724ef0b1c6 100644 --- a/dom/localstorage/ActorsChild.cpp +++ b/dom/localstorage/ActorsChild.cpp @@ -133,13 +133,13 @@ mozilla::ipc::IPCResult LSObserverChild::RecvObserve( return IPC_OK(); } - nsresult rv; - nsCOMPtr principal = - PrincipalInfoToPrincipal(aPrincipalInfo, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { + auto principalOrErr = PrincipalInfoToPrincipal(aPrincipalInfo); + if (NS_WARN_IF(principalOrErr.isErr())) { return IPC_FAIL_NO_REASON(this); } + nsCOMPtr principal = principalOrErr.unwrap(); + Storage::NotifyChange(/* aStorage */ nullptr, principal, aKey, aOldValue.AsString(), aNewValue.AsString(), /* aStorageType */ kLocalStorageType, aDocumentURI, diff --git a/dom/reporting/ReportingHeader.cpp b/dom/reporting/ReportingHeader.cpp index ec04ba0182b1..8671cef7ed8b 100644 --- a/dom/reporting/ReportingHeader.cpp +++ b/dom/reporting/ReportingHeader.cpp @@ -15,6 +15,7 @@ #include "mozilla/Services.h" #include "mozilla/StaticPrefs_dom.h" #include "mozilla/StaticPtr.h" +#include "nsCOMPtr.h" #include "nsContentUtils.h" #include "nsIEffectiveTLDService.h" #include "nsIHttpChannel.h" @@ -481,10 +482,12 @@ void ReportingHeader::GetEndpointForReport( const nsAString& aGroupName, const mozilla::ipc::PrincipalInfo& aPrincipalInfo, nsACString& aEndpointURI) { - nsCOMPtr principal = PrincipalInfoToPrincipal(aPrincipalInfo); - if (NS_WARN_IF(!principal)) { + auto principalOrErr = PrincipalInfoToPrincipal(aPrincipalInfo); + if (NS_WARN_IF(principalOrErr.isErr())) { return; } + + nsCOMPtr principal = principalOrErr.unwrap(); GetEndpointForReport(aGroupName, principal, aEndpointURI); } @@ -593,13 +596,13 @@ void ReportingHeader::RemoveEndpoint( return; } - nsCOMPtr principal = PrincipalInfoToPrincipal(aPrincipalInfo); - if (NS_WARN_IF(!principal)) { + auto principalOrErr = PrincipalInfoToPrincipal(aPrincipalInfo); + if (NS_WARN_IF(principalOrErr.isErr())) { return; } nsAutoCString origin; - rv = principal->GetOrigin(origin); + rv = principalOrErr.unwrap()->GetOrigin(origin); if (NS_WARN_IF(NS_FAILED(rv))) { return; } diff --git a/dom/serviceworkers/ServiceWorkerContainer.cpp b/dom/serviceworkers/ServiceWorkerContainer.cpp index 6a9044c4a49e..75382d905233 100644 --- a/dom/serviceworkers/ServiceWorkerContainer.cpp +++ b/dom/serviceworkers/ServiceWorkerContainer.cpp @@ -731,14 +731,14 @@ nsresult FillInOriginNoSuffix(const ServiceWorkerDescriptor& aServiceWorker, nsresult rv; - nsCOMPtr principal = - PrincipalInfoToPrincipal(aServiceWorker.PrincipalInfo(), &rv); - if (NS_FAILED(rv) || !principal) { - return rv; + auto principalOrErr = + PrincipalInfoToPrincipal(aServiceWorker.PrincipalInfo()); + if (NS_WARN_IF(principalOrErr.isErr())) { + return principalOrErr.unwrapErr(); } nsAutoCString originUTF8; - rv = principal->GetOriginNoSuffix(originUTF8); + rv = principalOrErr.unwrap()->GetOriginNoSuffix(originUTF8); if (NS_FAILED(rv)) { return rv; } diff --git a/dom/serviceworkers/ServiceWorkerDescriptor.cpp b/dom/serviceworkers/ServiceWorkerDescriptor.cpp index ee3ed3985466..a418623f566a 100644 --- a/dom/serviceworkers/ServiceWorkerDescriptor.cpp +++ b/dom/serviceworkers/ServiceWorkerDescriptor.cpp @@ -93,10 +93,10 @@ const mozilla::ipc::PrincipalInfo& ServiceWorkerDescriptor::PrincipalInfo() return mData->principalInfo(); } -nsCOMPtr ServiceWorkerDescriptor::GetPrincipal() const { +Result, nsresult> ServiceWorkerDescriptor::GetPrincipal() + const { AssertIsOnMainThread(); - nsCOMPtr ref = PrincipalInfoToPrincipal(mData->principalInfo()); - return ref; + return PrincipalInfoToPrincipal(mData->principalInfo()); } const nsCString& ServiceWorkerDescriptor::Scope() const { diff --git a/dom/serviceworkers/ServiceWorkerDescriptor.h b/dom/serviceworkers/ServiceWorkerDescriptor.h index cb99ff5fe24a..b85890089b36 100644 --- a/dom/serviceworkers/ServiceWorkerDescriptor.h +++ b/dom/serviceworkers/ServiceWorkerDescriptor.h @@ -70,7 +70,7 @@ class ServiceWorkerDescriptor final { const mozilla::ipc::PrincipalInfo& PrincipalInfo() const; - nsCOMPtr GetPrincipal() const; + Result, nsresult> GetPrincipal() const; const nsCString& Scope() const; diff --git a/dom/serviceworkers/ServiceWorkerInterceptController.cpp b/dom/serviceworkers/ServiceWorkerInterceptController.cpp index af5d2da74b08..2fcc8ddbb15f 100644 --- a/dom/serviceworkers/ServiceWorkerInterceptController.cpp +++ b/dom/serviceworkers/ServiceWorkerInterceptController.cpp @@ -8,9 +8,11 @@ #include "mozilla/BasePrincipal.h" #include "mozilla/StorageAccess.h" +#include "nsCOMPtr.h" #include "nsContentUtils.h" #include "nsIChannel.h" #include "ServiceWorkerManager.h" +#include "nsIPrincipal.h" namespace mozilla { namespace dom { @@ -49,9 +51,10 @@ ServiceWorkerInterceptController::ShouldPrepareForIntercept( // Get ServiceWorkerRegistrationInfo by the ServiceWorkerInfo's principal // and scope if (!*aShouldIntercept && swm) { + nsCOMPtr principal = + controller.ref().GetPrincipal().unwrap(); RefPtr registration = - swm->GetRegistration(controller.ref().GetPrincipal().get(), - controller.ref().Scope()); + swm->GetRegistration(principal, controller.ref().Scope()); // Could not get ServiceWorkerRegistration here if unregister is // executed before getting here. if (NS_WARN_IF(!registration)) { diff --git a/dom/serviceworkers/ServiceWorkerManager.cpp b/dom/serviceworkers/ServiceWorkerManager.cpp index d7a1cd8a92a7..994394d59a73 100644 --- a/dom/serviceworkers/ServiceWorkerManager.cpp +++ b/dom/serviceworkers/ServiceWorkerManager.cpp @@ -8,12 +8,14 @@ #include +#include "nsCOMPtr.h" #include "nsIEffectiveTLDService.h" #include "nsIHttpChannel.h" #include "nsIHttpChannelInternal.h" #include "nsINamed.h" #include "nsINetworkInterceptController.h" #include "nsIMutableArray.h" +#include "nsIPrincipal.h" #include "nsITimer.h" #include "nsIUploadChannel2.h" #include "nsServiceManagerUtils.h" @@ -966,8 +968,14 @@ RefPtr ServiceWorkerManager::Register( } // If the previous validation step passed then we must have a principal. - nsCOMPtr principal = aClientInfo.GetPrincipal(); + auto principalOrErr = aClientInfo.GetPrincipal(); + if (NS_WARN_IF(principalOrErr.isErr())) { + return ServiceWorkerRegistrationPromise::CreateAndReject( + CopyableErrorResult(principalOrErr.unwrapErr()), __func__); + } + + nsCOMPtr principal = principalOrErr.unwrap(); nsAutoCString scopeKey; rv = PrincipalToScopeKey(principal, scopeKey); if (NS_WARN_IF(NS_FAILED(rv))) { @@ -1021,11 +1029,13 @@ class GetRegistrationsRunnable final : public Runnable { return NS_OK; } - nsCOMPtr principal = mClientInfo.GetPrincipal(); - if (!principal) { + auto principalOrErr = mClientInfo.GetPrincipal(); + if (NS_WARN_IF(principalOrErr.isErr())) { return NS_OK; } + nsCOMPtr principal = principalOrErr.unwrap(); + nsTArray array; if (NS_WARN_IF(!BasePrincipal::Cast(principal)->IsContentPrincipal())) { @@ -1111,12 +1121,13 @@ class GetRegistrationRunnable final : public Runnable { return NS_OK; } - nsCOMPtr principal = mClientInfo.GetPrincipal(); - if (!principal) { + auto principalOrErr = mClientInfo.GetPrincipal(); + if (NS_WARN_IF(principalOrErr.isErr())) { mPromise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); return NS_OK; } + nsCOMPtr principal = principalOrErr.unwrap(); nsCOMPtr uri; nsresult rv = NS_NewURI(getter_AddRefs(uri), mURL); if (NS_WARN_IF(NS_FAILED(rv))) { @@ -1344,10 +1355,13 @@ void ServiceWorkerManager::NoteInheritedController( const ClientInfo& aClientInfo, const ServiceWorkerDescriptor& aController) { MOZ_ASSERT(NS_IsMainThread()); - nsCOMPtr principal = - PrincipalInfoToPrincipal(aController.PrincipalInfo()); - NS_ENSURE_TRUE_VOID(principal); + auto principalOrErr = PrincipalInfoToPrincipal(aController.PrincipalInfo()); + if (NS_WARN_IF(principalOrErr.isErr())) { + return; + } + + nsCOMPtr principal = principalOrErr.unwrap(); nsCOMPtr scope; nsresult rv = NS_NewURI(getter_AddRefs(scope), aController.Scope()); NS_ENSURE_SUCCESS_VOID(rv); @@ -1637,11 +1651,11 @@ void ServiceWorkerManager::LoadRegistration( const ServiceWorkerRegistrationData& aRegistration) { MOZ_ASSERT(NS_IsMainThread()); - nsCOMPtr principal = - PrincipalInfoToPrincipal(aRegistration.principal()); - if (!principal) { + auto principalOrErr = PrincipalInfoToPrincipal(aRegistration.principal()); + if (NS_WARN_IF(principalOrErr.isErr())) { return; } + nsCOMPtr principal = principalOrErr.unwrap(); RefPtr registration = GetRegistration(principal, aRegistration.scope()); @@ -1722,9 +1736,12 @@ void ServiceWorkerManager::StoreRegistration( already_AddRefed ServiceWorkerManager::GetServiceWorkerRegistrationInfo( const ClientInfo& aClientInfo) const { - nsCOMPtr principal = aClientInfo.GetPrincipal(); - NS_ENSURE_TRUE(principal, nullptr); + auto principalOrErr = aClientInfo.GetPrincipal(); + if (NS_WARN_IF(principalOrErr.isErr())) { + return nullptr; + } + nsCOMPtr principal = principalOrErr.unwrap(); nsCOMPtr uri; nsresult rv = NS_NewURI(getter_AddRefs(uri), aClientInfo.URL()); NS_ENSURE_SUCCESS(rv, nullptr); @@ -1948,9 +1965,14 @@ bool ServiceWorkerManager::StartControlling( const ServiceWorkerDescriptor& aServiceWorker) { MOZ_ASSERT(NS_IsMainThread()); - nsCOMPtr principal = + auto principalOrErr = PrincipalInfoToPrincipal(aServiceWorker.PrincipalInfo()); - NS_ENSURE_TRUE(principal, false); + + if (NS_WARN_IF(principalOrErr.isErr())) { + return false; + } + + nsCOMPtr principal = principalOrErr.unwrap(); nsCOMPtr scope; nsresult rv = NS_NewURI(getter_AddRefs(scope), aServiceWorker.Scope()); @@ -2231,8 +2253,13 @@ void ServiceWorkerManager::DispatchFetchEvent(nsIInterceptedChannel* aChannel, // here and perform the ClientChannelHelper's replacement of // reserved client automatically. if (!XRE_IsParentProcess()) { - nsCOMPtr clientPrincipal = - clientInfo.ref().GetPrincipal(); + auto clientPrincipalOrErr = clientInfo.ref().GetPrincipal(); + + nsCOMPtr clientPrincipal; + if (clientPrincipalOrErr.isOk()) { + clientPrincipal = clientPrincipalOrErr.unwrap(); + } + if (!clientPrincipal || !clientPrincipal->Equals(principal)) { UniquePtr reservedClient = loadInfo->TakeReservedClientSource(); @@ -2601,7 +2628,15 @@ RefPtr ServiceWorkerManager::MaybeClaimClient( } // Same origin check - nsCOMPtr principal(aClientInfo.GetPrincipal()); + auto principalOrErr = aClientInfo.GetPrincipal(); + + if (NS_WARN_IF(principalOrErr.isErr())) { + CopyableErrorResult rv; + rv.ThrowSecurityError("Could not extract client's principal"); + return GenericErrorResultPromise::CreateAndReject(rv, __func__); + } + + nsCOMPtr principal = principalOrErr.unwrap(); if (!aWorkerRegistration->Principal()->Equals(principal)) { CopyableErrorResult rv; rv.ThrowSecurityError("Worker is for a different origin"); @@ -2627,11 +2662,13 @@ RefPtr ServiceWorkerManager::MaybeClaimClient( RefPtr ServiceWorkerManager::MaybeClaimClient( const ClientInfo& aClientInfo, const ServiceWorkerDescriptor& aServiceWorker) { - nsCOMPtr principal = aServiceWorker.GetPrincipal(); - if (!principal) { + auto principalOrErr = aServiceWorker.GetPrincipal(); + if (NS_WARN_IF(principalOrErr.isErr())) { return GenericErrorResultPromise::CreateAndResolve(false, __func__); } + nsCOMPtr principal = principalOrErr.unwrap(); + RefPtr registration = GetRegistration(principal, aServiceWorker.Scope()); diff --git a/dom/serviceworkers/ServiceWorkerManagerChild.cpp b/dom/serviceworkers/ServiceWorkerManagerChild.cpp index bca901ab82ca..5d33f5c08474 100644 --- a/dom/serviceworkers/ServiceWorkerManagerChild.cpp +++ b/dom/serviceworkers/ServiceWorkerManagerChild.cpp @@ -55,11 +55,13 @@ mozilla::ipc::IPCResult ServiceWorkerManagerChild::RecvNotifyUnregister( return IPC_OK(); } - nsCOMPtr principal = PrincipalInfoToPrincipal(aPrincipalInfo); - if (NS_WARN_IF(!principal)) { + auto principalOrErr = PrincipalInfoToPrincipal(aPrincipalInfo); + if (NS_WARN_IF(principalOrErr.isErr())) { return IPC_OK(); } + nsCOMPtr principal = principalOrErr.unwrap(); + nsresult rv = swm->NotifyUnregister(principal, aScope); Unused << NS_WARN_IF(NS_FAILED(rv)); return IPC_OK(); diff --git a/dom/serviceworkers/ServiceWorkerManagerService.cpp b/dom/serviceworkers/ServiceWorkerManagerService.cpp index c83a9d91a68c..90c1defec685 100644 --- a/dom/serviceworkers/ServiceWorkerManagerService.cpp +++ b/dom/serviceworkers/ServiceWorkerManagerService.cpp @@ -104,8 +104,9 @@ void ServiceWorkerManagerService::PropagateRegistration( nsTArray cps; ContentParent::GetAll(cps); for (auto* cp : cps) { - nsCOMPtr principal = PrincipalInfoToPrincipal(pi); - if (principal) { + auto principalOrErr = PrincipalInfoToPrincipal(pi); + if (principalOrErr.isOk()) { + nsCOMPtr principal = principalOrErr.unwrap(); cp->TransmitPermissionsForPrincipal(principal); } } diff --git a/dom/serviceworkers/ServiceWorkerRegistrationDescriptor.cpp b/dom/serviceworkers/ServiceWorkerRegistrationDescriptor.cpp index f4f676430ef4..0cb6ab917903 100644 --- a/dom/serviceworkers/ServiceWorkerRegistrationDescriptor.cpp +++ b/dom/serviceworkers/ServiceWorkerRegistrationDescriptor.cpp @@ -119,11 +119,10 @@ ServiceWorkerRegistrationDescriptor::PrincipalInfo() const { return mData->principalInfo(); } -nsCOMPtr ServiceWorkerRegistrationDescriptor::GetPrincipal() - const { +Result, nsresult> +ServiceWorkerRegistrationDescriptor::GetPrincipal() const { AssertIsOnMainThread(); - nsCOMPtr ref = PrincipalInfoToPrincipal(mData->principalInfo()); - return ref; + return PrincipalInfoToPrincipal(mData->principalInfo()); } const nsCString& ServiceWorkerRegistrationDescriptor::Scope() const { diff --git a/dom/serviceworkers/ServiceWorkerRegistrationDescriptor.h b/dom/serviceworkers/ServiceWorkerRegistrationDescriptor.h index 81d92a3e7b59..ec0f96986805 100644 --- a/dom/serviceworkers/ServiceWorkerRegistrationDescriptor.h +++ b/dom/serviceworkers/ServiceWorkerRegistrationDescriptor.h @@ -70,7 +70,7 @@ class ServiceWorkerRegistrationDescriptor final { const mozilla::ipc::PrincipalInfo& PrincipalInfo() const; - nsCOMPtr GetPrincipal() const; + Result, nsresult> GetPrincipal() const; const nsCString& Scope() const; diff --git a/dom/serviceworkers/ServiceWorkerRegistrationImpl.cpp b/dom/serviceworkers/ServiceWorkerRegistrationImpl.cpp index b067217a2a40..173868439f74 100644 --- a/dom/serviceworkers/ServiceWorkerRegistrationImpl.cpp +++ b/dom/serviceworkers/ServiceWorkerRegistrationImpl.cpp @@ -19,6 +19,7 @@ #include "mozilla/Services.h" #include "mozilla/Unused.h" #include "nsCycleCollectionParticipant.h" +#include "nsIPrincipal.h" #include "nsNetUtil.h" #include "nsServiceManagerUtils.h" #include "ServiceWorker.h" @@ -285,8 +286,8 @@ class SWRUpdateRunnable final : public Runnable { MOZ_ASSERT(NS_IsMainThread()); ErrorResult result; - nsCOMPtr principal = mDescriptor.GetPrincipal(); - if (NS_WARN_IF(!principal)) { + auto principalOrErr = mDescriptor.GetPrincipal(); + if (NS_WARN_IF(principalOrErr.isErr())) { mPromise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); return NS_OK; } @@ -297,6 +298,8 @@ class SWRUpdateRunnable final : public Runnable { return NS_OK; } + nsCOMPtr principal = principalOrErr.unwrap(); + // This will delay update jobs originating from a service worker thread. // We don't currently handle ServiceWorkerRegistration.update() from other // worker types. Also, we assume this registration matches self.registration @@ -445,12 +448,14 @@ class StartUnregisterRunnable final : public Runnable { Run() override { MOZ_ASSERT(NS_IsMainThread()); - nsCOMPtr principal = mDescriptor.GetPrincipal(); - if (!principal) { + auto principalOrErr = mDescriptor.GetPrincipal(); + if (NS_WARN_IF(principalOrErr.isErr())) { mPromise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); return NS_OK; } + nsCOMPtr principal = principalOrErr.unwrap(); + nsCOMPtr swm = mozilla::services::GetServiceWorkerManager(); if (!swm) { @@ -493,12 +498,14 @@ void ServiceWorkerRegistrationMainThread::Update( return; } - nsCOMPtr principal = mDescriptor.GetPrincipal(); - if (!principal) { + auto principalOrErr = mDescriptor.GetPrincipal(); + if (NS_WARN_IF(principalOrErr.isErr())) { aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR)); return; } + nsCOMPtr principal = principalOrErr.unwrap(); + RefPtr cb = new MainThreadUpdateCallback(); UpdateInternal(principal, NS_ConvertUTF16toUTF8(mScope), aNewestWorkerScriptUrl, cb); @@ -542,12 +549,14 @@ void ServiceWorkerRegistrationMainThread::Unregister( return; } - nsCOMPtr principal = mDescriptor.GetPrincipal(); - if (!principal) { + auto principalOrErr = mDescriptor.GetPrincipal(); + if (NS_WARN_IF(principalOrErr.isErr())) { aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR)); return; } + nsCOMPtr principal = principalOrErr.unwrap(); + RefPtr cb = new UnregisterCallback(); nsresult rv = swm->Unregister(principal, cb, diff --git a/dom/serviceworkers/ServiceWorkerUtils.cpp b/dom/serviceworkers/ServiceWorkerUtils.cpp index 015b10a1a4c1..34d0d1537fb0 100644 --- a/dom/serviceworkers/ServiceWorkerUtils.cpp +++ b/dom/serviceworkers/ServiceWorkerUtils.cpp @@ -9,6 +9,8 @@ #include "mozilla/Preferences.h" #include "mozilla/dom/ClientInfo.h" #include "mozilla/dom/ServiceWorkerRegistrarTypes.h" +#include "nsCOMPtr.h" +#include "nsIPrincipal.h" #include "nsIURL.h" namespace mozilla { @@ -71,8 +73,8 @@ void ServiceWorkerScopeAndScriptAreValid(const ClientInfo& aClientInfo, MOZ_DIAGNOSTIC_ASSERT(aScopeURI); MOZ_DIAGNOSTIC_ASSERT(aScriptURI); - nsCOMPtr principal = aClientInfo.GetPrincipal(); - if (NS_WARN_IF(!principal)) { + auto principalOrErr = aClientInfo.GetPrincipal(); + if (NS_WARN_IF(principalOrErr.isErr())) { aRv.ThrowInvalidStateError("Can't make security decisions about Client"); return; } @@ -116,6 +118,8 @@ void ServiceWorkerScopeAndScriptAreValid(const ClientInfo& aClientInfo, return; } + nsCOMPtr principal = principalOrErr.unwrap(); + // Unfortunately we don't seem to have an obvious window id here; in // particular ClientInfo does not have one. nsresult rv = principal->CheckMayLoadWithReporting( diff --git a/dom/simpledb/ActorsParent.cpp b/dom/simpledb/ActorsParent.cpp index 155836aa97a0..418d7a3d0013 100644 --- a/dom/simpledb/ActorsParent.cpp +++ b/dom/simpledb/ActorsParent.cpp @@ -19,6 +19,7 @@ #include "mozilla/ipc/PBackgroundSharedTypes.h" #include "nsIFileStreams.h" #include "nsIDirectoryEnumerator.h" +#include "nsIPrincipal.h" #include "nsStringStream.h" #include "prio.h" #include "SimpleDBCommon.h" @@ -1042,15 +1043,15 @@ nsresult OpenOp::Open() { } else { MOZ_ASSERT(principalInfo.type() == PrincipalInfo::TContentPrincipalInfo); - nsresult rv; - nsCOMPtr principal = - PrincipalInfoToPrincipal(principalInfo, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + auto principalOrErr = PrincipalInfoToPrincipal(principalInfo); + if (NS_WARN_IF(principalOrErr.isErr())) { + return principalOrErr.unwrapErr(); } - rv = QuotaManager::GetInfoFromPrincipal(principal, &mSuffix, &mGroup, - &mOrigin); + nsCOMPtr principal = principalOrErr.unwrap(); + + nsresult rv = QuotaManager::GetInfoFromPrincipal(principal, &mSuffix, + &mGroup, &mOrigin); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } diff --git a/dom/storage/StorageActivityService.cpp b/dom/storage/StorageActivityService.cpp index 1bd2de845b2f..bea0bed0690b 100644 --- a/dom/storage/StorageActivityService.cpp +++ b/dom/storage/StorageActivityService.cpp @@ -9,7 +9,9 @@ #include "mozilla/ipc/BackgroundUtils.h" #include "mozilla/SchedulerGroup.h" #include "mozilla/StaticPtr.h" +#include "nsCOMPtr.h" #include "nsIMutableArray.h" +#include "nsIPrincipal.h" #include "nsSupportsPrimitives.h" #include "nsXPCOM.h" @@ -54,10 +56,17 @@ void StorageActivityService::SendActivity( "StorageActivityService::SendActivity", [aPrincipalInfo]() { MOZ_ASSERT(NS_IsMainThread()); - nsCOMPtr principal = + auto principalOrErr = mozilla::ipc::PrincipalInfoToPrincipal(aPrincipalInfo); - StorageActivityService::SendActivity(principal); + if (principalOrErr.isOk()) { + nsCOMPtr principal = principalOrErr.unwrap(); + StorageActivityService::SendActivity(principal); + } else { + NS_WARNING( + "Could not obtain principal from " + "mozilla::ipc::PrincipalInfoToPrincipal"); + } }); SchedulerGroup::Dispatch(TaskCategory::Other, r.forget()); diff --git a/dom/storage/StorageIPC.cpp b/dom/storage/StorageIPC.cpp index a882dd141867..71ebdd15c434 100644 --- a/dom/storage/StorageIPC.cpp +++ b/dom/storage/StorageIPC.cpp @@ -14,6 +14,8 @@ #include "mozilla/ipc/PBackgroundParent.h" #include "mozilla/dom/ContentParent.h" #include "mozilla/Unused.h" +#include "nsCOMPtr.h" +#include "nsIPrincipal.h" #include "nsThreadUtils.h" namespace mozilla { @@ -76,19 +78,19 @@ mozilla::ipc::IPCResult LocalStorageCacheChild::RecvObserve( const nsString& aNewValue) { AssertIsOnOwningThread(); - nsresult rv; - nsCOMPtr principal = - PrincipalInfoToPrincipal(aPrincipalInfo, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { + auto principalOrErr = PrincipalInfoToPrincipal(aPrincipalInfo); + if (NS_WARN_IF(principalOrErr.isErr())) { return IPC_FAIL_NO_REASON(this); } - nsCOMPtr cachePrincipal = - PrincipalInfoToPrincipal(aCachePrincipalInfo, &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { + auto cachePrincipalOrErr = PrincipalInfoToPrincipal(aCachePrincipalInfo); + if (NS_WARN_IF(cachePrincipalOrErr.isErr())) { return IPC_FAIL_NO_REASON(this); } + nsCOMPtr principal = principalOrErr.unwrap(); + nsCOMPtr cachePrincipal = cachePrincipalOrErr.unwrap(); + if (StorageUtils::PrincipalsEqual(principal, cachePrincipal)) { Storage::NotifyChange(/* aStorage */ nullptr, principal, aKey, aOldValue, aNewValue, diff --git a/dom/webbrowserpersist/WebBrowserPersistRemoteDocument.cpp b/dom/webbrowserpersist/WebBrowserPersistRemoteDocument.cpp index f07f202470b1..12bc4be5c549 100644 --- a/dom/webbrowserpersist/WebBrowserPersistRemoteDocument.cpp +++ b/dom/webbrowserpersist/WebBrowserPersistRemoteDocument.cpp @@ -12,6 +12,7 @@ #include "mozilla/Unused.h" #include "mozilla/ipc/BackgroundUtils.h" +#include "nsDebug.h" #include "nsIPrincipal.h" namespace mozilla { @@ -22,8 +23,12 @@ WebBrowserPersistRemoteDocument ::WebBrowserPersistRemoteDocument( WebBrowserPersistDocumentParent* aActor, const Attrs& aAttrs, nsIInputStream* aPostData) : mActor(aActor), mAttrs(aAttrs), mPostData(aPostData) { - nsresult rv; - mPrincipal = ipc::PrincipalInfoToPrincipal(mAttrs.principal(), &rv); + auto principalOrErr = ipc::PrincipalInfoToPrincipal(mAttrs.principal()); + if (principalOrErr.isOk()) { + mPrincipal = principalOrErr.unwrap(); + } else { + NS_WARNING("Failed to obtain principal!"); + } if (mAttrs.sessionHistoryEntryOrCacheKey().type() == SessionHistoryEntryOrCacheKey::TPSHEntryParent) { mSHEntry = static_cast( diff --git a/dom/workers/ScriptLoader.cpp b/dom/workers/ScriptLoader.cpp index c81d2eb34c59..4c30d4163aff 100644 --- a/dom/workers/ScriptLoader.cpp +++ b/dom/workers/ScriptLoader.cpp @@ -18,6 +18,7 @@ #include "nsIInputStreamPump.h" #include "nsIIOService.h" #include "nsIOService.h" +#include "nsIPrincipal.h" #include "nsIProtocolHandler.h" #include "nsIScriptError.h" #include "nsIScriptSecurityManager.h" @@ -1365,9 +1366,8 @@ class ScriptLoaderRunnable final : public nsIRunnable, public nsINamed { ScriptLoadInfo& loadInfo = mLoadInfos[aIndex]; MOZ_ASSERT(loadInfo.mCacheStatus == ScriptLoadInfo::Cached); - nsCOMPtr responsePrincipal = - PrincipalInfoToPrincipal(*aPrincipalInfo); - MOZ_DIAGNOSTIC_ASSERT(responsePrincipal); + auto responsePrincipalOrErr = PrincipalInfoToPrincipal(*aPrincipalInfo); + MOZ_DIAGNOSTIC_ASSERT(responsePrincipalOrErr.isOk()); nsIPrincipal* principal = mWorkerPrivate->GetPrincipal(); if (!principal) { @@ -1376,6 +1376,8 @@ class ScriptLoaderRunnable final : public nsIRunnable, public nsINamed { principal = parentWorker->GetPrincipal(); } + nsCOMPtr responsePrincipal = responsePrincipalOrErr.unwrap(); + loadInfo.mMutedErrorFlag.emplace(!principal->Subsumes(responsePrincipal)); // May be null. diff --git a/dom/workers/remoteworkers/RemoteWorkerChild.cpp b/dom/workers/remoteworkers/RemoteWorkerChild.cpp index c5469b7a22dd..ce965bedd50a 100644 --- a/dom/workers/remoteworkers/RemoteWorkerChild.cpp +++ b/dom/workers/remoteworkers/RemoteWorkerChild.cpp @@ -9,6 +9,7 @@ #include #include "MainThreadUtils.h" +#include "nsCOMPtr.h" #include "nsDebug.h" #include "nsError.h" #include "nsIConsoleReportCollector.h" @@ -309,26 +310,25 @@ nsresult RemoteWorkerChild::ExecWorkerOnMainThread(RemoteWorkerData&& aData) { // Ensure that the IndexedDatabaseManager is initialized Unused << NS_WARN_IF(!IndexedDatabaseManager::GetOrCreate()); - nsresult rv = NS_OK; - auto scopeExit = MakeScopeExit([&] { TransitionStateToTerminated(); }); - nsCOMPtr principal = - PrincipalInfoToPrincipal(aData.principalInfo(), &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + auto principalOrErr = PrincipalInfoToPrincipal(aData.principalInfo()); + if (NS_WARN_IF(principalOrErr.isErr())) { + return principalOrErr.unwrapErr(); } - nsCOMPtr loadingPrincipal = - PrincipalInfoToPrincipal(aData.loadingPrincipalInfo(), &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + nsCOMPtr principal = principalOrErr.unwrap(); + + auto loadingPrincipalOrErr = + PrincipalInfoToPrincipal(aData.loadingPrincipalInfo()); + if (NS_WARN_IF(loadingPrincipalOrErr.isErr())) { + return loadingPrincipalOrErr.unwrapErr(); } - nsCOMPtr storagePrincipal = - PrincipalInfoToPrincipal(aData.storagePrincipalInfo(), &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + auto storagePrincipalOrErr = + PrincipalInfoToPrincipal(aData.storagePrincipalInfo()); + if (NS_WARN_IF(storagePrincipalOrErr.isErr())) { + return storagePrincipalOrErr.unwrapErr(); } WorkerLoadInfo info; @@ -342,8 +342,8 @@ nsresult RemoteWorkerChild::ExecWorkerOnMainThread(RemoteWorkerData&& aData) { info.mReferrerInfo = aData.referrerInfo(); info.mDomain = aData.domain(); info.mPrincipal = principal; - info.mStoragePrincipal = storagePrincipal; - info.mLoadingPrincipal = loadingPrincipal; + info.mStoragePrincipal = storagePrincipalOrErr.unwrap(); + info.mLoadingPrincipal = loadingPrincipalOrErr.unwrap(); info.mStorageAccess = aData.storageAccess(); info.mOriginAttributes = BasePrincipal::Cast(principal)->OriginAttributesRef(); @@ -368,6 +368,8 @@ nsresult RemoteWorkerChild::ExecWorkerOnMainThread(RemoteWorkerData&& aData) { clientInfo.emplace(ClientInfo(aData.clientInfo().ref())); } + nsresult rv = NS_OK; + if (clientInfo.isSome()) { Maybe cspInfo = clientInfo.ref().GetCspInfo(); if (cspInfo.isSome()) { diff --git a/dom/workers/remoteworkers/RemoteWorkerManager.cpp b/dom/workers/remoteworkers/RemoteWorkerManager.cpp index 98a00f1fbdb1..8c3d5def5341 100644 --- a/dom/workers/remoteworkers/RemoteWorkerManager.cpp +++ b/dom/workers/remoteworkers/RemoteWorkerManager.cpp @@ -54,7 +54,13 @@ void TransmitPermissionsAndBlobURLsForPrincipalInfo( AssertIsOnMainThread(); MOZ_ASSERT(aContentParent); - nsCOMPtr principal = PrincipalInfoToPrincipal(aPrincipalInfo); + auto principalOrErr = PrincipalInfoToPrincipal(aPrincipalInfo); + + if (NS_WARN_IF(principalOrErr.isErr())) { + return; + } + + nsCOMPtr principal = principalOrErr.unwrap(); aContentParent->TransmitBlobURLsForPrincipal(principal); diff --git a/dom/workers/sharedworkers/SharedWorkerService.cpp b/dom/workers/sharedworkers/SharedWorkerService.cpp index 7144636ff10f..5ab0595e3428 100644 --- a/dom/workers/sharedworkers/SharedWorkerService.cpp +++ b/dom/workers/sharedworkers/SharedWorkerService.cpp @@ -9,6 +9,7 @@ #include "mozilla/ipc/BackgroundParent.h" #include "mozilla/SchedulerGroup.h" #include "mozilla/StaticMutex.h" +#include "nsIPrincipal.h" #include "nsProxyRelease.h" namespace mozilla { @@ -171,23 +172,27 @@ void SharedWorkerService::GetOrCreateWorkerManagerOnMainThread( MOZ_ASSERT(aBackgroundEventTarget); MOZ_ASSERT(aActor); - nsresult rv = NS_OK; - nsCOMPtr storagePrincipal = - PrincipalInfoToPrincipal(aData.storagePrincipalInfo(), &rv); - if (NS_WARN_IF(!storagePrincipal)) { - ErrorPropagationOnMainThread(aBackgroundEventTarget, aActor, rv); + auto storagePrincipalOrErr = + PrincipalInfoToPrincipal(aData.storagePrincipalInfo()); + if (NS_WARN_IF(storagePrincipalOrErr.isErr())) { + ErrorPropagationOnMainThread(aBackgroundEventTarget, aActor, + storagePrincipalOrErr.unwrapErr()); return; } - nsCOMPtr loadingPrincipal = - PrincipalInfoToPrincipal(aData.loadingPrincipalInfo(), &rv); - if (NS_WARN_IF(!loadingPrincipal)) { - ErrorPropagationOnMainThread(aBackgroundEventTarget, aActor, rv); + auto loadingPrincipalOrErr = + PrincipalInfoToPrincipal(aData.loadingPrincipalInfo()); + if (NS_WARN_IF(loadingPrincipalOrErr.isErr())) { + ErrorPropagationOnMainThread(aBackgroundEventTarget, aActor, + loadingPrincipalOrErr.unwrapErr()); return; } RefPtr managerHolder; + nsCOMPtr loadingPrincipal = loadingPrincipalOrErr.unwrap(); + nsCOMPtr storagePrincipal = storagePrincipalOrErr.unwrap(); + // Let's see if there is already a SharedWorker to share. nsCOMPtr resolvedScriptURL = DeserializeURI(aData.resolvedScriptURL()); diff --git a/ipc/glue/BackgroundParentImpl.cpp b/ipc/glue/BackgroundParentImpl.cpp index 8d6cb48e3b5d..e5f37802f0b7 100644 --- a/ipc/glue/BackgroundParentImpl.cpp +++ b/ipc/glue/BackgroundParentImpl.cpp @@ -864,10 +864,14 @@ class CheckPrincipalRunnable final : public Runnable { NullifyContentParentRAII raii(mContentParent); - nsCOMPtr principal = PrincipalInfoToPrincipal(mPrincipalInfo); + auto principalOrErr = PrincipalInfoToPrincipal(mPrincipalInfo); + if (NS_WARN_IF(principalOrErr.isErr())) { + mContentParent->KillHard( + "BroadcastChannel killed: PrincipalInfoToPrincipal failed."); + } nsAutoCString origin; - nsresult rv = principal->GetOrigin(origin); + nsresult rv = principalOrErr.unwrap()->GetOrigin(origin); if (NS_FAILED(rv)) { mContentParent->KillHard( "BroadcastChannel killed: principal::GetOrigin failed."); diff --git a/ipc/glue/BackgroundUtils.cpp b/ipc/glue/BackgroundUtils.cpp index a551e00bcfa3..c2d153612ecc 100644 --- a/ipc/glue/BackgroundUtils.cpp +++ b/ipc/glue/BackgroundUtils.cpp @@ -39,30 +39,28 @@ using namespace mozilla::net; namespace ipc { -already_AddRefed PrincipalInfoToPrincipal( - const PrincipalInfo& aPrincipalInfo, nsresult* aOptionalResult) { +Result, nsresult> PrincipalInfoToPrincipal( + const PrincipalInfo& aPrincipalInfo) { MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(aPrincipalInfo.type() != PrincipalInfo::T__None); - nsresult stackResult; - nsresult& rv = aOptionalResult ? *aOptionalResult : stackResult; - nsCOMPtr secMan = nsContentUtils::GetSecurityManager(); if (!secMan) { - return nullptr; + return Err(NS_ERROR_NULL_POINTER); } nsCOMPtr principal; + nsresult rv; switch (aPrincipalInfo.type()) { case PrincipalInfo::TSystemPrincipalInfo: { rv = secMan->GetSystemPrincipal(getter_AddRefs(principal)); if (NS_WARN_IF(NS_FAILED(rv))) { - return nullptr; + return Err(rv); } - return principal.forget(); + return principal; } case PrincipalInfo::TNullPrincipalInfo: { @@ -71,11 +69,11 @@ already_AddRefed PrincipalInfoToPrincipal( nsCOMPtr uri; rv = NS_NewURI(getter_AddRefs(uri), info.spec()); if (NS_WARN_IF(NS_FAILED(rv))) { - return nullptr; + return Err(rv); } principal = NullPrincipal::Create(info.attrs(), uri); - return principal.forget(); + return principal; } case PrincipalInfo::TContentPrincipalInfo: { @@ -85,53 +83,50 @@ already_AddRefed PrincipalInfoToPrincipal( nsCOMPtr uri; rv = NS_NewURI(getter_AddRefs(uri), info.spec()); if (NS_WARN_IF(NS_FAILED(rv))) { - return nullptr; + return Err(rv); } principal = BasePrincipal::CreateContentPrincipal(uri, info.attrs()); if (NS_WARN_IF(!principal)) { - return nullptr; + return Err(NS_ERROR_NULL_POINTER); } // Origin must match what the_new_principal.getOrigin returns. nsAutoCString originNoSuffix; rv = principal->GetOriginNoSuffix(originNoSuffix); - if (NS_WARN_IF(NS_FAILED(rv)) || - !info.originNoSuffix().Equals(originNoSuffix)) { -#ifdef FUZZING - return nullptr; -#else - MOZ_CRASH("Origin must be available when deserialized"); -#endif /* FUZZING */ + if (NS_WARN_IF(NS_FAILED(rv))) { + return Err(rv); + } + + if (NS_WARN_IF(!info.originNoSuffix().Equals(originNoSuffix))) { + return Err(NS_ERROR_FAILURE); } if (info.domain()) { nsCOMPtr domain; rv = NS_NewURI(getter_AddRefs(domain), *info.domain()); if (NS_WARN_IF(NS_FAILED(rv))) { - return nullptr; + return Err(rv); } rv = principal->SetDomain(domain); if (NS_WARN_IF(NS_FAILED(rv))) { - return nullptr; + return Err(rv); } } if (!info.baseDomain().IsVoid()) { nsAutoCString baseDomain; rv = principal->GetBaseDomain(baseDomain); - if (NS_WARN_IF(NS_FAILED(rv)) || - !info.baseDomain().Equals(baseDomain)) { -#ifdef FUZZING - return nullptr; -#else - MOZ_CRASH("Base domain must be available when deserialized"); -#endif /* FUZZING */ + if (NS_WARN_IF(NS_FAILED(rv))) { + return Err(rv); + } + + if (NS_WARN_IF(!info.baseDomain().Equals(baseDomain))) { + return Err(NS_ERROR_FAILURE); } } - - return principal.forget(); + return principal; } case PrincipalInfo::TExpandedPrincipalInfo: { @@ -142,30 +137,29 @@ already_AddRefed PrincipalInfoToPrincipal( nsCOMPtr alPrincipal; for (uint32_t i = 0; i < info.allowlist().Length(); i++) { - alPrincipal = PrincipalInfoToPrincipal(info.allowlist()[i], &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { - return nullptr; + auto principalOrErr = PrincipalInfoToPrincipal(info.allowlist()[i]); + if (NS_WARN_IF(principalOrErr.isErr())) { + nsresult ret = principalOrErr.unwrapErr(); + return Err(ret); } // append that principal to the allowlist - allowlist.AppendElement(alPrincipal); + allowlist.AppendElement(principalOrErr.unwrap()); } RefPtr expandedPrincipal = ExpandedPrincipal::Create(allowlist, info.attrs()); if (!expandedPrincipal) { - NS_WARNING("could not instantiate expanded principal"); - return nullptr; + return Err(NS_ERROR_FAILURE); } principal = expandedPrincipal; - return principal.forget(); + return principal; } default: - MOZ_CRASH("Unknown PrincipalInfo type!"); + return Err(NS_ERROR_FAILURE); } - - MOZ_CRASH("Should never get here!"); + return Err(NS_ERROR_FAILURE); } already_AddRefed CSPInfoToCSP( @@ -184,9 +178,9 @@ already_AddRefed CSPInfoToCSP( return nullptr; } } else { - nsCOMPtr requestingPrincipal = - PrincipalInfoToPrincipal(aCSPInfo.requestPrincipalInfo(), &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { + auto principalOrErr = + PrincipalInfoToPrincipal(aCSPInfo.requestPrincipalInfo()); + if (NS_WARN_IF(principalOrErr.isErr())) { return nullptr; } @@ -197,9 +191,11 @@ already_AddRefed CSPInfoToCSP( return nullptr; } } - rv = csp->SetRequestContextWithPrincipal(requestingPrincipal, selfURI, - aCSPInfo.referrer(), - aCSPInfo.innerWindowID()); + + nsCOMPtr principal = principalOrErr.unwrap(); + + rv = csp->SetRequestContextWithPrincipal( + principal, selfURI, aCSPInfo.referrer(), aCSPInfo.innerWindowID()); if (NS_WARN_IF(NS_FAILED(rv))) { return nullptr; } @@ -352,13 +348,12 @@ bool IsPrincipalInfoPrivate(const PrincipalInfo& aPrincipalInfo) { already_AddRefed RHEntryInfoToRHEntry( const RedirectHistoryEntryInfo& aRHEntryInfo) { - nsresult rv; - nsCOMPtr principal = - PrincipalInfoToPrincipal(aRHEntryInfo.principalInfo(), &rv); - if (NS_WARN_IF(NS_FAILED(rv))) { + auto principalOrErr = PrincipalInfoToPrincipal(aRHEntryInfo.principalInfo()); + if (NS_WARN_IF(principalOrErr.isErr())) { return nullptr; } + nsCOMPtr principal = principalOrErr.unwrap(); nsCOMPtr referrerUri = DeserializeURI(aRHEntryInfo.referrerUri()); nsCOMPtr entry = new nsRedirectHistoryEntry( @@ -588,45 +583,63 @@ nsresult LoadInfoArgsToLoadInfo( const LoadInfoArgs& loadInfoArgs = aOptionalLoadInfoArgs.ref(); - nsresult rv = NS_OK; nsCOMPtr loadingPrincipal; if (loadInfoArgs.requestingPrincipalInfo().isSome()) { - loadingPrincipal = PrincipalInfoToPrincipal( - loadInfoArgs.requestingPrincipalInfo().ref(), &rv); - NS_ENSURE_SUCCESS(rv, rv); + auto loadingPrincipalOrErr = + PrincipalInfoToPrincipal(loadInfoArgs.requestingPrincipalInfo().ref()); + if (NS_WARN_IF(loadingPrincipalOrErr.isErr())) { + return loadingPrincipalOrErr.unwrapErr(); + } + loadingPrincipal = loadingPrincipalOrErr.unwrap(); } - NS_ENSURE_SUCCESS(rv, rv); + auto triggeringPrincipalOrErr = + PrincipalInfoToPrincipal(loadInfoArgs.triggeringPrincipalInfo()); + if (NS_WARN_IF(triggeringPrincipalOrErr.isErr())) { + return triggeringPrincipalOrErr.unwrapErr(); + } nsCOMPtr triggeringPrincipal = - PrincipalInfoToPrincipal(loadInfoArgs.triggeringPrincipalInfo(), &rv); - NS_ENSURE_SUCCESS(rv, rv); + triggeringPrincipalOrErr.unwrap(); nsCOMPtr principalToInherit; if (loadInfoArgs.principalToInheritInfo().isSome()) { - principalToInherit = PrincipalInfoToPrincipal( - loadInfoArgs.principalToInheritInfo().ref(), &rv); - NS_ENSURE_SUCCESS(rv, rv); + auto principalToInheritOrErr = + PrincipalInfoToPrincipal(loadInfoArgs.principalToInheritInfo().ref()); + if (NS_WARN_IF(principalToInheritOrErr.isErr())) { + return principalToInheritOrErr.unwrapErr(); + } + principalToInherit = principalToInheritOrErr.unwrap(); } nsCOMPtr sandboxedLoadingPrincipal; if (loadInfoArgs.sandboxedLoadingPrincipalInfo().isSome()) { - sandboxedLoadingPrincipal = PrincipalInfoToPrincipal( - loadInfoArgs.sandboxedLoadingPrincipalInfo().ref(), &rv); - NS_ENSURE_SUCCESS(rv, rv); + auto sandboxedLoadingPrincipalOrErr = PrincipalInfoToPrincipal( + loadInfoArgs.sandboxedLoadingPrincipalInfo().ref()); + if (NS_WARN_IF(sandboxedLoadingPrincipalOrErr.isErr())) { + return sandboxedLoadingPrincipalOrErr.unwrapErr(); + } + sandboxedLoadingPrincipal = sandboxedLoadingPrincipalOrErr.unwrap(); } + nsresult rv = NS_OK; nsCOMPtr topLevelPrincipal; if (loadInfoArgs.topLevelPrincipalInfo().isSome()) { - topLevelPrincipal = PrincipalInfoToPrincipal( - loadInfoArgs.topLevelPrincipalInfo().ref(), &rv); - NS_ENSURE_SUCCESS(rv, rv); + auto topLevelPrincipalOrErr = + PrincipalInfoToPrincipal(loadInfoArgs.topLevelPrincipalInfo().ref()); + if (NS_WARN_IF(topLevelPrincipalOrErr.isErr())) { + return topLevelPrincipalOrErr.unwrapErr(); + } + topLevelPrincipal = topLevelPrincipalOrErr.unwrap(); } nsCOMPtr topLevelStorageAreaPrincipal; if (loadInfoArgs.topLevelStorageAreaPrincipalInfo().isSome()) { - topLevelStorageAreaPrincipal = PrincipalInfoToPrincipal( - loadInfoArgs.topLevelStorageAreaPrincipalInfo().ref(), &rv); - NS_ENSURE_SUCCESS(rv, rv); + auto topLevelStorageAreaPrincipalOrErr = PrincipalInfoToPrincipal( + loadInfoArgs.topLevelStorageAreaPrincipalInfo().ref()); + if (NS_WARN_IF(topLevelStorageAreaPrincipalOrErr.isErr())) { + return topLevelStorageAreaPrincipalOrErr.unwrapErr(); + } + topLevelStorageAreaPrincipal = topLevelStorageAreaPrincipalOrErr.unwrap(); } nsCOMPtr resultPrincipalURI; @@ -657,9 +670,11 @@ nsresult LoadInfoArgsToLoadInfo( nsTArray> ancestorPrincipals; ancestorPrincipals.SetCapacity(loadInfoArgs.ancestorPrincipals().Length()); for (const PrincipalInfo& principalInfo : loadInfoArgs.ancestorPrincipals()) { - nsCOMPtr ancestorPrincipal = - PrincipalInfoToPrincipal(principalInfo, &rv); - NS_ENSURE_SUCCESS(rv, rv); + auto ancestorPrincipalOrErr = PrincipalInfoToPrincipal(principalInfo); + if (NS_WARN_IF(ancestorPrincipalOrErr.isErr())) { + return ancestorPrincipalOrErr.unwrapErr(); + } + nsCOMPtr ancestorPrincipal = ancestorPrincipalOrErr.unwrap(); ancestorPrincipals.AppendElement(ancestorPrincipal.forget()); } diff --git a/ipc/glue/BackgroundUtils.h b/ipc/glue/BackgroundUtils.h index 2ddf1c23590a..b2b0f66fe8cc 100644 --- a/ipc/glue/BackgroundUtils.h +++ b/ipc/glue/BackgroundUtils.h @@ -65,10 +65,10 @@ class PrincipalInfo; /** * Convert a PrincipalInfo to an nsIPrincipal. * - * MUST be called on the main thread only. + * MUST be called on the main thread. */ -already_AddRefed PrincipalInfoToPrincipal( - const PrincipalInfo& aPrincipalInfo, nsresult* aOptionalResult = nullptr); +Result, nsresult> PrincipalInfoToPrincipal( + const PrincipalInfo& aPrincipalInfo); /** * Convert an nsIPrincipal to a PrincipalInfo. diff --git a/netwerk/base/nsNetUtil.cpp b/netwerk/base/nsNetUtil.cpp index 180f99c012c3..1d4085144021 100644 --- a/netwerk/base/nsNetUtil.cpp +++ b/netwerk/base/nsNetUtil.cpp @@ -304,19 +304,21 @@ void AssertLoadingPrincipalAndClientInfoMatch( } // Perform a fast comparison for most principal checks. - nsCOMPtr clientPrincipal(aLoadingClientInfo.GetPrincipal()); - if (aLoadingPrincipal->Equals(clientPrincipal)) { - return; + auto clientPrincipalOrErr(aLoadingClientInfo.GetPrincipal()); + if (clientPrincipalOrErr.isOk()) { + nsCOMPtr clientPrincipal = clientPrincipalOrErr.unwrap(); + if (aLoadingPrincipal->Equals(clientPrincipal)) { + return; + } + // Fall back to a slower origin equality test to support null principals. + nsAutoCString loadingOrigin; + MOZ_ALWAYS_SUCCEEDS(aLoadingPrincipal->GetOrigin(loadingOrigin)); + + nsAutoCString clientOrigin; + MOZ_ALWAYS_SUCCEEDS(clientPrincipal->GetOrigin(clientOrigin)); + + MOZ_DIAGNOSTIC_ASSERT(loadingOrigin == clientOrigin); } - - // Fall back to a slower origin equality test to support null principals. - nsAutoCString loadingOrigin; - MOZ_ALWAYS_SUCCEEDS(aLoadingPrincipal->GetOrigin(loadingOrigin)); - - nsAutoCString clientOrigin; - MOZ_ALWAYS_SUCCEEDS(clientPrincipal->GetOrigin(clientOrigin)); - - MOZ_DIAGNOSTIC_ASSERT(loadingOrigin == clientOrigin); #endif } diff --git a/netwerk/cookie/CookieJarSettings.cpp b/netwerk/cookie/CookieJarSettings.cpp index 157c27ed73da..21f8420f3593 100644 --- a/netwerk/cookie/CookieJarSettings.cpp +++ b/netwerk/cookie/CookieJarSettings.cpp @@ -16,6 +16,7 @@ #include "mozilla/StaticPrefs_network.h" #include "mozilla/Unused.h" #include "nsGlobalWindowInner.h" +#include "nsIPrincipal.h" #if defined(MOZ_THUNDERBIRD) || defined(MOZ_SUITE) # include "nsIProtocolHandler.h" #endif @@ -286,12 +287,13 @@ void CookieJarSettings::Serialize(CookieJarSettingsArgs& aData) { CookiePermissionList list; for (const CookiePermissionData& data : aData.cookiePermissions()) { - nsCOMPtr principal = - PrincipalInfoToPrincipal(data.principalInfo()); - if (NS_WARN_IF(!principal)) { + auto principalOrErr = PrincipalInfoToPrincipal(data.principalInfo()); + if (NS_WARN_IF(principalOrErr.isErr())) { continue; } + nsCOMPtr principal = principalOrErr.unwrap(); + nsCOMPtr permission = Permission::Create(principal, NS_LITERAL_CSTRING("cookie"), data.cookiePermission(), 0, 0, 0); @@ -347,12 +349,12 @@ void CookieJarSettings::Merge(const CookieJarSettingsArgs& aData) { PermissionComparator comparator; for (const CookiePermissionData& data : aData.cookiePermissions()) { - nsCOMPtr principal = - PrincipalInfoToPrincipal(data.principalInfo()); - if (NS_WARN_IF(!principal)) { + auto principalOrErr = PrincipalInfoToPrincipal(data.principalInfo()); + if (NS_WARN_IF(principalOrErr.isErr())) { continue; } + nsCOMPtr principal = principalOrErr.unwrap(); nsCOMPtr permission = Permission::Create(principal, NS_LITERAL_CSTRING("cookie"), data.cookiePermission(), 0, 0, 0); diff --git a/netwerk/ipc/DocumentLoadListener.cpp b/netwerk/ipc/DocumentLoadListener.cpp index da1cb4e0a98d..1a30a8d10fb6 100644 --- a/netwerk/ipc/DocumentLoadListener.cpp +++ b/netwerk/ipc/DocumentLoadListener.cpp @@ -1463,11 +1463,12 @@ DocumentLoadListener::RedirectToRealChannel( CreateAndReject(ipc::ResponseRejectReason::SendError, __func__); } - nsresult rv; - nsCOMPtr triggeringPrincipal = - PrincipalInfoToPrincipal(loadInfo.ref().triggeringPrincipalInfo(), &rv); + auto triggeringPrincipalOrErr = + PrincipalInfoToPrincipal(loadInfo.ref().triggeringPrincipalInfo()); - if (NS_SUCCEEDED(rv) && triggeringPrincipal) { + if (triggeringPrincipalOrErr.isOk()) { + nsCOMPtr triggeringPrincipal = + triggeringPrincipalOrErr.unwrap(); cp->TransmitBlobDataIfBlobURL(args.uri(), triggeringPrincipal); } diff --git a/netwerk/ipc/NeckoParent.cpp b/netwerk/ipc/NeckoParent.cpp index d12fec8670c1..bad4d94d5a2c 100644 --- a/netwerk/ipc/NeckoParent.cpp +++ b/netwerk/ipc/NeckoParent.cpp @@ -110,7 +110,8 @@ static already_AddRefed GetRequestingPrincipal( const PrincipalInfo& principalInfo = optionalPrincipalInfo.ref(); - return PrincipalInfoToPrincipal(principalInfo); + auto principalOrErr = PrincipalInfoToPrincipal(principalInfo); + return principalOrErr.isOk() ? principalOrErr.unwrap().forget() : nullptr; } static already_AddRefed GetRequestingPrincipal( diff --git a/netwerk/protocol/http/HttpChannelParent.cpp b/netwerk/protocol/http/HttpChannelParent.cpp index 891af3dca8fb..02b3be9698c2 100644 --- a/netwerk/protocol/http/HttpChannelParent.cpp +++ b/netwerk/protocol/http/HttpChannelParent.cpp @@ -1288,11 +1288,11 @@ mozilla::ipc::IPCResult HttpChannelParent::RecvRemoveCorsPreflightCacheEntry( if (!deserializedURI) { return IPC_FAIL_NO_REASON(this); } - nsCOMPtr principal = - PrincipalInfoToPrincipal(requestingPrincipal); - if (!principal) { + auto principalOrErr = PrincipalInfoToPrincipal(requestingPrincipal); + if (NS_WARN_IF(principalOrErr.isErr())) { return IPC_FAIL_NO_REASON(this); } + nsCOMPtr principal = principalOrErr.unwrap(); nsCORSListenerProxy::RemoveFromCorsPreflightCache(deserializedURI, principal); return IPC_OK(); } diff --git a/uriloader/prefetch/OfflineCacheUpdateParent.cpp b/uriloader/prefetch/OfflineCacheUpdateParent.cpp index 08111aba6e72..341461738537 100644 --- a/uriloader/prefetch/OfflineCacheUpdateParent.cpp +++ b/uriloader/prefetch/OfflineCacheUpdateParent.cpp @@ -12,6 +12,7 @@ #include "mozilla/ipc/URIUtils.h" #include "mozilla/Unused.h" #include "nsContentUtils.h" +#include "nsDebug.h" #include "nsOfflineCacheUpdate.h" #include "nsIApplicationCache.h" #include "nsNetUtil.h" @@ -75,15 +76,18 @@ nsresult OfflineCacheUpdateParent::Schedule( const CookieJarSettingsArgs& aCookieJarSettingsArgs) { LOG(("OfflineCacheUpdateParent::RecvSchedule [%p]", this)); - nsresult rv; - RefPtr update; if (!aManifestURI) { return NS_ERROR_FAILURE; } - mLoadingPrincipal = PrincipalInfoToPrincipal(aLoadingPrincipalInfo, &rv); - NS_ENSURE_SUCCESS(rv, rv); + auto loadingPrincipalOrErr = PrincipalInfoToPrincipal(aLoadingPrincipalInfo); + + if (NS_WARN_IF(loadingPrincipalOrErr.isErr())) { + return loadingPrincipalOrErr.unwrapErr(); + } + + mLoadingPrincipal = loadingPrincipalOrErr.unwrap(); nsOfflineCacheUpdateService* service = nsOfflineCacheUpdateService::EnsureService(); @@ -93,7 +97,8 @@ nsresult OfflineCacheUpdateParent::Schedule( bool offlinePermissionAllowed = false; - rv = service->OfflineAppAllowed(mLoadingPrincipal, &offlinePermissionAllowed); + nsresult rv = + service->OfflineAppAllowed(mLoadingPrincipal, &offlinePermissionAllowed); NS_ENSURE_SUCCESS(rv, rv); if (!offlinePermissionAllowed) {