@sveltejs/kit 1.0.0-next.98 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/package.json +91 -77
- package/postinstall.js +47 -0
- package/src/cli.js +44 -0
- package/src/constants.js +5 -0
- package/src/core/adapt/builder.js +221 -0
- package/src/core/adapt/index.js +31 -0
- package/src/core/config/default-error.html +56 -0
- package/src/core/config/index.js +100 -0
- package/src/core/config/options.js +387 -0
- package/src/core/config/types.d.ts +1 -0
- package/src/core/env.js +138 -0
- package/src/core/generate_manifest/index.js +116 -0
- package/src/core/prerender/crawl.js +207 -0
- package/src/core/prerender/entities.js +2252 -0
- package/src/core/prerender/fallback.js +43 -0
- package/src/core/prerender/prerender.js +459 -0
- package/src/core/prerender/queue.js +80 -0
- package/src/core/sync/create_manifest_data/conflict.js +0 -0
- package/src/core/sync/create_manifest_data/index.js +523 -0
- package/src/core/sync/create_manifest_data/sort.js +161 -0
- package/src/core/sync/create_manifest_data/types.d.ts +37 -0
- package/src/core/sync/sync.js +59 -0
- package/src/core/sync/utils.js +33 -0
- package/src/core/sync/write_ambient.js +58 -0
- package/src/core/sync/write_client_manifest.js +107 -0
- package/src/core/sync/write_matchers.js +25 -0
- package/src/core/sync/write_root.js +91 -0
- package/src/core/sync/write_tsconfig.js +195 -0
- package/src/core/sync/write_types/index.js +809 -0
- package/src/core/utils.js +67 -0
- package/src/exports/hooks/index.js +1 -0
- package/src/exports/hooks/sequence.js +44 -0
- package/src/exports/index.js +55 -0
- package/src/exports/node/index.js +172 -0
- package/src/exports/node/polyfills.js +28 -0
- package/src/exports/vite/build/build_server.js +359 -0
- package/src/exports/vite/build/build_service_worker.js +85 -0
- package/src/exports/vite/build/utils.js +230 -0
- package/src/exports/vite/dev/index.js +597 -0
- package/src/exports/vite/graph_analysis/index.js +99 -0
- package/src/exports/vite/graph_analysis/types.d.ts +5 -0
- package/src/exports/vite/graph_analysis/utils.js +6 -0
- package/src/exports/vite/index.js +708 -0
- package/src/exports/vite/preview/index.js +194 -0
- package/src/exports/vite/types.d.ts +3 -0
- package/src/exports/vite/utils.js +184 -0
- package/src/runtime/app/env.js +1 -0
- package/src/runtime/app/environment.js +13 -0
- package/src/runtime/app/forms.js +135 -0
- package/src/runtime/app/navigation.js +22 -0
- package/src/runtime/app/paths.js +1 -0
- package/src/runtime/app/stores.js +57 -0
- package/src/runtime/client/ambient.d.ts +30 -0
- package/src/runtime/client/client.js +1725 -0
- package/src/runtime/client/constants.js +10 -0
- package/src/runtime/client/fetcher.js +127 -0
- package/src/runtime/client/parse.js +60 -0
- package/src/runtime/client/singletons.js +21 -0
- package/src/runtime/client/start.js +45 -0
- package/src/runtime/client/types.d.ts +86 -0
- package/src/runtime/client/utils.js +257 -0
- package/src/runtime/components/error.svelte +6 -0
- package/{assets → src/runtime}/components/layout.svelte +0 -0
- package/src/runtime/control.js +45 -0
- package/src/runtime/env/dynamic/private.js +1 -0
- package/src/runtime/env/dynamic/public.js +1 -0
- package/src/runtime/env-private.js +6 -0
- package/src/runtime/env-public.js +6 -0
- package/src/runtime/env.js +12 -0
- package/src/runtime/hash.js +20 -0
- package/src/runtime/paths.js +11 -0
- package/src/runtime/server/cookie.js +228 -0
- package/src/runtime/server/data/index.js +158 -0
- package/src/runtime/server/endpoint.js +86 -0
- package/src/runtime/server/fetch.js +175 -0
- package/src/runtime/server/index.js +405 -0
- package/src/runtime/server/page/actions.js +267 -0
- package/src/runtime/server/page/crypto.js +239 -0
- package/src/runtime/server/page/csp.js +250 -0
- package/src/runtime/server/page/index.js +326 -0
- package/src/runtime/server/page/load_data.js +270 -0
- package/src/runtime/server/page/render.js +393 -0
- package/src/runtime/server/page/respond_with_error.js +103 -0
- package/src/runtime/server/page/serialize_data.js +87 -0
- package/src/runtime/server/page/types.d.ts +35 -0
- package/src/runtime/server/utils.js +179 -0
- package/src/utils/array.js +9 -0
- package/src/utils/error.js +22 -0
- package/src/utils/escape.js +46 -0
- package/src/utils/exports.js +54 -0
- package/src/utils/filesystem.js +178 -0
- package/src/utils/functions.js +16 -0
- package/src/utils/http.js +72 -0
- package/src/utils/misc.js +1 -0
- package/src/utils/promises.js +17 -0
- package/src/utils/routing.js +201 -0
- package/src/utils/unit_test.js +11 -0
- package/src/utils/url.js +161 -0
- package/svelte-kit.js +1 -1
- package/types/ambient.d.ts +451 -0
- package/types/index.d.ts +1168 -5
- package/types/internal.d.ts +348 -160
- package/types/private.d.ts +237 -0
- package/types/synthetic/$env+dynamic+private.md +10 -0
- package/types/synthetic/$env+dynamic+public.md +8 -0
- package/types/synthetic/$env+static+private.md +19 -0
- package/types/synthetic/$env+static+public.md +7 -0
- package/types/synthetic/$lib.md +5 -0
- package/CHANGELOG.md +0 -819
- package/assets/components/error.svelte +0 -21
- package/assets/runtime/app/env.js +0 -16
- package/assets/runtime/app/navigation.js +0 -53
- package/assets/runtime/app/paths.js +0 -1
- package/assets/runtime/app/stores.js +0 -87
- package/assets/runtime/chunks/utils.js +0 -13
- package/assets/runtime/env.js +0 -8
- package/assets/runtime/internal/singletons.js +0 -20
- package/assets/runtime/internal/start.js +0 -1061
- package/assets/runtime/paths.js +0 -12
- package/dist/chunks/_commonjsHelpers.js +0 -8
- package/dist/chunks/cert.js +0 -29079
- package/dist/chunks/constants.js +0 -3
- package/dist/chunks/index.js +0 -3526
- package/dist/chunks/index2.js +0 -583
- package/dist/chunks/index3.js +0 -31
- package/dist/chunks/index4.js +0 -1005
- package/dist/chunks/index5.js +0 -327
- package/dist/chunks/index6.js +0 -325
- package/dist/chunks/standard.js +0 -99
- package/dist/chunks/utils.js +0 -149
- package/dist/cli.js +0 -711
- package/dist/http.js +0 -66
- package/dist/install-fetch.js +0 -1699
- package/dist/ssr.js +0 -1529
- package/types/ambient-modules.d.ts +0 -115
- package/types/config.d.ts +0 -101
- package/types/endpoint.d.ts +0 -23
- package/types/helper.d.ts +0 -19
- package/types/hooks.d.ts +0 -23
- package/types/page.d.ts +0 -30
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
const encoder = new TextEncoder();
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* SHA-256 hashing function adapted from https://bitwiseshiftleft.github.io/sjcl
|
|
5
|
+
* modified and redistributed under BSD license
|
|
6
|
+
* @param {string} data
|
|
7
|
+
*/
|
|
8
|
+
export function sha256(data) {
|
|
9
|
+
if (!key[0]) precompute();
|
|
10
|
+
|
|
11
|
+
const out = init.slice(0);
|
|
12
|
+
const array = encode(data);
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < array.length; i += 16) {
|
|
15
|
+
const w = array.subarray(i, i + 16);
|
|
16
|
+
|
|
17
|
+
let tmp;
|
|
18
|
+
let a;
|
|
19
|
+
let b;
|
|
20
|
+
|
|
21
|
+
let out0 = out[0];
|
|
22
|
+
let out1 = out[1];
|
|
23
|
+
let out2 = out[2];
|
|
24
|
+
let out3 = out[3];
|
|
25
|
+
let out4 = out[4];
|
|
26
|
+
let out5 = out[5];
|
|
27
|
+
let out6 = out[6];
|
|
28
|
+
let out7 = out[7];
|
|
29
|
+
|
|
30
|
+
/* Rationale for placement of |0 :
|
|
31
|
+
* If a value can overflow is original 32 bits by a factor of more than a few
|
|
32
|
+
* million (2^23 ish), there is a possibility that it might overflow the
|
|
33
|
+
* 53-bit mantissa and lose precision.
|
|
34
|
+
*
|
|
35
|
+
* To avoid this, we clamp back to 32 bits by |'ing with 0 on any value that
|
|
36
|
+
* propagates around the loop, and on the hash state out[]. I don't believe
|
|
37
|
+
* that the clamps on out4 and on out0 are strictly necessary, but it's close
|
|
38
|
+
* (for out4 anyway), and better safe than sorry.
|
|
39
|
+
*
|
|
40
|
+
* The clamps on out[] are necessary for the output to be correct even in the
|
|
41
|
+
* common case and for short inputs.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
for (let i = 0; i < 64; i++) {
|
|
45
|
+
// load up the input word for this round
|
|
46
|
+
|
|
47
|
+
if (i < 16) {
|
|
48
|
+
tmp = w[i];
|
|
49
|
+
} else {
|
|
50
|
+
a = w[(i + 1) & 15];
|
|
51
|
+
|
|
52
|
+
b = w[(i + 14) & 15];
|
|
53
|
+
|
|
54
|
+
tmp = w[i & 15] =
|
|
55
|
+
(((a >>> 7) ^ (a >>> 18) ^ (a >>> 3) ^ (a << 25) ^ (a << 14)) +
|
|
56
|
+
((b >>> 17) ^ (b >>> 19) ^ (b >>> 10) ^ (b << 15) ^ (b << 13)) +
|
|
57
|
+
w[i & 15] +
|
|
58
|
+
w[(i + 9) & 15]) |
|
|
59
|
+
0;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
tmp =
|
|
63
|
+
tmp +
|
|
64
|
+
out7 +
|
|
65
|
+
((out4 >>> 6) ^ (out4 >>> 11) ^ (out4 >>> 25) ^ (out4 << 26) ^ (out4 << 21) ^ (out4 << 7)) +
|
|
66
|
+
(out6 ^ (out4 & (out5 ^ out6))) +
|
|
67
|
+
key[i]; // | 0;
|
|
68
|
+
|
|
69
|
+
// shift register
|
|
70
|
+
out7 = out6;
|
|
71
|
+
out6 = out5;
|
|
72
|
+
out5 = out4;
|
|
73
|
+
|
|
74
|
+
out4 = (out3 + tmp) | 0;
|
|
75
|
+
|
|
76
|
+
out3 = out2;
|
|
77
|
+
out2 = out1;
|
|
78
|
+
out1 = out0;
|
|
79
|
+
|
|
80
|
+
out0 =
|
|
81
|
+
(tmp +
|
|
82
|
+
((out1 & out2) ^ (out3 & (out1 ^ out2))) +
|
|
83
|
+
((out1 >>> 2) ^
|
|
84
|
+
(out1 >>> 13) ^
|
|
85
|
+
(out1 >>> 22) ^
|
|
86
|
+
(out1 << 30) ^
|
|
87
|
+
(out1 << 19) ^
|
|
88
|
+
(out1 << 10))) |
|
|
89
|
+
0;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
out[0] = (out[0] + out0) | 0;
|
|
93
|
+
out[1] = (out[1] + out1) | 0;
|
|
94
|
+
out[2] = (out[2] + out2) | 0;
|
|
95
|
+
out[3] = (out[3] + out3) | 0;
|
|
96
|
+
out[4] = (out[4] + out4) | 0;
|
|
97
|
+
out[5] = (out[5] + out5) | 0;
|
|
98
|
+
out[6] = (out[6] + out6) | 0;
|
|
99
|
+
out[7] = (out[7] + out7) | 0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const bytes = new Uint8Array(out.buffer);
|
|
103
|
+
reverse_endianness(bytes);
|
|
104
|
+
|
|
105
|
+
return base64(bytes);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The SHA-256 initialization vector */
|
|
109
|
+
const init = new Uint32Array(8);
|
|
110
|
+
|
|
111
|
+
/** The SHA-256 hash key */
|
|
112
|
+
const key = new Uint32Array(64);
|
|
113
|
+
|
|
114
|
+
/** Function to precompute init and key. */
|
|
115
|
+
function precompute() {
|
|
116
|
+
/** @param {number} x */
|
|
117
|
+
function frac(x) {
|
|
118
|
+
return (x - Math.floor(x)) * 0x100000000;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let prime = 2;
|
|
122
|
+
|
|
123
|
+
for (let i = 0; i < 64; prime++) {
|
|
124
|
+
let is_prime = true;
|
|
125
|
+
|
|
126
|
+
for (let factor = 2; factor * factor <= prime; factor++) {
|
|
127
|
+
if (prime % factor === 0) {
|
|
128
|
+
is_prime = false;
|
|
129
|
+
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (is_prime) {
|
|
135
|
+
if (i < 8) {
|
|
136
|
+
init[i] = frac(prime ** (1 / 2));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
key[i] = frac(prime ** (1 / 3));
|
|
140
|
+
|
|
141
|
+
i++;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** @param {Uint8Array} bytes */
|
|
147
|
+
function reverse_endianness(bytes) {
|
|
148
|
+
for (let i = 0; i < bytes.length; i += 4) {
|
|
149
|
+
const a = bytes[i + 0];
|
|
150
|
+
const b = bytes[i + 1];
|
|
151
|
+
const c = bytes[i + 2];
|
|
152
|
+
const d = bytes[i + 3];
|
|
153
|
+
|
|
154
|
+
bytes[i + 0] = d;
|
|
155
|
+
bytes[i + 1] = c;
|
|
156
|
+
bytes[i + 2] = b;
|
|
157
|
+
bytes[i + 3] = a;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** @param {string} str */
|
|
162
|
+
function encode(str) {
|
|
163
|
+
const encoded = encoder.encode(str);
|
|
164
|
+
const length = encoded.length * 8;
|
|
165
|
+
|
|
166
|
+
// result should be a multiple of 512 bits in length,
|
|
167
|
+
// with room for a 1 (after the data) and two 32-bit
|
|
168
|
+
// words containing the original input bit length
|
|
169
|
+
const size = 512 * Math.ceil((length + 65) / 512);
|
|
170
|
+
const bytes = new Uint8Array(size / 8);
|
|
171
|
+
bytes.set(encoded);
|
|
172
|
+
|
|
173
|
+
// append a 1
|
|
174
|
+
bytes[encoded.length] = 0b10000000;
|
|
175
|
+
|
|
176
|
+
reverse_endianness(bytes);
|
|
177
|
+
|
|
178
|
+
// add the input bit length
|
|
179
|
+
const words = new Uint32Array(bytes.buffer);
|
|
180
|
+
words[words.length - 2] = Math.floor(length / 0x100000000); // this will always be zero for us
|
|
181
|
+
words[words.length - 1] = length;
|
|
182
|
+
|
|
183
|
+
return words;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/*
|
|
187
|
+
Based on https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
|
|
188
|
+
|
|
189
|
+
MIT License
|
|
190
|
+
Copyright (c) 2020 Egor Nepomnyaschih
|
|
191
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
192
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
193
|
+
in the Software without restriction, including without limitation the rights
|
|
194
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
195
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
196
|
+
furnished to do so, subject to the following conditions:
|
|
197
|
+
The above copyright notice and this permission notice shall be included in all
|
|
198
|
+
copies or substantial portions of the Software.
|
|
199
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
200
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
201
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
202
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
203
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
204
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
205
|
+
SOFTWARE.
|
|
206
|
+
*/
|
|
207
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
|
|
208
|
+
|
|
209
|
+
/** @param {Uint8Array} bytes */
|
|
210
|
+
export function base64(bytes) {
|
|
211
|
+
const l = bytes.length;
|
|
212
|
+
|
|
213
|
+
let result = '';
|
|
214
|
+
let i;
|
|
215
|
+
|
|
216
|
+
for (i = 2; i < l; i += 3) {
|
|
217
|
+
result += chars[bytes[i - 2] >> 2];
|
|
218
|
+
result += chars[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
|
|
219
|
+
result += chars[((bytes[i - 1] & 0x0f) << 2) | (bytes[i] >> 6)];
|
|
220
|
+
result += chars[bytes[i] & 0x3f];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (i === l + 1) {
|
|
224
|
+
// 1 octet yet to write
|
|
225
|
+
result += chars[bytes[i - 2] >> 2];
|
|
226
|
+
result += chars[(bytes[i - 2] & 0x03) << 4];
|
|
227
|
+
result += '==';
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (i === l) {
|
|
231
|
+
// 2 octets yet to write
|
|
232
|
+
result += chars[bytes[i - 2] >> 2];
|
|
233
|
+
result += chars[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
|
|
234
|
+
result += chars[(bytes[i - 1] & 0x0f) << 2];
|
|
235
|
+
result += '=';
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return result;
|
|
239
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { escape_html_attr } from '../../../utils/escape.js';
|
|
2
|
+
import { base64, sha256 } from './crypto.js';
|
|
3
|
+
|
|
4
|
+
const array = new Uint8Array(16);
|
|
5
|
+
|
|
6
|
+
function generate_nonce() {
|
|
7
|
+
crypto.getRandomValues(array);
|
|
8
|
+
return base64(array);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const quoted = new Set([
|
|
12
|
+
'self',
|
|
13
|
+
'unsafe-eval',
|
|
14
|
+
'unsafe-hashes',
|
|
15
|
+
'unsafe-inline',
|
|
16
|
+
'none',
|
|
17
|
+
'strict-dynamic',
|
|
18
|
+
'report-sample',
|
|
19
|
+
'wasm-unsafe-eval'
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
const crypto_pattern = /^(nonce|sha\d\d\d)-/;
|
|
23
|
+
|
|
24
|
+
// CSP and CSP Report Only are extremely similar with a few caveats
|
|
25
|
+
// the easiest/DRYest way to express this is with some private encapsulation
|
|
26
|
+
class BaseProvider {
|
|
27
|
+
/** @type {boolean} */
|
|
28
|
+
#use_hashes;
|
|
29
|
+
|
|
30
|
+
/** @type {boolean} */
|
|
31
|
+
#script_needs_csp;
|
|
32
|
+
|
|
33
|
+
/** @type {boolean} */
|
|
34
|
+
#style_needs_csp;
|
|
35
|
+
|
|
36
|
+
/** @type {import('types').CspDirectives} */
|
|
37
|
+
#directives;
|
|
38
|
+
|
|
39
|
+
/** @type {import('types').Csp.Source[]} */
|
|
40
|
+
#script_src;
|
|
41
|
+
|
|
42
|
+
/** @type {import('types').Csp.Source[]} */
|
|
43
|
+
#style_src;
|
|
44
|
+
|
|
45
|
+
/** @type {string} */
|
|
46
|
+
#nonce;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @param {boolean} use_hashes
|
|
50
|
+
* @param {import('types').CspDirectives} directives
|
|
51
|
+
* @param {string} nonce
|
|
52
|
+
* @param {boolean} dev
|
|
53
|
+
*/
|
|
54
|
+
constructor(use_hashes, directives, nonce, dev) {
|
|
55
|
+
this.#use_hashes = use_hashes;
|
|
56
|
+
this.#directives = dev ? { ...directives } : directives; // clone in dev so we can safely mutate
|
|
57
|
+
|
|
58
|
+
const d = this.#directives;
|
|
59
|
+
|
|
60
|
+
if (dev) {
|
|
61
|
+
// remove strict-dynamic in dev...
|
|
62
|
+
// TODO reinstate this if we can figure out how to make strict-dynamic work
|
|
63
|
+
// if (d['default-src']) {
|
|
64
|
+
// d['default-src'] = d['default-src'].filter((name) => name !== 'strict-dynamic');
|
|
65
|
+
// if (d['default-src'].length === 0) delete d['default-src'];
|
|
66
|
+
// }
|
|
67
|
+
|
|
68
|
+
// if (d['script-src']) {
|
|
69
|
+
// d['script-src'] = d['script-src'].filter((name) => name !== 'strict-dynamic');
|
|
70
|
+
// if (d['script-src'].length === 0) delete d['script-src'];
|
|
71
|
+
// }
|
|
72
|
+
|
|
73
|
+
const effective_style_src = d['style-src'] || d['default-src'];
|
|
74
|
+
|
|
75
|
+
// ...and add unsafe-inline so we can inject <style> elements
|
|
76
|
+
if (effective_style_src && !effective_style_src.includes('unsafe-inline')) {
|
|
77
|
+
d['style-src'] = [...effective_style_src, 'unsafe-inline'];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this.#script_src = [];
|
|
82
|
+
this.#style_src = [];
|
|
83
|
+
|
|
84
|
+
const effective_script_src = d['script-src'] || d['default-src'];
|
|
85
|
+
const effective_style_src = d['style-src'] || d['default-src'];
|
|
86
|
+
|
|
87
|
+
this.#script_needs_csp =
|
|
88
|
+
!!effective_script_src &&
|
|
89
|
+
effective_script_src.filter((value) => value !== 'unsafe-inline').length > 0;
|
|
90
|
+
|
|
91
|
+
this.#style_needs_csp =
|
|
92
|
+
!dev &&
|
|
93
|
+
!!effective_style_src &&
|
|
94
|
+
effective_style_src.filter((value) => value !== 'unsafe-inline').length > 0;
|
|
95
|
+
|
|
96
|
+
this.script_needs_nonce = this.#script_needs_csp && !this.#use_hashes;
|
|
97
|
+
this.style_needs_nonce = this.#style_needs_csp && !this.#use_hashes;
|
|
98
|
+
this.#nonce = nonce;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** @param {string} content */
|
|
102
|
+
add_script(content) {
|
|
103
|
+
if (this.#script_needs_csp) {
|
|
104
|
+
if (this.#use_hashes) {
|
|
105
|
+
this.#script_src.push(`sha256-${sha256(content)}`);
|
|
106
|
+
} else if (this.#script_src.length === 0) {
|
|
107
|
+
this.#script_src.push(`nonce-${this.#nonce}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** @param {string} content */
|
|
113
|
+
add_style(content) {
|
|
114
|
+
if (this.#style_needs_csp) {
|
|
115
|
+
if (this.#use_hashes) {
|
|
116
|
+
this.#style_src.push(`sha256-${sha256(content)}`);
|
|
117
|
+
} else if (this.#style_src.length === 0) {
|
|
118
|
+
this.#style_src.push(`nonce-${this.#nonce}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @param {boolean} [is_meta]
|
|
125
|
+
*/
|
|
126
|
+
get_header(is_meta = false) {
|
|
127
|
+
const header = [];
|
|
128
|
+
|
|
129
|
+
// due to browser inconsistencies, we can't append sources to default-src
|
|
130
|
+
// (specifically, Firefox appears to not ignore nonce-{nonce} directives
|
|
131
|
+
// on default-src), so we ensure that script-src and style-src exist
|
|
132
|
+
|
|
133
|
+
const directives = { ...this.#directives };
|
|
134
|
+
|
|
135
|
+
if (this.#style_src.length > 0) {
|
|
136
|
+
directives['style-src'] = [
|
|
137
|
+
...(directives['style-src'] || directives['default-src'] || []),
|
|
138
|
+
...this.#style_src
|
|
139
|
+
];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (this.#script_src.length > 0) {
|
|
143
|
+
directives['script-src'] = [
|
|
144
|
+
...(directives['script-src'] || directives['default-src'] || []),
|
|
145
|
+
...this.#script_src
|
|
146
|
+
];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
for (const key in directives) {
|
|
150
|
+
if (is_meta && (key === 'frame-ancestors' || key === 'report-uri' || key === 'sandbox')) {
|
|
151
|
+
// these values cannot be used with a <meta> tag
|
|
152
|
+
// TODO warn?
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// @ts-expect-error gimme a break typescript, `key` is obviously a member of internal_directives
|
|
157
|
+
const value = /** @type {string[] | true} */ (directives[key]);
|
|
158
|
+
|
|
159
|
+
if (!value) continue;
|
|
160
|
+
|
|
161
|
+
const directive = [key];
|
|
162
|
+
if (Array.isArray(value)) {
|
|
163
|
+
value.forEach((value) => {
|
|
164
|
+
if (quoted.has(value) || crypto_pattern.test(value)) {
|
|
165
|
+
directive.push(`'${value}'`);
|
|
166
|
+
} else {
|
|
167
|
+
directive.push(value);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
header.push(directive.join(' '));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return header.join('; ');
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
class CspProvider extends BaseProvider {
|
|
180
|
+
get_meta() {
|
|
181
|
+
const content = escape_html_attr(this.get_header(true));
|
|
182
|
+
return `<meta http-equiv="content-security-policy" content=${content}>`;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
class CspReportOnlyProvider extends BaseProvider {
|
|
187
|
+
/**
|
|
188
|
+
* @param {boolean} use_hashes
|
|
189
|
+
* @param {import('types').CspDirectives} directives
|
|
190
|
+
* @param {string} nonce
|
|
191
|
+
* @param {boolean} dev
|
|
192
|
+
*/
|
|
193
|
+
constructor(use_hashes, directives, nonce, dev) {
|
|
194
|
+
super(use_hashes, directives, nonce, dev);
|
|
195
|
+
|
|
196
|
+
if (Object.values(directives).filter((v) => !!v).length > 0) {
|
|
197
|
+
// If we're generating content-security-policy-report-only,
|
|
198
|
+
// if there are any directives, we need a report-uri or report-to (or both)
|
|
199
|
+
// else it's just an expensive noop.
|
|
200
|
+
const has_report_to = directives['report-to']?.length ?? 0 > 0;
|
|
201
|
+
const has_report_uri = directives['report-uri']?.length ?? 0 > 0;
|
|
202
|
+
if (!has_report_to && !has_report_uri) {
|
|
203
|
+
throw Error(
|
|
204
|
+
'`content-security-policy-report-only` must be specified with either the `report-to` or `report-uri` directives, or both'
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export class Csp {
|
|
212
|
+
/** @readonly */
|
|
213
|
+
nonce = generate_nonce();
|
|
214
|
+
|
|
215
|
+
/** @type {CspProvider} */
|
|
216
|
+
csp_provider;
|
|
217
|
+
|
|
218
|
+
/** @type {CspReportOnlyProvider} */
|
|
219
|
+
report_only_provider;
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* @param {import('./types').CspConfig} config
|
|
223
|
+
* @param {import('./types').CspOpts} opts
|
|
224
|
+
*/
|
|
225
|
+
constructor({ mode, directives, reportOnly }, { prerender, dev }) {
|
|
226
|
+
const use_hashes = mode === 'hash' || (mode === 'auto' && prerender);
|
|
227
|
+
this.csp_provider = new CspProvider(use_hashes, directives, this.nonce, dev);
|
|
228
|
+
this.report_only_provider = new CspReportOnlyProvider(use_hashes, reportOnly, this.nonce, dev);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
get script_needs_nonce() {
|
|
232
|
+
return this.csp_provider.script_needs_nonce || this.report_only_provider.script_needs_nonce;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
get style_needs_nonce() {
|
|
236
|
+
return this.csp_provider.style_needs_nonce || this.report_only_provider.style_needs_nonce;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** @param {string} content */
|
|
240
|
+
add_script(content) {
|
|
241
|
+
this.csp_provider.add_script(content);
|
|
242
|
+
this.report_only_provider.add_script(content);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** @param {string} content */
|
|
246
|
+
add_style(content) {
|
|
247
|
+
this.csp_provider.add_style(content);
|
|
248
|
+
this.report_only_provider.add_style(content);
|
|
249
|
+
}
|
|
250
|
+
}
|