Project

General

Profile

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

haketilo / common / misc.js @ ecb78704

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
/* Sign a given policy for a given time */
108
function sign_policy(policy, now, hours_offset) {
109
    let time = Math.floor(now / 3600000) + (hours_offset || 0);
110
    return gen_unique(time + policy);
111
}
112

    
113
/* Extract any policy present in the URL */
114
function url_extract_policy(url)
115
{
116
    const targets = url_extract_target(url);
117
    if (!targets.target)
118
	return targets;
119

    
120
    /* %7B -> { */
121
    const index = targets.target.indexOf('%7B');
122
    if (index === -1)
123
	return targets;
124
    
125
    const now = new Date();
126
    const sig = targets.target.substring(1, index);
127
    const policy = targets.target.substring(index);
128
    if (
129
	sig !== sign_policy(policy, now) &&
130
	sig !== sign_policy(policy, now, -1) &&
131
	sig !== sign_policy(policy, now, 1)
132
    )
133
	return targets;
134
    
135
    try {
136
	targets.policy = JSON.parse(decodeURIComponent(policy));
137
	targets.current = targets.policy.base_url === targets.base_url;
138
    } catch (e) {
139
	/* TODO what should happen here? */
140
    }
141

    
142
    return targets;
143
}
144

    
145
/*
146
 * EXPORTS_START
147
 * EXPORT gen_nonce
148
 * EXPORT gen_unique
149
 * EXPORT url_item
150
 * EXPORT url_extract_target
151
 * EXPORT url_extract_policy
152
 * EXPORT sign_policy
153
 * EXPORT csp_rule
154
 * EXPORT nice_name
155
 * EXPORT open_in_settings
156
 * EXPORT is_privileged_url
157
 * EXPORTS_END
158
 */
(3-3/8)