Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions chromium/background-scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -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};
Expand Down
45 changes: 45 additions & 0 deletions chromium/background-scripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}