aboutsummaryrefslogtreecommitdiff
path: root/background/cookie_filter.js
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2021-08-26 15:53:23 +0200
committerWojtek Kosior <koszko@koszko.org>2021-08-26 15:53:23 +0200
commit3303d7d70d4b9749c39ca87085d17495beab6030 (patch)
treee8f6b436771982a95d82a9eae214b50c6d3867f8 /background/cookie_filter.js
parent2875397fb887a5b09b5f39d6b3a75419a516dd07 (diff)
downloadbrowser-extension-3303d7d70d4b9749c39ca87085d17495beab6030.tar.gz
browser-extension-3303d7d70d4b9749c39ca87085d17495beab6030.zip
filter HTTP request headers to remove Hachette cookies in case they slip through
Diffstat (limited to 'background/cookie_filter.js')
-rw-r--r--background/cookie_filter.js45
1 files changed, 45 insertions, 0 deletions
diff --git a/background/cookie_filter.js b/background/cookie_filter.js
new file mode 100644
index 0000000..fea2d23
--- /dev/null
+++ b/background/cookie_filter.js
@@ -0,0 +1,45 @@
+/**
+ * part of Hachette
+ * Filtering request headers to remove hachette cookies that might have slipped
+ * through.
+ *
+ * Copyright (C) 2021 Wojtek Kosior
+ * Redistribution terms are gathered in the `copyright' file.
+ */
+
+/*
+ * IMPORTS_START
+ * IMPORT extract_signed
+ * IMPORTS_END
+ */
+
+function is_valid_hachette_cookie(cookie)
+{
+ const match = /^hachette-(\w*)=(.*)$/.exec(cookie);
+ if (!match)
+ return false;
+
+ return !extract_signed(match.slice(1, 3)).fail;
+}
+
+function remove_hachette_cookies(header)
+{
+ if (header.name !== "Cookie")
+ return header;
+
+ const cookies = header.value.split("; ");
+ const value = cookies.filter(c => !is_valid_hachette_cookie(c)).join("; ");
+
+ return value ? {name: "Cookie", value} : null;
+}
+
+function filter_cookie_headers(headers)
+{
+ return headers.map(remove_hachette_cookies).filter(h => h);
+}
+
+/*
+ * EXPORTS_START
+ * EXPORT filter_cookie_headers
+ * EXPORTS_END
+ */