Project

General

Profile

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

haketilo / background / policy_injector.js @ 5dab077b

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 sanitize_csp_header
14
 * IMPORT make_csp_rule
15
 * IMPORT is_csp_header_name
16
 * IMPORTS_END
17
 */
18

    
19
function inject_csp_headers(headers, policy)
20
{
21
    if (!policy.allow || policy.has_payload) {
22
	/* Remove report-only CSP headers that snitch on us. */
23
	headers = headers.filter(h => !is_csp_header_name(h.name, true));
24

    
25
	/* Add our own CSP header */
26
	headers.push({
27
	    name: "content-security-policy",
28
	    value: make_csp_rule(policy)
29
	});
30
    }
31
    
32
    const policy_str = encodeURIComponent(JSON.stringify(policy));
33
    const signed_policy = sign_data(policy_str, new Date().getTime());
34
    const later_30sec = new Date(new Date().getTime() + 30000).toGMTString();
35
    headers.push({
36
	name: "Set-Cookie",
37
	value: `hachette-${signed_policy.join("=")}; Expires=${later_30sec};`
38
    });
39

    
40
    return headers;
41
}
42

    
43
/*
44
 * EXPORTS_START
45
 * EXPORT inject_csp_headers
46
 * EXPORTS_END
47
 */
(4-4/7)