Bug 791943. r=fabrice
authorMyk Melez <myk@mozilla.org>
Mon, 24 Sep 2012 22:41:21 -0400
changeset 108082 59877fe3f1e2c80cda0d016c322ebbd62b2a8dec
parent 108081 a8489b92ad020a2c4683fdeff4bbd268f35b39cf
child 108083 ec8e231a43747aeed06cb41898ed17bbd2925d40
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewersfabrice
bugs791943
milestone18.0a1
Bug 791943. r=fabrice
dom/apps/src/Webapps.js
dom/tests/mochitest/webapps/test_install_errors.xul
--- a/dom/apps/src/Webapps.js
+++ b/dom/apps/src/Webapps.js
@@ -75,19 +75,31 @@ WebappsRegistry.prototype = {
     this.removeRequest(msg.requestID);
   },
 
   _getOrigin: function(aURL) {
     let uri = Services.io.newURI(aURL, null, null);
     return uri.prePath;
   },
 
+  _validateScheme: function(aURL) {
+    let scheme = Services.io.newURI(aURL, null, null).scheme;
+    if (scheme != "http" && scheme != "https") {
+      throw new Components.Exception(
+        "INVALID_URL_SCHEME: '" + scheme + "'; must be 'http' or 'https'",
+        Cr.NS_ERROR_FAILURE
+      );
+    }
+  },
+
   // mozIDOMApplicationRegistry implementation
 
   install: function(aURL, aParams) {
+    this._validateScheme(aURL);
+
     let installURL = this._window.location.href;
     let installOrigin = this._getOrigin(installURL);
     let request = this.createRequest();
     let requestID = this.getRequestId(request);
     let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
     xhr.open("GET", aURL, true);
     xhr.channel.loadFlags |= Ci.nsIRequest.VALIDATE_ALWAYS;
 
@@ -157,16 +169,18 @@ WebappsRegistry.prototype = {
     this._mgmt = null;
     cpmm.sendAsyncMessage("Webapps:UnregisterForMessages",
                           ["Webapps:Install:Return:OK"]);
   },
 
   // mozIDOMApplicationRegistry2 implementation
 
   installPackage: function(aPackageURL, aParams) {
+    this._validateScheme(aPackageURL);
+
     let request = this.createRequest();
     let requestID = this.getRequestId(request);
 
     let receipts = (aParams && aParams.receipts &&
                     Array.isArray(aParams.receipts)) ? aParams.receipts : [];
     let categories = (aParams && aParams.categories &&
                       Array.isArray(aParams.categories)) ? aParams.categories : [];
     cpmm.sendAsyncMessage("Webapps:InstallPackage", { url: aPackageURL,
--- a/dom/tests/mochitest/webapps/test_install_errors.xul
+++ b/dom/tests/mochitest/webapps/test_install_errors.xul
@@ -23,16 +23,17 @@ var steps = [
   parseError,
   invalidManifest,
   permissionDenied,
   invalidContent,
   installPackageNotImplemented,
   invalidLaunchPath,
   invalidEntryPoint,
   invalidLocaleEntryPoint,
+  fileURL,
 ];
 
 runAll(steps);
 
 function noArgs(next) {
   try { 
     navigator.mozApps.install();
   } catch (e) {
@@ -131,10 +132,31 @@ function invalidLocaleEntryPoint(next) {
 }
 
 function installPackageNotImplemented(next) {
   ok(!("installPackage" in navigator.mozApps),
      "installPackage not in navigator.mozApps");
   next();
 }
 
+function fileURL(next) {
+  try {
+    navigator.mozApps.install("file:///nonexistent");
+    ok(false,
+       "attempt to install nonexistent file: URL doesn't throw exception");
+  } catch(ex) {
+    is(ex.message, "INVALID_URL_SCHEME: 'file'; must be 'http' or 'https'",
+       "attempt to install nonexistent file: URL throws exception");
+  }
+
+  try {
+    navigator.mozApps.install("file:///");
+    ok(false, "attempt to install existent file: URL doesn't throw exception");
+  } catch(ex) {
+    is(ex.message, "INVALID_URL_SCHEME: 'file'; must be 'http' or 'https'",
+       "attempt to install existent file: URL throws exception");
+  }
+
+  next();
+}
+
 </script>
 </window>