aboutsummaryrefslogtreecommitdiff
path: root/background
diff options
context:
space:
mode:
Diffstat (limited to 'background')
-rw-r--r--background/message_server.js37
-rw-r--r--background/page_info_server.js9
-rw-r--r--background/policy_injector.js65
3 files changed, 35 insertions, 76 deletions
diff --git a/background/message_server.js b/background/message_server.js
deleted file mode 100644
index ea40487..0000000
--- a/background/message_server.js
+++ /dev/null
@@ -1,37 +0,0 @@
-/**
- * Hachette message server
- *
- * Copyright (C) 2021 Wojtek Kosior
- * Redistribution terms are gathered in the `copyright' file.
- */
-
-/*
- * IMPORTS_START
- * IMPORT browser
- * IMPORTS_END
- */
-
-var listeners = {};
-
-/* magic should be one of the constants from /common/connection_types.js */
-
-function listen_for_connection(magic, cb)
-{
- listeners[magic] = cb;
-}
-
-function raw_listen(port)
-{
- if (listeners[port.name] === undefined)
- return;
-
- listeners[port.name](port);
-}
-
-browser.runtime.onConnect.addListener(raw_listen);
-
-/*
- * EXPORTS_START
- * EXPORT listen_for_connection
- * EXPORTS_END
- */
diff --git a/background/page_info_server.js b/background/page_info_server.js
index 49919fd..6f02750 100644
--- a/background/page_info_server.js
+++ b/background/page_info_server.js
@@ -40,11 +40,6 @@ async function handle_subscription(connection_data, message)
connection_data.port.postMessage(["new_url", query_all(url)]);
}
-function remove_storage_listener(cb)
-{
- storage.remove_change_listener(cb);
-}
-
function new_connection(port)
{
console.log("new page info connection!");
@@ -59,7 +54,9 @@ function new_connection(port)
storage.add_change_listener(_handle_change);
port.onMessage.addListener(m => handle_subscription(connection_data, m));
- port.onDisconnect.addListener(() => remove_storage_listener(handle_change));
+ port.onDisconnect.addListener(
+ () => storage.remove_change_listener(_handle_change)
+ );
}
async function start_page_info_server()
diff --git a/background/policy_injector.js b/background/policy_injector.js
index 8301c3f..01da094 100644
--- a/background/policy_injector.js
+++ b/background/policy_injector.js
@@ -15,8 +15,9 @@
* IMPORT is_mozilla
* IMPORT gen_unique
* IMPORT gen_nonce
+ * IMPORT is_privileged_url
* IMPORT url_item
- * IMPORT url_extract_policy
+ * IMPORT url_extract_target
* IMPORT sign_policy
* IMPORT get_query_best
* IMPORT csp_rule
@@ -39,27 +40,24 @@ function is_csp_header(header)
return !!csp_header_names[header.name.toLowerCase()];
}
-function is_our_header(header, rule)
-{
- return header.value === rule
-}
-
function url_inject(details)
{
- const targets = url_extract_policy(details.url);
- if (targets.current) {
+ if (is_privileged_url(details.url))
return;
- } else if (targets.policy) {
- /* Redirect; update policy */
- targets.target = targets.target2;
- delete targets.target2
- }
+
+ const targets = url_extract_target(details.url);
+ if (targets.current)
+ return;
+
+ /* Redirect; update policy */
+ if (targets.policy)
+ targets.target = "";
let [pattern, settings] = query_best(targets.base_url);
+ /* Defaults */
if (!pattern)
- /* Defaults */
settings = {};
-
+
const policy = encodeURIComponent(
JSON.stringify({
allow: settings.allow,
@@ -67,39 +65,40 @@ function url_inject(details)
base_url: targets.base_url
})
);
-
- let redirect_url = targets.base_url;
- redirect_url += '#' + sign_policy(policy, new Date()) + policy;
- if (targets.target)
- redirect_url += targets.target;
- if (targets.target2)
- redirect_url += targets.target2;
-
- return {redirectUrl: redirect_url};
+
+ return {
+ redirectUrl: [
+ targets.base_url,
+ '#', sign_policy(policy, new Date()), policy,
+ targets.target,
+ targets.target2
+ ].join("")
+ };
}
-function inject(details)
+function headers_inject(details)
{
- const targets = url_extract_policy(details.url);
+ const targets = url_extract_target(details.url);
+ /* Block mis-/unsigned requests */
if (!targets.current)
- /* Block mis-/unsigned requests */
return {cancel: true};
const rule = csp_rule(targets.policy.nonce);
var headers = details.responseHeaders;
+ /*
+ * Chrome doesn't have the buggy behavior of caching headers
+ * we injected. Firefox does and we have to remove it there.
+ */
if (!targets.policy.allow || is_mozilla)
- /*
- * Chrome doesn't have the buggy behavior of caching headers
- * we injected. Firefox does and we have to remove it there.
- */
headers = headers.filter(h => !is_csp_header(h));
- if (!targets.policy.allow)
+ if (!targets.policy.allow) {
headers.push({
name : header_name,
value : rule
});
+ }
return {responseHeaders: headers};
}
@@ -123,7 +122,7 @@ async function start_policy_injector()
);
browser.webRequest.onHeadersReceived.addListener(
- inject,
+ headers_inject,
{
urls: ["<all_urls>"],
types: ["main_frame", "sub_frame"]