Revision bbc9fae4
Added by koszko over 1 year ago
| common/misc.js | ||
|---|---|---|
| 42 | 42 |
* proprietary program, I am not going to enforce this in court. |
| 43 | 43 |
*/ |
| 44 | 44 |
|
| 45 |
/* uint8_to_hex is a separate function used in cryptographic functions. */ |
|
| 45 |
/* |
|
| 46 |
* uint8_to_hex is a separate function used in cryptographic functions and when |
|
| 47 |
* dealing with binary data. |
|
| 48 |
*/ |
|
| 46 | 49 |
const uint8_to_hex = |
| 47 | 50 |
array => [...array].map(b => ("0" + b.toString(16)).slice(-2)).join("");
|
| 51 |
#EXPORT uint8_to_hex |
|
| 48 | 52 |
|
| 49 | 53 |
/* |
| 50 | 54 |
* Asynchronously compute hex string representation of a sha256 digest of a |
| ... | ... | |
| 61 | 65 |
* Generate a unique value that can be computed synchronously and is impossible |
| 62 | 66 |
* to guess for a malicious website. |
| 63 | 67 |
*/ |
| 64 |
function gen_nonce(length=16) |
|
| 65 |
{
|
|
| 68 |
function gen_nonce(length=16) {
|
|
| 66 | 69 |
const random_data = new Uint8Array(length); |
| 67 | 70 |
crypto.getRandomValues(random_data); |
| 68 | 71 |
return uint8_to_hex(random_data); |
| ... | ... | |
| 71 | 74 |
|
| 72 | 75 |
/* Check if some HTTP header might define CSP rules. */ |
| 73 | 76 |
const csp_header_regex = |
| 74 |
/^\s*(content-security-policy|x-webkit-csp|x-content-security-policy)/i; |
|
| 77 |
/^\s*(content-security-policy|x-webkit-csp|x-content-security-policy)\s*$/i;
|
|
| 75 | 78 |
#EXPORT csp_header_regex |
| 76 | 79 |
|
| 77 |
/* |
|
| 78 |
* Print item together with type, e.g. |
|
| 79 |
* nice_name("s", "hello") → "hello (script)"
|
|
| 80 |
*/ |
|
| 81 |
#EXPORT (prefix, name) => `${name} (${TYPE_NAME[prefix]})` AS nice_name
|
|
| 82 |
|
|
| 83 | 80 |
/* |
| 84 | 81 |
* Check if url corresponds to a browser's special page (or a directory index in |
| 85 | 82 |
* case of `file://' protocol). |
| ... | ... | |
| 90 | 87 |
const priv_reg = /^chrome(-extension)?:\/\/|^about:|^view-source:|^file:\/\/[^?#]*\/([?#]|$)/; |
| 91 | 88 |
#ENDIF |
| 92 | 89 |
#EXPORT url => priv_reg.test(url) AS is_privileged_url |
| 90 |
|
|
| 91 |
/* Make it possible to serialize en Error object. */ |
|
| 92 |
function error_data_jsonifiable(error) {
|
|
| 93 |
const jsonifiable = {};
|
|
| 94 |
for (const property of ["name", "message", "fileName", "lineNumber"]) |
|
| 95 |
jsonifiable[property] = error[property]; |
|
| 96 |
|
|
| 97 |
return jsonifiable; |
|
| 98 |
} |
|
| 99 |
#EXPORT error_data_jsonifiable |
|
Also available in: Unified diff
serialize and deserialize entire Response object when relaying fetch() calls to other contexts using sendMessage