aboutsummaryrefslogtreecommitdiff
path: root/background/policy_injector.js
blob: 90c65bd1dcd3b9f4e7ddd7da5e01789564a5c626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
 * Hachette injecting policy to page using webRequest
 *
 * Copyright (C) 2021 Wojtek Kosior
 * Copyright (C) 2021 jahoti
 * Redistribution terms are gathered in the `copyright' file.
 */

/*
 * IMPORTS_START
 * IMPORT TYPE_PREFIX
 * IMPORT get_storage
 * IMPORT browser
 * IMPORT is_chrome
 * IMPORT gen_unique
 * IMPORT gen_nonce
 * IMPORT is_privileged_url
 * IMPORT url_extract_target
 * IMPORT sign_policy
 * IMPORT get_query_best
 * IMPORT parse_csp
 * IMPORTS_END
 */

var storage;
var query_best;

const csp_header_names = {
    "content-security-policy" : true,
    "x-webkit-csp" : true,
    "x-content-security-policy" : true
};

const unwanted_csp_directives = {
    "report-to" : true,
    "report-uri" : true,
    "script-src" : true,
    "script-src-elem" : true,
    "prefetch-src": true
};

const header_name = "content-security-policy";

function not_csp_header(header)
{
    return !csp_header_names[header.name.toLowerCase()];
}

function url_inject(details)
{
    if (is_privileged_url(details.url))
	return;

    const targets = url_extract_target(details.url);
    if (targets.current)
	return;

    /* Redirect; update policy */
    if (targets.policy)
	targets.target = "";

    let [pattern, settings] = query_best(targets.base_url);
    /* Defaults */
    if (!pattern)
	settings = {};

    const policy = encodeURIComponent(
	JSON.stringify({
	    allow: settings.allow,
	    nonce: gen_nonce(),
	    base_url: targets.base_url
	})
    );

    return {
	redirectUrl: [
	    targets.base_url,
	    '#', sign_policy(policy, new Date()), policy,
	    targets.target,
	    targets.target2
	].join("")
    };
}

function headers_inject(details)
{
    const targets = url_extract_target(details.url);
    /* Block mis-/unsigned requests */
    if (!targets.current)
	return {cancel: true};

    const headers = [];
    for (let header of details.responseHeaders) {
	if (not_csp_header(header)) {
	    /* Retain all non-snitching headers */
	    if (header.name.toLowerCase() !==
	        'content-security-policy-report-only')
		headers.push(header);

	    continue;
	}

	const csp = parse_csp(header.value);
	const rule = `'nonce-${targets.policy.nonce}'`
	
	/* TODO: confirm deleting non-existent things is OK everywhere */
	/* No snitching or prefetching/rendering */
	delete csp['report-to'];
	delete csp['report-uri'];
	
	if (!targets.policy.allow) {
	    delete csp['script-src'];
	    delete csp['script-src-elem'];
	    csp['script-src-attr'] = ["'none'"];
	    csp['prefetch-src'] = ["'none'"];
	}
	
	if ('script-src' in csp)
	    csp['script-src'].push(rule);
	else
	    csp['script-src'] = [rule];

	if ('script-src-elem' in csp)
	    csp['script-src-elem'].push(rule);
	else
	    csp['script-src-elem'] = [rule];
	
	/* TODO: is this safe */
	let new_policy = Object.entries(csp).map(
	    i => i[0] + ' ' + i[1].join(' ') + ';'
	);
	
	headers.push({name: header.name, value: new_policy.join('')});
    }

    return {responseHeaders: headers};
}

async function start_policy_injector()
{
    storage = await get_storage();
    query_best = await get_query_best();

    let extra_opts = ["blocking", "responseHeaders"];
    if (is_chrome)
	extra_opts.push("extraHeaders");

    browser.webRequest.onBeforeRequest.addListener(
	url_inject,
	{
	    urls: ["<all_urls>"],
	    types: ["main_frame", "sub_frame"]
	},
	["blocking"]
    );

    browser.webRequest.onHeadersReceived.addListener(
	headers_inject,
	{
	    urls: ["<all_urls>"],
	    types: ["main_frame", "sub_frame"]
	},
	extra_opts
    );
}

/*
 * EXPORTS_START
 * EXPORT start_policy_injector
 * EXPORTS_END
 */