From 159ab546dbb85512f0649ca5a157ddd56dd11f9b Mon Sep 17 00:00:00 2001 From: Sebastian Streich Date: Tue, 19 May 2020 14:51:40 +0000 Subject: [PATCH] Bug 1625845 - Remove GetUri in Navigator.cpp r=ckerschb,baku Differential Revision: https://phabricator.services.mozilla.com/D68749 --- caps/BasePrincipal.cpp | 21 ++++++++++++++++++++- caps/BasePrincipal.h | 3 +++ caps/nsIPrincipal.idl | 10 +++++++++- dom/base/Navigator.cpp | 13 +++++-------- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/caps/BasePrincipal.cpp b/caps/BasePrincipal.cpp index 51451430c773..91c158845414 100644 --- a/caps/BasePrincipal.cpp +++ b/caps/BasePrincipal.cpp @@ -34,7 +34,8 @@ #include "prnetdb.h" #include "nsIURIFixup.h" #include "mozilla/dom/StorageUtils.h" - +#include "mozilla/ContentBlocking.h" +#include "nsPIDOMWindow.h" #include "nsIURIMutator.h" #include "json/json.h" @@ -689,6 +690,24 @@ BasePrincipal::GetPrefLightCacheKey(nsIURI* aURI, bool aWithCredentials, return NS_OK; } +NS_IMETHODIMP +BasePrincipal::HasFirstpartyStorageAccess(mozIDOMWindow* aCheckWindow, + uint32_t* aRejectedReason, + bool* aOutAllowed) { + *aRejectedReason = 0; + *aOutAllowed = false; + + nsPIDOMWindowInner* win = nsPIDOMWindowInner::From(aCheckWindow); + nsCOMPtr uri; + nsresult rv = GetURI(getter_AddRefs(uri)); + if (NS_FAILED(rv)) { + return rv; + } + *aOutAllowed = + ContentBlocking::ShouldAllowAccessFor(win, uri, aRejectedReason); + return NS_OK; +} + NS_IMETHODIMP BasePrincipal::GetIsNullPrincipal(bool* aResult) { *aResult = Kind() == eNullPrincipal; diff --git a/caps/BasePrincipal.h b/caps/BasePrincipal.h index c224a9037365..73b0ee7e73a1 100644 --- a/caps/BasePrincipal.h +++ b/caps/BasePrincipal.h @@ -152,6 +152,9 @@ class BasePrincipal : public nsJSPrincipals { bool* aRes) override; NS_IMETHOD GetPrefLightCacheKey(nsIURI* aURI, bool aWithCredentials, nsACString& _retval) override; + NS_IMETHOD HasFirstpartyStorageAccess(mozIDOMWindow* aCheckWindow, + uint32_t* aRejectedReason, + bool* aOutAllowed) override; NS_IMETHOD GetAsciiHost(nsACString& aAsciiHost) override; NS_IMETHOD GetLocalStorageQuotaKey(nsACString& aRes) override; NS_IMETHOD AllowsRelaxStrictFileOriginPolicy(nsIURI* aURI, diff --git a/caps/nsIPrincipal.idl b/caps/nsIPrincipal.idl index ca17e4b148ba..6a2be928a201 100644 --- a/caps/nsIPrincipal.idl +++ b/caps/nsIPrincipal.idl @@ -10,7 +10,7 @@ #include "nsIAboutModule.idl" #include "nsIReferrerInfo.idl" interface nsIChannel; - +#include "mozIDOMWindow.idl" %{C++ struct JSPrincipals; @@ -330,6 +330,14 @@ interface nsIPrincipal : nsISerializable ACString getPrefLightCacheKey(in nsIURI aURI ,in bool aWithCredentials); + /* + * Checks if the Principals URI has first party storage access + * when loaded inside the provided 3rd party resource window. + * See also: ContentBlocking::ShouldAllowAccessFor + */ + bool hasFirstpartyStorageAccess(in mozIDOMWindow aWindow, out uint32_t rejectedReason); + + /* * Returns a Key for the LocalStorage Manager, used to * check the Principals Origin Storage usage. diff --git a/dom/base/Navigator.cpp b/dom/base/Navigator.cpp index 855e07b2e733..774296cedc17 100644 --- a/dom/base/Navigator.cpp +++ b/dom/base/Navigator.cpp @@ -522,19 +522,16 @@ bool Navigator::CookieEnabled() { return cookieEnabled; } - nsCOMPtr contentURI; - BasePrincipal::Cast(doc->NodePrincipal())->GetURI(getter_AddRefs(contentURI)); - - if (!contentURI) { + uint32_t rejectedReason = 0; + bool granted = false; + nsresult rv = doc->NodePrincipal()->HasFirstpartyStorageAccess( + mWindow, &rejectedReason, &granted); + if (NS_FAILED(rv)) { // Not a content, so technically can't set cookies, but let's // just return the default value. return cookieEnabled; } - uint32_t rejectedReason = 0; - bool granted = ContentBlocking::ShouldAllowAccessFor(mWindow, contentURI, - &rejectedReason); - ContentBlockingNotifier::OnDecision( mWindow, granted ? ContentBlockingNotifier::BlockingDecision::eAllow