[Pkg-mozext-commits] [requestpolicy] 199/257: [imp] old rules: detect if wildcard is needed

David Prévot taffit at moszumanska.debian.org
Thu Jan 28 03:20:13 UTC 2016


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository requestpolicy.

commit 717eecd0c981a00b023904ed8c005c15299223c8
Author: Martin Kimmerle <dev at 256k.de>
Date:   Sun Dec 6 12:29:33 2015 +0100

    [imp] old rules: detect if wildcard is needed
    
    Automatically detect if a wildcard should be added in front of a
    host specification when importing old rules.
    
    Resolves #730.
---
 src/content/lib/old-rules.jsm                     | 68 ++++++++++++++++++-----
 src/content/settings/oldrules.html                |  7 ---
 src/content/settings/oldrules.js                  | 27 ++++-----
 src/content/settings/setup.js                     |  3 +-
 tests/marionette/rp_ui_harness/test_data/rules.py | 16 +++---
 tests/xpcshell/test_oldrules.js                   | 35 +++++++-----
 6 files changed, 94 insertions(+), 62 deletions(-)

diff --git a/src/content/lib/old-rules.jsm b/src/content/lib/old-rules.jsm
index 76d6e56..68c556f 100644
--- a/src/content/lib/old-rules.jsm
+++ b/src/content/lib/old-rules.jsm
@@ -22,11 +22,13 @@
  */
 
 /* global Components */
-const {interfaces: Ci, utils: Cu} = Components;
+const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
 
 /* exported OldRules, OldRulesParseError */
 this.EXPORTED_SYMBOLS = ["OldRules", "OldRulesParseError"];
 
+let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
+
 let {ScriptLoader: {importModule}} = Cu.import(
     "chrome://rpcontinued/content/lib/script-loader.jsm", {});
 let {DomainUtil} = importModule("lib/utils/domains");
@@ -97,19 +99,19 @@ var OldRules = (function() {
   /**
    * Convert the pref strings to rule objects.
    */
-  OldRules.prototype.getAsNewRules = function(addHostWildcard) {
+  OldRules.prototype.getAsNewRules = function() {
     var rules = [];
     var {origins, dests, originsToDests} = this.prefStringSets;
 
     for (let origin of origins) {
       rules.push({
-        o: OldRules.getEndpointSpecFromString(origin, addHostWildcard)
+        o: OldRules.getEndpointSpecFromString(origin)
       });
     }
 
     for (let dest of dests) {
       rules.push({
-        d: OldRules.getEndpointSpecFromString(dest, addHostWildcard)
+        d: OldRules.getEndpointSpecFromString(dest)
       });
     }
 
@@ -120,8 +122,8 @@ var OldRules = (function() {
         let [origin, dest] = parts;
         if (origin !== "" && dest !== "") {
           rules.push({
-            o: OldRules.getEndpointSpecFromString(origin, addHostWildcard),
-            d: OldRules.getEndpointSpecFromString(dest, addHostWildcard)
+            o: OldRules.getEndpointSpecFromString(origin),
+            d: OldRules.getEndpointSpecFromString(dest)
           });
           continue;
         }
@@ -135,28 +137,64 @@ var OldRules = (function() {
 
   /**
    * @static
+   * @param  {string|nsIURI} aEndpoint
+   * @return {boolean}
+   */
+  OldRules.shouldWildcardBeAddedToEndpoint = function(aEndpoint) {
+    if (!OldRules._IDNService) {
+      OldRules._IDNService = Cc["@mozilla.org/network/idn-service;1"].
+          getService(Ci.nsIIDNService);
+    }
+    let host;
+    let getBaseDomain;
+    if (aEndpoint instanceof Ci.nsIURI) {
+      let uri = aEndpoint;
+      host = uri.host;
+      getBaseDomain = () => Services.eTLD.getBaseDomain(uri, 0);
+    } else {
+      host = aEndpoint;
+      getBaseDomain = () => Services.eTLD.getBaseDomainFromHost(host, 0);
+    }
+
+    try {
+      let baseDomain = getBaseDomain();
+      baseDomain = OldRules._IDNService.convertToDisplayIDN(baseDomain, {});
+      return host === baseDomain;
+    } catch (e) {
+      if (e.name === "NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS") {
+        return false;
+      } else if (e.name === "NS_ERROR_HOST_IS_IP_ADDRESS") {
+        return false;
+      } else {
+        throw e;
+      }
+    }
+  };
+
+  /**
+   * @static
    * @param {string} aEndpointString
-   * @param {boolean} aAddHostWildcard
    * @return {Object} The endpoints' specifications.
    */
-  OldRules.getEndpointSpecFromString = function(aEndpointString,
-                                                 aAddHostWildcard) {
+  OldRules.getEndpointSpecFromString = function(aEndpointString) {
     var spec = {};
     if (DomainUtil.isValidUri(aEndpointString)) {
       let uriObj = DomainUtil.getUriObject(aEndpointString);
       spec.s = uriObj.scheme;
       if (DomainUtil.uriObjHasHost(uriObj)) {
         spec.h = uriObj.host;
+        if (OldRules.shouldWildcardBeAddedToEndpoint(uriObj)) {
+          spec.h = "*." + spec.h;
+        }
         if (uriObj.port !== -1) {
           spec.port = uriObj.port;
         }
       }
     } else {
       spec.h = aEndpointString.split("/")[0];
-    }
-    // FIXME: Issue #731;  Detect if the host is a Base Domain.
-    if (spec.h && aAddHostWildcard && OldRules._isHostname(spec.h)) {
-      spec.h = "*." + spec.h;
+      if (OldRules.shouldWildcardBeAddedToEndpoint(spec.h)) {
+        spec.h = "*." + spec.h;
+      }
     }
     return spec;
   };
@@ -186,12 +224,12 @@ var OldRules = (function() {
 }());
 
 //==============================================================================
-// ParseError
+// OldRulesParseError
 //==============================================================================
 
 function OldRulesParseError() {
   Error.apply(this, arguments);
-  this.name = 'OldRulesParseError';
+  this.name = "OldRulesParseError";
 }
 
 OldRulesParseError.prototype = Object.create(Error.prototype);
diff --git a/src/content/settings/oldrules.html b/src/content/settings/oldrules.html
index 60e0d77..227a13b 100644
--- a/src/content/settings/oldrules.html
+++ b/src/content/settings/oldrules.html
@@ -107,13 +107,6 @@
               </div>
             </div>
 
-            <div id="importoptions">
-              <label>
-                <input type="checkbox" id="addhostwildcards" checked="checked"/>
-                Add wildcards to origin and destination hostnames
-              </label>
-            </div>
-
             <div id="doimport">
               <button onclick="importOldRules()"
                   data-string="importOldRules"></button>
diff --git a/src/content/settings/oldrules.js b/src/content/settings/oldrules.js
index b8facdd..c49f409 100644
--- a/src/content/settings/oldrules.js
+++ b/src/content/settings/oldrules.js
@@ -27,23 +27,23 @@
   });
 
   var rules = null;
-  var addHostWildcard = true;
 
-  function clearRulesTable() {
-    var table = $id("rules");
-    var children = table.getElementsByTagName("tr");
-    while (children.length) {
-      var child = children.item(0);
-      child.parentNode.removeChild(child);
-    }
-  }
+  // currently unused
+  // function clearRulesTable() {
+  //   var table = $id("rules");
+  //   var children = table.getElementsByTagName("tr");
+  //   while (children.length) {
+  //     var child = children.item(0);
+  //     child.parentNode.removeChild(child);
+  //   }
+  // }
 
   function populateRuleTable() {
     var table = $id("rules");
 
     var oldRules = new OldRules();
     // Setting the global rules var here.
-    rules = oldRules.getAsNewRules(addHostWildcard);
+    rules = oldRules.getAsNewRules();
 
     for (var i = 0; i < rules.length; i++) {
       var entry = rules[i];
@@ -93,12 +93,6 @@
     $("#importdone").show();
   };
 
-  function handleAddHostWildcardsChange(event) {
-    addHostWildcard = event.target.checked;
-    clearRulesTable();
-    populateRuleTable();
-  }
-
   window.onload = function() {
     var oldRulesExist = Prefs.oldRulesExist();
     if (!oldRulesExist) {
@@ -107,7 +101,6 @@
       return;
     }
     populateRuleTable();
-    $("#addhostwildcards").change(handleAddHostWildcardsChange);
   };
 
 }());
diff --git a/src/content/settings/setup.js b/src/content/settings/setup.js
index e2cbf5e..198b528 100644
--- a/src/content/settings/setup.js
+++ b/src/content/settings/setup.js
@@ -155,9 +155,8 @@
       Logger.dump("Rule count: " + ruleCount);
       if (ruleCount <= 0) {
         Logger.dump("Performing rule import.");
-        var addHostWildcard = identLevel === 1;
         var oldRules = new OldRules();
-        var rules = oldRules.getAsNewRules(addHostWildcard);
+        var rules = oldRules.getAsNewRules();
         PolicyManager.addAllowRules(rules);
       }
 
diff --git a/tests/marionette/rp_ui_harness/test_data/rules.py b/tests/marionette/rp_ui_harness/test_data/rules.py
index 1bf193c..323d15f 100644
--- a/tests/marionette/rp_ui_harness/test_data/rules.py
+++ b/tests/marionette/rp_ui_harness/test_data/rules.py
@@ -153,19 +153,19 @@ class ExemplaryOldRules(ExemplaryRules_Meta):
             },
             "expected": [
                 # origin-to-destination rules
-                self._rule({"o": {"s": "https", "h": "*.www.mozilla.org"},
-                            "d": {"s": "https", "h": "*.mozorg.cdn.mozilla.net"}}),
-                self._rule({"o": {"h": "*.www.mozilla.org"},
-                            "d": {"h": "*.mozorg.cdn.mozilla.net"}}),
+                self._rule({"o": {"s": "https", "h": "www.mozilla.org"},
+                            "d": {"s": "https", "h": "mozorg.cdn.mozilla.net"}}),
+                self._rule({"o": {"h": "www.mozilla.org"},
+                            "d": {"h": "mozorg.cdn.mozilla.net"}}),
                 self._rule({"o": {"h": "*.mozilla.org"},
                             "d": {"h": "*.mozilla.net"}}),
                 # origin rules
-                self._rule({"o": {"s": "https", "h": "*.www.mozilla.org"}}),
-                self._rule({"o": {"h": "*.www.mozilla.org"}}),
+                self._rule({"o": {"s": "https", "h": "www.mozilla.org"}}),
+                self._rule({"o": {"h": "www.mozilla.org"}}),
                 self._rule({"o": {"h": "*.mozilla.org"}}),
                 # destination rules
-                self._rule({"d": {"s": "https", "h": "*.mozorg.cdn.mozilla.net"}}),
-                self._rule({"d": {"h": "*.mozorg.cdn.mozilla.net"}}),
+                self._rule({"d": {"s": "https", "h": "mozorg.cdn.mozilla.net"}}),
+                self._rule({"d": {"h": "mozorg.cdn.mozilla.net"}}),
                 self._rule({"d": {"h": "*.mozilla.net"}})
             ]
         }
diff --git a/tests/xpcshell/test_oldrules.js b/tests/xpcshell/test_oldrules.js
index ea30ea0..af57c7e 100644
--- a/tests/xpcshell/test_oldrules.js
+++ b/tests/xpcshell/test_oldrules.js
@@ -49,20 +49,20 @@ function test_0() {
         "mozilla.org|mozilla.net"
       ],
       [
-        {o: {s: "https", h: "*.www.mozilla.org"}},
-        {o: {h: "*.www.mozilla.org"}},
+        {o: {s: "https", h: "www.mozilla.org"}},
+        {o: {h: "www.mozilla.org"}},
         {o: {h: "*.mozilla.org"}},
 
-        {d: {s: "https", h: "*.mozorg.cdn.mozilla.net"}},
-        {d: {h: "*.mozorg.cdn.mozilla.net"}},
+        {d: {s: "https", h: "mozorg.cdn.mozilla.net"}},
+        {d: {h: "mozorg.cdn.mozilla.net"}},
         {d: {h: "*.mozilla.net"}},
 
         {
-          o: {s: "https", h: "*.www.mozilla.org"},
-          d: {s: "https", h: "*.mozorg.cdn.mozilla.net"}
+          o: {s: "https", h: "www.mozilla.org"},
+          d: {s: "https", h: "mozorg.cdn.mozilla.net"}
         }, {
-          o: {h: "*.www.mozilla.org"},
-          d: {h: "*.mozorg.cdn.mozilla.net"}
+          o: {h: "www.mozilla.org"},
+          d: {h: "mozorg.cdn.mozilla.net"}
         }, {
           o: {h: "*.mozilla.org"},
           d: {h: "*.mozilla.net"}
@@ -149,10 +149,19 @@ function test_2() {
         " e|f  g|h "
       ],
       [
-        {o: {h: "*.a"}}, {o: {h: "*.b"}},
-        {d: {h: "*.c"}}, {d: {h: "*.d"}},
-        {o: {h: "*.e"}, d: {h: "*.f"}},
-        {o: {h: "*.g"}, d: {h: "*.h"}}
+        {o: {h: "a"}}, {o: {h: "b"}},
+        {d: {h: "c"}}, {d: {h: "d"}},
+        {o: {h: "e"}, d: {h: "f"}},
+        {o: {h: "g"}, d: {h: "h"}}
+      ]);
+
+  // UTF8 domain names
+
+  testGetOldRulesAsNewRules(
+      ["müller.de http://foo.bar.الاردن", "", ""],
+      [
+        {o: {h: "*.müller.de"}},
+        {o: {s: "http", h: "foo.bar.الاردن"}}
       ]);
 }
 
@@ -195,7 +204,7 @@ function testGetOldRulesAsNewRules(
   "use strict";
 
   var oldRules = new OldRules(origins, destinations, originsToDestinations);
-  var actualRuleSpecs = oldRules.getAsNewRules(true);
+  var actualRuleSpecs = oldRules.getAsNewRules();
   assertRuleSpecsEqual(actualRuleSpecs, expectedRuleSpecs);
 }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/requestpolicy.git



More information about the Pkg-mozext-commits mailing list