Revision 2bd35bc4
Added by koszko almost 2 years ago
background/policy_injector.js | ||
---|---|---|
1 | 1 |
/** |
2 |
* Hachette injecting policy to page using webRequest |
|
2 |
* This file is part of Haketilo. |
|
3 |
* |
|
4 |
* Function: Injecting policy to page by modifying HTTP headers. |
|
3 | 5 |
* |
4 | 6 |
* Copyright (C) 2021 Wojtek Kosior |
5 | 7 |
* Copyright (C) 2021 jahoti |
... | ... | |
19 | 21 |
{ |
20 | 22 |
let csp_headers; |
21 | 23 |
let old_signature; |
22 |
let hachette_header;
|
|
24 |
let haketilo_header;
|
|
23 | 25 |
|
24 |
for (const header of headers.filter(h => h.name === "x-hachette")) {
|
|
25 |
/* x-hachette header has format: <signature>_0_<data> */
|
|
26 |
for (const header of headers.filter(h => h.name === "x-haketilo")) {
|
|
27 |
/* x-haketilo header has format: <signature>_0_<data> */
|
|
26 | 28 |
const match = /^([^_]+)_(0_.*)$/.exec(header.value); |
27 | 29 |
if (!match) |
28 | 30 |
continue; |
... | ... | |
38 | 40 |
csp_headers = old_data.csp_headers; |
39 | 41 |
old_signature = old_data.policy_sig; |
40 | 42 |
|
41 |
hachette_header = header;
|
|
43 |
haketilo_header = header;
|
|
42 | 44 |
break; |
43 | 45 |
} |
44 | 46 |
|
... | ... | |
53 | 55 |
headers.push(...csp_headers || []); |
54 | 56 |
} |
55 | 57 |
|
56 |
if (!hachette_header) {
|
|
57 |
hachette_header = {name: "x-hachette"};
|
|
58 |
headers.push(hachette_header);
|
|
58 |
if (!haketilo_header) {
|
|
59 |
haketilo_header = {name: "x-haketilo"};
|
|
60 |
headers.push(haketilo_header);
|
|
59 | 61 |
} |
60 | 62 |
|
61 | 63 |
if (old_signature) |
... | ... | |
66 | 68 |
const later_30sec = new Date(new Date().getTime() + 30000).toGMTString(); |
67 | 69 |
headers.push({ |
68 | 70 |
name: "Set-Cookie", |
69 |
value: `hachette-${signed_policy.join("=")}; Expires=${later_30sec};`
|
|
71 |
value: `haketilo-${signed_policy.join("=")}; Expires=${later_30sec};`
|
|
70 | 72 |
}); |
71 | 73 |
|
72 | 74 |
/* |
... | ... | |
74 | 76 |
* These are signed with a time of 0, as it's not clear there is a limit on |
75 | 77 |
* how long Firefox might retain headers in the cache. |
76 | 78 |
*/ |
77 |
let hachette_data = {csp_headers, policy_sig: signed_policy[0]};
|
|
78 |
hachette_data = encodeURIComponent(JSON.stringify(hachette_data));
|
|
79 |
hachette_header.value = sign_data(hachette_data, 0).join("_");
|
|
79 |
let haketilo_data = {csp_headers, policy_sig: signed_policy[0]};
|
|
80 |
haketilo_data = encodeURIComponent(JSON.stringify(haketilo_data));
|
|
81 |
haketilo_header.value = sign_data(haketilo_data, 0).join("_");
|
|
80 | 82 |
|
81 | 83 |
if (!policy.allow) { |
82 | 84 |
headers.push({ |
Also available in: Unified diff
rename the extension to "Haketilo"