@venizia/ignis-helpers 0.4.8-4 → 0.4.8-5
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/dist/esm/common/redact.d.ts +5 -0
- package/dist/esm/common/redact.js +47 -3
- package/dist/esm/common/redact.js.map +1 -1
- package/dist/esm/modules/crypto/algorithms/aes.algorithm.d.ts +7 -9
- package/dist/esm/modules/crypto/algorithms/aes.algorithm.js +56 -64
- package/dist/esm/modules/crypto/algorithms/aes.algorithm.js.map +1 -1
- package/dist/esm/modules/crypto/algorithms/base.algorithm.d.ts +12 -2
- package/dist/esm/modules/crypto/algorithms/base.algorithm.js +31 -21
- package/dist/esm/modules/crypto/algorithms/base.algorithm.js.map +1 -1
- package/dist/esm/modules/crypto/algorithms/derived-key-cache.d.ts +24 -0
- package/dist/esm/modules/crypto/algorithms/derived-key-cache.js +55 -0
- package/dist/esm/modules/crypto/algorithms/derived-key-cache.js.map +1 -0
- package/dist/esm/modules/crypto/algorithms/rsa.algorithm.d.ts +0 -1
- package/dist/esm/modules/crypto/algorithms/rsa.algorithm.js +10 -24
- package/dist/esm/modules/crypto/algorithms/rsa.algorithm.js.map +1 -1
- package/dist/esm/modules/logger/formatting/deep-splat.js +2 -1
- package/dist/esm/modules/logger/formatting/deep-splat.js.map +1 -1
- package/dist/esm/modules/network/utilities/url-safety/common/types.d.ts +7 -0
- package/dist/esm/modules/network/utilities/url-safety/common/types.js.map +1 -1
- package/dist/esm/modules/network/utilities/url-safety/ingest.d.ts +51 -8
- package/dist/esm/modules/network/utilities/url-safety/ingest.js +189 -25
- package/dist/esm/modules/network/utilities/url-safety/ingest.js.map +1 -1
- package/dist/esm/modules/socket/socket-io/common/constants.d.ts +2 -0
- package/dist/esm/modules/socket/socket-io/common/constants.js +2 -0
- package/dist/esm/modules/socket/socket-io/common/constants.js.map +1 -1
- package/dist/esm/modules/socket/socket-io/server/helper.js +9 -5
- package/dist/esm/modules/socket/socket-io/server/helper.js.map +1 -1
- package/dist/esm/modules/socket/websocket/server/helper.js +9 -3
- package/dist/esm/modules/socket/websocket/server/helper.js.map +1 -1
- package/dist/esm/modules/storage/base.d.ts +1 -1
- package/dist/esm/modules/storage/base.js +19 -4
- package/dist/esm/modules/storage/base.js.map +1 -1
- package/dist/esm/modules/storage/bun-s3/helper.d.ts +6 -0
- package/dist/esm/modules/storage/bun-s3/helper.js +28 -8
- package/dist/esm/modules/storage/bun-s3/helper.js.map +1 -1
- package/dist/esm/modules/storage/disk/helper.d.ts +21 -1
- package/dist/esm/modules/storage/disk/helper.js +116 -15
- package/dist/esm/modules/storage/disk/helper.js.map +1 -1
- package/dist/esm/utilities/form-body.utility.d.ts +8 -0
- package/dist/esm/utilities/form-body.utility.js +5 -0
- package/dist/esm/utilities/form-body.utility.js.map +1 -1
- package/dist/esm/utilities/record.utility.d.ts +11 -1
- package/dist/esm/utilities/record.utility.js +17 -1
- package/dist/esm/utilities/record.utility.js.map +1 -1
- package/dist/esm/utilities/request.utility.d.ts +4 -0
- package/dist/esm/utilities/request.utility.js +55 -4
- package/dist/esm/utilities/request.utility.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The keys PBKDF2 has derived, kept so a second call (one that starts while the first runs included) shares the result.
|
|
3
|
+
* Bounded, least recently used out first: the entries are keyed by what went into the derivation, which includes a secret
|
|
4
|
+
* a caller may supply per request, so an unbounded map would grow with every distinct one. The key is an HMAC of the
|
|
5
|
+
* inputs under a random key made once per process, so the secret itself is not kept as a map key, and a key read from
|
|
6
|
+
* memory cannot be tested against guesses at SHA-256 speed without that process key.
|
|
7
|
+
*/
|
|
8
|
+
export declare class DerivedKeyCache {
|
|
9
|
+
static readonly MAX_ENTRIES = 64;
|
|
10
|
+
/** Made once per process and never leaves it: a cache key from one process means nothing in another. */
|
|
11
|
+
private static readonly PROCESS_KEY;
|
|
12
|
+
private readonly entries;
|
|
13
|
+
get size(): number;
|
|
14
|
+
/** The keyed digest of the derivation inputs: what the cache is keyed by. */
|
|
15
|
+
static keyOf(opts: {
|
|
16
|
+
parts: Array<string | number>;
|
|
17
|
+
}): string;
|
|
18
|
+
/** Runs `derive` once per key. A failed derivation leaves nothing behind, so the next call tries again. */
|
|
19
|
+
deriveOnce(opts: {
|
|
20
|
+
key: string;
|
|
21
|
+
derive: () => Promise<Buffer>;
|
|
22
|
+
}): Promise<Buffer>;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=derived-key-cache.d.ts.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { createHmac, randomBytes } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* The keys PBKDF2 has derived, kept so a second call (one that starts while the first runs included) shares the result.
|
|
4
|
+
* Bounded, least recently used out first: the entries are keyed by what went into the derivation, which includes a secret
|
|
5
|
+
* a caller may supply per request, so an unbounded map would grow with every distinct one. The key is an HMAC of the
|
|
6
|
+
* inputs under a random key made once per process, so the secret itself is not kept as a map key, and a key read from
|
|
7
|
+
* memory cannot be tested against guesses at SHA-256 speed without that process key.
|
|
8
|
+
*/
|
|
9
|
+
export class DerivedKeyCache {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.entries = new Map();
|
|
12
|
+
}
|
|
13
|
+
static { this.MAX_ENTRIES = 64; }
|
|
14
|
+
/** Made once per process and never leaves it: a cache key from one process means nothing in another. */
|
|
15
|
+
static { this.PROCESS_KEY = randomBytes(32); }
|
|
16
|
+
get size() {
|
|
17
|
+
return this.entries.size;
|
|
18
|
+
}
|
|
19
|
+
/** The keyed digest of the derivation inputs: what the cache is keyed by. */
|
|
20
|
+
static keyOf(opts) {
|
|
21
|
+
const hash = createHmac('sha256', DerivedKeyCache.PROCESS_KEY);
|
|
22
|
+
for (const part of opts.parts) {
|
|
23
|
+
const text = String(part);
|
|
24
|
+
// Length-prefixed, so ["ab", "c"] and ["a", "bc"] cannot meet.
|
|
25
|
+
hash.update(`${text.length}:${text}`);
|
|
26
|
+
}
|
|
27
|
+
return hash.digest('hex');
|
|
28
|
+
}
|
|
29
|
+
/** Runs `derive` once per key. A failed derivation leaves nothing behind, so the next call tries again. */
|
|
30
|
+
deriveOnce(opts) {
|
|
31
|
+
const { key, derive } = opts;
|
|
32
|
+
const pending = this.entries.get(key);
|
|
33
|
+
if (pending) {
|
|
34
|
+
// Most recently used moves to the end.
|
|
35
|
+
this.entries.delete(key);
|
|
36
|
+
this.entries.set(key, pending);
|
|
37
|
+
return pending;
|
|
38
|
+
}
|
|
39
|
+
const derived = derive();
|
|
40
|
+
this.entries.set(key, derived);
|
|
41
|
+
for (const oldest of this.entries.keys()) {
|
|
42
|
+
if (this.entries.size <= DerivedKeyCache.MAX_ENTRIES) {
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
this.entries.delete(oldest);
|
|
46
|
+
}
|
|
47
|
+
derived.catch(() => {
|
|
48
|
+
if (this.entries.get(key) === derived) {
|
|
49
|
+
this.entries.delete(key);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return derived;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=derived-key-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"derived-key-cache.js","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/derived-key-cache.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEtD;;;;;;GAMG;AACH,MAAM,OAAO,eAAe;IAA5B;QAMmB,YAAO,GAAG,IAAI,GAAG,EAA2B,CAAC;IAoDhE,CAAC;aAzDiB,gBAAW,GAAG,EAAE,AAAL,CAAM;IAEjC,wGAAwG;aAChF,gBAAW,GAAG,WAAW,CAAC,EAAE,CAAC,AAAlB,CAAmB;IAItD,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;IAC3B,CAAC;IAED,6EAA6E;IAC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CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,OAAO,EAAE,CAAC;gBACtC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAC3B,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,OAAO,OAAO,CAAC;IACjB,CAAC","sourcesContent":["import { createHmac, randomBytes } from 'node:crypto';\n\n/**\n * The keys PBKDF2 has derived, kept so a second call (one that starts while the first runs included) shares the result.\n * Bounded, least recently used out first: the entries are keyed by what went into the derivation, which includes a secret\n * a caller may supply per request, so an unbounded map would grow with every distinct one. The key is an HMAC of the\n * inputs under a random key made once per process, so the secret itself is not kept as a map key, and a key read from\n * memory cannot be tested against guesses at SHA-256 speed without that process key.\n */\nexport class DerivedKeyCache {\n static readonly MAX_ENTRIES = 64;\n\n /** Made once per process and never leaves it: a cache key from one process means nothing in another. */\n private static readonly PROCESS_KEY = randomBytes(32);\n\n private readonly entries = new Map<string, Promise<Buffer>>();\n\n get size(): number {\n return this.entries.size;\n }\n\n /** The keyed digest of the derivation inputs: what the cache is keyed by. */\n static keyOf(opts: { parts: Array<string | number> }): string {\n const hash = createHmac('sha256', DerivedKeyCache.PROCESS_KEY);\n\n for (const part of opts.parts) {\n const text = String(part);\n\n // Length-prefixed, so [\"ab\", \"c\"] and [\"a\", \"bc\"] cannot meet.\n hash.update(`${text.length}:${text}`);\n }\n\n return hash.digest('hex');\n }\n\n /** Runs `derive` once per key. A failed derivation leaves nothing behind, so the next call tries again. */\n deriveOnce(opts: { key: string; derive: () => Promise<Buffer> }): Promise<Buffer> {\n const { key, derive } = opts;\n const pending = this.entries.get(key);\n\n if (pending) {\n // Most recently used moves to the end.\n this.entries.delete(key);\n this.entries.set(key, pending);\n\n return pending;\n }\n\n const derived = derive();\n this.entries.set(key, derived);\n\n for (const oldest of this.entries.keys()) {\n if (this.entries.size <= DerivedKeyCache.MAX_ENTRIES) {\n break;\n }\n\n this.entries.delete(oldest);\n }\n\n derived.catch(() => {\n if (this.entries.get(key) === derived) {\n this.entries.delete(key);\n }\n });\n\n return derived;\n }\n}\n"]}
|
|
@@ -35,33 +35,19 @@ export class RSA extends BaseCryptoAlgorithm {
|
|
|
35
35
|
}
|
|
36
36
|
async encrypt(opts) {
|
|
37
37
|
const { message, secret: pubKey } = opts;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
if (doThrow) {
|
|
46
|
-
throw error;
|
|
47
|
-
}
|
|
48
|
-
return message;
|
|
49
|
-
}
|
|
38
|
+
this.assertNoRemovedOptions({ method: 'encrypt', options: opts.opts });
|
|
39
|
+
const { inputEncoding = { key: 'base64', message: 'utf-8' }, outputEncoding = 'base64' } = opts.opts ?? {};
|
|
40
|
+
const key = await crypto.subtle.importKey('spki', Buffer.from(pubKey, inputEncoding.key), OAEP, false, ['encrypt']);
|
|
41
|
+
const cipherBytes = await crypto.subtle.encrypt({ name: OAEP.name }, key, Buffer.from(message, inputEncoding.message));
|
|
42
|
+
return Buffer.from(cipherBytes).toString(outputEncoding);
|
|
50
43
|
}
|
|
51
44
|
async decrypt(opts) {
|
|
52
45
|
const { message, secret: privKey } = opts;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
if (doThrow) {
|
|
61
|
-
throw error;
|
|
62
|
-
}
|
|
63
|
-
return message;
|
|
64
|
-
}
|
|
46
|
+
this.assertNoRemovedOptions({ method: 'decrypt', options: opts.opts });
|
|
47
|
+
const { inputEncoding = { key: 'base64', message: 'base64' }, outputEncoding = 'utf-8' } = opts.opts ?? {};
|
|
48
|
+
const key = await crypto.subtle.importKey('pkcs8', Buffer.from(privKey, inputEncoding.key), OAEP, false, ['decrypt']);
|
|
49
|
+
const plainBytes = await crypto.subtle.decrypt({ name: OAEP.name }, key, Buffer.from(message, inputEncoding.message));
|
|
50
|
+
return Buffer.from(plainBytes).toString(outputEncoding);
|
|
65
51
|
}
|
|
66
52
|
}
|
|
67
53
|
//# sourceMappingURL=rsa.algorithm.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rsa.algorithm.js","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/rsa.algorithm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"rsa.algorithm.js","sourceRoot":"","sources":["../../../../../src/modules/crypto/algorithms/rsa.algorithm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAUvD;;;;;GAKG;AACH,MAAM,IAAI,GAAG,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,OAAO,EAAW,CAAC;AAC1D,MAAM,eAAe,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAE3D,8FAA8F;AAC9F,MAAM,OAAO,GAAI,SAAQ,mBAQxB;IACC,YAAY,IAAqC;QAC/C,KAAK,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,IAAI,EAAE,GAAG,IAAI,EAAE,CAAC,CAAC;IACtC,CAAC;IAED,MAAM,CAAC,aAAa;QAClB,OAAO,IAAI,GAAG,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;IACvC,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,kBAAkB,CAAC,IAA0B;QACjD,MAAM,OAAO,GAAG,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,cAAc,CAAC,CAAC;IAC1D,CAAC;CACF","sourcesContent":["import { getError } from '@/modules/error';\nimport { MINIMUM_RSA_MODULUS_BITS } from '../common/constants';\nimport { BaseCryptoAlgorithm } from './base.algorithm';\n\ninterface IRSAExtraOptions {\n inputEncoding?: { key: BufferEncoding; message: BufferEncoding };\n outputEncoding?: BufferEncoding;\n}\n\n/** The algorithm name `RSA` reports as its `algorithm`. */\nexport type RSAAlgorithmType = 'rsa';\n\n/**\n * OAEP with SHA-1 for both the label hash and MGF1, and the public exponent 65537: what Node's\n * `publicEncrypt`/`privateDecrypt` and `generateKeyPairSync('rsa')` used by default, so keys and\n * ciphertext written by earlier releases still work. Do not change them: every ciphertext stored under\n * them would stop decrypting.\n */\nconst OAEP = { name: 'RSA-OAEP', hash: 'SHA-1' } as const;\nconst PUBLIC_EXPONENT = new Uint8Array([0x01, 0x00, 0x01]);\n\n/** `generateDERKeyPair`, `encrypt` and `decrypt` are async: they run over `crypto.subtle`. */\nexport class RSA extends BaseCryptoAlgorithm<\n RSAAlgorithmType,\n string,\n string,\n string,\n Promise<string>,\n Promise<string>,\n IRSAExtraOptions\n> {\n constructor(opts: { algorithm: RSAAlgorithmType }) {\n super({ scope: RSA.name, ...opts });\n }\n\n static withAlgorithm() {\n return new RSA({ algorithm: 'rsa' });\n }\n\n /**\n * Rejects a modulus below `MINIMUM_RSA_MODULUS_BITS` rather than generating it - a weak key still\n * works, signs, and verifies, so this is the only point where the size is ever checked.\n */\n async generateDERKeyPair(opts?: { modulus: number }) {\n const modulus = opts?.modulus ?? MINIMUM_RSA_MODULUS_BITS;\n\n if (modulus < MINIMUM_RSA_MODULUS_BITS) {\n throw getError({\n message: `[RSA][generateDERKeyPair] Modulus ${modulus} is below the ${MINIMUM_RSA_MODULUS_BITS}-bit minimum`,\n });\n }\n\n const keys = await crypto.subtle.generateKey(\n { ...OAEP, modulusLength: modulus, publicExponent: PUBLIC_EXPONENT },\n true,\n ['encrypt', 'decrypt'],\n );\n\n const publicKey = await crypto.subtle.exportKey('spki', keys.publicKey);\n const privateKey = await crypto.subtle.exportKey('pkcs8', keys.privateKey);\n\n return { publicKey: Buffer.from(publicKey), privateKey: Buffer.from(privateKey) };\n }\n\n async encrypt(opts: {\n message: string;\n secret: string;\n opts?: IRSAExtraOptions;\n }): Promise<string> {\n const { message, secret: pubKey } = opts;\n this.assertNoRemovedOptions({ method: 'encrypt', options: opts.opts });\n\n const { inputEncoding = { key: 'base64', message: 'utf-8' }, outputEncoding = 'base64' } =\n opts.opts ?? {};\n\n const key = await crypto.subtle.importKey(\n 'spki',\n Buffer.from(pubKey, inputEncoding.key),\n OAEP,\n false,\n ['encrypt'],\n );\n const cipherBytes = await crypto.subtle.encrypt(\n { name: OAEP.name },\n key,\n Buffer.from(message, inputEncoding.message),\n );\n\n return Buffer.from(cipherBytes).toString(outputEncoding);\n }\n\n async decrypt(opts: {\n message: string;\n secret: string;\n opts?: IRSAExtraOptions;\n }): Promise<string> {\n const { message, secret: privKey } = opts;\n this.assertNoRemovedOptions({ method: 'decrypt', options: opts.opts });\n\n const { inputEncoding = { key: 'base64', message: 'base64' }, outputEncoding = 'utf-8' } =\n opts.opts ?? {};\n\n const key = await crypto.subtle.importKey(\n 'pkcs8',\n Buffer.from(privKey, inputEncoding.key),\n OAEP,\n false,\n ['decrypt'],\n );\n const plainBytes = await crypto.subtle.decrypt(\n { name: OAEP.name },\n key,\n Buffer.from(message, inputEncoding.message),\n );\n\n return Buffer.from(plainBytes).toString(outputEncoding);\n }\n}\n"]}
|
|
@@ -36,7 +36,8 @@ export class LogFormatUtility {
|
|
|
36
36
|
const placeholder = placeholders[index];
|
|
37
37
|
const isInspectable = typeof arg === 'object' && arg !== null;
|
|
38
38
|
if (!isInspectable) {
|
|
39
|
-
|
|
39
|
+
// A string argument (`%s`, or none) is printed as written: the credentials it carries are masked first.
|
|
40
|
+
return typeof arg === 'string' ? RedactUtility.scrubText({ value: arg }) : arg;
|
|
40
41
|
}
|
|
41
42
|
// `%j` runs JSON.stringify, which answers `[Circular]` for the WHOLE argument when one cycle sits anywhere inside it - a transaction handle in the payload wipes out every other field. It also has no notion of secret keys, and no depth limit of its own. Project the value first, under the same depth cap `%s` gets.
|
|
42
43
|
if (placeholder === '%j') {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deep-splat.js","sourceRoot":"","sources":["../../../../../src/modules/logger/formatting/deep-splat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,wFAAwF;AACxF,MAAM,mBAAmB,GAAG,eAAe,CAAC;AAE5C,uQAAuQ;AACvQ,MAAM,qBAAqB,GAAG,CAAC,CAAC;AAEhC,0KAA0K;AAC1K,MAAM,OAAO,gBAAgB;IACnB,MAAM,CAAC,YAAY;QACzB,MAAM,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,4BAA4B,CAAC;QAE5D,6IAA6I;QAC7I,IAAI,UAAU,KAAK,SAAS,IAAI,UAAU,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;YACzD,OAAO,qBAAqB,CAAC;QAC/B,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;QAE/C,mJAAmJ;QACnJ,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;YACvC,OAAO,qBAAqB,CAAC;QAC/B,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAEO,MAAM,CAAC,mBAAmB;QAChC,OAAO;YACL,KAAK,EAAE,gBAAgB,CAAC,YAAY,EAAE;YACtC,cAAc,EAAE,IAAI;YACpB,eAAe,EAAE,IAAI;YACrB,WAAW,EAAE,QAAQ;SACtB,CAAC;IACJ,CAAC;IAED,2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Content":["import { RedactUtility } from '@/common/redact';\nimport util from 'node:util';\nimport { ErrorPrettier } from './error-prettier';\n\n/** The placeholders `util.format` understands. `%%` is an escape, not a placeholder. */\nconst PLACEHOLDER_PATTERN = /%[sjdifoOc%]/g;\n\n/** How deep an object is inspected before it collapses to `[Object]`: Node's default of 2 hides nested data (e.g. wrapped `cause`), 5 reaches it while staying bounded. `APP_ENV_LOGGER_INSPECT_DEPTH` overrides with a NON-NEGATIVE number - no unlimited setting. */\nconst DEFAULT_INSPECT_DEPTH = 5;\n\n/** Renders a log message and its arguments to one string, redacting secret keys and inspecting objects to a bounded depth (`APP_ENV_LOGGER_INSPECT_DEPTH`, default 5). */\nexport class LogFormatUtility {\n private static resolveDepth(): number {\n const configured = process.env.APP_ENV_LOGGER_INSPECT_DEPTH;\n\n // Not `ParseUtility.toInt()`: it answers 0 for an ABSENT value, which would silently mean \"depth 0\" - the one setting that hides everything.\n if (configured === undefined || configured.trim() === '') {\n return DEFAULT_INSPECT_DEPTH;\n }\n\n const parsed = Number.parseInt(configured, 10);\n\n // A negative or unparseable value is a misconfiguration, not a request: fall back rather than hand `util.inspect` a depth it reads as \"unlimited\".\n if (Number.isNaN(parsed) || parsed < 0) {\n return DEFAULT_INSPECT_DEPTH;\n }\n\n return parsed;\n }\n\n private static buildInspectOptions(): util.InspectOptions {\n return {\n depth: LogFormatUtility.resolveDepth(),\n maxArrayLength: null,\n maxStringLength: null,\n breakLength: Infinity,\n };\n }\n\n /** Formats like `util.format`, but `%s` inspects to configured depth instead of Node's hard-coded `depth: 0` (no inspect option overrides that), and `%j` is projected into a JSON-safe shape first. Per-placeholder only - handing `%j` the widened STRING would emit a JSON-quoted inspect dump instead of an object. */\n static formatMessage(opts: {\n message: string;\n args: Array<unknown>;\n inspectOptions?: util.InspectOptions;\n }): string {\n const { message, args, inspectOptions = LogFormatUtility.buildInspectOptions() } = opts;\n\n const placeholders = (message.match(PLACEHOLDER_PATTERN) ?? []).filter(\n (token) => token !== '%%',\n );\n\n const widened = args.map((arg, index) => {\n const placeholder = placeholders[index];\n const isInspectable = typeof arg === 'object' && arg !== null;\n\n if (!isInspectable) {\n // A string argument (`%s`, or none) is printed as written: the credentials it carries are masked first.\n return typeof arg === 'string' ? RedactUtility.scrubText({ value: arg }) : arg;\n }\n\n // `%j` runs JSON.stringify, which answers `[Circular]` for the WHOLE argument when one cycle sits anywhere inside it - a transaction handle in the payload wipes out every other field. It also has no notion of secret keys, and no depth limit of its own. Project the value first, under the same depth cap `%s` gets.\n if (placeholder === '%j') {\n return RedactUtility.toJsonSafe({\n value: arg,\n depth: inspectOptions.depth ?? LogFormatUtility.resolveDepth(),\n });\n }\n\n // `%o`, `%O` and arguments with NO placeholder (`util.format` appends those) are redacted but\n // left as objects, so `util.formatWithOptions` still renders them the way that placeholder\n // means. Pre-inspecting them into a string here would make `%o` print a quoted, escaped string\n // instead of an object. Only the redaction is added; the rendering is unchanged.\n //\n // Before this, `logger.info('login failed', { password })` printed the password verbatim: the\n // redactor only ever saw `%s`, while the reference docs recommend `%o` for data objects.\n //\n // Bounded like the `%s` path below, and for the same reason: an unbounded walk on a very deep\n // graph is work whose result the renderer discards, and deep enough it is a `RangeError` on a\n // call that could not throw before. `%o` renders at `depth`, so `+2` covers what is shown.\n if (placeholder === undefined || placeholder === '%o' || placeholder === '%O') {\n return RedactUtility.redactSecrets({\n value: arg,\n depth: (inspectOptions.depth ?? LogFormatUtility.resolveDepth()) + 2,\n });\n }\n\n if (placeholder !== '%s') {\n return arg;\n }\n\n // Inspecting an Error dumps every own property - a `jose` failure carries the whole JWT payload, a driver failure the whole query. Project it instead, onto its own line so it never trails off the end of the caller's sentence.\n if (arg instanceof Error) {\n return `\\n${ErrorPrettier.format({ error: arg, inspectOptions })}`;\n }\n\n // Bounded to just past what `util.inspect` will actually render. `+2`, not `+1`: at exactly\n // `depth + 1` the redactor replaces the last level with a string, and inspect then prints a\n // QUOTED `'[Object]'` instead of its own bare `[Object]`.\n return util.inspect(\n RedactUtility.redactSecrets({\n value: arg,\n depth: (inspectOptions.depth ?? LogFormatUtility.resolveDepth()) + 2,\n }),\n inspectOptions,\n );\n });\n\n return util.formatWithOptions(inspectOptions, message, ...widened);\n }\n}\n"]}
|
|
@@ -28,6 +28,13 @@ export interface IUrlSafetyPolicy {
|
|
|
28
28
|
* @default UrlSafetyDefaults.TIMEOUT_MILLISECONDS
|
|
29
29
|
*/
|
|
30
30
|
timeout?: IDuration;
|
|
31
|
+
/**
|
|
32
|
+
* A proxy for `UrlIngest.fetchGuarded` to send the request through, as `http://host:port`. Off by default, and the
|
|
33
|
+
* environment's `HTTP_PROXY` / `HTTPS_PROXY` is ignored: a proxy resolves the host name itself, so the vetted address
|
|
34
|
+
* the guard pins to would never be used. With this set the address check still runs, but the connection is NOT pinned:
|
|
35
|
+
* the proxy decides where the request goes, so only point it at a proxy you trust to refuse internal addresses.
|
|
36
|
+
*/
|
|
37
|
+
proxy?: string;
|
|
31
38
|
/**
|
|
32
39
|
* Largest body, in bytes. The read stops and cancels the moment the body crosses it.
|
|
33
40
|
* @default UrlSafetyDefaults.MAX_BYTES
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../../../src/modules/network/utilities/url-safety/common/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { IDuration } from '@/common/constants/duration';\n\n/**\n * What a caller will accept from an untrusted url. Every field is per call: an app that must reach\n * plain `http` says so at the call site rather than weakening a build-time constant for everyone.\n */\nexport interface IUrlSafetyPolicy {\n /**\n * The schemes a url may use.\n * @default ['https:']\n */\n allowedSchemes?: string[];\n\n /**\n * Whether a url may resolve to a private address. Turning it on in a path that takes urls from user input re-opens the\n * server-side request forgery this guard exists to close - a request can then be steered at\n * `169.254.169.254` and the cloud credentials behind it.\n * @default false\n */\n allowPrivateAddress?: boolean;\n\n /** When set, nothing outside the list passes, whatever it resolves to. */\n allowedHosts?: string[];\n\n /**\n * Redirects followed. Each hop is re-checked, so a public host cannot bounce into a private one.\n * @default 3\n */\n maxRedirects?: number;\n\n /**\n * How long a request may take. A slow host holds a worker slot for exactly this long.\n * @default UrlSafetyDefaults.TIMEOUT_MILLISECONDS\n */\n timeout?: IDuration;\n\n /**\n * Largest body, in bytes. The read stops and cancels the moment the body crosses it.\n * @default UrlSafetyDefaults.MAX_BYTES\n */\n maxBytes?: number;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../../../src/modules/network/utilities/url-safety/common/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { IDuration } from '@/common/constants/duration';\n\n/**\n * What a caller will accept from an untrusted url. Every field is per call: an app that must reach\n * plain `http` says so at the call site rather than weakening a build-time constant for everyone.\n */\nexport interface IUrlSafetyPolicy {\n /**\n * The schemes a url may use.\n * @default ['https:']\n */\n allowedSchemes?: string[];\n\n /**\n * Whether a url may resolve to a private address. Turning it on in a path that takes urls from user input re-opens the\n * server-side request forgery this guard exists to close - a request can then be steered at\n * `169.254.169.254` and the cloud credentials behind it.\n * @default false\n */\n allowPrivateAddress?: boolean;\n\n /** When set, nothing outside the list passes, whatever it resolves to. */\n allowedHosts?: string[];\n\n /**\n * Redirects followed. Each hop is re-checked, so a public host cannot bounce into a private one.\n * @default 3\n */\n maxRedirects?: number;\n\n /**\n * How long a request may take. A slow host holds a worker slot for exactly this long.\n * @default UrlSafetyDefaults.TIMEOUT_MILLISECONDS\n */\n timeout?: IDuration;\n\n /**\n * A proxy for `UrlIngest.fetchGuarded` to send the request through, as `http://host:port`. Off by default, and the\n * environment's `HTTP_PROXY` / `HTTPS_PROXY` is ignored: a proxy resolves the host name itself, so the vetted address\n * the guard pins to would never be used. With this set the address check still runs, but the connection is NOT pinned:\n * the proxy decides where the request goes, so only point it at a proxy you trust to refuse internal addresses.\n */\n proxy?: string;\n\n /**\n * Largest body, in bytes. The read stops and cancels the moment the body crosses it.\n * @default UrlSafetyDefaults.MAX_BYTES\n */\n maxBytes?: number;\n}\n"]}
|
|
@@ -2,12 +2,20 @@ import type { IUrlSafetyPolicy } from './common/types.js';
|
|
|
2
2
|
/** Fetching an untrusted url without letting it reach inside the network. */
|
|
3
3
|
export declare class UrlIngest {
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* host that publishes one public A record beside an internal one.
|
|
5
|
+
* Resolves `hostname` once and returns every address it answered, refusing the host when ANY of them is non-public.
|
|
6
|
+
* Checking only the first walks straight into a host that publishes one public A record beside an internal one.
|
|
7
|
+
* An IP literal answers itself. `allowPrivateAddress` skips the check and returns an empty list: nothing to pin to.
|
|
8
|
+
*/
|
|
9
|
+
static resolvePublicAddresses(opts: {
|
|
10
|
+
hostname: string;
|
|
11
|
+
policy?: IUrlSafetyPolicy;
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
}): Promise<string[]>;
|
|
14
|
+
/**
|
|
15
|
+
* Rejects when ANY resolved address is non-public.
|
|
7
16
|
*
|
|
8
|
-
* A rebinding window remains: the address can change between this check and
|
|
9
|
-
* it
|
|
10
|
-
* exposes.
|
|
17
|
+
* A rebinding window remains when this is called on its own: the address can change between this check and a
|
|
18
|
+
* later connect. {@link UrlIngest.fetchGuarded} closes it by connecting to the address it vetted.
|
|
11
19
|
*/
|
|
12
20
|
static assertPublicHost(opts: {
|
|
13
21
|
hostname: string;
|
|
@@ -19,10 +27,43 @@ export declare class UrlIngest {
|
|
|
19
27
|
policy?: IUrlSafetyPolicy;
|
|
20
28
|
}): Promise<URL>;
|
|
21
29
|
private static readTimeoutMilliseconds;
|
|
30
|
+
/** Settles with `work`, or rejects with the signal's reason when the deadline passes first: a slow resolver must not outlive it. */
|
|
31
|
+
private static withDeadline;
|
|
32
|
+
/** Request headers that name the body; they go when a redirect turns the request into a GET. */
|
|
33
|
+
private static readonly BODY_HEADERS;
|
|
34
|
+
/**
|
|
35
|
+
* The only caller headers a cross-origin redirect keeps. Every other one is dropped: a tenant's webhook secret
|
|
36
|
+
* (`x-api-key`, a signature) must not follow a redirect to a host that tenant did not name.
|
|
37
|
+
*/
|
|
38
|
+
private static readonly CROSS_ORIGIN_HEADERS;
|
|
39
|
+
/** The statuses that name a new location; a 304 or a 300 is an answer, not a redirect. */
|
|
40
|
+
private static readonly REDIRECT_STATUSES;
|
|
41
|
+
/** Connect-phase errors: nothing was sent, so trying the next vetted address cannot deliver a request twice. */
|
|
42
|
+
private static readonly CONNECT_ERROR_CODES;
|
|
43
|
+
private static onlyHeaders;
|
|
44
|
+
private static withoutHeaders;
|
|
45
|
+
/**
|
|
46
|
+
* One request to `target`. With `addresses` it connects to those vetted addresses and nowhere else, in order, and
|
|
47
|
+
* moves to the next one only when the connection itself fails: the url's host is set as the `Host` header, and for
|
|
48
|
+
* https as the TLS server name, so the certificate is still checked against the name the caller asked for.
|
|
49
|
+
*/
|
|
50
|
+
private static send;
|
|
22
51
|
/**
|
|
23
|
-
* A
|
|
24
|
-
*
|
|
25
|
-
*
|
|
52
|
+
* A request that an untrusted url cannot steer inside the network. GET by default; `method`, `headers` and `body`
|
|
53
|
+
* (text or bytes, so a redirect can replay it) make it a signed webhook dispatch.
|
|
54
|
+
*
|
|
55
|
+
* The host is resolved ONCE per hop, every answer is checked, and the request connects to those addresses and to no
|
|
56
|
+
* other, so a DNS answer that changes after the check (DNS rebinding) is never looked up again. The `Host` header and,
|
|
57
|
+
* for https, the TLS server name stay the url's own, so the certificate is verified for that name. A `Host` in
|
|
58
|
+
* `headers` is ignored. The environment's `HTTP_PROXY` / `HTTPS_PROXY` is ignored on a pinned
|
|
59
|
+
* connection, because a proxy would resolve the name itself and undo the pin. `policy.proxy` sends the request through a
|
|
60
|
+
* proxy on purpose; that connection is not pinned (see `IUrlSafetyPolicy.proxy`). `allowPrivateAddress` turns the guard,
|
|
61
|
+
* and so the pin, off.
|
|
62
|
+
*
|
|
63
|
+
* Redirects are followed by hand so every hop is re-checked and re-pinned - `redirect: 'follow'` would land on an
|
|
64
|
+
* internal host with no second look. 301, 302 (for a POST) and 303 turn the request into a GET without a body. A 307 or
|
|
65
|
+
* 308 repeats the request; with a body it is refused when it leaves the origin, so a signed payload is never replayed to
|
|
66
|
+
* a host a redirect chose. When a hop changes origin every caller header is dropped except `accept`, `accept-language` and `user-agent`.
|
|
26
67
|
*
|
|
27
68
|
* The body is returned unread: the caller decides whether to stream it onward or cap it with
|
|
28
69
|
* {@link UrlIngest.readCappedBody}. Nothing is buffered here.
|
|
@@ -30,7 +71,9 @@ export declare class UrlIngest {
|
|
|
30
71
|
static fetchGuarded(opts: {
|
|
31
72
|
url: string;
|
|
32
73
|
policy?: IUrlSafetyPolicy;
|
|
74
|
+
method?: string;
|
|
33
75
|
headers?: Record<string, string>;
|
|
76
|
+
body?: string | Uint8Array;
|
|
34
77
|
}): Promise<Response>;
|
|
35
78
|
/**
|
|
36
79
|
* Enforces `maxBytes` on a stream nothing reads into this process - the shape a caller wants when
|
|
@@ -9,22 +9,22 @@ import { UrlPolicy } from './policy.js';
|
|
|
9
9
|
/** Fetching an untrusted url without letting it reach inside the network. */
|
|
10
10
|
export class UrlIngest {
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
* host that publishes one public A record beside an internal one.
|
|
14
|
-
*
|
|
15
|
-
* A rebinding window remains: the address can change between this check and the connect. Closing
|
|
16
|
-
* it means connecting to the resolved address and setting `Host` by hand, which no fetch API
|
|
17
|
-
* exposes.
|
|
12
|
+
* Resolves `hostname` once and returns every address it answered, refusing the host when ANY of them is non-public.
|
|
13
|
+
* Checking only the first walks straight into a host that publishes one public A record beside an internal one.
|
|
14
|
+
* An IP literal answers itself. `allowPrivateAddress` skips the check and returns an empty list: nothing to pin to.
|
|
18
15
|
*/
|
|
19
|
-
static async
|
|
20
|
-
const { hostname, policy } = opts;
|
|
16
|
+
static async resolvePublicAddresses(opts) {
|
|
17
|
+
const { hostname, policy, signal } = opts;
|
|
21
18
|
if (policy?.allowPrivateAddress ?? UrlSafetyDefaults.ALLOW_PRIVATE_ADDRESS) {
|
|
22
|
-
return;
|
|
19
|
+
return [];
|
|
23
20
|
}
|
|
24
21
|
// `URL.hostname` keeps the brackets on an IPv6 literal; `lookup` does not accept them, and the
|
|
25
22
|
// ENOTFOUND that follows reads as a transient DNS failure rather than the refusal it should be.
|
|
26
23
|
const address = UrlPolicy.readHostAddress({ hostname });
|
|
27
|
-
const addresses = await
|
|
24
|
+
const addresses = await UrlIngest.withDeadline({
|
|
25
|
+
work: lookup(address, { all: true }),
|
|
26
|
+
signal,
|
|
27
|
+
});
|
|
28
28
|
if (!addresses.length) {
|
|
29
29
|
throw getError({
|
|
30
30
|
error: UrlSafetyErrors.URL_REFUSED,
|
|
@@ -38,6 +38,16 @@ export class UrlIngest {
|
|
|
38
38
|
message: `[assertPublicHost] Host resolves to a non-public address | host: ${hostname} | address: ${offender.address}`,
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
|
+
return addresses.map((entry) => entry.address);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Rejects when ANY resolved address is non-public.
|
|
45
|
+
*
|
|
46
|
+
* A rebinding window remains when this is called on its own: the address can change between this check and a
|
|
47
|
+
* later connect. {@link UrlIngest.fetchGuarded} closes it by connecting to the address it vetted.
|
|
48
|
+
*/
|
|
49
|
+
static async assertPublicHost(opts) {
|
|
50
|
+
await UrlIngest.resolvePublicAddresses(opts);
|
|
41
51
|
}
|
|
42
52
|
/** Both halves, in the order they must run: shape first, then the network. */
|
|
43
53
|
static async assertReachableUrl(opts) {
|
|
@@ -50,26 +60,166 @@ export class UrlIngest {
|
|
|
50
60
|
const milliseconds = timeout ? DurationMultipliers.toMilliseconds(timeout) : null;
|
|
51
61
|
return milliseconds ?? UrlSafetyDefaults.TIMEOUT_MILLISECONDS;
|
|
52
62
|
}
|
|
63
|
+
/** Settles with `work`, or rejects with the signal's reason when the deadline passes first: a slow resolver must not outlive it. */
|
|
64
|
+
static async withDeadline(opts) {
|
|
65
|
+
const { work, signal } = opts;
|
|
66
|
+
if (!signal) {
|
|
67
|
+
return work;
|
|
68
|
+
}
|
|
69
|
+
signal.throwIfAborted();
|
|
70
|
+
let onAbort;
|
|
71
|
+
const aborted = new Promise((_resolve, reject) => {
|
|
72
|
+
onAbort = () => reject(signal.reason);
|
|
73
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
74
|
+
});
|
|
75
|
+
try {
|
|
76
|
+
const settled = await Promise.race([work, aborted]);
|
|
77
|
+
return settled;
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
signal.removeEventListener('abort', onAbort);
|
|
81
|
+
// The lookup that lost keeps running; its late answer or error must not surface as an unhandled rejection. Nothing
|
|
82
|
+
// is left to report: the caller already got the deadline's error.
|
|
83
|
+
// eslint-disable-next-line venizia/no-silent-catch -- the work lost the race; its outcome is irrelevant by now
|
|
84
|
+
work.catch(() => undefined);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** Request headers that name the body; they go when a redirect turns the request into a GET. */
|
|
88
|
+
static { this.BODY_HEADERS = ['content-type', 'content-length', 'content-encoding']; }
|
|
89
|
+
/**
|
|
90
|
+
* The only caller headers a cross-origin redirect keeps. Every other one is dropped: a tenant's webhook secret
|
|
91
|
+
* (`x-api-key`, a signature) must not follow a redirect to a host that tenant did not name.
|
|
92
|
+
*/
|
|
93
|
+
static { this.CROSS_ORIGIN_HEADERS = ['accept', 'accept-language', 'user-agent']; }
|
|
94
|
+
/** The statuses that name a new location; a 304 or a 300 is an answer, not a redirect. */
|
|
95
|
+
static { this.REDIRECT_STATUSES = [301, 302, 303, 307, 308]; }
|
|
96
|
+
/** Connect-phase errors: nothing was sent, so trying the next vetted address cannot deliver a request twice. */
|
|
97
|
+
static { this.CONNECT_ERROR_CODES = [
|
|
98
|
+
'ECONNREFUSED',
|
|
99
|
+
'ENETUNREACH',
|
|
100
|
+
'EHOSTUNREACH',
|
|
101
|
+
'EADDRNOTAVAIL',
|
|
102
|
+
]; }
|
|
103
|
+
static onlyHeaders(opts) {
|
|
104
|
+
const { headers, names } = opts;
|
|
105
|
+
return Object.fromEntries(Object.entries(headers).filter(([name]) => names.includes(name.toLowerCase())));
|
|
106
|
+
}
|
|
107
|
+
static withoutHeaders(opts) {
|
|
108
|
+
const { headers, names } = opts;
|
|
109
|
+
return Object.fromEntries(Object.entries(headers).filter(([name]) => !names.includes(name.toLowerCase())));
|
|
110
|
+
}
|
|
53
111
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
112
|
+
* One request to `target`. With `addresses` it connects to those vetted addresses and nowhere else, in order, and
|
|
113
|
+
* moves to the next one only when the connection itself fails: the url's host is set as the `Host` header, and for
|
|
114
|
+
* https as the TLS server name, so the certificate is still checked against the name the caller asked for.
|
|
115
|
+
*/
|
|
116
|
+
static async send(opts) {
|
|
117
|
+
const { target, addresses, init } = opts;
|
|
118
|
+
if (!addresses.length) {
|
|
119
|
+
return fetch(target, init);
|
|
120
|
+
}
|
|
121
|
+
// `proxy: false` is Bun's way to ignore HTTP_PROXY / HTTPS_PROXY for this call (an empty string or undefined keeps
|
|
122
|
+
// the environment's proxy, which would resolve the name itself and undo the pin). A test pins it.
|
|
123
|
+
const noProxy = { proxy: false };
|
|
124
|
+
const isHttps = target.protocol === 'https:';
|
|
125
|
+
const hasNameToVerify = isHttps &&
|
|
126
|
+
!UrlPolicy.readHostAddress({ hostname: target.hostname }).includes(':') &&
|
|
127
|
+
!/^[\d.]+$/.test(target.hostname);
|
|
128
|
+
const headers = {
|
|
129
|
+
...UrlIngest.withoutHeaders({
|
|
130
|
+
headers: init.headers ?? {},
|
|
131
|
+
names: ['host'],
|
|
132
|
+
}),
|
|
133
|
+
Host: target.host,
|
|
134
|
+
};
|
|
135
|
+
let lastError;
|
|
136
|
+
for (const address of addresses) {
|
|
137
|
+
const pinned = new URL(target);
|
|
138
|
+
pinned.hostname = address.includes(':') ? `[${address}]` : address;
|
|
139
|
+
try {
|
|
140
|
+
const result = await fetch(pinned, {
|
|
141
|
+
...init,
|
|
142
|
+
...noProxy,
|
|
143
|
+
headers,
|
|
144
|
+
...(hasNameToVerify ? { tls: { serverName: target.hostname } } : {}),
|
|
145
|
+
});
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
const name = error?.name;
|
|
150
|
+
// The deadline is not a reason to try another address: it is spent.
|
|
151
|
+
if (name === 'AbortError' || name === 'TimeoutError') {
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
// A request that may have reached the server (a reset after the body went out) is not repeated on another
|
|
155
|
+
// address unless it is safe to repeat: a webhook must not be delivered twice.
|
|
156
|
+
const code = error?.code ?? '';
|
|
157
|
+
const method = (init.method ?? 'GET').toUpperCase();
|
|
158
|
+
if (method !== 'GET' &&
|
|
159
|
+
method !== 'HEAD' &&
|
|
160
|
+
!UrlIngest.CONNECT_ERROR_CODES.includes(code)) {
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
lastError = error;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
throw lastError;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* A request that an untrusted url cannot steer inside the network. GET by default; `method`, `headers` and `body`
|
|
170
|
+
* (text or bytes, so a redirect can replay it) make it a signed webhook dispatch.
|
|
171
|
+
*
|
|
172
|
+
* The host is resolved ONCE per hop, every answer is checked, and the request connects to those addresses and to no
|
|
173
|
+
* other, so a DNS answer that changes after the check (DNS rebinding) is never looked up again. The `Host` header and,
|
|
174
|
+
* for https, the TLS server name stay the url's own, so the certificate is verified for that name. A `Host` in
|
|
175
|
+
* `headers` is ignored. The environment's `HTTP_PROXY` / `HTTPS_PROXY` is ignored on a pinned
|
|
176
|
+
* connection, because a proxy would resolve the name itself and undo the pin. `policy.proxy` sends the request through a
|
|
177
|
+
* proxy on purpose; that connection is not pinned (see `IUrlSafetyPolicy.proxy`). `allowPrivateAddress` turns the guard,
|
|
178
|
+
* and so the pin, off.
|
|
179
|
+
*
|
|
180
|
+
* Redirects are followed by hand so every hop is re-checked and re-pinned - `redirect: 'follow'` would land on an
|
|
181
|
+
* internal host with no second look. 301, 302 (for a POST) and 303 turn the request into a GET without a body. A 307 or
|
|
182
|
+
* 308 repeats the request; with a body it is refused when it leaves the origin, so a signed payload is never replayed to
|
|
183
|
+
* a host a redirect chose. When a hop changes origin every caller header is dropped except `accept`, `accept-language` and `user-agent`.
|
|
57
184
|
*
|
|
58
185
|
* The body is returned unread: the caller decides whether to stream it onward or cap it with
|
|
59
186
|
* {@link UrlIngest.readCappedBody}. Nothing is buffered here.
|
|
60
187
|
*/
|
|
61
188
|
static async fetchGuarded(opts) {
|
|
62
|
-
const { url, policy
|
|
189
|
+
const { url, policy } = opts;
|
|
63
190
|
const maxRedirects = policy?.maxRedirects ?? UrlSafetyDefaults.MAX_REDIRECTS;
|
|
64
191
|
const timeoutMilliseconds = UrlIngest.readTimeoutMilliseconds({ policy });
|
|
65
|
-
let
|
|
192
|
+
let method = (opts.method ?? 'GET').toUpperCase();
|
|
193
|
+
let headers = opts.headers ?? {};
|
|
194
|
+
let body = opts.body;
|
|
195
|
+
if (body !== undefined && (method === 'GET' || method === 'HEAD')) {
|
|
196
|
+
throw getError({
|
|
197
|
+
message: `[fetchGuarded] A ${method} request cannot carry a body | url: ${url}`,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
let target = UrlPolicy.assertSafeUrl({ url, policy });
|
|
201
|
+
// ONE deadline for the whole call, every lookup and every hop included, not one per hop.
|
|
202
|
+
const signal = AbortSignal.timeout(timeoutMilliseconds);
|
|
66
203
|
for (let hop = 0; hop <= maxRedirects; hop += 1) {
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
204
|
+
const addresses = await UrlIngest.resolvePublicAddresses({
|
|
205
|
+
hostname: target.hostname,
|
|
206
|
+
policy,
|
|
207
|
+
signal,
|
|
208
|
+
});
|
|
209
|
+
const response = await UrlIngest.send({
|
|
210
|
+
target,
|
|
211
|
+
// A proxy resolves the name itself, so there is nothing to pin: the caller chose it (`policy.proxy`).
|
|
212
|
+
addresses: policy?.proxy ? [] : addresses,
|
|
213
|
+
init: {
|
|
214
|
+
method,
|
|
215
|
+
...(policy?.proxy ? { proxy: policy.proxy } : {}),
|
|
216
|
+
redirect: 'manual',
|
|
217
|
+
signal,
|
|
218
|
+
...(Object.keys(headers).length ? { headers } : {}),
|
|
219
|
+
...(body === undefined ? {} : { body }),
|
|
220
|
+
},
|
|
71
221
|
});
|
|
72
|
-
if (
|
|
222
|
+
if (!UrlIngest.REDIRECT_STATUSES.includes(response.status)) {
|
|
73
223
|
if (!response.ok) {
|
|
74
224
|
// Cancelled, or the socket stays open on every 4xx and 5xx.
|
|
75
225
|
await response.body?.cancel();
|
|
@@ -87,11 +237,25 @@ export class UrlIngest {
|
|
|
87
237
|
message: `[fetchGuarded] Redirect carried no location | url: ${url}`,
|
|
88
238
|
});
|
|
89
239
|
}
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
240
|
+
const next = UrlPolicy.assertSafeUrl({ url: new URL(location, target).toString(), policy });
|
|
241
|
+
const isSameOrigin = next.origin === target.origin;
|
|
242
|
+
const becomesGet = response.status === 303 ||
|
|
243
|
+
((response.status === 301 || response.status === 302) && method === 'POST');
|
|
244
|
+
if (becomesGet) {
|
|
245
|
+
method = 'GET';
|
|
246
|
+
body = undefined;
|
|
247
|
+
headers = UrlIngest.withoutHeaders({ headers, names: UrlIngest.BODY_HEADERS });
|
|
248
|
+
}
|
|
249
|
+
else if (body !== undefined && !isSameOrigin) {
|
|
250
|
+
throw getError({
|
|
251
|
+
error: UrlSafetyErrors.URL_REFUSED,
|
|
252
|
+
message: `[fetchGuarded] Redirect would replay the request body to another origin | from: ${target.origin} | to: ${next.origin}`,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
if (!isSameOrigin) {
|
|
256
|
+
headers = UrlIngest.onlyHeaders({ headers, names: UrlIngest.CROSS_ORIGIN_HEADERS });
|
|
257
|
+
}
|
|
258
|
+
target = next;
|
|
95
259
|
}
|
|
96
260
|
throw getError({
|
|
97
261
|
error: UrlSafetyErrors.URL_REFUSED,
|