Project

General

Profile

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

haketilo / common / misc.js @ 8b823e1a

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
/* Generate a random base64-encoded 128-bit sequence */
19
function gen_nonce()
20
{
21
    let randomData = new Uint8Array(16);
22
    crypto.getRandomValues(randomData);
23
    return btoa(String.fromCharCode.apply(null, randomData));
24
}
25

    
26
/*
27
 * generating unique, per-site value that can be computed synchronously
28
 * and is impossible to guess for a malicious website
29
 */
30
function gen_unique(url)
31
{
32
    return sha256(get_secure_salt() + url);
33
}
34

    
35
function get_secure_salt()
36
{
37
    if (is_chrome)
38
	return browser.runtime.getManifest().key.substring(0, 50);
39
    else
40
	return browser.runtime.getURL("dummy");
41
}
42

    
43
/*
44
 * stripping url from query and target (everything after `#' or `?'
45
 * gets removed)
46
 */
47
function url_item(url)
48
{
49
    let url_re = /^([^?#]*).*$/;
50
    let match = url_re.exec(url);
51
    return match[1];
52
}
53

    
54
/*
55
 * Assume a url like: https://example.com/green?illuminati=confirmed#tinky#winky
56
 * This function will make it into an object like:
57
 * {
58
 *     "base_url" : "https://example.com/green?illuminati=confirmed",
59
 *     "target" : "#tinky",
60
 *     "target2" : "#winky"
61
 * }
62
 * In case url doesn't have 2 #'s, target2 and target can be set to undefined.
63
 */
64
function url_extract_target(url)
65
{
66
    let url_re = /^([^#]*)((#[^#]*)(#.*)?)?$/;
67
    let match = url_re.exec(url);
68
    return {
69
	base_url : match[1],
70
	target : match[3],
71
	target2 : match[4]
72
    };
73
}
74

    
75
/* csp rule that blocks all scripts except for those injected by us */
76
function csp_rule(nonce)
77
{
78
    let rule = `script-src 'nonce-${nonce}';`;
79
    if (is_chrome)
80
	rule += `script-src-elem 'nonce-${nonce}';`;
81
    return rule;
82
}
83

    
84
/*
85
 * Print item together with type, e.g.
86
 * nice_name("s", "hello") → "hello (script)"
87
 */
88
function nice_name(prefix, name)
89
{
90
    return `${name} (${TYPE_NAME[prefix]})`;
91
}
92

    
93
/* Open settings tab with given item's editing already on. */
94
function open_in_settings(prefix, name)
95
{
96
    name = encodeURIComponent(name);
97
    const url = browser.runtime.getURL("html/options.html#" + prefix + name);
98
    window.open(url, "_blank");
99
}
100

    
101
/* Check if url corresponds to a browser's special page */
102
function is_privileged_url(url)
103
{
104
    return !!/^(chrome(-extension)?|moz-extension):\/\/|^about:/i.exec(url);
105
}
106

    
107
/* Extract any policy present in the URL */
108
function url_extract_policy(url)
109
{
110
    var policy_string;
111
    const targets = url_extract_target(url);
112
    
113
    try {
114
	policy_string = targets.target.substring(65);
115
	targets.policy = JSON.parse(decodeURIComponent(policy_string));
116
    } catch (e) {
117
	/* TODO what should happen here? */
118
    }
119
    
120
    if (targets.policy) {
121
	const sig = gen_unique(policy_string + targets.base_url);
122
	targets.valid_sig = targets.target.substring(1, 65) === sig;
123
    }
124

    
125
    return targets;
126
}
127

    
128
/*
129
 * EXPORTS_START
130
 * EXPORT gen_nonce
131
 * EXPORT gen_unique
132
 * EXPORT url_item
133
 * EXPORT url_extract_target
134
 * EXPORT url_extract_policy
135
 * EXPORT csp_rule
136
 * EXPORT nice_name
137
 * EXPORT open_in_settings
138
 * EXPORT is_privileged_url
139
 * EXPORTS_END
140
 */
(3-3/8)