1
|
/**
|
2
|
* This file is part of Haketilo.
|
3
|
*
|
4
|
* Function: Main background script.
|
5
|
*
|
6
|
* Copyright (C) 2021 Wojtek Kosior <koszko@koszko.org>
|
7
|
* Copyright (C) 2021 Jahoti <jahoti@envs.net>
|
8
|
*
|
9
|
* This program is free software: you can redistribute it and/or modify
|
10
|
* it under the terms of the GNU General Public License as published by
|
11
|
* the Free Software Foundation, either version 3 of the License, or
|
12
|
* (at your option) any later version.
|
13
|
*
|
14
|
* This program is distributed in the hope that it will be useful,
|
15
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
* GNU General Public License for more details.
|
18
|
*
|
19
|
* As additional permission under GNU GPL version 3 section 7, you
|
20
|
* may distribute forms of that code without the copy of the GNU
|
21
|
* GPL normally required by section 4, provided you include this
|
22
|
* license notice and, in case of non-source distribution, a URL
|
23
|
* through which recipients can access the Corresponding Source.
|
24
|
* If you modify file(s) with this exception, you may extend this
|
25
|
* exception to your version of the file(s), but you are not
|
26
|
* obligated to do so. If you do not wish to do so, delete this
|
27
|
* exception statement from your version.
|
28
|
*
|
29
|
* As a special exception to the GPL, any HTML file which merely
|
30
|
* makes function calls to this code, and for that purpose
|
31
|
* includes it by reference shall be deemed a separate work for
|
32
|
* copyright law purposes. If you modify this code, you may extend
|
33
|
* this exception to your version of the code, but you are not
|
34
|
* obligated to do so. If you do not wish to do so, delete this
|
35
|
* exception statement from your version.
|
36
|
*
|
37
|
* You should have received a copy of the GNU General Public License
|
38
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
39
|
*
|
40
|
* I, Wojtek Kosior, thereby promise not to sue for violation of this file's
|
41
|
* license. Although I request that you do not make use this code in a
|
42
|
* proprietary program, I am not going to enforce this in court.
|
43
|
*/
|
44
|
|
45
|
/*
|
46
|
* IMPORTS_START
|
47
|
* IMPORT initial_data
|
48
|
* IMPORT TYPE_PREFIX
|
49
|
* IMPORT get_storage
|
50
|
* IMPORT light_storage
|
51
|
* IMPORT start_storage_server
|
52
|
* IMPORT start_page_actions_server
|
53
|
* IMPORT browser
|
54
|
* IMPORT is_privileged_url
|
55
|
* IMPORT query_best
|
56
|
* IMPORT inject_csp_headers
|
57
|
* IMPORT apply_stream_filter
|
58
|
* IMPORT is_chrome
|
59
|
* IMPORT is_mozilla
|
60
|
* IMPORTS_END
|
61
|
*/
|
62
|
|
63
|
start_storage_server();
|
64
|
start_page_actions_server();
|
65
|
|
66
|
async function init_ext(install_details)
|
67
|
{
|
68
|
if (install_details.reason != "install")
|
69
|
return;
|
70
|
|
71
|
let storage = await get_storage();
|
72
|
|
73
|
await storage.clear();
|
74
|
|
75
|
/* Below we add sample settings to the extension. */
|
76
|
for (let setting of initial_data) {
|
77
|
let [key, value] = Object.entries(setting)[0];
|
78
|
storage.set(key[0], key.substring(1), value);
|
79
|
}
|
80
|
}
|
81
|
|
82
|
browser.runtime.onInstalled.addListener(init_ext);
|
83
|
|
84
|
/*
|
85
|
* The function below implements a more practical interface for what it does by
|
86
|
* wrapping the old query_best() function.
|
87
|
*/
|
88
|
function decide_policy_for_url(storage, policy_observable, url)
|
89
|
{
|
90
|
if (storage === undefined)
|
91
|
return {allow: false};
|
92
|
|
93
|
const settings =
|
94
|
{allow: policy_observable !== undefined && policy_observable.value};
|
95
|
|
96
|
const [pattern, queried_settings] = query_best(storage, url);
|
97
|
|
98
|
if (queried_settings) {
|
99
|
settings.payload = queried_settings.components;
|
100
|
settings.allow = !!queried_settings.allow && !settings.payload;
|
101
|
settings.pattern = pattern;
|
102
|
}
|
103
|
|
104
|
return settings;
|
105
|
}
|
106
|
|
107
|
let storage;
|
108
|
let policy_observable = {};
|
109
|
|
110
|
function sanitize_web_page(details)
|
111
|
{
|
112
|
const url = details.url;
|
113
|
if (is_privileged_url(details.url))
|
114
|
return;
|
115
|
|
116
|
const policy =
|
117
|
decide_policy_for_url(storage, policy_observable, details.url);
|
118
|
|
119
|
let headers = details.responseHeaders;
|
120
|
|
121
|
headers = inject_csp_headers(headers, policy);
|
122
|
|
123
|
let skip = false;
|
124
|
for (const header of headers) {
|
125
|
if ((header.name.toLowerCase().trim() === "content-disposition" &&
|
126
|
/^\s*attachment\s*(;.*)$/i.test(header.value)))
|
127
|
skip = true;
|
128
|
}
|
129
|
skip = skip || (details.statusCode >= 300 && details.statusCode < 400);
|
130
|
|
131
|
if (!skip) {
|
132
|
/* Check for API availability. */
|
133
|
if (browser.webRequest.filterResponseData)
|
134
|
headers = apply_stream_filter(details, headers, policy);
|
135
|
}
|
136
|
|
137
|
return {responseHeaders: headers};
|
138
|
}
|
139
|
|
140
|
const request_url_regex = /^[^?]*\?url=(.*)$/;
|
141
|
const redirect_url_template = browser.runtime.getURL("dummy") + "?settings=";
|
142
|
|
143
|
function synchronously_smuggle_policy(details)
|
144
|
{
|
145
|
/*
|
146
|
* Content script will make a synchronous XmlHttpRequest to extension's
|
147
|
* `dummy` file to query settings for given URL. We smuggle that
|
148
|
* information in query parameter of the URL we redirect to.
|
149
|
* A risk of fingerprinting arises if a page with script execution allowed
|
150
|
* guesses the dummy file URL and makes an AJAX call to it. It is currently
|
151
|
* a problem in ManifestV2 Chromium-family port of Haketilo because Chromium
|
152
|
* uses predictable URLs for web-accessible resources. We plan to fix it in
|
153
|
* the future ManifestV3 port.
|
154
|
*/
|
155
|
if (details.type !== "xmlhttprequest")
|
156
|
return {cancel: true};
|
157
|
|
158
|
console.debug(`Settings queried using XHR for '${details.url}'.`);
|
159
|
|
160
|
let policy = {allow: false};
|
161
|
|
162
|
try {
|
163
|
/*
|
164
|
* request_url should be of the following format:
|
165
|
* <url_for_extension's_dummy_file>?url=<valid_urlencoded_url>
|
166
|
*/
|
167
|
const match = request_url_regex.exec(details.url);
|
168
|
const queried_url = decodeURIComponent(match[1]);
|
169
|
|
170
|
if (details.initiator && !queried_url.startsWith(details.initiator)) {
|
171
|
console.warn(`Blocked suspicious query of '${url}' by '${details.initiator}'. This might be the result of page fingerprinting the browser.`);
|
172
|
return {cancel: true};
|
173
|
}
|
174
|
|
175
|
policy = decide_policy_for_url(storage, policy_observable, queried_url);
|
176
|
} catch (e) {
|
177
|
console.warn(`Bad request! Expected ${browser.runtime.getURL("dummy")}?url=<valid_urlencoded_url>. Got ${request_url}. This might be the result of page fingerprinting the browser.`);
|
178
|
}
|
179
|
|
180
|
const encoded_policy = encodeURIComponent(JSON.stringify(policy));
|
181
|
|
182
|
return {redirectUrl: redirect_url_template + encoded_policy};
|
183
|
}
|
184
|
|
185
|
const all_types = [
|
186
|
"main_frame", "sub_frame", "stylesheet", "script", "image", "font",
|
187
|
"object", "xmlhttprequest", "ping", "csp_report", "media", "websocket",
|
188
|
"other", "main_frame", "sub_frame"
|
189
|
];
|
190
|
|
191
|
async function start_webRequest_operations()
|
192
|
{
|
193
|
storage = await get_storage();
|
194
|
|
195
|
const extra_opts = ["blocking"];
|
196
|
if (is_chrome)
|
197
|
extra_opts.push("extraHeaders");
|
198
|
|
199
|
browser.webRequest.onHeadersReceived.addListener(
|
200
|
sanitize_web_page,
|
201
|
{urls: ["<all_urls>"], types: ["main_frame", "sub_frame"]},
|
202
|
extra_opts.concat("responseHeaders")
|
203
|
);
|
204
|
|
205
|
const dummy_url_pattern = browser.runtime.getURL("dummy") + "?url=*";
|
206
|
browser.webRequest.onBeforeRequest.addListener(
|
207
|
synchronously_smuggle_policy,
|
208
|
{urls: [dummy_url_pattern], types: ["xmlhttprequest"]},
|
209
|
extra_opts
|
210
|
);
|
211
|
|
212
|
policy_observable = await light_storage.observe_var("default_allow");
|
213
|
}
|
214
|
|
215
|
start_webRequest_operations();
|
216
|
|
217
|
const code = `\
|
218
|
console.warn("Hi, I'm Mr Dynamic!");
|
219
|
|
220
|
console.debug("let's see how window.haketilo_exports looks like now");
|
221
|
|
222
|
console.log("haketilo_exports", window.haketilo_exports);
|
223
|
`
|
224
|
|
225
|
async function test_dynamic_content_scripts()
|
226
|
{
|
227
|
browser.contentScripts.register({
|
228
|
"js": [{code}],
|
229
|
"matches": ["<all_urls>"],
|
230
|
"allFrames": true,
|
231
|
"runAt": "document_start"
|
232
|
});
|
233
|
}
|
234
|
|
235
|
if (is_mozilla)
|
236
|
test_dynamic_content_scripts();
|