Project

General

Profile

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

haketilo / background / webrequest.js @ 7f0b5ded

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Modify HTTP traffic usng webRequest API.
5
 *
6
 * Copyright (C) 2021, 2022 Wojtek Kosior <koszko@koszko.org>
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * As additional permission under GNU GPL version 3 section 7, you
19
 * may distribute forms of that code without the copy of the GNU
20
 * GPL normally required by section 4, provided you include this
21
 * license notice and, in case of non-source distribution, a URL
22
 * through which recipients can access the Corresponding Source.
23
 * If you modify file(s) with this exception, you may extend this
24
 * exception to your version of the file(s), but you are not
25
 * obligated to do so. If you do not wish to do so, delete this
26
 * exception statement from your version.
27
 *
28
 * As a special exception to the GPL, any HTML file which merely
29
 * makes function calls to this code, and for that purpose
30
 * includes it by reference shall be deemed a separate work for
31
 * copyright law purposes. If you modify this code, you may extend
32
 * this exception to your version of the code, but you are not
33
 * obligated to do so. If you do not wish to do so, delete this
34
 * exception statement from your version.
35
 *
36
 * You should have received a copy of the GNU General Public License
37
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
38
 *
39
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
40
 * license. Although I request that you do not make use of this code in a
41
 * proprietary program, I am not going to enforce this in court.
42
 */
43

    
44
#IMPORT common/indexeddb.js AS haketilodb
45

    
46
#IF MOZILLA
47
#IMPORT background/stream_filter.js
48
#ENDIF
49

    
50
#FROM common/browser.js IMPORT browser
51
#FROM common/misc.js    IMPORT is_privileged_url, csp_header_regex, \
52
                               sha256_async AS sha256
53
#FROM common/policy.js  IMPORT decide_policy
54

    
55
#FROM background/patterns_query_manager.js IMPORT tree, default_allow
56

    
57
let secret;
58

    
59
#IF MOZILLA
60
/*
61
 * Under Mozilla-based browsers, responses are cached together with headers as
62
 * they appear *after* modifications by Haketilo. This means Haketilo's CSP
63
 * script-blocking headers might be present in responses loaded from cache. In
64
 * the meantime the user might have changes Haketilo settings to instead allow
65
 * the scripts on the page in question. This causes a problem and creates the
66
 * need to somehow restore the response headers to the state in which they
67
 * arrived from the server.
68
 * To cope with this, Haketilo will inject some additional headers with private
69
 * data. Those will include a hard-to-guess value derived from extension's
70
 * internal ID. It is assumed the internal ID has a longer lifetime than cached
71
 * responses.
72
 */
73

    
74
const settings_page_url = browser.runtime.getURL("html/settings.html");
75
const header_prefix_prom = sha256(settings_page_url)
76
      .then(hash => `X-Haketilo-${hash}`);
77

    
78
/*
79
 * Mozilla, unlike Chrome, allows webRequest callbacks to return promises. Here
80
 * we leverage that to be able to use asynchronous sha256 computation.
81
 */
82
async function on_headers_received(details) {
83
#IF NEVER
84
} /* Help auto-indent in editors. */
85
#ENDIF
86
#ELSE
87
function on_headers_received(details) {
88
#ENDIF
89
    const url = details.url;
90
    if (is_privileged_url(details.url))
91
	return;
92

    
93
    let headers = details.responseHeaders;
94

    
95
#IF MOZILLA
96
    const prefix = await header_prefix_prom;
97

    
98
    /*
99
     * We assume that the original CSP headers of a response are always
100
     * preserved under names of the form:
101
     *     X-Haketilo-<some_secret>-<original_name>
102
     * In some cases the original response may contain no CSP headers. To still
103
     * be able to tell whether the headers we were provided were modified by
104
     * Haketilo in the past, all modifications are accompanied by addition of an
105
     * extra header with name:
106
     *     X-Haketilo-<some_secret>
107
     */
108

    
109
    const restore_old_headers = details.fromCache &&
110
	  !!headers.filter(h => h.name === prefix).length;
111

    
112
    if (restore_old_headers) {
113
	const restored_headers = [];
114

    
115
	for (const h of headers) {
116
	    if (csp_header_regex.test(h.name) || h.name === prefix)
117
		continue;
118

    
119
	    if (h.name.startsWith(prefix)) {
120
		restored_headers.push({
121
		    name:  h.name.substring(prefix.length + 1),
122
		    value: h.value
123
		});
124
	    } else {
125
		restored_headers.push(h);
126
	    }
127
	}
128

    
129
	headers = restored_headers;
130
    }
131
#ENDIF
132

    
133
    const policy =
134
	  decide_policy(tree, details.url, !!default_allow.value, secret);
135

    
136
    if (!policy.allow) {
137
#IF MOZILLA
138
	const to_append = [{name: prefix, value: ":)"}];
139

    
140
	for (const h of headers.filter(h => csp_header_regex.test(h.name))) {
141
	    if (!policy.payload)
142
		to_append.push(Object.assign({}, h));
143

    
144
	    h.name = `${prefix}-${h.name}`;
145
	}
146

    
147
	headers.push(...to_append);
148
#ELSE
149
	if (policy.payload)
150
	    headers = headers.filter(h => !csp_header_regex.test(h.name));
151
#ENDIF
152

    
153
	headers.push({name: "Content-Security-Policy", value: policy.csp});
154
    }
155

    
156
#IF MOZILLA
157
    /*
158
     * When page is meant to be viewed in the browser, use streamFilter to
159
     * inject a dummy <script> at the very beginning of it. This <script>
160
     * will cause extension's content scripts to run before page's first <meta>
161
     * tag is rendered so that they can prevent CSP rules inside <meta> tags
162
     * from blocking the payload we want to inject.
163
     */
164

    
165
    let use_stream_filter = !!policy.payload;
166
    if (use_stream_filter) {
167
	for (const header of headers) {
168
	    if (header.name.toLowerCase().trim() !== "content-disposition")
169
		continue;
170

    
171
	    if (/^\s*attachment\s*(;.*)$/i.test(header.value)) {
172
		use_stream_filter = false;
173
	    } else {
174
		use_stream_filter = true;
175
		break;
176
	    }
177
	}
178
    }
179
    use_stream_filter = use_stream_filter &&
180
	(details.statusCode < 300 || details.statusCode >= 400);
181

    
182
    if (use_stream_filter)
183
	headers = stream_filter.apply(details, headers, policy);
184
#ENDIF
185

    
186
    return {responseHeaders: headers};
187
}
188

    
189
#IF CHROMIUM && MV2
190
const request_url_regex = /^[^?]*\?url=(.*)$/;
191
const redirect_url_template = browser.runtime.getURL("dummy") + "?settings=";
192

    
193
function on_before_request(details)
194
{
195
    /*
196
     * Content script will make a synchronous XmlHttpRequest to extension's
197
     * `dummy` file to query settings for given URL. We smuggle that
198
     * information in query parameter of the URL we redirect to.
199
     * A risk of fingerprinting arises if a page with script execution allowed
200
     * guesses the dummy file URL and makes an AJAX call to it. It is currently
201
     * a problem in ManifestV2 Chromium-family port of Haketilo because Chromium
202
     * uses predictable URLs for web-accessible resources. We plan to fix it in
203
     * the future ManifestV3 port.
204
     */
205
    if (details.type !== "xmlhttprequest")
206
	return {cancel: true};
207

    
208
    if (details.url.startsWith(redirect_url_template))
209
	return;
210

    
211
#IF DEBUG
212
    console.debug(`Haketilo: Settings queried using XHR for '${details.url}'.`);
213
#ENDIF
214

    
215
    /*
216
     * request_url should be of the following format:
217
     *     <url_for_extension's_dummy_file>?url=<valid_urlencoded_url>
218
     */
219
    const match = request_url_regex.exec(details.url);
220
    if (match) {
221
	const queried_url = decodeURIComponent(match[1]);
222

    
223
	if (details.initiator && details.initiator !== "null" &&
224
	    !queried_url.startsWith(details.initiator)) {
225
	    console.warn(`Haketilo: Blocked suspicious query of '${queried_url}' by '${details.initiator}'. This might be the result of page fingerprinting the browser.`);
226
	    return {cancel: true};
227
	}
228

    
229
	const policy =
230
	      decide_policy(tree, queried_url, !!default_allow.value, secret);
231
	if (!policy.error) {
232
	    const encoded_policy = encodeURIComponent(JSON.stringify(policy));
233
	    return {redirectUrl: redirect_url_template + encoded_policy};
234
	}
235
    }
236

    
237
    console.warn(`Haketilo: Bad request! Expected ${browser.runtime.getURL("dummy")}?url=<valid_urlencoded_url>. Got ${details.url}. This might be the result of page fingerprinting the browser.`);
238

    
239
    return {cancel: true};
240
}
241

    
242
const all_types = [
243
    "main_frame", "sub_frame", "stylesheet", "script", "image", "font",
244
    "object", "xmlhttprequest", "ping", "csp_report", "media", "websocket",
245
    "other", "main_frame", "sub_frame"
246
];
247
#ENDIF
248

    
249
async function start(secret_)
250
{
251
    secret = secret_;
252

    
253
#IF CHROMIUM
254
    const extra_opts = ["blocking", "extraHeaders"];
255
#ELSE
256
    const extra_opts = ["blocking"];
257
#ENDIF
258

    
259
    browser.webRequest.onHeadersReceived.addListener(
260
	on_headers_received,
261
	{urls: ["<all_urls>"], types: ["main_frame", "sub_frame"]},
262
	extra_opts.concat("responseHeaders")
263
    );
264

    
265
#IF CHROMIUM && MV2
266
    browser.webRequest.onBeforeRequest.addListener(
267
	on_before_request,
268
	{urls: [browser.runtime.getURL("dummy") + "*"], types: all_types},
269
	extra_opts
270
    );
271
#ENDIF
272
}
273
#EXPORT start
(7-7/7)