From 12fd4fc3a01eb9718a60c8d04860c4e797049b26 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Wed, 30 Jun 2021 16:39:53 +0200 Subject: fix whitelisting under Firefox --- background/policy_injector.js | 45 ++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'background') diff --git a/background/policy_injector.js b/background/policy_injector.js index 4f70aac..eb67963 100644 --- a/background/policy_injector.js +++ b/background/policy_injector.js @@ -21,33 +21,52 @@ var storage; var query_best; -let csp_header_names = { +const csp_header_names = { "content-security-policy" : true, "x-webkit-csp" : true, "x-content-security-policy" : true }; -function is_noncsp_header(header) +const header_name = "content-security-policy"; + +function is_csp_header(header) +{ + return !!csp_header_names[header.name.toLowerCase()]; +} + +function is_our_header(header, rule) { - return !csp_header_names[header.name.toLowerCase()]; + return header.value === rule } function inject(details) { - let url = url_item(details.url); + const url = url_item(details.url); + + const [pattern, settings] = query_best(url); + + const nonce = gen_unique(url); + const rule = csp_rule(nonce); - let [pattern, settings] = query_best(url); + var headers; - if (settings !== undefined && settings.allow) - return {cancel : false}; + if (settings !== undefined && settings.allow) { + /* + * Chrome doesn't have the buggy behavior of repeatedly injecting a + * header we injected once. Firefox does and we have to remove it there. + */ + if (is_chrome) + return {cancel: false}; - let nonce = gen_unique(url); - let headers = details.responseHeaders.filter(is_noncsp_header); + headers = details.responseHeaders.filter(h => !is_our_header(h, rule)); + } else { + headers = details.responseHeaders.filter(h => !is_csp_header(h)); - headers.push({ - name : "content-security-policy", - value : csp_rule(nonce) - }); + headers.push({ + name : header_name, + value : rule + }); + } return {responseHeaders: headers}; } -- cgit v1.2.3