From 0fa804f9b2b3e5d3a832834072a4d693311d16de Mon Sep 17 00:00:00 2001 From: Pasu Chan Chak Shing Date: Sun, 11 Feb 2018 17:31:20 +0800 Subject: [PATCH 1/5] Add upgrade-insecure-requests to responseHeaders in HTTP Nowhere Mode, Fix #8506 --- chromium/background.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/chromium/background.js b/chromium/background.js index 7b77731f6a65..f597f6793441 100644 --- a/chromium/background.js +++ b/chromium/background.js @@ -497,11 +497,25 @@ function onErrorOccurred(details) { } } +/** + * handle webrequest.onHeadersReceived, insert upgrade-insecure-requests directive + * @param details details for the chrome.webRequest (see chrome doc) + */ +function onHeadersReceived(details) { + if (httpNowhereOn) { + const upgradeInsecureRequests = { + name: 'Content-Security-Policy', + value: 'upgrade-insecure-requests' + } + details.responseHeaders.push(upgradeInsecureRequests); + } + return {responseHeaders: details.responseHeaders}; +} + // Registers the handler for requests // See: https://github.com/EFForg/https-everywhere/issues/10039 chrome.webRequest.onBeforeRequest.addListener(onBeforeRequest, {urls: ["*://*/*"]}, ["blocking"]); - // Try to catch redirect loops on URLs we've redirected to HTTPS. chrome.webRequest.onBeforeRedirect.addListener(onBeforeRedirect, {urls: ["https://*/*"]}); @@ -511,9 +525,13 @@ chrome.webRequest.onCompleted.addListener(onCompleted, {urls: ["*://*/*"]}); // Cleanup redirectCounter if neccessary chrome.webRequest.onErrorOccurred.addListener(onErrorOccurred, {urls: ["*://*/*"]}) +// Insert upgrade-insecure-requests directive in httpNowhere mode +chrome.webRequest.onHeadersReceived.addListener(onHeadersReceived, {urls: ["https://*/*"]}, ["blocking", "responseHeaders"]); + // Listen for cookies set/updated and secure them if applicable. This function is async/nonblocking. chrome.cookies.onChanged.addListener(onCookieChanged); + /** * disable switch Planner * @param tabId the Tab to disable for From 0d1f114009296e1c9bca4443f9cdff93f76284f6 Mon Sep 17 00:00:00 2001 From: Pasu Chan Chak Shing Date: Sun, 11 Feb 2018 17:47:35 +0800 Subject: [PATCH 2/5] When extension is disabled, do nothing --- chromium/background.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chromium/background.js b/chromium/background.js index f597f6793441..da585b7753ec 100644 --- a/chromium/background.js +++ b/chromium/background.js @@ -502,7 +502,7 @@ function onErrorOccurred(details) { * @param details details for the chrome.webRequest (see chrome doc) */ function onHeadersReceived(details) { - if (httpNowhereOn) { + if (isExtensionEnabled && httpNowhereOn) { const upgradeInsecureRequests = { name: 'Content-Security-Policy', value: 'upgrade-insecure-requests' From 1e06bf18c5c33093648071f91d87d94cbbb95b82 Mon Sep 17 00:00:00 2001 From: Pasu Chan Chak Shing Date: Tue, 13 Feb 2018 15:26:30 +0800 Subject: [PATCH 3/5] If CSP already existed, modify it insteads --- chromium/background.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/chromium/background.js b/chromium/background.js index da585b7753ec..77dc2f858b2a 100644 --- a/chromium/background.js +++ b/chromium/background.js @@ -503,6 +503,20 @@ function onErrorOccurred(details) { */ function onHeadersReceived(details) { if (isExtensionEnabled && httpNowhereOn) { + for (const idx in details.responseHeaders) { + if (details.responseHeaders[idx].name.match(/Content-Security-Policy/i)) { + // Existing CSP headers found + const value = details.responseHeaders[idx].value; + + // Prepend if no upgrade-insecure-requests directive exists + if (!value.match(/upgrade-insecure-requests/i)) { + details.responseHeaders[idx].value = "upgrade-insecure-requests; " + value; + } + return {responseHeaders: details.responseHeaders}; + } + } + + // CSP headers not found const upgradeInsecureRequests = { name: 'Content-Security-Policy', value: 'upgrade-insecure-requests' From e4b17e8f7ec99136597b28907176029540aee021 Mon Sep 17 00:00:00 2001 From: Pasu Chan Chak Shing Date: Wed, 14 Feb 2018 12:52:48 +0800 Subject: [PATCH 4/5] Exclude .onion requests from being upgraded --- chromium/background.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/chromium/background.js b/chromium/background.js index 77dc2f858b2a..b41e15d1dc7c 100644 --- a/chromium/background.js +++ b/chromium/background.js @@ -503,6 +503,13 @@ function onErrorOccurred(details) { */ function onHeadersReceived(details) { if (isExtensionEnabled && httpNowhereOn) { + // Do not upgrade the .onion requests in HTTP Nowhere Mode, + // See https://github.com/EFForg/https-everywhere/pull/14600#discussion_r168072480 + const uri = new URL(details.url); + if (uri.hostname.slice(-6) == '.onion') { + return {responseHeaders: details.responseHeaders}; + } + for (const idx in details.responseHeaders) { if (details.responseHeaders[idx].name.match(/Content-Security-Policy/i)) { // Existing CSP headers found From 9541530521d37f52565fd77a585f89419879733f Mon Sep 17 00:00:00 2001 From: Pasu Chan Chak Shing Date: Wed, 14 Feb 2018 15:28:57 +0800 Subject: [PATCH 5/5] Return responseHeaders only if it is modified --- chromium/background.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/chromium/background.js b/chromium/background.js index b41e15d1dc7c..077332343f58 100644 --- a/chromium/background.js +++ b/chromium/background.js @@ -507,7 +507,7 @@ function onHeadersReceived(details) { // See https://github.com/EFForg/https-everywhere/pull/14600#discussion_r168072480 const uri = new URL(details.url); if (uri.hostname.slice(-6) == '.onion') { - return {responseHeaders: details.responseHeaders}; + return {}; } for (const idx in details.responseHeaders) { @@ -518,8 +518,9 @@ function onHeadersReceived(details) { // Prepend if no upgrade-insecure-requests directive exists if (!value.match(/upgrade-insecure-requests/i)) { details.responseHeaders[idx].value = "upgrade-insecure-requests; " + value; + return {responseHeaders: details.responseHeaders}; } - return {responseHeaders: details.responseHeaders}; + return {}; } } @@ -529,8 +530,9 @@ function onHeadersReceived(details) { value: 'upgrade-insecure-requests' } details.responseHeaders.push(upgradeInsecureRequests); + return {responseHeaders: details.responseHeaders}; } - return {responseHeaders: details.responseHeaders}; + return {}; } // Registers the handler for requests