Bug 1382834 fix ws/wss protocol matching, r?kmag draft
authorShane Caraveo <scaraveo@mozilla.com>
Mon, 24 Jul 2017 17:00:29 -0700
changeset 614778 a158310d8699c8f340cfb8ab7f1923a2b4f558a1
parent 614544 462d7561089c98e33382384896434861ad7bc491
child 638959 aa392d0af6b5e72eac2a76cb9aac0d98416b69d1
push id70118
push usermixedpuppy@gmail.com
push dateTue, 25 Jul 2017 00:01:25 +0000
reviewerskmag
bugs1382834
milestone56.0a1
Bug 1382834 fix ws/wss protocol matching, r?kmag MozReview-Commit-ID: 6IKTPA1sJgh
toolkit/components/extensions/test/mochitest/test_ext_webrequest_websocket.html
toolkit/modules/addons/MatchPattern.jsm
toolkit/modules/addons/WebRequest.jsm
--- a/toolkit/components/extensions/test/mochitest/test_ext_webrequest_websocket.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_webrequest_websocket.html
@@ -21,17 +21,17 @@ add_task(async function test_webSocket()
         "webRequestBlocking",
         "<all_urls>",
       ],
     },
     background() {
       browser.webRequest.onBeforeRequest.addListener(details => {
         browser.test.assertEq("ws:", new URL(details.url).protocol, "ws protocol worked");
         browser.test.notifyPass("websocket");
-      }, {urls: ["*://mochi.test/*"]}, ["blocking"]);
+      }, {urls: ["ws://mochi.test/*"]}, ["blocking"]);
 
       browser.test.onMessage.addListener(msg => {
         let ws = new WebSocket("ws://mochi.test:8888/tests/dom/base/test/file_websocket_hello");
         ws.onopen = (e) => {
           ws.send("data");
         };
         ws.onclose = (e) => {};
         ws.onerror = (e) => {};
--- a/toolkit/modules/addons/MatchPattern.jsm
+++ b/toolkit/modules/addons/MatchPattern.jsm
@@ -13,17 +13,17 @@ XPCOMUtils.defineLazyModuleGetter(this, 
                                   "resource://gre/modules/NetUtil.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "Services",
                                   "resource://gre/modules/Services.jsm");
 
 this.EXPORTED_SYMBOLS = ["MatchPattern", "MatchGlobs", "MatchURLFilters"];
 
 /* globals MatchPattern, MatchGlobs */
 
-const PERMITTED_SCHEMES = ["http", "https", "file", "ftp", "data"];
+const PERMITTED_SCHEMES = ["http", "https", "ws", "wss", "file", "ftp", "data"];
 const PERMITTED_SCHEMES_REGEXP = [...PERMITTED_SCHEMES, "moz-extension"].join("|");
 
 // The basic RE for matching patterns
 const PATTERN_REGEXP = new RegExp(`^(${PERMITTED_SCHEMES_REGEXP}|\\*)://(\\*|\\*\\.[^*/]+|[^*/]+|)(/.*)$`);
 
 // The schemes/protocols implied by a pattern that starts with *://
 const WILDCARD_SCHEMES = ["http", "https"];
 
--- a/toolkit/modules/addons/WebRequest.jsm
+++ b/toolkit/modules/addons/WebRequest.jsm
@@ -676,18 +676,18 @@ HttpObserverManager = {
     } else if (lastActivity !== this.GOOD_LAST_ACTIVITY &&
                lastActivity !== nsIHttpActivityObserver.ACTIVITY_SUBTYPE_TRANSACTION_CLOSE) {
       channelData.lastActivity = activitySubtype;
     }
   },
 
   shouldRunListener(policyType, uri, filter) {
     // force the protocol to be ws again.
-    if (policyType == "websocket" && uri.startsWith("http")) {
-      uri = `ws${uri.substring(4)}`;
+    if (policyType == "websocket" && ["http", "https"].includes(uri.scheme)) {
+      uri = new Services.io.newURI(`ws${uri.spec.substring(4)}`);
     }
     return WebRequestCommon.typeMatches(policyType, filter.types) &&
            WebRequestCommon.urlMatches(uri, filter.urls);
   },
 
   get resultsMap() {
     delete this.resultsMap;
     this.resultsMap = new Map(Object.keys(Cr).map(name => [Cr[name], name]));