Project

General

Profile

Download (1.15 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / common / misc.js @ cd5272ac

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
/* csp rule that blocks all scripts except for those injected by us */
45
function csp_rule(nonce)
46
{
47
    let rule = `script-src 'nonce-${nonce}';`;
48
    if (is_chrome)
49
	rule += `script-src-elem 'nonce-${nonce}';`;
50
    return rule;
51
}
52

    
53
/*
54
 * EXPORTS_START
55
 * EXPORT gen_unique
56
 * EXPORT url_item
57
 * EXPORT csp_rule
58
 * EXPORTS_END
59
 */
(4-4/8)