From fab9adeccb22953ebc51781533a23e6974344e4d Mon Sep 17 00:00:00 2001 From: Bennett Cyphers Date: Fri, 6 Apr 2018 17:43:28 -0700 Subject: [PATCH] Use upgradeToSecure when possible Use Firefox's new upgradeToSecure flag instead of standard URI rewrites when applying simple rulesets. Should fix issue with CORS blocking certain HTTPS rewrites (#49). --- chromium/background-scripts/background.js | 23 +++++++++--- chromium/background-scripts/util.js | 45 +++++++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/chromium/background-scripts/background.js b/chromium/background-scripts/background.js index cbe1161e4b96..9d19dfb1c017 100644 --- a/chromium/background-scripts/background.js +++ b/chromium/background-scripts/background.js @@ -306,6 +306,8 @@ function onBeforeRequest(details) { return {cancel: shouldCancel}; } + // if the request is for the main (top-level) frame of a tab, remove the + // ruleset for that tab if (details.type == "main_frame") { appliedRulesets.removeTab(details.tabId); } @@ -320,12 +322,21 @@ function onBeforeRequest(details) { return {cancel: shouldCancel}; } + // whether to use mozilla's upgradeToSecure BlockingResponse if available + var upgradeToSecure = false; var newuristr = null; + // check rewritten URIs against the trivially upgraded URI + var trivialUpgradeUri = canonical_url.replace(rules.trivial_rule_from_c, + rules.trivial_rule_to); for (let ruleset of potentiallyApplicable) { appliedRulesets.addRulesetToTab(details.tabId, details.type, ruleset); if (ruleset.active && !newuristr) { newuristr = ruleset.apply(canonical_url); + // for now, only upgradeToSecure for trivial rulesets + if (newuristr == trivialUpgradeUri) { + upgradeToSecure = true; + } } } @@ -358,19 +369,21 @@ function onBeforeRequest(details) { // If loading a main frame, try the HTTPS version as an alternative to // failing. if (shouldCancel) { + upgradeToSecure = true; if (!newuristr) { - return {redirectUrl: canonical_url.replace(/^http:/, "https:")}; + newuristr = canonical_url.replace(/^http:/, "https:"); } else { - return {redirectUrl: newuristr.replace(/^http:/, "https:")}; + newuristr = newuristr.replace(/^http:/, "https:"); } - } - if (newuristr && newuristr.substring(0, 5) === "http:") { + } else if (newuristr && newuristr.substring(0, 5) === "http:") { // Abort early if we're about to redirect to HTTP in HTTP Nowhere mode return {cancel: true}; } } - if (newuristr) { + if (upgradeToSecure && getBrowserInfo().name == 'Firefox' && getBrowserInfo().version >= 59) { + return {upgradeToSecure: true}; + } else if (newuristr) { return {redirectUrl: newuristr}; } else { return {cancel: shouldCancel}; diff --git a/chromium/background-scripts/util.js b/chromium/background-scripts/util.js index 9d7dfc98bde3..001cb0edd08a 100644 --- a/chromium/background-scripts/util.js +++ b/chromium/background-scripts/util.js @@ -106,3 +106,48 @@ Object.assign(exports, { }); })(typeof exports == 'undefined' ? require.scopes.util = {} : exports); + +// from +// https://www.gregoryvarghese.com/how-to-get-browser-name-and-version-via-javascript/ +function getBrowserInfo() { + var agent = navigator.userAgent, + match = agent.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i); + + if (!match) { + return { + name: null, + version: null + }; + } + + var name = match[1], + version = match[2]; + + if (/trident/i.test(name)) { + var ieMatch = /\brv[ :]+(\d+)/g.exec(agent) || []; + return { + name: 'IE ', + version: (ieMatch[1] || '') + }; + } + + if (name === 'Chrome') { + var opMatch = agent.match(/\bOPR\/(\d+)/); + if (tem != null) { + return { + name: 'Opera', + version: match[1] + }; + } + } + + var vMatch = agent.match(/version\/(\d+)/i); + if (vMatch != null) { + version = vMatch[1]; + } + + return { + name: name, + version: version + }; +}