@johnhenry/sitefile 0.0.2 → 0.0.4
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/CHANGELOG.md +516 -0
- package/README.md +59 -9
- package/lib/src/archive-writers.d.ts +12 -2
- package/lib/src/archive-writers.js +53 -10
- package/lib/src/bake.d.ts +2 -0
- package/lib/src/bake.js +30 -1
- package/lib/src/bcrypt.d.ts +25 -0
- package/lib/src/bcrypt.js +201 -0
- package/lib/src/canonical.d.ts +19 -0
- package/lib/src/canonical.js +60 -0
- package/lib/src/car.d.ts +34 -0
- package/lib/src/car.js +203 -0
- package/lib/src/checklinks.js +8 -2
- package/lib/src/cli/bake.js +4 -3
- package/lib/src/cli/build-image.d.ts +1 -0
- package/lib/src/cli/build-image.js +1 -0
- package/lib/src/cli/common.js +4 -1
- package/lib/src/cli/hosts.d.ts +5 -0
- package/lib/src/cli/hosts.js +78 -0
- package/lib/src/cli/images.js +23 -5
- package/lib/src/cli/import-config.d.ts +2 -0
- package/lib/src/cli/import-config.js +56 -0
- package/lib/src/cli/index.d.ts +6 -1
- package/lib/src/cli/index.js +34 -2
- package/lib/src/cli/integrity.d.ts +2 -0
- package/lib/src/cli/integrity.js +77 -0
- package/lib/src/cli/network.d.ts +7 -0
- package/lib/src/cli/network.js +14 -3
- package/lib/src/cli/serve-keys.d.ts +39 -0
- package/lib/src/cli/serve-keys.js +106 -0
- package/lib/src/cli/serve-worker.js +119 -8
- package/lib/src/cli/servers.d.ts +130 -1
- package/lib/src/cli/servers.js +754 -72
- package/lib/src/cli/up.d.ts +2 -0
- package/lib/src/cli/up.js +193 -0
- package/lib/src/cli/watch.d.ts +14 -1
- package/lib/src/cli/watch.js +20 -3
- package/lib/src/cli.js +138 -18
- package/lib/src/csp.d.ts +17 -0
- package/lib/src/csp.js +52 -0
- package/lib/src/dictionary.d.ts +16 -0
- package/lib/src/dictionary.js +89 -0
- package/lib/src/epub.d.ts +5 -0
- package/lib/src/epub.js +113 -0
- package/lib/src/errors.d.ts +22 -9
- package/lib/src/errors.js +20 -1
- package/lib/src/executor.d.ts +31 -1
- package/lib/src/executor.js +231 -24
- package/lib/src/explain.js +33 -3
- package/lib/src/export-for.d.ts +42 -0
- package/lib/src/export-for.js +466 -0
- package/lib/src/export-formats.d.ts +2 -2
- package/lib/src/export-formats.js +52 -2
- package/lib/src/fingerprint.d.ts +3 -1
- package/lib/src/fingerprint.js +32 -5
- package/lib/src/host-config.d.ts +34 -0
- package/lib/src/host-config.js +645 -0
- package/lib/src/hosts.d.ts +47 -0
- package/lib/src/hosts.js +106 -0
- package/lib/src/hreflang.d.ts +25 -0
- package/lib/src/hreflang.js +72 -0
- package/lib/src/index.d.ts +1 -1
- package/lib/src/index.js +1 -1
- package/lib/src/integrity.d.ts +115 -0
- package/lib/src/integrity.js +273 -0
- package/lib/src/language/ast.d.ts +126 -3
- package/lib/src/language/ast.js +7 -1
- package/lib/src/language/parser.js +189 -7
- package/lib/src/language/validate.js +24 -0
- package/lib/src/links.d.ts +47 -0
- package/lib/src/links.js +289 -3
- package/lib/src/lint.d.ts +1 -1
- package/lib/src/lint.js +26 -1
- package/lib/src/mime-data.d.ts +2 -0
- package/lib/src/mime-data.js +92 -0
- package/lib/src/oci.d.ts +20 -2
- package/lib/src/oci.js +70 -28
- package/lib/src/outdated.d.ts +3 -0
- package/lib/src/outdated.js +4 -4
- package/lib/src/planner.d.ts +87 -2
- package/lib/src/planner.js +189 -8
- package/lib/src/plugins/cache.d.ts +11 -0
- package/lib/src/plugins/cache.js +29 -0
- package/lib/src/plugins/host.d.ts +51 -2
- package/lib/src/plugins/host.js +141 -16
- package/lib/src/plugins/wasm.js +1 -3
- package/lib/src/portable.d.ts +8 -0
- package/lib/src/portable.js +47 -0
- package/lib/src/precache.d.ts +33 -0
- package/lib/src/precache.js +75 -0
- package/lib/src/precompress.d.ts +19 -0
- package/lib/src/precompress.js +35 -0
- package/lib/src/qr.d.ts +18 -0
- package/lib/src/qr.js +372 -0
- package/lib/src/registry-ops.js +4 -2
- package/lib/src/reload-client.d.ts +2 -0
- package/lib/src/reload-client.js +419 -0
- package/lib/src/resources/archive.d.ts +37 -1
- package/lib/src/resources/archive.js +353 -71
- package/lib/src/resources/convert.js +3 -1
- package/lib/src/resources/https.d.ts +9 -1
- package/lib/src/resources/https.js +36 -0
- package/lib/src/resources/network.d.ts +25 -3
- package/lib/src/resources/network.js +77 -6
- package/lib/src/secrets.d.ts +12 -3
- package/lib/src/secrets.js +18 -5
- package/lib/src/serve-auth.d.ts +33 -0
- package/lib/src/serve-auth.js +120 -0
- package/lib/src/serve-rules.d.ts +83 -7
- package/lib/src/serve-rules.js +264 -53
- package/lib/src/server.d.ts +529 -11
- package/lib/src/server.js +1439 -57
- package/lib/src/servers-file.d.ts +36 -0
- package/lib/src/servers-file.js +101 -0
- package/lib/src/servers.d.ts +115 -7
- package/lib/src/servers.js +151 -29
- package/lib/src/single-file.d.ts +12 -0
- package/lib/src/single-file.js +147 -0
- package/lib/src/site.d.ts +58 -0
- package/lib/src/site.js +216 -5
- package/lib/src/store.js +9 -5
- package/lib/src/swbn.d.ts +10 -0
- package/lib/src/swbn.js +86 -0
- package/lib/src/toml.d.ts +17 -0
- package/lib/src/toml.js +323 -0
- package/lib/src/wacz.d.ts +6 -0
- package/lib/src/wacz.js +96 -0
- package/lib/src/warc.d.ts +28 -0
- package/lib/src/warc.js +252 -0
- package/lib/src/watch.d.ts +12 -1
- package/lib/src/watch.js +67 -1
- package/lib/src/wbn.d.ts +9 -1
- package/lib/src/wbn.js +23 -33
- package/lib/src/zstd.d.ts +4 -0
- package/lib/src/zstd.js +328 -0
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isIP } from 'node:net';
|
|
2
|
-
import { SitefileError } from '../errors.js';
|
|
2
|
+
import { needsHost, needsSecretHost, SitefileError } from '../errors.js';
|
|
3
3
|
/** Split a list given as one string (commas and/or whitespace). */
|
|
4
4
|
export function splitList(raw) {
|
|
5
5
|
return (raw ?? '')
|
|
@@ -193,8 +193,57 @@ export function checkHostRules(url, network, shown, span) {
|
|
|
193
193
|
if (denied !== undefined) {
|
|
194
194
|
throw new SitefileError('E_HOST_BLOCKED', `refusing to connect to ${host}: it matches deny-hosts rule '${denied}' (${shown})`, span);
|
|
195
195
|
}
|
|
196
|
-
if ((network.allowHosts ?? []).length > 0 && firstMatch(host, port, network.allowHosts) === undefined) {
|
|
197
|
-
throw new SitefileError('E_HOST_BLOCKED', `refusing to connect to ${host}: it matches no allow-hosts rule (${shown})`, span
|
|
196
|
+
if ((network.strict || (network.allowHosts ?? []).length > 0) && firstMatch(host, port, network.allowHosts) === undefined) {
|
|
197
|
+
throw new SitefileError('E_HOST_BLOCKED', `refusing to connect to ${host}: it matches no allow-hosts rule${network.strict ? ' (strict network mode)' : ''} (${shown}); add --allow-hosts ${host}`, span, {
|
|
198
|
+
needs: needsHost(host),
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/** True when `host:port` may be contacted under `network`'s host rules (see {@link checkHostRules}). */
|
|
203
|
+
export function hostAllowed(host, port, network) {
|
|
204
|
+
if (!network)
|
|
205
|
+
return true;
|
|
206
|
+
const h = normalizeHost(host);
|
|
207
|
+
if (firstMatch(h, port, network.denyHosts) !== undefined)
|
|
208
|
+
return false;
|
|
209
|
+
return !(network.strict || (network.allowHosts ?? []).length > 0) || firstMatch(h, port, network.allowHosts) !== undefined;
|
|
210
|
+
}
|
|
211
|
+
/** Split `<id>=<pattern>`; undefined when malformed. */
|
|
212
|
+
function secretHostRule(raw) {
|
|
213
|
+
const eq = raw.indexOf('=');
|
|
214
|
+
if (eq <= 0)
|
|
215
|
+
return undefined;
|
|
216
|
+
const id = raw.slice(0, eq).trim();
|
|
217
|
+
const pattern = raw.slice(eq + 1).trim();
|
|
218
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(id) || !parsePattern(pattern))
|
|
219
|
+
return undefined;
|
|
220
|
+
return { id, pattern };
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Before an `IMPORT … SECRET` request to `target` (the import's own origin, the only one its secrets are
|
|
224
|
+
* sent to): in strict network mode each secret needs an `--allow-secret-host <id>=<pattern>` that matches
|
|
225
|
+
* the host. Throws E_HOST_BLOCKED with a `needs`. Nothing is checked outside strict mode, and a host the
|
|
226
|
+
* host rules refuse is left to the request itself (its E_HOST_BLOCKED asks for `--allow-hosts` first).
|
|
227
|
+
*/
|
|
228
|
+
export function checkSecretHosts(importName, secretIds, target, network, span) {
|
|
229
|
+
if (!network?.strict || secretIds.length === 0)
|
|
230
|
+
return;
|
|
231
|
+
let url;
|
|
232
|
+
try {
|
|
233
|
+
url = new URL(target);
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
return; // the request itself reports the bad URL
|
|
237
|
+
}
|
|
238
|
+
const host = normalizeHost(url.hostname);
|
|
239
|
+
const port = urlPort(url);
|
|
240
|
+
if (!hostAllowed(host, port, network))
|
|
241
|
+
return;
|
|
242
|
+
const rules = (network.secretHosts ?? []).map(secretHostRule).filter((r) => r !== undefined);
|
|
243
|
+
for (const id of secretIds) {
|
|
244
|
+
if (rules.some((r) => r.id === id && hostMatches(host, port, r.pattern)))
|
|
245
|
+
continue;
|
|
246
|
+
throw new SitefileError('E_HOST_BLOCKED', `IMPORT ${importName}: refusing to send secret '${id}' to ${host} (strict network mode); add --allow-secret-host ${id}=${host}`, span, { needs: needsSecretHost(id, host) });
|
|
198
247
|
}
|
|
199
248
|
}
|
|
200
249
|
/** The proxy to use for `url`, or undefined for a direct connection (no proxy, or a no-proxy match). */
|
|
@@ -226,6 +275,10 @@ export function networkProblem(network) {
|
|
|
226
275
|
return `invalid ${name} pattern '${p}': expected *, a host name (example.com, .example.com, *.example.com), an IP address or a CIDR network, optionally with :port`;
|
|
227
276
|
}
|
|
228
277
|
}
|
|
278
|
+
for (const r of network.secretHosts ?? []) {
|
|
279
|
+
if (!secretHostRule(r))
|
|
280
|
+
return `invalid --allow-secret-host '${r}': expected <secret id>=<host pattern> (a pattern as for --allow-hosts)`;
|
|
281
|
+
}
|
|
229
282
|
for (const [name, v] of [
|
|
230
283
|
['connect timeout', network.connectTimeoutMs],
|
|
231
284
|
['read timeout', network.readTimeoutMs],
|
|
@@ -263,22 +316,38 @@ export function parseFetchTimeout(raw) {
|
|
|
263
316
|
}
|
|
264
317
|
return out;
|
|
265
318
|
}
|
|
319
|
+
/** Parse a network mode (`open` or `strict`; empty: unset); `what` names the source in the E_USAGE message. */
|
|
320
|
+
export function parseNetworkMode(raw, what) {
|
|
321
|
+
const s = raw.trim();
|
|
322
|
+
if (s === '')
|
|
323
|
+
return undefined;
|
|
324
|
+
if (s === 'open')
|
|
325
|
+
return false;
|
|
326
|
+
if (s === 'strict')
|
|
327
|
+
return true;
|
|
328
|
+
throw new SitefileError('E_USAGE', `${what} expects open or strict, got '${raw}'`);
|
|
329
|
+
}
|
|
266
330
|
/**
|
|
267
331
|
* Network settings from `SITEFILE_PROXY`, `SITEFILE_NO_PROXY`, `SITEFILE_ALLOW_HOSTS`,
|
|
268
|
-
* `SITEFILE_DENY_HOSTS` (comma- or space-separated)
|
|
269
|
-
* `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` are never read.
|
|
332
|
+
* `SITEFILE_DENY_HOSTS` (comma- or space-separated), `SITEFILE_FETCH_TIMEOUT` and `SITEFILE_NETWORK`
|
|
333
|
+
* (`open` or `strict`). Empty values are unset. `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` are never read.
|
|
270
334
|
*/
|
|
271
335
|
export function networkFromEnv(env = process.env) {
|
|
272
336
|
const proxy = (env.SITEFILE_PROXY ?? '').trim();
|
|
337
|
+
const strict = parseNetworkMode(env.SITEFILE_NETWORK ?? '', 'SITEFILE_NETWORK');
|
|
273
338
|
return {
|
|
274
339
|
...(proxy !== '' ? { proxy } : {}),
|
|
340
|
+
...(strict ? { strict } : {}),
|
|
275
341
|
...(splitList(env.SITEFILE_NO_PROXY).length ? { noProxy: splitList(env.SITEFILE_NO_PROXY) } : {}),
|
|
276
342
|
...(splitList(env.SITEFILE_ALLOW_HOSTS).length ? { allowHosts: splitList(env.SITEFILE_ALLOW_HOSTS) } : {}),
|
|
277
343
|
...(splitList(env.SITEFILE_DENY_HOSTS).length ? { denyHosts: splitList(env.SITEFILE_DENY_HOSTS) } : {}),
|
|
278
344
|
...parseFetchTimeout(env.SITEFILE_FETCH_TIMEOUT ?? ''),
|
|
279
345
|
};
|
|
280
346
|
}
|
|
281
|
-
/**
|
|
347
|
+
/**
|
|
348
|
+
* `over` on top of `base`: the proxy and timeouts are replaced, the host lists are joined (duplicates
|
|
349
|
+
* dropped), and strict mode is on when either is strict.
|
|
350
|
+
*/
|
|
282
351
|
export function mergeNetwork(base, over) {
|
|
283
352
|
const join = (a, b) => {
|
|
284
353
|
const all = [...new Set([...(a ?? []), ...(b ?? [])])];
|
|
@@ -289,6 +358,8 @@ export function mergeNetwork(base, over) {
|
|
|
289
358
|
noProxy: join(base.noProxy, over.noProxy),
|
|
290
359
|
allowHosts: join(base.allowHosts, over.allowHosts),
|
|
291
360
|
denyHosts: join(base.denyHosts, over.denyHosts),
|
|
361
|
+
strict: base.strict || over.strict ? true : undefined,
|
|
362
|
+
secretHosts: join(base.secretHosts, over.secretHosts),
|
|
292
363
|
connectTimeoutMs: over.connectTimeoutMs ?? base.connectTimeoutMs,
|
|
293
364
|
readTimeoutMs: over.readTimeoutMs ?? base.readTimeoutMs,
|
|
294
365
|
};
|
package/lib/src/secrets.d.ts
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
|
+
import { type NetworkSettings } from './resources/network.js';
|
|
1
2
|
export type Secrets = ReadonlyMap<string, string>;
|
|
2
3
|
/**
|
|
3
4
|
* Parse `--secret` flags: `id=<id>,env=<VAR>` (an environment variable) or `id=<id>,src=<file>` (a
|
|
4
5
|
* file, relative to the working directory; one trailing newline is dropped). Values are checked to
|
|
5
|
-
* be
|
|
6
|
+
* be printable ASCII (tabs and line breaks allowed: a PEM key for EXPORT ... KEY); one used as an HTTP
|
|
7
|
+
* header (IMPORT ... SECRET) must have no line breaks. Messages never include a value.
|
|
6
8
|
*/
|
|
7
9
|
export declare function loadSecrets(flags: readonly string[], env?: NodeJS.ProcessEnv): Promise<Map<string, string>>;
|
|
8
10
|
/** Replace every occurrence of a secret value (longest first) with `***`. */
|
|
9
11
|
export declare function redactSecrets(text: string, secrets: Secrets | undefined): string;
|
|
10
12
|
/** A SitefileError whose message has the secrets redacted (the same error when nothing changes). */
|
|
11
13
|
export declare function redactError(e: unknown, secrets: Secrets | undefined): unknown;
|
|
12
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* The request headers of an import's SECRET clauses; a secret that was not given is E_SECRET. With
|
|
16
|
+
* `target` (the origin they are about to be sent to, and the network settings), strict network mode
|
|
17
|
+
* also requires an `--allow-secret-host` rule for each secret (E_HOST_BLOCKED, see checkSecretHosts).
|
|
18
|
+
*/
|
|
13
19
|
export declare function secretHeaders(spec: {
|
|
14
20
|
readonly name: string;
|
|
15
21
|
readonly secrets?: readonly {
|
|
@@ -18,4 +24,7 @@ export declare function secretHeaders(spec: {
|
|
|
18
24
|
readonly bearer: boolean;
|
|
19
25
|
}[];
|
|
20
26
|
readonly span: import('./errors.js').SourceSpan;
|
|
21
|
-
}, secrets: Secrets | undefined
|
|
27
|
+
}, secrets: Secrets | undefined, target?: {
|
|
28
|
+
readonly url: string;
|
|
29
|
+
readonly network: NetworkSettings | undefined;
|
|
30
|
+
}): Record<string, string> | undefined;
|
package/lib/src/secrets.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
2
|
import { SitefileError, isSitefileError } from './errors.js';
|
|
3
|
+
import { checkSecretHosts } from './resources/network.js';
|
|
3
4
|
/**
|
|
4
5
|
* Build secrets (issue #85), modelled on Docker's `--secret id=…,env=…|src=…`: values that remote
|
|
5
6
|
* IMPORTs send as HTTP request headers (`IMPORT … SECRET <id> AS HEADER <Name>` / `AS BEARER`).
|
|
@@ -12,13 +13,16 @@ import { SitefileError, isSitefileError } from './errors.js';
|
|
|
12
13
|
const ID_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
13
14
|
/** Visible ASCII, space and tab: what every HTTP client sends unchanged in a header value. */
|
|
14
15
|
const HEADER_VALUE_RE = /^[\t\x20-\x7e]+$/;
|
|
16
|
+
/** What a secret may hold: printable ASCII, tabs and line breaks (a PEM key for EXPORT ... KEY). */
|
|
17
|
+
const SECRET_VALUE_RE = /^[\t\n\r\x20-\x7e]+$/;
|
|
15
18
|
function usage(message) {
|
|
16
19
|
return new SitefileError('E_USAGE', message);
|
|
17
20
|
}
|
|
18
21
|
/**
|
|
19
22
|
* Parse `--secret` flags: `id=<id>,env=<VAR>` (an environment variable) or `id=<id>,src=<file>` (a
|
|
20
23
|
* file, relative to the working directory; one trailing newline is dropped). Values are checked to
|
|
21
|
-
* be
|
|
24
|
+
* be printable ASCII (tabs and line breaks allowed: a PEM key for EXPORT ... KEY); one used as an HTTP
|
|
25
|
+
* header (IMPORT ... SECRET) must have no line breaks. Messages never include a value.
|
|
22
26
|
*/
|
|
23
27
|
export async function loadSecrets(flags, env = process.env) {
|
|
24
28
|
const out = new Map();
|
|
@@ -62,8 +66,8 @@ export async function loadSecrets(flags, env = process.env) {
|
|
|
62
66
|
}
|
|
63
67
|
if (value === '')
|
|
64
68
|
throw new SitefileError('E_SECRET', `--secret id=${id} is empty`);
|
|
65
|
-
if (!
|
|
66
|
-
throw new SitefileError('E_SECRET', `--secret id=${id}
|
|
69
|
+
if (!SECRET_VALUE_RE.test(value)) {
|
|
70
|
+
throw new SitefileError('E_SECRET', `--secret id=${id} must be printable ASCII (spaces, tabs and line breaks allowed)`);
|
|
67
71
|
}
|
|
68
72
|
out.set(id, value);
|
|
69
73
|
}
|
|
@@ -86,8 +90,12 @@ export function redactError(e, secrets) {
|
|
|
86
90
|
const detail = redactSecrets(e.detail, secrets);
|
|
87
91
|
return detail === e.detail ? e : new SitefileError(e.code, detail, e.span, e.needs ? { needs: e.needs } : undefined);
|
|
88
92
|
}
|
|
89
|
-
/**
|
|
90
|
-
|
|
93
|
+
/**
|
|
94
|
+
* The request headers of an import's SECRET clauses; a secret that was not given is E_SECRET. With
|
|
95
|
+
* `target` (the origin they are about to be sent to, and the network settings), strict network mode
|
|
96
|
+
* also requires an `--allow-secret-host` rule for each secret (E_HOST_BLOCKED, see checkSecretHosts).
|
|
97
|
+
*/
|
|
98
|
+
export function secretHeaders(spec, secrets, target) {
|
|
91
99
|
if (!spec.secrets?.length)
|
|
92
100
|
return undefined;
|
|
93
101
|
const headers = {};
|
|
@@ -96,8 +104,13 @@ export function secretHeaders(spec, secrets) {
|
|
|
96
104
|
if (v === undefined) {
|
|
97
105
|
throw new SitefileError('E_SECRET', `IMPORT ${spec.name}: secret '${s.id}' was not given; pass --secret id=${s.id},env=<VAR> or --secret id=${s.id},src=<file>`, spec.span);
|
|
98
106
|
}
|
|
107
|
+
if (!HEADER_VALUE_RE.test(v)) {
|
|
108
|
+
throw new SitefileError('E_SECRET', `IMPORT ${spec.name}: secret '${s.id}' cannot be sent in an HTTP header (only printable ASCII, spaces and tabs)`, spec.span);
|
|
109
|
+
}
|
|
99
110
|
headers[s.header.toLowerCase()] = s.bearer ? `Bearer ${v}` : v;
|
|
100
111
|
}
|
|
112
|
+
if (target)
|
|
113
|
+
checkSecretHosts(spec.name, [...new Set(spec.secrets.map((s) => s.id))], target.url, target.network, spec.span);
|
|
101
114
|
return headers;
|
|
102
115
|
}
|
|
103
116
|
//# sourceMappingURL=secrets.js.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export interface AuthEntry {
|
|
2
|
+
readonly user: string;
|
|
3
|
+
readonly verifier: string;
|
|
4
|
+
}
|
|
5
|
+
/** PBKDF2 rounds of a verifier made here; checks are cached per server, so this is paid once per credential. */
|
|
6
|
+
export declare const AUTH_ITERATIONS = 20000;
|
|
7
|
+
/** `user:password` (`--auth`, `SITEFILE_SERVE_AUTH`): the user is non-empty and has no `:`; the password is the rest. */
|
|
8
|
+
export declare function parseAuthPair(value: string, what: string): {
|
|
9
|
+
user: string;
|
|
10
|
+
password: string;
|
|
11
|
+
};
|
|
12
|
+
/** A pbkdf2-sha256 verifier for `password` (a random salt unless one is given). */
|
|
13
|
+
export declare function makeVerifier(password: string, salt?: Uint8Array, iterations?: number): string;
|
|
14
|
+
/** Whether `password` matches `verifier` (false for a malformed verifier). Constant time in the key comparison. */
|
|
15
|
+
export declare function verifyPassword(verifier: string, password: string): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* An htpasswd file: `user:hash` lines (blank lines and `#` comments skipped). `{SHA}` (htpasswd -s) and bcrypt
|
|
18
|
+
* (`$2y$`, `$2a$`, `$2b$`, cost 4 to 31; htpasswd -B) entries are accepted; a malformed bcrypt hash, `$apr1$`,
|
|
19
|
+
* crypt and plain-text entries are `E_USAGE` errors naming the user.
|
|
20
|
+
*/
|
|
21
|
+
export declare function parseHtpasswd(text: string, origin: string): AuthEntry[];
|
|
22
|
+
/** The `user` and `password` of an `Authorization: Basic …` value, or undefined. */
|
|
23
|
+
export declare function basicCredentials(header: string | undefined): {
|
|
24
|
+
user: string;
|
|
25
|
+
password: string;
|
|
26
|
+
} | undefined;
|
|
27
|
+
/** Accepts `Authorization` values for a set of entries; good values are remembered (at most 64) so PBKDF2 runs once per credential. */
|
|
28
|
+
export declare class BasicAuth {
|
|
29
|
+
readonly entries: readonly AuthEntry[];
|
|
30
|
+
private readonly good;
|
|
31
|
+
constructor(entries: readonly AuthEntry[]);
|
|
32
|
+
allows(header: string | undefined): boolean;
|
|
33
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// HTTP basic authentication for `sitefile serve --auth` / `--auth-file` (johnhenry/sitefile#177).
|
|
2
|
+
// Ported as rust/src/serve_auth.rs (same verifier strings, same rules).
|
|
3
|
+
//
|
|
4
|
+
// A credential is kept only as a verifier, never as the password:
|
|
5
|
+
// pbkdf2-sha256$<iterations>$<salt base64>$<32-byte key base64> (what --auth and SITEFILE_SERVE_AUTH become)
|
|
6
|
+
// {SHA}<base64 SHA-1 of the password> (an htpasswd -s entry, --auth-file)
|
|
7
|
+
// $2y$<cost>$<salt><hash> (also $2a$, $2b$) (an htpasswd -B bcrypt entry, --auth-file; #278)
|
|
8
|
+
import { createHash, pbkdf2Sync, randomBytes, timingSafeEqual } from 'node:crypto';
|
|
9
|
+
import { isBcryptPrefix, parseBcrypt, verifyBcrypt } from './bcrypt.js';
|
|
10
|
+
import { SitefileError } from './errors.js';
|
|
11
|
+
/** PBKDF2 rounds of a verifier made here; checks are cached per server, so this is paid once per credential. */
|
|
12
|
+
export const AUTH_ITERATIONS = 20000;
|
|
13
|
+
/** `user:password` (`--auth`, `SITEFILE_SERVE_AUTH`): the user is non-empty and has no `:`; the password is the rest. */
|
|
14
|
+
export function parseAuthPair(value, what) {
|
|
15
|
+
const i = value.indexOf(':');
|
|
16
|
+
if (i <= 0 || i === value.length - 1)
|
|
17
|
+
throw new SitefileError('E_USAGE', `${what} expects user:password`);
|
|
18
|
+
return { user: value.slice(0, i), password: value.slice(i + 1) };
|
|
19
|
+
}
|
|
20
|
+
/** A pbkdf2-sha256 verifier for `password` (a random salt unless one is given). */
|
|
21
|
+
export function makeVerifier(password, salt = randomBytes(16), iterations = AUTH_ITERATIONS) {
|
|
22
|
+
const key = pbkdf2Sync(Buffer.from(password, 'utf8'), salt, iterations, 32, 'sha256');
|
|
23
|
+
return `pbkdf2-sha256$${iterations}$${Buffer.from(salt).toString('base64')}$${key.toString('base64')}`;
|
|
24
|
+
}
|
|
25
|
+
/** Canonical base64 (standard alphabet, padded) decoded, else undefined. */
|
|
26
|
+
function b64(s) {
|
|
27
|
+
const b = Buffer.from(s, 'base64');
|
|
28
|
+
return b.toString('base64') === s ? b : undefined;
|
|
29
|
+
}
|
|
30
|
+
function equal(a, b) {
|
|
31
|
+
return a.length === b.length && timingSafeEqual(a, b);
|
|
32
|
+
}
|
|
33
|
+
/** Whether `password` matches `verifier` (false for a malformed verifier). Constant time in the key comparison. */
|
|
34
|
+
export function verifyPassword(verifier, password) {
|
|
35
|
+
if (isBcryptPrefix(verifier))
|
|
36
|
+
return verifyBcrypt(verifier, password);
|
|
37
|
+
const pw = Buffer.from(password, 'utf8');
|
|
38
|
+
if (verifier.startsWith('{SHA}')) {
|
|
39
|
+
const want = b64(verifier.slice(5));
|
|
40
|
+
return want !== undefined && equal(createHash('sha1').update(pw).digest(), want);
|
|
41
|
+
}
|
|
42
|
+
const m = /^pbkdf2-sha256\$([0-9]{1,7})\$([A-Za-z0-9+/=]+)\$([A-Za-z0-9+/=]+)$/.exec(verifier);
|
|
43
|
+
if (!m)
|
|
44
|
+
return false;
|
|
45
|
+
const iterations = Number(m[1]);
|
|
46
|
+
const salt = b64(m[2]);
|
|
47
|
+
const want = b64(m[3]);
|
|
48
|
+
if (iterations < 1 || salt === undefined || want === undefined || want.length !== 32)
|
|
49
|
+
return false;
|
|
50
|
+
return equal(pbkdf2Sync(pw, salt, iterations, 32, 'sha256'), want);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* An htpasswd file: `user:hash` lines (blank lines and `#` comments skipped). `{SHA}` (htpasswd -s) and bcrypt
|
|
54
|
+
* (`$2y$`, `$2a$`, `$2b$`, cost 4 to 31; htpasswd -B) entries are accepted; a malformed bcrypt hash, `$apr1$`,
|
|
55
|
+
* crypt and plain-text entries are `E_USAGE` errors naming the user.
|
|
56
|
+
*/
|
|
57
|
+
export function parseHtpasswd(text, origin) {
|
|
58
|
+
const out = [];
|
|
59
|
+
for (const [n, raw] of text.split('\n').entries()) {
|
|
60
|
+
const line = raw.endsWith('\r') ? raw.slice(0, -1) : raw;
|
|
61
|
+
if (line.trim() === '' || line.trimStart().startsWith('#'))
|
|
62
|
+
continue;
|
|
63
|
+
const i = line.indexOf(':');
|
|
64
|
+
if (i <= 0)
|
|
65
|
+
throw new SitefileError('E_USAGE', `${origin}:${n + 1}: expected user:hash`);
|
|
66
|
+
const user = line.slice(0, i);
|
|
67
|
+
const hash = line.slice(i + 1);
|
|
68
|
+
if (isBcryptPrefix(hash)) {
|
|
69
|
+
if (!parseBcrypt(hash))
|
|
70
|
+
throw new SitefileError('E_USAGE', `${origin}:${n + 1}: user '${user}': malformed bcrypt hash (expected $2y$, $2a$ or $2b$, a cost of 04 to 31 and 53 characters of salt and hash)`);
|
|
71
|
+
}
|
|
72
|
+
else if (!/^\{SHA\}[A-Za-z0-9+/]{27}=$/.test(hash)) {
|
|
73
|
+
throw new SitefileError('E_USAGE', `${origin}:${n + 1}: user '${user}': unsupported password hash (only {SHA} and bcrypt entries, htpasswd -s or -B)`);
|
|
74
|
+
}
|
|
75
|
+
out.push({ user, verifier: hash });
|
|
76
|
+
}
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
/** The `user` and `password` of an `Authorization: Basic …` value, or undefined. */
|
|
80
|
+
export function basicCredentials(header) {
|
|
81
|
+
if (header === undefined)
|
|
82
|
+
return undefined;
|
|
83
|
+
const m = /^[Bb][Aa][Ss][Ii][Cc] +([A-Za-z0-9+/]+=*) *$/.exec(header.trim());
|
|
84
|
+
if (!m)
|
|
85
|
+
return undefined;
|
|
86
|
+
const raw = b64(m[1]);
|
|
87
|
+
if (raw === undefined)
|
|
88
|
+
return undefined;
|
|
89
|
+
const text = raw.toString('utf8');
|
|
90
|
+
const i = text.indexOf(':');
|
|
91
|
+
return i < 0 ? undefined : { user: text.slice(0, i), password: text.slice(i + 1) };
|
|
92
|
+
}
|
|
93
|
+
/** Accepts `Authorization` values for a set of entries; good values are remembered (at most 64) so PBKDF2 runs once per credential. */
|
|
94
|
+
export class BasicAuth {
|
|
95
|
+
entries;
|
|
96
|
+
good = new Set();
|
|
97
|
+
constructor(entries) {
|
|
98
|
+
this.entries = entries;
|
|
99
|
+
}
|
|
100
|
+
allows(header) {
|
|
101
|
+
if (header === undefined)
|
|
102
|
+
return false;
|
|
103
|
+
if (this.good.has(header))
|
|
104
|
+
return true;
|
|
105
|
+
const c = basicCredentials(header);
|
|
106
|
+
if (c === undefined)
|
|
107
|
+
return false;
|
|
108
|
+
let ok = false;
|
|
109
|
+
for (const e of this.entries)
|
|
110
|
+
if (e.user === c.user && verifyPassword(e.verifier, c.password))
|
|
111
|
+
ok = true;
|
|
112
|
+
if (ok) {
|
|
113
|
+
if (this.good.size >= 64)
|
|
114
|
+
this.good.clear();
|
|
115
|
+
this.good.add(header);
|
|
116
|
+
}
|
|
117
|
+
return ok;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=serve-auth.js.map
|
package/lib/src/serve-rules.d.ts
CHANGED
|
@@ -1,29 +1,105 @@
|
|
|
1
|
+
import type { Tree } from './tree.js';
|
|
2
|
+
/** A segment of a `_redirects` source: a literal (decoded) or a `:placeholder`. */
|
|
3
|
+
export type RedirectSegment = {
|
|
4
|
+
readonly lit: string;
|
|
5
|
+
} | {
|
|
6
|
+
readonly param: string;
|
|
7
|
+
};
|
|
8
|
+
/** A `_redirects` condition: `Language=`, `Country=` or `Cookie=` (lowercase key) and its values. */
|
|
9
|
+
export interface RedirectCondition {
|
|
10
|
+
readonly key: 'language' | 'country' | 'cookie';
|
|
11
|
+
/** ASCII-lowercased for language and country; cookie names as written. */
|
|
12
|
+
readonly values: readonly string[];
|
|
13
|
+
}
|
|
1
14
|
/** A `_redirects` rule `serve` understands. */
|
|
2
15
|
export interface ServeRedirect {
|
|
3
|
-
/**
|
|
16
|
+
/** 1-based line in `_redirects` (for the access log). */
|
|
17
|
+
readonly line: number;
|
|
18
|
+
/**
|
|
19
|
+
* Decoded path without a trailing slash (`/` for the root), placeholders written `:name`; for a
|
|
20
|
+
* splat rule, the prefix before `/*` (`''` for `/*`).
|
|
21
|
+
*/
|
|
4
22
|
readonly from: string;
|
|
23
|
+
readonly segments: readonly RedirectSegment[];
|
|
5
24
|
readonly splat: boolean;
|
|
25
|
+
/** Query parameters the request must have: `[name, value]` (decoded); a value `:name` captures it. */
|
|
26
|
+
readonly query: ReadonlyArray<readonly [string, string]>;
|
|
6
27
|
readonly to: string;
|
|
7
28
|
readonly status: number;
|
|
8
29
|
/** `301!`: applies even when a file exists at the path. */
|
|
9
30
|
readonly force: boolean;
|
|
31
|
+
readonly conditions: readonly RedirectCondition[];
|
|
32
|
+
}
|
|
33
|
+
/** A line of `_redirects` or `_headers` that `serve` ignores, and why. */
|
|
34
|
+
export interface IgnoredLine {
|
|
35
|
+
readonly line: number;
|
|
36
|
+
readonly reason: string;
|
|
37
|
+
}
|
|
38
|
+
/** What a request offers a rule's query parameters and conditions. */
|
|
39
|
+
export interface RedirectRequest {
|
|
40
|
+
/** The query string with its `?` (`''` when none). */
|
|
41
|
+
readonly search: string;
|
|
42
|
+
readonly acceptLanguage?: string | undefined;
|
|
43
|
+
readonly cookie?: string | undefined;
|
|
44
|
+
/** `serve --country`. */
|
|
45
|
+
readonly country?: string | undefined;
|
|
10
46
|
}
|
|
11
47
|
export interface ServeHeaders {
|
|
12
48
|
readonly pattern: string;
|
|
13
49
|
/** Lowercase names. */
|
|
14
50
|
readonly headers: ReadonlyArray<readonly [string, string]>;
|
|
15
51
|
}
|
|
52
|
+
export declare const SERVE_STATUSES: readonly number[];
|
|
53
|
+
/** Statuses that serve the target path's file with that status instead of redirecting. */
|
|
54
|
+
export declare const isRewriteStatus: (s: number) => boolean;
|
|
55
|
+
/** The decoded `name=value` pairs of a query string (without its `?`); malformed pairs are skipped. */
|
|
56
|
+
export declare function parseQuery(q: string): Array<[string, string]>;
|
|
16
57
|
/**
|
|
17
|
-
* Parse `_redirects
|
|
18
|
-
*
|
|
19
|
-
* (
|
|
58
|
+
* Parse `_redirects` (Netlify / Cloudflare Pages): `from [name=value…] to [status[!]] [Key=values…]`
|
|
59
|
+
* per line, `#` comments. Supported: literal segments, `:placeholders` (one segment each) and a
|
|
60
|
+
* trailing `/*` (`:splat`); query parameters, literal or `:placeholder`; statuses 301, 302, 303,
|
|
61
|
+
* 307, 308 (redirect), 200 (rewrite), 404, 410 and 451 (the target's file with that status);
|
|
62
|
+
* conditions `Language=`, `Country=` and `Cookie=`. Every other line is in `ignored`, with why.
|
|
20
63
|
*/
|
|
64
|
+
export declare function parseRedirects(text: string): {
|
|
65
|
+
rules: ServeRedirect[];
|
|
66
|
+
ignored: IgnoredLine[];
|
|
67
|
+
};
|
|
68
|
+
/** {@link parseRedirects}' rules. */
|
|
21
69
|
export declare function parseRedirectRules(text: string): ServeRedirect[];
|
|
22
|
-
/**
|
|
70
|
+
/** A rule that depends on nothing but the path: no placeholders, query parameters or conditions. */
|
|
71
|
+
export declare function isPlainRule(r: ServeRedirect): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Parse `_headers`: a path pattern line (`*` matches anything), then indented `Name: value` lines.
|
|
74
|
+
* Every other line is in `ignored`, with why.
|
|
75
|
+
*/
|
|
76
|
+
export declare function parseHeaders(text: string): {
|
|
77
|
+
rules: ServeHeaders[];
|
|
78
|
+
ignored: IgnoredLine[];
|
|
79
|
+
};
|
|
80
|
+
/** {@link parseHeaders}' rules. */
|
|
23
81
|
export declare function parseHeaderRules(text: string): ServeHeaders[];
|
|
82
|
+
/**
|
|
83
|
+
* The warnings for a tree's `/_redirects` and `/_headers` lines `serve` ignores, one per line:
|
|
84
|
+
* `_redirects:3: ignored: <reason>`. `content` reads files a zip served in place holds only as digests.
|
|
85
|
+
*/
|
|
86
|
+
export declare function siteFileWarnings(tree: Tree, content?: {
|
|
87
|
+
read(path: string): Uint8Array | undefined;
|
|
88
|
+
}): string[];
|
|
24
89
|
/** Glob match where `*` matches any run of characters (including `/`). */
|
|
25
90
|
export declare function globMatch(pattern: string, s: string): boolean;
|
|
26
91
|
/** Every matching block's headers; a name set by several blocks gets its values joined with `, `. */
|
|
27
92
|
export declare function headersFor(rules: readonly ServeHeaders[], path: string): Record<string, string>;
|
|
28
|
-
/**
|
|
29
|
-
|
|
93
|
+
/**
|
|
94
|
+
* The values a rule's source captures from `path` (decoded, no trailing slash): each placeholder,
|
|
95
|
+
* and `splat` (the remainder, `''` for the prefix itself) for a splat rule; undefined when it does
|
|
96
|
+
* not match.
|
|
97
|
+
*/
|
|
98
|
+
export declare function matchRedirect(r: ServeRedirect, path: string): Map<string, string> | undefined;
|
|
99
|
+
/** {@link matchRedirect}, then the rule's query parameters (adding their captures) and conditions. */
|
|
100
|
+
export declare function matchRule(r: ServeRedirect, path: string, req: RedirectRequest): Map<string, string> | undefined;
|
|
101
|
+
/**
|
|
102
|
+
* A rule's target with `:name` (letters, digits and `_`, the longest run) replaced by the captured
|
|
103
|
+
* value, percent-encoded like encodeURIComponent; `:splat` keeps its slashes. Other names stay.
|
|
104
|
+
*/
|
|
105
|
+
export declare function substituteTarget(to: string, caps: ReadonlyMap<string, string>): string;
|