1
|
/**
|
2
|
* Myext 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
|
* IMPORTS_END
|
16
|
*/
|
17
|
|
18
|
/*
|
19
|
* generating unique, per-site value that can be computed synchronously
|
20
|
* and is impossible to guess for a malicious website
|
21
|
*/
|
22
|
|
23
|
/* Uint8toHex is a separate function not exported as (a) it's useful and (b) it will be used in crypto.subtle-based digests */
|
24
|
function Uint8toHex(data)
|
25
|
{
|
26
|
let returnValue = '';
|
27
|
for (let byte of data)
|
28
|
returnValue += ('00' + byte.toString(16)).slice(-2);
|
29
|
return returnValue;
|
30
|
}
|
31
|
|
32
|
function gen_nonce(length) // Default 16
|
33
|
{
|
34
|
let randomData = new Uint8Array(length || 16);
|
35
|
crypto.getRandomValues(randomData);
|
36
|
return Uint8toHex(randomData);
|
37
|
}
|
38
|
|
39
|
function gen_unique(url)
|
40
|
{
|
41
|
return sha256(get_secure_salt() + url);
|
42
|
}
|
43
|
|
44
|
function get_secure_salt()
|
45
|
{
|
46
|
if (is_chrome)
|
47
|
return browser.runtime.getManifest().key.substring(0, 50);
|
48
|
else
|
49
|
return browser.runtime.getURL("dummy");
|
50
|
}
|
51
|
|
52
|
/*
|
53
|
* stripping url from query and target (everything after `#' or `?'
|
54
|
* gets removed)
|
55
|
*/
|
56
|
function url_item(url)
|
57
|
{
|
58
|
let url_re = /^([^?#]*).*$/;
|
59
|
let match = url_re.exec(url);
|
60
|
return match[1];
|
61
|
}
|
62
|
|
63
|
/*
|
64
|
* Assume a url like: https://example.com/green?illuminati=confirmed#tinky#winky
|
65
|
* This function will make it into an object like:
|
66
|
* {
|
67
|
* "base_url" : "https://example.com/green?illuminati=confirmed",
|
68
|
* "target" : "#tinky",
|
69
|
* "target2" : "#winky"
|
70
|
* }
|
71
|
* In case url doesn't have 2 #'s, target2 and target can be set to undefined.
|
72
|
*/
|
73
|
function url_extract_target(url)
|
74
|
{
|
75
|
let url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/;
|
76
|
let match = url_re.exec(url);
|
77
|
return {
|
78
|
base_url : match[1],
|
79
|
target : match[3],
|
80
|
target2 : match[4]
|
81
|
};
|
82
|
}
|
83
|
|
84
|
/* csp rule that blocks all scripts except for those injected by us */
|
85
|
function csp_rule(nonce)
|
86
|
{
|
87
|
let rule = `script-src 'nonce-${nonce}';`;
|
88
|
if (is_chrome)
|
89
|
rule += `script-src-elem 'nonce-${nonce}';`;
|
90
|
return rule;
|
91
|
}
|
92
|
|
93
|
/*
|
94
|
* Print item together with type, e.g.
|
95
|
* nice_name("s", "hello") → "hello (script)"
|
96
|
*/
|
97
|
function nice_name(prefix, name)
|
98
|
{
|
99
|
return `${name} (${TYPE_NAME[prefix]})`;
|
100
|
}
|
101
|
|
102
|
/* Open settings tab with given item's editing already on. */
|
103
|
function open_in_settings(prefix, name)
|
104
|
{
|
105
|
name = encodeURIComponent(name);
|
106
|
const url = browser.runtime.getURL("html/options.html#" + prefix + name);
|
107
|
window.open(url, "_blank");
|
108
|
}
|
109
|
|
110
|
/* Check if url corresponds to a browser's special page */
|
111
|
function is_privileged_url(url)
|
112
|
{
|
113
|
return !!/^(chrome(-extension)?|moz-extension):\/\/|^about:/i.exec(url);
|
114
|
}
|
115
|
|
116
|
/*
|
117
|
* EXPORTS_START
|
118
|
* EXPORT gen_unique
|
119
|
* EXPORT gen_nonce
|
120
|
* EXPORT url_item
|
121
|
* EXPORT url_extract_target
|
122
|
* EXPORT csp_rule
|
123
|
* EXPORT nice_name
|
124
|
* EXPORT open_in_settings
|
125
|
* EXPORT is_privileged_url
|
126
|
* EXPORTS_END
|
127
|
*/
|