Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.
Merged
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
43 changes: 40 additions & 3 deletions chromium/background-scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async function initialize() {
await store.initialize();
await store.performMigrations();
await initializeStoredGlobals();
await getUpgradeToSecureAvailable();
await update.initialize(store, initializeAllRules);
await all_rules.loadFromBrowserStorage(store, update.applyStoredRulesets);
await incognito.onIncognitoDestruction(destroy_caches);
Expand Down Expand Up @@ -59,6 +60,26 @@ function initializeStoredGlobals(){
});
}

let upgradeToSecureAvailable;

function getUpgradeToSecureAvailable() {
if (typeof browser !== 'undefined') {
return browser.runtime.getBrowserInfo().then(function(info) {
let version = info.version.match(/^(\d+)/)[1];
if (info.name == "Firefox" && version >= 59) {
upgradeToSecureAvailable = true;
} else {
upgradeToSecureAvailable = false;
}
});
} else {
return new Promise(resolve => {
upgradeToSecureAvailable = false;
resolve();
});
}
}

chrome.storage.onChanged.addListener(async function(changes, areaName) {
if (areaName === 'sync' || areaName === 'local') {
if ('httpNowhere' in changes) {
Expand Down Expand Up @@ -276,6 +297,8 @@ function onBeforeRequest(details) {
}

// Should the request be canceled?
// true if the URL is a http:// connection to a remote canonical host, and not
// a tor hidden service
const shouldCancel = httpNowhereOn &&
uri.protocol === 'http:' &&
uri.hostname.slice(-6) !== '.onion' &&
Expand Down Expand Up @@ -320,12 +343,20 @@ function onBeforeRequest(details) {
return {cancel: shouldCancel};
}

// whether to use mozilla's upgradeToSecure BlockingResponse if available
let upgradeToSecure = false;
var newuristr = null;
// check rewritten URIs against the trivially upgraded URI
let trivialUpgradeUri = canonical_url.replace(/^http:/, "https:");

for (let ruleset of potentiallyApplicable) {
appliedRulesets.addRulesetToTab(details.tabId, details.type, ruleset);
if (ruleset.active && !newuristr) {
newuristr = ruleset.apply(canonical_url);
// only use upgradeToSecure for trivial rulesets
if (newuristr == trivialUpgradeUri) {
upgradeToSecure = true;
}
}
}

Expand Down Expand Up @@ -358,10 +389,11 @@ 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:") {
Expand All @@ -370,9 +402,14 @@ function onBeforeRequest(details) {
}
}

if (newuristr) {
if (upgradeToSecureAvailable && upgradeToSecure) {
util.log(util.INFO, 'onBeforeRequest returning upgradeToSecure: true');
return {upgradeToSecure: true};
} else if (newuristr) {
util.log(util.INFO, 'onBeforeRequest returning redirectUrl: ' + newuristr);
return {redirectUrl: newuristr};
} else {
util.log(util.INFO, 'onBeforeRequest returning shouldCancel: ' + shouldCancel);
return {cancel: shouldCancel};
}
}
Expand Down