Project

General

Profile

Download (2.48 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / background / policy_injector.js @ 44e89d8e

1
/**
2
 * Hachette injecting policy to page using webRequest
3
 *
4
 * Copyright (C) 2021 Wojtek Kosior
5
 * Copyright (C) 2021 jahoti
6
 * Redistribution terms are gathered in the `copyright' file.
7
 */
8

    
9
/*
10
 * IMPORTS_START
11
 * IMPORT sign_data
12
 * IMPORT extract_signed
13
 * IMPORT make_csp_rule
14
 * IMPORT csp_header_regex
15
 * IMPORTS_END
16
 */
17

    
18
function inject_csp_headers(headers, policy)
19
{
20
    let csp_headers;
21
    let old_signature;
22
    let hachette_header;
23

    
24
    for (const header of headers.filter(h => h.name === "x-hachette")) {
25
	/* x-hachette header has format: <signature>_0_<data> */
26
	const match = /^([^_]+)_(0_.*)$/.exec(header.value);
27
	if (!match)
28
	    continue;
29

    
30
	const result = extract_signed(...match.slice(1, 3));
31
	if (result.fail)
32
	    continue;
33

    
34
	/* This should succeed - it's our self-produced valid JSON. */
35
	const old_data = JSON.parse(decodeURIComponent(result.data));
36

    
37
	/* Confirmed- it's the originals, smuggled in! */
38
	csp_headers = old_data.csp_headers;
39
	old_signature = old_data.policy_sig;
40

    
41
	hachette_header = header;
42
	break;
43
    }
44

    
45
    if (policy.has_payload) {
46
	csp_headers = [];
47
	const non_csp_headers = [];
48
	const header_list =
49
	      h => csp_header_regex.test(h) ? csp_headers : non_csp_headers;
50
	headers.forEach(h => header_list(h.name).push(h));
51
	headers = non_csp_headers;
52
    } else {
53
	headers.push(...csp_headers || []);
54
    }
55

    
56
    if (!hachette_header) {
57
	hachette_header = {name: "x-hachette"};
58
	headers.push(hachette_header);
59
    }
60

    
61
    if (old_signature)
62
	headers = headers.filter(h => h.value.search(old_signature) === -1);
63

    
64
    const policy_str = encodeURIComponent(JSON.stringify(policy));
65
    const signed_policy = sign_data(policy_str, new Date().getTime());
66
    const later_30sec = new Date(new Date().getTime() + 30000).toGMTString();
67
    headers.push({
68
	name: "Set-Cookie",
69
	value: `hachette-${signed_policy.join("=")}; Expires=${later_30sec};`
70
    });
71

    
72
    /*
73
     * Smuggle in the signature and the original CSP headers for future use.
74
     * These are signed with a time of 0, as it's not clear there is a limit on
75
     * how long Firefox might retain headers in the cache.
76
     */
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("_");
80

    
81
    if (!policy.allow) {
82
	headers.push({
83
	    name: "content-security-policy",
84
	    value: make_csp_rule(policy)
85
	});
86
    }
87

    
88
    return headers;
89
}
90

    
91
/*
92
 * EXPORTS_START
93
 * EXPORT inject_csp_headers
94
 * EXPORTS_END
95
 */
(4-4/7)