Project

General

Profile

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

haketilo / common / misc.js @ 44e89d8e

1
/**
2
 * Hachette 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 browser
12
 * IMPORT TYPE_NAME
13
 * IMPORT TYPE_PREFIX
14
 * IMPORTS_END
15
 */
16

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

    
25
/*
26
 * generating unique, per-site value that can be computed synchronously
27
 * and is impossible to guess for a malicious website
28
 */
29

    
30
/* Uint8toHex is a separate function not exported as (a) it's useful and (b) it will be used in crypto.subtle-based digests */
31
function Uint8toHex(data)
32
{
33
    let returnValue = '';
34
    for (let byte of data)
35
	returnValue += ('00' + byte.toString(16)).slice(-2);
36
    return returnValue;
37
}
38

    
39
function gen_nonce(length=16)
40
{
41
    let randomData = new Uint8Array(length);
42
    crypto.getRandomValues(randomData);
43
    return Uint8toHex(randomData);
44
}
45

    
46
/* CSP rule that blocks scripts according to policy's needs. */
47
function make_csp_rule(policy)
48
{
49
    let rule = "prefetch-src 'none'; script-src-attr 'none';";
50
    const script_src = policy.has_payload ?
51
	  `'nonce-${policy.nonce}'` : "'none'";
52
    rule += ` script-src ${script_src}; script-src-elem ${script_src};`;
53
    return rule;
54
}
55

    
56
/* Check if some HTTP header might define CSP rules. */
57
const csp_header_regex =
58
      /^\s*(content-security-policy|x-webkit-csp|x-content-security-policy)/i;
59

    
60
/*
61
 * Print item together with type, e.g.
62
 * nice_name("s", "hello") → "hello (script)"
63
 */
64
function nice_name(prefix, name)
65
{
66
    return `${name} (${TYPE_NAME[prefix]})`;
67
}
68

    
69
/* Open settings tab with given item's editing already on. */
70
function open_in_settings(prefix, name)
71
{
72
    name = encodeURIComponent(name);
73
    const url = browser.runtime.getURL("html/options.html#" + prefix + name);
74
    window.open(url, "_blank");
75
}
76

    
77
/*
78
 * Check if url corresponds to a browser's special page (or a directory index in
79
 * case of `file://' protocol).
80
 */
81
const privileged_reg =
82
      /^(chrome(-extension)?|moz-extension):\/\/|^about:|^file:\/\/.*\/$/;
83
const is_privileged_url = url => privileged_reg.test(url);
84

    
85
/* Parse a CSP header */
86
function parse_csp(csp) {
87
    let directive, directive_array;
88
    let directives = {};
89
    for (directive of csp.split(';')) {
90
	directive = directive.trim();
91
	if (directive === '')
92
	    continue;
93

    
94
	directive_array = directive.split(/\s+/);
95
	directive = directive_array.shift();
96
	/* The "true" case should never occur; nevertheless... */
97
	directives[directive] = directive in directives ?
98
	    directives[directive].concat(directive_array) :
99
	    directive_array;
100
    }
101
    return directives;
102
}
103

    
104
/* Regexes and objects to use as/in schemas for parse_json_with_schema(). */
105
const nonempty_string_matcher = /.+/;
106

    
107
const matchers = {
108
    sha256: /^[0-9a-f]{64}$/,
109
    nonempty_string: nonempty_string_matcher,
110
    component: [
111
	new RegExp(`^[${TYPE_PREFIX.SCRIPT}${TYPE_PREFIX.BAG}]$`),
112
	nonempty_string_matcher
113
    ]
114
};
115

    
116
/*
117
 * EXPORTS_START
118
 * EXPORT gen_nonce
119
 * EXPORT make_csp_rule
120
 * EXPORT csp_header_regex
121
 * EXPORT nice_name
122
 * EXPORT open_in_settings
123
 * EXPORT is_privileged_url
124
 * EXPORT matchers
125
 * EXPORTS_END
126
 */
(5-5/16)