1
|
/**
|
2
|
* Myext miscellaneous operations refactored to a separate file
|
3
|
*
|
4
|
* Copyright (C) 2021 Wojtek Kosior
|
5
|
* Redistribution terms are gathered in the `copyright' file.
|
6
|
*/
|
7
|
|
8
|
/*
|
9
|
* IMPORTS_START
|
10
|
* IMPORT sha256
|
11
|
* IMPORT browser
|
12
|
* IMPORT is_chrome
|
13
|
* IMPORTS_END
|
14
|
*/
|
15
|
|
16
|
/*
|
17
|
* generating unique, per-site value that can be computed synchronously
|
18
|
* and is impossible to guess for a malicious website
|
19
|
*/
|
20
|
function gen_unique(url)
|
21
|
{
|
22
|
return sha256(get_secure_salt() + url);
|
23
|
}
|
24
|
|
25
|
function get_secure_salt()
|
26
|
{
|
27
|
if (is_chrome)
|
28
|
return browser.runtime.getManifest().key.substring(0, 50);
|
29
|
else
|
30
|
return browser.runtime.getURL("dummy");
|
31
|
}
|
32
|
|
33
|
/*
|
34
|
* stripping url from query and target (everything after `#' or `?'
|
35
|
* gets removed)
|
36
|
*/
|
37
|
function url_item(url)
|
38
|
{
|
39
|
let url_re = /^([^?#]*).*$/;
|
40
|
let match = url_re.exec(url);
|
41
|
return match[1];
|
42
|
}
|
43
|
|
44
|
/*
|
45
|
* Assume a url like: https://example.com/green?illuminati=confirmed#tinky#winky
|
46
|
* This function will make it into an object like:
|
47
|
* {
|
48
|
* "base_url" : "https://example.com/green?illuminati=confirmed",
|
49
|
* "target" : "#tinky",
|
50
|
* "target2" : "#winky"
|
51
|
* }
|
52
|
* In case url doesn't have 2 #'s, target2 and target can be set to undefined.
|
53
|
*/
|
54
|
function url_extract_target(url)
|
55
|
{
|
56
|
let url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/;
|
57
|
let match = url_re.exec(url);
|
58
|
return {
|
59
|
base_url : match[1],
|
60
|
target : match[3],
|
61
|
target2 : match[4]
|
62
|
};
|
63
|
}
|
64
|
|
65
|
/* csp rule that blocks all scripts except for those injected by us */
|
66
|
function csp_rule(nonce)
|
67
|
{
|
68
|
let rule = `script-src 'nonce-${nonce}';`;
|
69
|
if (is_chrome)
|
70
|
rule += `script-src-elem 'nonce-${nonce}';`;
|
71
|
return rule;
|
72
|
}
|
73
|
|
74
|
/*
|
75
|
* EXPORTS_START
|
76
|
* EXPORT gen_unique
|
77
|
* EXPORT url_item
|
78
|
* EXPORT url_extract_target
|
79
|
* EXPORT csp_rule
|
80
|
* EXPORTS_END
|
81
|
*/
|