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
|
headers = headers.filter(h => !csp_header_regex.test(h.name));
|
150
|
#ENDIF
|
151
|
|
152
|
headers.push({name: "Content-Security-Policy", value: policy.csp});
|
153
|
}
|
154
|
|
155
|
#IF MOZILLA
|
156
|
/*
|
157
|
* When page is meant to be viewed in the browser, use streamFilter to
|
158
|
* inject a dummy <script> at the very beginning of it. This <script>
|
159
|
* will cause extension's content scripts to run before page's first <meta>
|
160
|
* tag is rendered so that they can prevent CSP rules inside <meta> tags
|
161
|
* from blocking the payload we want to inject.
|
162
|
*/
|
163
|
|
164
|
let use_stream_filter = !!policy.payload;
|
165
|
if (use_stream_filter) {
|
166
|
for (const header of headers) {
|
167
|
if (header.name.toLowerCase().trim() !== "content-disposition")
|
168
|
continue;
|
169
|
|
170
|
if (/^\s*attachment\s*(;.*)$/i.test(header.value)) {
|
171
|
use_stream_filter = false;
|
172
|
} else {
|
173
|
use_stream_filter = true;
|
174
|
break;
|
175
|
}
|
176
|
}
|
177
|
}
|
178
|
use_stream_filter = use_stream_filter &&
|
179
|
(details.statusCode < 300 || details.statusCode >= 400);
|
180
|
|
181
|
if (use_stream_filter)
|
182
|
headers = stream_filter.apply(details, headers, policy);
|
183
|
#ENDIF
|
184
|
|
185
|
return {responseHeaders: headers};
|
186
|
}
|
187
|
|
188
|
#IF CHROMIUM && MV2
|
189
|
const request_url_regex = /^[^?]*\?url=(.*)$/;
|
190
|
const redirect_url_template = browser.runtime.getURL("dummy") + "?settings=";
|
191
|
|
192
|
function on_before_request(details)
|
193
|
{
|
194
|
/*
|
195
|
* Content script will make a synchronous XmlHttpRequest to extension's
|
196
|
* `dummy` file to query settings for given URL. We smuggle that
|
197
|
* information in query parameter of the URL we redirect to.
|
198
|
* A risk of fingerprinting arises if a page with script execution allowed
|
199
|
* guesses the dummy file URL and makes an AJAX call to it. It is currently
|
200
|
* a problem in ManifestV2 Chromium-family port of Haketilo because Chromium
|
201
|
* uses predictable URLs for web-accessible resources. We plan to fix it in
|
202
|
* the future ManifestV3 port.
|
203
|
*/
|
204
|
if (details.type !== "xmlhttprequest")
|
205
|
return {cancel: true};
|
206
|
|
207
|
if (details.url.startsWith(redirect_url_template))
|
208
|
return;
|
209
|
|
210
|
#IF DEBUG
|
211
|
console.debug(`Haketilo: Settings queried using XHR for '${details.url}'.`);
|
212
|
#ENDIF
|
213
|
|
214
|
/*
|
215
|
* request_url should be of the following format:
|
216
|
* <url_for_extension's_dummy_file>?url=<valid_urlencoded_url>
|
217
|
*/
|
218
|
const match = request_url_regex.exec(details.url);
|
219
|
if (match) {
|
220
|
const queried_url = decodeURIComponent(match[1]);
|
221
|
|
222
|
if (details.initiator && details.initiator !== "null" &&
|
223
|
!queried_url.startsWith(details.initiator)) {
|
224
|
console.warn(`Haketilo: Blocked suspicious query of '${queried_url}' by '${details.initiator}'. This might be the result of page fingerprinting the browser.`);
|
225
|
return {cancel: true};
|
226
|
}
|
227
|
|
228
|
const policy =
|
229
|
decide_policy(tree, queried_url, !!default_allow.value, secret);
|
230
|
if (!policy.error) {
|
231
|
const encoded_policy = encodeURIComponent(JSON.stringify(policy));
|
232
|
return {redirectUrl: redirect_url_template + encoded_policy};
|
233
|
}
|
234
|
}
|
235
|
|
236
|
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.`);
|
237
|
|
238
|
return {cancel: true};
|
239
|
}
|
240
|
|
241
|
const all_types = [
|
242
|
"main_frame", "sub_frame", "stylesheet", "script", "image", "font",
|
243
|
"object", "xmlhttprequest", "ping", "csp_report", "media", "websocket",
|
244
|
"other", "main_frame", "sub_frame"
|
245
|
];
|
246
|
#ENDIF
|
247
|
|
248
|
async function start(secret_)
|
249
|
{
|
250
|
secret = secret_;
|
251
|
|
252
|
#IF CHROMIUM
|
253
|
const extra_opts = ["blocking", "extraHeaders"];
|
254
|
#ELSE
|
255
|
const extra_opts = ["blocking"];
|
256
|
#ENDIF
|
257
|
|
258
|
browser.webRequest.onHeadersReceived.addListener(
|
259
|
on_headers_received,
|
260
|
{urls: ["<all_urls>"], types: ["main_frame", "sub_frame"]},
|
261
|
extra_opts.concat("responseHeaders")
|
262
|
);
|
263
|
|
264
|
#IF CHROMIUM && MV2
|
265
|
browser.webRequest.onBeforeRequest.addListener(
|
266
|
on_before_request,
|
267
|
{urls: [browser.runtime.getURL("dummy") + "*"], types: all_types},
|
268
|
extra_opts
|
269
|
);
|
270
|
#ENDIF
|
271
|
}
|
272
|
#EXPORT start
|