1
|
/**
|
2
|
* This file is part of Haketilo.
|
3
|
*
|
4
|
* Function: Miscellaneous operations refactored to a separate file.
|
5
|
*
|
6
|
* Copyright (C) 2021 Wojtek Kosior
|
7
|
* Copyright (C) 2021 jahoti
|
8
|
* Redistribution terms are gathered in the `copyright' file.
|
9
|
*/
|
10
|
|
11
|
/*
|
12
|
* IMPORTS_START
|
13
|
* IMPORT browser
|
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=16)
|
42
|
{
|
43
|
let randomData = new Uint8Array(length);
|
44
|
crypto.getRandomValues(randomData);
|
45
|
return Uint8toHex(randomData);
|
46
|
}
|
47
|
|
48
|
/* CSP rule that blocks scripts according to policy's needs. */
|
49
|
function make_csp_rule(policy)
|
50
|
{
|
51
|
let rule = "prefetch-src 'none'; script-src-attr 'none';";
|
52
|
const script_src = policy.has_payload ?
|
53
|
`'nonce-${policy.nonce}'` : "'none'";
|
54
|
rule += ` script-src ${script_src}; script-src-elem ${script_src};`;
|
55
|
return rule;
|
56
|
}
|
57
|
|
58
|
/* Check if some HTTP header might define CSP rules. */
|
59
|
const csp_header_regex =
|
60
|
/^\s*(content-security-policy|x-webkit-csp|x-content-security-policy)/i;
|
61
|
|
62
|
/*
|
63
|
* Print item together with type, e.g.
|
64
|
* nice_name("s", "hello") → "hello (script)"
|
65
|
*/
|
66
|
function nice_name(prefix, name)
|
67
|
{
|
68
|
return `${name} (${TYPE_NAME[prefix]})`;
|
69
|
}
|
70
|
|
71
|
/* Open settings tab with given item's editing already on. */
|
72
|
function open_in_settings(prefix, name)
|
73
|
{
|
74
|
name = encodeURIComponent(name);
|
75
|
const url = browser.runtime.getURL("html/options.html#" + prefix + name);
|
76
|
window.open(url, "_blank");
|
77
|
}
|
78
|
|
79
|
/*
|
80
|
* Check if url corresponds to a browser's special page (or a directory index in
|
81
|
* case of `file://' protocol).
|
82
|
*/
|
83
|
const privileged_reg =
|
84
|
/^(chrome(-extension)?|moz-extension):\/\/|^about:|^file:\/\/.*\/$/;
|
85
|
const is_privileged_url = url => privileged_reg.test(url);
|
86
|
|
87
|
/* Parse a CSP header */
|
88
|
function parse_csp(csp) {
|
89
|
let directive, directive_array;
|
90
|
let directives = {};
|
91
|
for (directive of csp.split(';')) {
|
92
|
directive = directive.trim();
|
93
|
if (directive === '')
|
94
|
continue;
|
95
|
|
96
|
directive_array = directive.split(/\s+/);
|
97
|
directive = directive_array.shift();
|
98
|
/* The "true" case should never occur; nevertheless... */
|
99
|
directives[directive] = directive in directives ?
|
100
|
directives[directive].concat(directive_array) :
|
101
|
directive_array;
|
102
|
}
|
103
|
return directives;
|
104
|
}
|
105
|
|
106
|
/* Regexes and objects to use as/in schemas for parse_json_with_schema(). */
|
107
|
const nonempty_string_matcher = /.+/;
|
108
|
|
109
|
const matchers = {
|
110
|
sha256: /^[0-9a-f]{64}$/,
|
111
|
nonempty_string: nonempty_string_matcher,
|
112
|
component: [
|
113
|
new RegExp(`^[${TYPE_PREFIX.SCRIPT}${TYPE_PREFIX.BAG}]$`),
|
114
|
nonempty_string_matcher
|
115
|
]
|
116
|
};
|
117
|
|
118
|
/*
|
119
|
* EXPORTS_START
|
120
|
* EXPORT gen_nonce
|
121
|
* EXPORT make_csp_rule
|
122
|
* EXPORT csp_header_regex
|
123
|
* EXPORT nice_name
|
124
|
* EXPORT open_in_settings
|
125
|
* EXPORT is_privileged_url
|
126
|
* EXPORT matchers
|
127
|
* EXPORTS_END
|
128
|
*/
|