1
|
/**
|
2
|
* Hachette miscellaneous operations refactored to a separate file
|
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 sha256
|
12
|
* IMPORT browser
|
13
|
* IMPORT is_chrome
|
14
|
* IMPORT TYPE_NAME
|
15
|
* IMPORT TYPE_PREFIX
|
16
|
* IMPORTS_END
|
17
|
*/
|
18
|
|
19
|
/* Generate a random base64-encoded 128-bit sequence */
|
20
|
function gen_nonce()
|
21
|
{
|
22
|
let randomData = new Uint8Array(16);
|
23
|
crypto.getRandomValues(randomData);
|
24
|
return btoa(String.fromCharCode.apply(null, randomData));
|
25
|
}
|
26
|
|
27
|
/*
|
28
|
* generating unique, per-site value that can be computed synchronously
|
29
|
* and is impossible to guess for a malicious website
|
30
|
*/
|
31
|
|
32
|
/* Uint8toHex is a separate function not exported as (a) it's useful and (b) it will be used in crypto.subtle-based digests */
|
33
|
function Uint8toHex(data)
|
34
|
{
|
35
|
let returnValue = '';
|
36
|
for (let byte of data)
|
37
|
returnValue += ('00' + byte.toString(16)).slice(-2);
|
38
|
return returnValue;
|
39
|
}
|
40
|
|
41
|
function gen_nonce(length) // Default 16
|
42
|
{
|
43
|
let randomData = new Uint8Array(length || 16);
|
44
|
crypto.getRandomValues(randomData);
|
45
|
return Uint8toHex(randomData);
|
46
|
}
|
47
|
|
48
|
function get_secure_salt()
|
49
|
{
|
50
|
if (is_chrome)
|
51
|
return browser.runtime.getManifest().key.substring(0, 50);
|
52
|
else
|
53
|
return browser.runtime.getURL("dummy");
|
54
|
}
|
55
|
|
56
|
function extract_signed(signature, data, times)
|
57
|
{
|
58
|
const now = new Date();
|
59
|
times = times || [[now], [now, -1]];
|
60
|
|
61
|
const reductor =
|
62
|
(ok, time) => ok || signature === sign_data(data, ...time);
|
63
|
if (!times.reduce(reductor, false))
|
64
|
return undefined;
|
65
|
|
66
|
try {
|
67
|
return JSON.parse(decodeURIComponent(data));
|
68
|
} catch (e) {
|
69
|
/* This should not be reached - it's our self-produced valid JSON. */
|
70
|
console.log("Unexpected internal error - invalid JSON smuggled!", e);
|
71
|
}
|
72
|
}
|
73
|
|
74
|
/* csp rule that blocks all scripts except for those injected by us */
|
75
|
function csp_rule(nonce)
|
76
|
{
|
77
|
const rule = `'nonce-${nonce}'`;
|
78
|
return `script-src ${rule}; script-src-elem ${rule}; script-src-attr 'none'; prefetch-src 'none';`;
|
79
|
}
|
80
|
|
81
|
/*
|
82
|
* Print item together with type, e.g.
|
83
|
* nice_name("s", "hello") → "hello (script)"
|
84
|
*/
|
85
|
function nice_name(prefix, name)
|
86
|
{
|
87
|
return `${name} (${TYPE_NAME[prefix]})`;
|
88
|
}
|
89
|
|
90
|
/* Open settings tab with given item's editing already on. */
|
91
|
function open_in_settings(prefix, name)
|
92
|
{
|
93
|
name = encodeURIComponent(name);
|
94
|
const url = browser.runtime.getURL("html/options.html#" + prefix + name);
|
95
|
window.open(url, "_blank");
|
96
|
}
|
97
|
|
98
|
/* Check if url corresponds to a browser's special page */
|
99
|
function is_privileged_url(url)
|
100
|
{
|
101
|
return !!/^(chrome(-extension)?|moz-extension):\/\/|^about:/i.exec(url);
|
102
|
}
|
103
|
|
104
|
/* Sign a given string for a given time */
|
105
|
function sign_data(data, now, hours_offset) {
|
106
|
let time = Math.floor(now / 3600000) + (hours_offset || 0);
|
107
|
return sha256(get_secure_salt() + time + data);
|
108
|
}
|
109
|
|
110
|
/* Parse a CSP header */
|
111
|
function parse_csp(csp) {
|
112
|
let directive, directive_array;
|
113
|
let directives = {};
|
114
|
for (directive of csp.split(';')) {
|
115
|
directive = directive.trim();
|
116
|
if (directive === '')
|
117
|
continue;
|
118
|
|
119
|
directive_array = directive.split(/\s+/);
|
120
|
directive = directive_array.shift();
|
121
|
/* The "true" case should never occur; nevertheless... */
|
122
|
directives[directive] = directive in directives ?
|
123
|
directives[directive].concat(directive_array) :
|
124
|
directive_array;
|
125
|
}
|
126
|
return directives;
|
127
|
}
|
128
|
|
129
|
/* Make CSP headers do our bidding, not interfere */
|
130
|
function sanitize_csp_header(header, rule, allow)
|
131
|
{
|
132
|
const csp = parse_csp(header.value);
|
133
|
|
134
|
if (!allow) {
|
135
|
/* No snitching */
|
136
|
delete csp['report-to'];
|
137
|
delete csp['report-uri'];
|
138
|
|
139
|
delete csp['script-src'];
|
140
|
delete csp['script-src-elem'];
|
141
|
|
142
|
csp['script-src-attr'] = ["'none'"];
|
143
|
csp['prefetch-src'] = ["'none'"];
|
144
|
}
|
145
|
|
146
|
if ('script-src' in csp)
|
147
|
csp['script-src'].push(rule);
|
148
|
else
|
149
|
csp['script-src'] = [rule];
|
150
|
|
151
|
if ('script-src-elem' in csp)
|
152
|
csp['script-src-elem'].push(rule);
|
153
|
else
|
154
|
csp['script-src-elem'] = [rule];
|
155
|
|
156
|
const new_policy = Object.entries(csp).map(
|
157
|
i => `${i[0]} ${i[1].join(' ')};`
|
158
|
);
|
159
|
|
160
|
return {name: header.name, value: new_policy.join('')};
|
161
|
}
|
162
|
|
163
|
/* Regexes and objest to use as/in schemas for parse_json_with_schema(). */
|
164
|
const nonempty_string_matcher = /.+/;
|
165
|
|
166
|
const matchers = {
|
167
|
sha256: /^[0-9a-f]{64}$/,
|
168
|
nonempty_string: nonempty_string_matcher,
|
169
|
component: [
|
170
|
new RegExp(`^[${TYPE_PREFIX.SCRIPT}${TYPE_PREFIX.BAG}]$`),
|
171
|
nonempty_string_matcher
|
172
|
]
|
173
|
};
|
174
|
|
175
|
/*
|
176
|
* EXPORTS_START
|
177
|
* EXPORT gen_nonce
|
178
|
* EXPORT extract_signed
|
179
|
* EXPORT sign_data
|
180
|
* EXPORT csp_rule
|
181
|
* EXPORT nice_name
|
182
|
* EXPORT open_in_settings
|
183
|
* EXPORT is_privileged_url
|
184
|
* EXPORT sanitize_csp_header
|
185
|
* EXPORT matchers
|
186
|
* EXPORTS_END
|
187
|
*/
|