Bug 1308688 - Expose mozAddonManager allowed hosts to chrome. r=bkelly, a=gchang
authorAndrew Swan <aswan@mozilla.com>
Thu, 01 Dec 2016 10:14:52 -0800
changeset 356845 7f9a7d1ed122ca49ae05e4e4ee42ab51418407b5
parent 356844 8b86ace0637ae48f3e507a650a32c9b4e5f8d624
child 356846 df6600c5e97daa02d4eeff2758da2ed325a51d85
push id6649
push userryanvm@gmail.com
push dateThu, 01 Dec 2016 19:21:39 +0000
treeherdermozilla-beta@df6600c5e97d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly, gchang
bugs1308688
milestone51.0
Bug 1308688 - Expose mozAddonManager allowed hosts to chrome. r=bkelly, a=gchang MozReview-Commit-ID: Igw1YsSP2Fv
dom/bindings/Bindings.conf
dom/webidl/AddonManager.webidl
toolkit/mozapps/extensions/AddonManagerWebAPI.cpp
toolkit/mozapps/extensions/AddonManagerWebAPI.h
--- a/dom/bindings/Bindings.conf
+++ b/dom/bindings/Bindings.conf
@@ -48,16 +48,21 @@
 # descriptor to use when generating that interface's binding.
 
 DOMInterfaces = {
 
 'AbstractWorker': {
     'concrete': False
 },
 
+'AddonManagerPermissions': {
+    'wrapperCache': False,
+    'concrete': False
+},
+
 'AnimationEffectReadOnly': {
     'concrete': False
 },
 
 'AnimationTimeline': {
     'concrete': False
 },
 
--- a/dom/webidl/AddonManager.webidl
+++ b/dom/webidl/AddonManager.webidl
@@ -78,8 +78,14 @@ interface AddonManager : EventTarget {
   Promise<AddonInstall> createInstall(optional addonInstallOptions options);
 
   /* Hooks for managing event listeners */
   [ChromeOnly]
   void eventListenerWasAdded(DOMString type);
   [ChromeOnly]
   void eventListenerWasRemoved(DOMString type);
 };
+
+[ChromeOnly,Exposed=System,HeaderFile="mozilla/AddonManagerWebAPI.h"]
+interface AddonManagerPermissions {
+  static boolean isHostPermitted(DOMString host);
+};
+
--- a/toolkit/mozapps/extensions/AddonManagerWebAPI.cpp
+++ b/toolkit/mozapps/extensions/AddonManagerWebAPI.cpp
@@ -13,57 +13,62 @@
 #include "nsGlobalWindow.h"
 
 #include "nsIDocShell.h"
 #include "nsIScriptObjectPrincipal.h"
 
 namespace mozilla {
 using namespace mozilla::dom;
 
+static bool
+IsValidHost(const nsACString& host) {
+  if (host.Equals("addons.mozilla.org") ||
+      host.Equals("discovery.addons.mozilla.org") ||
+      host.Equals("testpilot.firefox.com")) {
+    return true;
+  }
+
+  // When testing allow access to the developer sites.
+  if (Preferences::GetBool("extensions.webapi.testing", false)) {
+    if (host.LowerCaseEqualsLiteral("addons.allizom.org") ||
+        host.LowerCaseEqualsLiteral("discovery.addons.allizom.org") ||
+        host.LowerCaseEqualsLiteral("addons-dev.allizom.org") ||
+        host.LowerCaseEqualsLiteral("discovery.addons-dev.allizom.org") ||
+        host.LowerCaseEqualsLiteral("testpilot.stage.mozaws.net") ||
+        host.LowerCaseEqualsLiteral("testpilot.dev.mozaws.net") ||
+        host.LowerCaseEqualsLiteral("example.com")) {
+      return true;
+    }
+  }
+
+  return false;
+}
+
 // Checks if the given uri is secure and matches one of the hosts allowed to
 // access the API.
 bool
 AddonManagerWebAPI::IsValidSite(nsIURI* uri)
 {
   if (!uri) {
     return false;
   }
 
   bool isSecure;
   nsresult rv = uri->SchemeIs("https", &isSecure);
   if (NS_FAILED(rv) || !isSecure) {
     return false;
   }
 
-  nsCString host;
+  nsAutoCString host;
   rv = uri->GetHost(host);
   if (NS_FAILED(rv)) {
     return false;
   }
 
-  if (host.Equals("addons.mozilla.org") ||
-      host.Equals("discovery.addons.mozilla.org") ||
-      host.Equals("testpilot.firefox.com")) {
-    return true;
-  }
-
-  // When testing allow access to the developer sites.
-  if (Preferences::GetBool("extensions.webapi.testing", false)) {
-    if (host.Equals("addons.allizom.org") ||
-        host.Equals("discovery.addons.allizom.org") ||
-        host.Equals("addons-dev.allizom.org") ||
-        host.Equals("discovery.addons-dev.allizom.org") ||
-        host.Equals("testpilot.stage.mozaws.net") ||
-        host.Equals("testpilot.dev.mozaws.net") ||
-        host.Equals("example.com")) {
-      return true;
-    }
-  }
-
-  return false;
+  return IsValidHost(host);
 }
 
 bool
 AddonManagerWebAPI::IsAPIEnabled(JSContext* cx, JSObject* obj)
 {
   nsGlobalWindow* global = xpc::WindowGlobalOrNull(obj);
   if (!global) {
     return false;
@@ -131,9 +136,20 @@ AddonManagerWebAPI::IsAPIEnabled(JSConte
 
     win = doc->GetInnerWindow();
   }
 
   // Found a document with no inner window, don't grant access to the API.
   return false;
 }
 
+namespace dom {
+
+bool
+AddonManagerPermissions::IsHostPermitted(const GlobalObject& /*unused*/, const nsAString& host)
+{
+  return IsValidHost(NS_ConvertUTF16toUTF8(host));
+}
+
+} // namespace mozilla::dom
+
+
 } // namespace mozilla
--- a/toolkit/mozapps/extensions/AddonManagerWebAPI.h
+++ b/toolkit/mozapps/extensions/AddonManagerWebAPI.h
@@ -14,11 +14,20 @@ namespace mozilla {
 class AddonManagerWebAPI {
 public:
   static bool IsAPIEnabled(JSContext* cx, JSObject* obj);
 
 private:
   static bool IsValidSite(nsIURI* uri);
 };
 
+namespace dom {
+
+class AddonManagerPermissions {
+public:
+  static bool IsHostPermitted(const GlobalObject&, const nsAString& host);
+};
+
+} // namespace mozilla::dom
+
 } // namespace mozilla
 
 #endif // addonmanagerwebapi_h_