Project

General

Profile

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

haketilo / content / main.js @ 5dab077b

1
/**
2
 * Hachette main content script run in all frames
3
 *
4
 * Copyright (C) 2021 Wojtek Kosior
5
 * Copyright (C) 2021 jahoti
6
 * Redistribution terms are gathered in the `copyright' file.
7
 */
8

    
9
/*
10
 * IMPORTS_START
11
 * IMPORT handle_page_actions
12
 * IMPORT extract_signed
13
 * IMPORT sign_data
14
 * IMPORT gen_nonce
15
 * IMPORT is_privileged_url
16
 * IMPORT mozilla_suppress_scripts
17
 * IMPORT is_chrome
18
 * IMPORT is_mozilla
19
 * IMPORT start_activity_info_server
20
 * IMPORT make_csp_rule
21
 * IMPORT is_csp_header_name
22
 * IMPORT sanitize_csp_header
23
 * IMPORTS_END
24
 */
25

    
26
function extract_cookie_policy(cookie, min_time)
27
{
28
    let best_result = {time: -1};
29
    let policy = null;
30
    const extracted_signatures = [];
31

    
32
    for (const match of cookie.matchAll(/hachette-(\w*)=([^;]*)/g)) {
33
	const new_result = extract_signed(...match.slice(1, 3));
34
	if (new_result.fail)
35
	    continue;
36

    
37
	extracted_signatures.push(match[1]);
38

    
39
	if (new_result.time < Math.max(min_time, best_result.time))
40
	    continue;
41

    
42
	/* This should succeed - it's our self-produced valid JSON. */
43
	const new_policy = JSON.parse(decodeURIComponent(new_result.data));
44
	if (new_policy.url !== document.URL)
45
	    continue;
46

    
47
	best_result = new_result;
48
	policy = new_policy;
49
    }
50

    
51
    return [policy, extracted_signatures];
52
}
53

    
54
function extract_url_policy(url, min_time)
55
{
56
    const [base_url, payload, anchor] =
57
	  /^([^#]*)#?([^#]*)(#?.*)$/.exec(url).splice(1, 4);
58

    
59
    const match = /^hachette_([^_]+)_(.*)$/.exec(payload);
60
    if (!match)
61
	return [null, url];
62

    
63
    const result = extract_signed(...match.slice(1, 3));
64
    if (result.fail)
65
	return [null, url];
66

    
67
    const original_url = base_url + anchor;
68
    const policy = result.time < min_time ? null :
69
	  JSON.parse(decodeURIComponent(result.data));
70

    
71
    return [policy.url === original_url ? policy : null, original_url];
72
}
73

    
74
function employ_nonhttp_policy(policy)
75
{
76
    if (!policy.allow)
77
	return;
78

    
79
    policy.nonce = gen_nonce();
80
    const [base_url, target] = /^([^#]*)(#?.*)$/.exec(policy.url).slice(1, 3);
81
    const encoded_policy = encodeURIComponent(JSON.stringify(policy));
82
    const payload = "hachette_" +
83
	  sign_data(encoded_policy, new Date().getTime()).join("_");
84
    const resulting_url = `${base_url}#${payload}${target}`;
85
    location.href = resulting_url;
86
    location.reload();
87
}
88

    
89
/*
90
 * 1. When injecting some payload we need to sanitize <meta> CSP tags before
91
 *    they reach the document.
92
 * 2. Only <meta> tags inside <head> are considered valid by the browser and
93
 *    need to be considered.
94
 * 3. We want to detach <html> from document, wait until its <head> completes
95
 *    loading, sanitize it and re-attach <html>.
96
 * 4. Browsers are eager to add <meta>'s that appear after `</head>' but before
97
 *    `<body>'. Due to this behavior the `DOMContentLoaded' event is considered
98
 *    unreliable (although it could still work properly, it is just problematic
99
 *    to verify).
100
 * 5. We shall wait for anything to appear in or after <body> and take that as
101
 *    a sign <head> has _really_ finished loading.
102
 */
103

    
104
function make_body_start_observer(DOM_element, waiting)
105
{
106
    const observer = new MutationObserver(() => try_body_started(waiting));
107
    observer.observe(DOM_element, {childList: true});
108
    return observer;
109
}
110

    
111
function try_body_started(waiting)
112
{
113
    const body = waiting.detached_html.querySelector("body");
114

    
115
    if ((body && (body.firstChild || body.nextSibling)) ||
116
	waiting.doc.documentElement.nextSibling) {
117
	finish_waiting(waiting);
118
	return true;
119
    }
120

    
121
    if (body && waiting.observers.length < 2)
122
	waiting.observers.push(make_body_start_observer(body, waiting));
123
}
124

    
125
function finish_waiting(waiting)
126
{
127
    waiting.observers.forEach(observer => observer.disconnect());
128
    waiting.doc.removeEventListener("DOMContentLoaded", waiting.loaded_cb);
129
    setTimeout(waiting.callback, 0);
130
}
131

    
132
function _wait_for_head(doc, detached_html, callback)
133
{
134
    const waiting = {doc, detached_html, callback, observers: []};
135
    if (try_body_started(waiting))
136
	return;
137

    
138
    waiting.observers = [make_body_start_observer(detached_html, waiting)];
139
    waiting.loaded_cb = () => finish_waiting(waiting);
140
    doc.addEventListener("DOMContentLoaded", waiting.loaded_cb);
141
}
142

    
143
function wait_for_head(doc, detached_html)
144
{
145
    return new Promise(cb => _wait_for_head(doc, detached_html, cb));
146
}
147

    
148
const blocked_str = "blocked";
149

    
150
function block_attribute(node, attr)
151
{
152
    /*
153
     * Disabling attributes this way allows them to still be relatively
154
     * easily accessed in case they contain some useful data.
155
     */
156
    const construct_name = [attr];
157
    while (node.hasAttribute(construct_name.join("")))
158
	construct_name.unshift(blocked_str);
159

    
160
    while (construct_name.length > 1) {
161
	construct_name.shift();
162
	const name = construct_name.join("");
163
	node.setAttribute(`${blocked_str}-${name}`, node.getAttribute(name));
164
    }
165

    
166
    node.removeAttribute(attr);
167
}
168

    
169
function sanitize_meta(meta, policy)
170
{
171
    const http_equiv = meta.getAttribute("http-equiv");
172
    const value = meta.content;
173

    
174
    if (!value || !is_csp_header_name(http_equiv, true))
175
	return;
176

    
177
    block_attribute(meta, "content");
178
}
179

    
180
function sanitize_script(script)
181
{
182
    script.hachette_blocked_type = script.type;
183
    script.type = "text/plain";
184
}
185

    
186
/*
187
 * Executed after script has been connected to the DOM, when it is no longer
188
 * eligible for being executed by the browser
189
 */
190
function desanitize_script(script, policy)
191
{
192
    script.setAttribute("type", script.hachette_blocked_type);
193

    
194
    if (script.hachette_blocked_type === undefined)
195
	script.removeAttribute("type");
196

    
197
    delete script.hachette_blocked_type;
198
}
199

    
200
function apply_hachette_csp_rules(doc, policy)
201
{
202
    const meta = doc.createElement("meta");
203
    meta.setAttribute("http-equiv", "Content-Security-Policy");
204
    meta.setAttribute("content", make_csp_rule(policy));
205
    doc.head.append(meta);
206
    /* CSP is already in effect, we can remove the <meta> now. */
207
    meta.remove();
208
}
209

    
210
async function sanitize_document(doc, policy)
211
{
212
    /*
213
     * Ensure our CSP rules are employed from the beginning. This CSP injection
214
     * method is, when possible, going to be applied together with CSP rules
215
     * injected using webRequest.
216
     */
217
    const has_own_head = doc.head;
218
    if (!has_own_head)
219
	doc.documentElement.prepend(doc.createElement("head"));
220

    
221
    apply_hachette_csp_rules(doc, policy);
222

    
223
    /* Probably not needed, but...: proceed with DOM in its initial state. */
224
    if (!has_own_head)
225
	doc.head.remove();
226

    
227
    /*
228
     * <html> node gets hijacked now, to be re-attached after <head> is loaded
229
     * and sanitized.
230
     */
231
    const old_html = doc.documentElement;
232
    const new_html = doc.createElement("html");
233
    old_html.replaceWith(new_html);
234

    
235
    await wait_for_head(doc, old_html);
236

    
237
    for (const meta of old_html.querySelectorAll("head meta"))
238
	sanitize_meta(meta, policy);
239

    
240
    if (!policy.allow)
241
	for (const script of old_html.querySelectorAll("script"))
242
	     sanitize_script(script, policy);
243

    
244
    new_html.replaceWith(old_html);
245

    
246
    if (!policy.allow)
247
	for (const script of old_html.querySelectorAll("script"))
248
	    desanitize_script(script, policy);
249
}
250

    
251
if (!is_privileged_url(document.URL)) {
252
    let policy_received_callback = () => undefined;
253
    let policy;
254

    
255
    /* Signature valid for half an hour. */
256
    const min_time = new Date().getTime() - 1800 * 1000;
257

    
258
    if (/^https?:/.test(document.URL)) {
259
	let signatures;
260
	[policy, signatures] = extract_cookie_policy(document.cookie, min_time);
261
	for (const signature of signatures)
262
	    document.cookie = `hachette-${signature}=; Max-Age=-1;`;
263
    } else {
264
	const scheme = /^([^:]*)/.exec(document.URL)[1];
265
	const known_scheme = ["file", "ftp"].includes(scheme);
266

    
267
	if (!known_scheme)
268
	    console.warn(`Unknown url scheme: \`${scheme}'!`);
269

    
270
	let original_url;
271
	[policy, original_url] = extract_url_policy(document.URL, min_time);
272
	history.replaceState(null, "", original_url);
273

    
274
	if (known_scheme && !policy)
275
	    policy_received_callback = employ_nonhttp_policy;
276
    }
277

    
278
    if (!policy) {
279
	console.warn("Using fallback policy!");
280
	policy = {allow: false, nonce: gen_nonce()};
281
    }
282

    
283
    const doc_ready = Promise.all([
284
	(policy.allow && !policy.has_payload) ? Promise.resolve : sanitize_document(document, policy),
285
	new Promise(cb => document.addEventListener("DOMContentLoaded",
286
						    cb, {once: true}))
287
    ]);
288

    
289
    handle_page_actions(policy.nonce, policy_received_callback, doc_ready);
290

    
291
    start_activity_info_server();
292
}
(3-3/5)