Bug 1543911 - Move BasePrincipal::CloneStrippingUserContextIdAndFirst into nsPermission to avoid misuses, r=johannh
authorAndrea Marchesini <amarchesini@mozilla.com>
Mon, 15 Apr 2019 10:26:09 +0000
changeset 469493 b22a06518604
parent 469492 b66a064574bc
child 469494 b4a7ea516329
push id35873
push userccoroiu@mozilla.com
push dateMon, 15 Apr 2019 21:36:26 +0000
treeherdermozilla-central@b8f49a14c458 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjohannh
bugs1543911
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1543911 - Move BasePrincipal::CloneStrippingUserContextIdAndFirst into nsPermission to avoid misuses, r=johannh Differential Revision: https://phabricator.services.mozilla.com/D27235
caps/BasePrincipal.cpp
caps/BasePrincipal.h
dom/permission/PermissionStatus.cpp
dom/permission/moz.build
extensions/cookie/nsPermission.cpp
extensions/cookie/nsPermission.h
--- a/caps/BasePrincipal.cpp
+++ b/caps/BasePrincipal.cpp
@@ -433,33 +433,16 @@ already_AddRefed<BasePrincipal> BasePrin
 
   nsCOMPtr<nsIURI> uri;
   nsresult rv = NS_NewURI(getter_AddRefs(uri), originNoSuffix);
   NS_ENSURE_SUCCESS(rv, nullptr);
 
   return BasePrincipal::CreateCodebasePrincipal(uri, attrs);
 }
 
-already_AddRefed<BasePrincipal>
-BasePrincipal::CloneStrippingUserContextIdAndFirstPartyDomain() {
-  OriginAttributes attrs = OriginAttributesRef();
-  attrs.StripAttributes(OriginAttributes::STRIP_USER_CONTEXT_ID |
-                        OriginAttributes::STRIP_FIRST_PARTY_DOMAIN);
-
-  nsAutoCString originNoSuffix;
-  nsresult rv = GetOriginNoSuffix(originNoSuffix);
-  NS_ENSURE_SUCCESS(rv, nullptr);
-
-  nsCOMPtr<nsIURI> uri;
-  rv = NS_NewURI(getter_AddRefs(uri), originNoSuffix);
-  NS_ENSURE_SUCCESS(rv, nullptr);
-
-  return BasePrincipal::CreateCodebasePrincipal(uri, attrs);
-}
-
 already_AddRefed<BasePrincipal> BasePrincipal::CloneForcingFirstPartyDomain(
     nsIURI* aURI) {
   if (NS_WARN_IF(!IsCodebasePrincipal())) {
     return nullptr;
   }
 
   OriginAttributes attrs = OriginAttributesRef();
   // XXX this is slow. Maybe we should consider to make it faster.
--- a/caps/BasePrincipal.h
+++ b/caps/BasePrincipal.h
@@ -161,19 +161,16 @@ class BasePrincipal : public nsJSPrincip
     return mOriginAttributes.mPrivateBrowsingId;
   }
   bool IsInIsolatedMozBrowserElement() const {
     return mOriginAttributes.mInIsolatedMozBrowser;
   }
 
   PrincipalKind Kind() const { return mKind; }
 
-  already_AddRefed<BasePrincipal>
-  CloneStrippingUserContextIdAndFirstPartyDomain();
-
   already_AddRefed<BasePrincipal> CloneForcingFirstPartyDomain(nsIURI* aURI);
 
   already_AddRefed<BasePrincipal> CloneForcingOriginAttributes(
       const OriginAttributes& aOriginAttributes);
 
   // If this is an add-on content script principal, returns its AddonPolicy.
   // Otherwise returns null.
   extensions::WebExtensionPolicy* ContentScriptAddonPolicy();
--- a/dom/permission/PermissionStatus.cpp
+++ b/dom/permission/PermissionStatus.cpp
@@ -6,16 +6,17 @@
 
 #include "mozilla/dom/PermissionStatus.h"
 
 #include "mozilla/AsyncEventDispatcher.h"
 #include "mozilla/Services.h"
 #include "nsIPermissionManager.h"
 #include "PermissionObserver.h"
 #include "PermissionUtils.h"
+#include "nsPermission.h"
 
 namespace mozilla {
 namespace dom {
 
 /* static */
 already_AddRefed<PermissionStatus> PermissionStatus::Create(
     nsPIDOMWindowInner* aWindow, PermissionName aName, ErrorResult& aRv) {
   RefPtr<PermissionStatus> status = new PermissionStatus(aWindow, aName);
@@ -91,18 +92,17 @@ already_AddRefed<nsIPrincipal> Permissio
   }
 
   Document* doc = window->GetExtantDoc();
   if (NS_WARN_IF(!doc)) {
     return nullptr;
   }
 
   nsCOMPtr<nsIPrincipal> principal =
-      mozilla::BasePrincipal::Cast(doc->NodePrincipal())
-          ->CloneStrippingUserContextIdAndFirstPartyDomain();
+      nsPermission::ClonePrincipalForPermission(doc->NodePrincipal());
   NS_ENSURE_TRUE(principal, nullptr);
 
   return principal.forget();
 }
 
 void PermissionStatus::PermissionChanged() {
   auto oldState = mState;
   UpdateState();
--- a/dom/permission/moz.build
+++ b/dom/permission/moz.build
@@ -14,13 +14,17 @@ EXPORTS.mozilla.dom += [
 
 UNIFIED_SOURCES += [
     'PermissionObserver.cpp',
     'Permissions.cpp',
     'PermissionStatus.cpp',
     'PermissionUtils.cpp',
 ]
 
+LOCAL_INCLUDES += [
+    '/extensions/cookie',
+]
+
 MOCHITEST_MANIFESTS += ['tests/mochitest.ini']
 
 FINAL_LIBRARY = 'xul'
 
 include('/ipc/chromium/chromium-config.mozbuild')
--- a/extensions/cookie/nsPermission.cpp
+++ b/extensions/cookie/nsPermission.cpp
@@ -18,26 +18,44 @@ nsPermission::nsPermission(nsIPrincipal*
                            uint32_t aCapability, uint32_t aExpireType,
                            int64_t aExpireTime)
     : mPrincipal(aPrincipal),
       mType(aType),
       mCapability(aCapability),
       mExpireType(aExpireType),
       mExpireTime(aExpireTime) {}
 
+already_AddRefed<nsIPrincipal> nsPermission::ClonePrincipalForPermission(
+    nsIPrincipal* aPrincipal) {
+  MOZ_ASSERT(aPrincipal);
+
+  mozilla::OriginAttributes attrs = aPrincipal->OriginAttributesRef();
+  attrs.StripAttributes(mozilla::OriginAttributes::STRIP_USER_CONTEXT_ID |
+                        mozilla::OriginAttributes::STRIP_FIRST_PARTY_DOMAIN);
+
+  nsAutoCString originNoSuffix;
+  nsresult rv = aPrincipal->GetOriginNoSuffix(originNoSuffix);
+  NS_ENSURE_SUCCESS(rv, nullptr);
+
+  nsCOMPtr<nsIURI> uri;
+  rv = NS_NewURI(getter_AddRefs(uri), originNoSuffix);
+  NS_ENSURE_SUCCESS(rv, nullptr);
+
+  return mozilla::BasePrincipal::CreateCodebasePrincipal(uri, attrs);
+}
+
 already_AddRefed<nsPermission> nsPermission::Create(nsIPrincipal* aPrincipal,
                                                     const nsACString& aType,
                                                     uint32_t aCapability,
                                                     uint32_t aExpireType,
                                                     int64_t aExpireTime) {
   NS_ENSURE_TRUE(aPrincipal, nullptr);
+
   nsCOMPtr<nsIPrincipal> principal =
-      mozilla::BasePrincipal::Cast(aPrincipal)
-          ->CloneStrippingUserContextIdAndFirstPartyDomain();
-
+      nsPermission::ClonePrincipalForPermission(aPrincipal);
   NS_ENSURE_TRUE(principal, nullptr);
 
   RefPtr<nsPermission> permission =
       new nsPermission(principal, aType, aCapability, aExpireType, aExpireTime);
   return permission.forget();
 }
 
 NS_IMETHODIMP
@@ -75,19 +93,17 @@ NS_IMETHODIMP
 nsPermission::Matches(nsIPrincipal* aPrincipal, bool aExactHost,
                       bool* aMatches) {
   NS_ENSURE_ARG_POINTER(aPrincipal);
   NS_ENSURE_ARG_POINTER(aMatches);
 
   *aMatches = false;
 
   nsCOMPtr<nsIPrincipal> principal =
-      mozilla::BasePrincipal::Cast(aPrincipal)
-          ->CloneStrippingUserContextIdAndFirstPartyDomain();
-
+      nsPermission::ClonePrincipalForPermission(aPrincipal);
   if (!principal) {
     *aMatches = false;
     return NS_OK;
   }
 
   // If the principals are equal, then they match.
   if (mPrincipal->Equals(principal)) {
     *aMatches = true;
--- a/extensions/cookie/nsPermission.h
+++ b/extensions/cookie/nsPermission.h
@@ -18,16 +18,22 @@ class nsPermission : public nsIPermissio
   NS_DECL_NSIPERMISSION
 
   static already_AddRefed<nsPermission> Create(nsIPrincipal* aPrincipal,
                                                const nsACString& aType,
                                                uint32_t aCapability,
                                                uint32_t aExpireType,
                                                int64_t aExpireTime);
 
+  // This method creates a new nsIPrincipal with a stripped OriginAttributes (no
+  // userContextId, and no FirstPartyDomain) and a codebase equal to the origin
+  // of 'aPrincipal'.
+  static already_AddRefed<nsIPrincipal> ClonePrincipalForPermission(
+      nsIPrincipal* aPrincipal);
+
  protected:
   nsPermission(nsIPrincipal* aPrincipal, const nsACString& aType,
                uint32_t aCapability, uint32_t aExpireType, int64_t aExpireTime);
 
   virtual ~nsPermission(){};
 
   nsCOMPtr<nsIPrincipal> mPrincipal;
   nsCString mType;