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