Project

General

Profile

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

haketilo / background / policy_injector.js @ 2bd35bc4

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Injecting policy to page by modifying HTTP headers.
5
 *
6
 * Copyright (C) 2021 Wojtek Kosior
7
 * Copyright (C) 2021 jahoti
8
 * Redistribution terms are gathered in the `copyright' file.
9
 */
10

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

    
20
function inject_csp_headers(headers, policy)
21
{
22
    let csp_headers;
23
    let old_signature;
24
    let haketilo_header;
25

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

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

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

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

    
43
	haketilo_header = header;
44
	break;
45
    }
46

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

    
58
    if (!haketilo_header) {
59
	haketilo_header = {name: "x-haketilo"};
60
	headers.push(haketilo_header);
61
    }
62

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

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

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

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

    
90
    return headers;
91
}
92

    
93
/*
94
 * EXPORTS_START
95
 * EXPORT inject_csp_headers
96
 * EXPORTS_END
97
 */
(4-4/7)