@sockethub/util 1.0.0-alpha.1
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/LICENSE +165 -0
- package/dist/crypto/index.d.ts +28 -0
- package/dist/crypto/index.js +10941 -0
- package/dist/crypto/index.js.map +12 -0
- package/dist/error/index.d.ts +11 -0
- package/dist/error/index.js +53 -0
- package/dist/error/index.js.map +10 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +252 -0
- package/dist/index.js.map +14 -0
- package/dist/net/address.d.ts +11 -0
- package/dist/net/dispatcher.d.ts +39 -0
- package/dist/net/fetch.d.ts +32 -0
- package/dist/net/index.d.ts +4 -0
- package/dist/net/index.js +236 -0
- package/dist/net/index.js.map +13 -0
- package/dist/net/url.d.ts +14 -0
- package/package.json +72 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalize an unknown thrown/rejected value into an `Error`. Replaces the
|
|
3
|
+
* repeated `x instanceof Error ? x : new Error(String(x))` idiom so error
|
|
4
|
+
* handling is consistent across packages.
|
|
5
|
+
*/
|
|
6
|
+
export declare function toError(value: unknown): Error;
|
|
7
|
+
/**
|
|
8
|
+
* Extract a human-readable message from an unknown thrown/rejected value.
|
|
9
|
+
* Returns the `Error.message` for `Error` instances, otherwise `String(value)`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function errorMessage(value: unknown): string;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
function __accessProp(key) {
|
|
8
|
+
return this[key];
|
|
9
|
+
}
|
|
10
|
+
var __toESMCache_node;
|
|
11
|
+
var __toESMCache_esm;
|
|
12
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
13
|
+
var canCache = mod != null && typeof mod === "object";
|
|
14
|
+
if (canCache) {
|
|
15
|
+
var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
|
|
16
|
+
var cached = cache.get(mod);
|
|
17
|
+
if (cached)
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
20
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
21
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
22
|
+
for (let key of __getOwnPropNames(mod))
|
|
23
|
+
if (!__hasOwnProp.call(to, key))
|
|
24
|
+
__defProp(to, key, {
|
|
25
|
+
get: __accessProp.bind(mod, key),
|
|
26
|
+
enumerable: true
|
|
27
|
+
});
|
|
28
|
+
if (canCache)
|
|
29
|
+
cache.set(mod, to);
|
|
30
|
+
return to;
|
|
31
|
+
};
|
|
32
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
33
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
34
|
+
|
|
35
|
+
// src/error/index.ts
|
|
36
|
+
function toError(value) {
|
|
37
|
+
if (value instanceof Error) {
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
return new Error(errorMessage(value));
|
|
41
|
+
}
|
|
42
|
+
function errorMessage(value) {
|
|
43
|
+
if (value instanceof Error) {
|
|
44
|
+
return value.message;
|
|
45
|
+
}
|
|
46
|
+
return String(value);
|
|
47
|
+
}
|
|
48
|
+
export {
|
|
49
|
+
toError,
|
|
50
|
+
errorMessage
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
//# debugId=DD89AD6BD88C5C2164756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/error/index.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"/**\n * Normalize an unknown thrown/rejected value into an `Error`. Replaces the\n * repeated `x instanceof Error ? x : new Error(String(x))` idiom so error\n * handling is consistent across packages.\n */\nexport function toError(value: unknown): Error {\n if (value instanceof Error) {\n return value;\n }\n return new Error(errorMessage(value));\n}\n\n/**\n * Extract a human-readable message from an unknown thrown/rejected value.\n * Returns the `Error.message` for `Error` instances, otherwise `String(value)`.\n */\nexport function errorMessage(value: unknown): string {\n if (value instanceof Error) {\n return value.message;\n }\n return String(value);\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKO,SAAS,OAAO,CAAC,OAAuB;AAAA,EAC3C,IAAI,iBAAiB,OAAO;AAAA,IACxB,OAAO;AAAA,EACX;AAAA,EACA,OAAO,IAAI,MAAM,aAAa,KAAK,CAAC;AAAA;AAOjC,SAAS,YAAY,CAAC,OAAwB;AAAA,EACjD,IAAI,iBAAiB,OAAO;AAAA,IACxB,OAAO,MAAM;AAAA,EACjB;AAAA,EACA,OAAO,OAAO,KAAK;AAAA;",
|
|
8
|
+
"debugId": "DD89AD6BD88C5C2164756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
function __accessProp(key) {
|
|
8
|
+
return this[key];
|
|
9
|
+
}
|
|
10
|
+
var __toESMCache_node;
|
|
11
|
+
var __toESMCache_esm;
|
|
12
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
13
|
+
var canCache = mod != null && typeof mod === "object";
|
|
14
|
+
if (canCache) {
|
|
15
|
+
var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
|
|
16
|
+
var cached = cache.get(mod);
|
|
17
|
+
if (cached)
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
20
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
21
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
22
|
+
for (let key of __getOwnPropNames(mod))
|
|
23
|
+
if (!__hasOwnProp.call(to, key))
|
|
24
|
+
__defProp(to, key, {
|
|
25
|
+
get: __accessProp.bind(mod, key),
|
|
26
|
+
enumerable: true
|
|
27
|
+
});
|
|
28
|
+
if (canCache)
|
|
29
|
+
cache.set(mod, to);
|
|
30
|
+
return to;
|
|
31
|
+
};
|
|
32
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
33
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
34
|
+
|
|
35
|
+
// src/error/index.ts
|
|
36
|
+
function toError(value) {
|
|
37
|
+
if (value instanceof Error) {
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
return new Error(errorMessage(value));
|
|
41
|
+
}
|
|
42
|
+
function errorMessage(value) {
|
|
43
|
+
if (value instanceof Error) {
|
|
44
|
+
return value.message;
|
|
45
|
+
}
|
|
46
|
+
return String(value);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// src/net/address.ts
|
|
50
|
+
function isBlockedAddress(ip) {
|
|
51
|
+
const addr = ip.trim().toLowerCase();
|
|
52
|
+
if (addr.includes(":")) {
|
|
53
|
+
return isBlockedIpv6(addr);
|
|
54
|
+
}
|
|
55
|
+
return isBlockedIpv4(addr);
|
|
56
|
+
}
|
|
57
|
+
function isBlockedIpv4(ip) {
|
|
58
|
+
const parts = ip.split(".").map((p) => Number(p));
|
|
59
|
+
if (parts.length !== 4 || parts.some((p) => !Number.isInteger(p) || p < 0 || p > 255)) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
const [a, b] = parts;
|
|
63
|
+
if (a === 0)
|
|
64
|
+
return true;
|
|
65
|
+
if (a === 127)
|
|
66
|
+
return true;
|
|
67
|
+
if (a === 10)
|
|
68
|
+
return true;
|
|
69
|
+
if (a === 172 && b >= 16 && b <= 31)
|
|
70
|
+
return true;
|
|
71
|
+
if (a === 192 && b === 168)
|
|
72
|
+
return true;
|
|
73
|
+
if (a === 169 && b === 254)
|
|
74
|
+
return true;
|
|
75
|
+
if (a === 100 && b >= 64 && b <= 127)
|
|
76
|
+
return true;
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
function expandIpv6(address) {
|
|
80
|
+
let addr = address;
|
|
81
|
+
const dotted = addr.match(/^(.+:)(\d{1,3}(?:\.\d{1,3}){3})$/);
|
|
82
|
+
if (dotted) {
|
|
83
|
+
const octets = dotted[2].split(".").map(Number);
|
|
84
|
+
if (octets.some((o) => !Number.isInteger(o) || o < 0 || o > 255)) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
const hi = (octets[0] << 8 | octets[1]).toString(16);
|
|
88
|
+
const lo = (octets[2] << 8 | octets[3]).toString(16);
|
|
89
|
+
addr = `${dotted[1]}${hi}:${lo}`;
|
|
90
|
+
}
|
|
91
|
+
const halves = addr.split("::");
|
|
92
|
+
if (halves.length > 2)
|
|
93
|
+
return null;
|
|
94
|
+
const head = halves[0] ? halves[0].split(":") : [];
|
|
95
|
+
const tail = halves.length === 2 && halves[1] ? halves[1].split(":") : [];
|
|
96
|
+
if (halves.length === 2 ? head.length + tail.length > 8 : head.length !== 8)
|
|
97
|
+
return null;
|
|
98
|
+
const groupsStr = halves.length === 2 ? [
|
|
99
|
+
...head,
|
|
100
|
+
...new Array(8 - head.length - tail.length).fill("0"),
|
|
101
|
+
...tail
|
|
102
|
+
] : head;
|
|
103
|
+
const groups = [];
|
|
104
|
+
for (const group of groupsStr) {
|
|
105
|
+
if (!/^[0-9a-f]{1,4}$/.test(group))
|
|
106
|
+
return null;
|
|
107
|
+
groups.push(Number.parseInt(group, 16));
|
|
108
|
+
}
|
|
109
|
+
return groups;
|
|
110
|
+
}
|
|
111
|
+
function extractEmbeddedIpv4(groups) {
|
|
112
|
+
const [a, b, c, d, e, f, g, h] = groups;
|
|
113
|
+
const zeroPrefix = a === 0 && b === 0 && c === 0 && d === 0 && e === 0;
|
|
114
|
+
const mapped = zeroPrefix && f === 65535;
|
|
115
|
+
const compat = zeroPrefix && f === 0;
|
|
116
|
+
const nat64 = a === 100 && b === 65435 && c === 0 && d === 0 && e === 0 && f === 0;
|
|
117
|
+
if (!mapped && !compat && !nat64)
|
|
118
|
+
return null;
|
|
119
|
+
return `${g >> 8}.${g & 255}.${h >> 8}.${h & 255}`;
|
|
120
|
+
}
|
|
121
|
+
function isBlockedIpv6(ip) {
|
|
122
|
+
const addr = ip.split("%")[0];
|
|
123
|
+
const groups = expandIpv6(addr);
|
|
124
|
+
if (!groups) {
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
const embedded = extractEmbeddedIpv4(groups);
|
|
128
|
+
if (embedded) {
|
|
129
|
+
return isBlockedIpv4(embedded);
|
|
130
|
+
}
|
|
131
|
+
if ((groups[0] & 65024) === 64512)
|
|
132
|
+
return true;
|
|
133
|
+
if ((groups[0] & 65472) === 65152)
|
|
134
|
+
return true;
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
// src/net/dispatcher.ts
|
|
138
|
+
import { lookup as dnsLookup } from "node:dns";
|
|
139
|
+
import { Agent } from "undici";
|
|
140
|
+
var DEFAULT_MAX_RESPONSE_BYTES = 5 * 1024 * 1024;
|
|
141
|
+
function createGuardedLookup(allowPrivateAddresses) {
|
|
142
|
+
return (hostname, options, callback) => {
|
|
143
|
+
const opts = typeof options === "number" ? { family: options } : options ?? {};
|
|
144
|
+
dnsLookup(hostname, { ...opts, all: true }, (err, addresses) => {
|
|
145
|
+
if (err) {
|
|
146
|
+
callback(err);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
if (!addresses || addresses.length === 0) {
|
|
150
|
+
callback(Object.assign(new Error(`could not resolve ${hostname}`), {
|
|
151
|
+
code: "ENOTFOUND"
|
|
152
|
+
}));
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (!allowPrivateAddresses) {
|
|
156
|
+
for (const { address } of addresses) {
|
|
157
|
+
if (isBlockedAddress(address)) {
|
|
158
|
+
callback(new Error(`blocked non-public address ${address} for ${hostname}`));
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (opts.all) {
|
|
164
|
+
callback(null, addresses);
|
|
165
|
+
} else {
|
|
166
|
+
callback(null, addresses[0].address, addresses[0].family);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function createGuardedDispatcher(options = {}) {
|
|
172
|
+
const {
|
|
173
|
+
allowPrivateAddresses = false,
|
|
174
|
+
maxResponseBytes = DEFAULT_MAX_RESPONSE_BYTES
|
|
175
|
+
} = options;
|
|
176
|
+
return new Agent({
|
|
177
|
+
maxResponseSize: maxResponseBytes,
|
|
178
|
+
connect: {
|
|
179
|
+
lookup: createGuardedLookup(allowPrivateAddresses)
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
// src/net/url.ts
|
|
184
|
+
function redactUrl(url) {
|
|
185
|
+
try {
|
|
186
|
+
const parsed = new URL(url);
|
|
187
|
+
if (parsed.username || parsed.password) {
|
|
188
|
+
parsed.username = "";
|
|
189
|
+
parsed.password = "";
|
|
190
|
+
return parsed.toString();
|
|
191
|
+
}
|
|
192
|
+
return url;
|
|
193
|
+
} catch {
|
|
194
|
+
return url.replace(/\/\/[^/@\s]*@/, "//***@");
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
function assertHttpUrl(url) {
|
|
198
|
+
let parsed;
|
|
199
|
+
try {
|
|
200
|
+
parsed = new URL(url);
|
|
201
|
+
} catch {
|
|
202
|
+
throw new Error(`invalid URL: ${redactUrl(url)}`);
|
|
203
|
+
}
|
|
204
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
205
|
+
throw new Error(`unsupported scheme ${parsed.protocol} for ${redactUrl(url)}`);
|
|
206
|
+
}
|
|
207
|
+
return parsed;
|
|
208
|
+
}
|
|
209
|
+
function isHttpUrl(url) {
|
|
210
|
+
try {
|
|
211
|
+
assertHttpUrl(url);
|
|
212
|
+
return true;
|
|
213
|
+
} catch {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// src/net/fetch.ts
|
|
219
|
+
async function safeFetch(url, options = {}) {
|
|
220
|
+
assertHttpUrl(url);
|
|
221
|
+
const dispatcher = options.dispatcher ?? createGuardedDispatcher({
|
|
222
|
+
allowPrivateAddresses: options.allowPrivateAddresses,
|
|
223
|
+
maxResponseBytes: options.maxResponseBytes
|
|
224
|
+
});
|
|
225
|
+
const init = { dispatcher };
|
|
226
|
+
if (options.timeoutMs !== undefined && options.timeoutMs > 0) {
|
|
227
|
+
init.signal = AbortSignal.timeout(options.timeoutMs);
|
|
228
|
+
}
|
|
229
|
+
if (options.headers) {
|
|
230
|
+
init.headers = options.headers;
|
|
231
|
+
}
|
|
232
|
+
const res = await fetch(url, init);
|
|
233
|
+
if (!res.ok) {
|
|
234
|
+
await res.body?.cancel().catch(() => {});
|
|
235
|
+
const statusText = res.statusText ? ` ${res.statusText}` : "";
|
|
236
|
+
throw new Error(`request failed: ${res.status}${statusText} for ${redactUrl(url)}`);
|
|
237
|
+
}
|
|
238
|
+
return res;
|
|
239
|
+
}
|
|
240
|
+
export {
|
|
241
|
+
toError,
|
|
242
|
+
safeFetch,
|
|
243
|
+
redactUrl,
|
|
244
|
+
isHttpUrl,
|
|
245
|
+
isBlockedAddress,
|
|
246
|
+
errorMessage,
|
|
247
|
+
createGuardedDispatcher,
|
|
248
|
+
assertHttpUrl,
|
|
249
|
+
DEFAULT_MAX_RESPONSE_BYTES
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
//# debugId=FDAE0275EA985B4364756E2164756E21
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/error/index.ts", "../src/net/address.ts", "../src/net/dispatcher.ts", "../src/net/url.ts", "../src/net/fetch.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"/**\n * Normalize an unknown thrown/rejected value into an `Error`. Replaces the\n * repeated `x instanceof Error ? x : new Error(String(x))` idiom so error\n * handling is consistent across packages.\n */\nexport function toError(value: unknown): Error {\n if (value instanceof Error) {\n return value;\n }\n return new Error(errorMessage(value));\n}\n\n/**\n * Extract a human-readable message from an unknown thrown/rejected value.\n * Returns the `Error.message` for `Error` instances, otherwise `String(value)`.\n */\nexport function errorMessage(value: unknown): string {\n if (value instanceof Error) {\n return value.message;\n }\n return String(value);\n}\n",
|
|
6
|
+
"/**\n * IP address classification for SSRF defense. Used to decide whether a server\n * fetching client-supplied URLs may connect to a given resolved address.\n */\n\n/**\n * Returns true if `ip` is a loopback, private, link-local, or otherwise\n * non-public address that should not be reachable by a server fetching\n * client-supplied URLs (SSRF defense). Handles IPv4, IPv6, and IPv4-mapped\n * IPv6 addresses.\n */\nexport function isBlockedAddress(ip: string): boolean {\n const addr = ip.trim().toLowerCase();\n\n if (addr.includes(\":\")) {\n return isBlockedIpv6(addr);\n }\n\n return isBlockedIpv4(addr);\n}\n\nfunction isBlockedIpv4(ip: string): boolean {\n const parts = ip.split(\".\").map((p) => Number(p));\n if (\n parts.length !== 4 ||\n parts.some((p) => !Number.isInteger(p) || p < 0 || p > 255)\n ) {\n // Not a parseable IPv4 literal; block conservatively.\n return true;\n }\n const [a, b] = parts;\n // 0.0.0.0/8 (\"this network\", commonly routes to localhost)\n if (a === 0) return true;\n // 127.0.0.0/8 loopback\n if (a === 127) return true;\n // 10.0.0.0/8 private\n if (a === 10) return true;\n // 172.16.0.0/12 private\n if (a === 172 && b >= 16 && b <= 31) return true;\n // 192.168.0.0/16 private\n if (a === 192 && b === 168) return true;\n // 169.254.0.0/16 link-local (incl. 169.254.169.254 cloud metadata)\n if (a === 169 && b === 254) return true;\n // 100.64.0.0/10 carrier-grade NAT shared address space\n if (a === 100 && b >= 64 && b <= 127) return true;\n return false;\n}\n\n/**\n * Expand an IPv6 literal (lowercased, zone index already stripped) to its\n * eight 16-bit groups, or null if unparseable. A trailing dotted-quad\n * (e.g. ::ffff:127.0.0.1) is converted to two hex groups first, so every\n * notation of the same address normalizes identically.\n */\nfunction expandIpv6(address: string): Array<number> | null {\n let addr = address;\n const dotted = addr.match(/^(.+:)(\\d{1,3}(?:\\.\\d{1,3}){3})$/);\n if (dotted) {\n const octets = dotted[2].split(\".\").map(Number);\n if (octets.some((o) => !Number.isInteger(o) || o < 0 || o > 255)) {\n return null;\n }\n const hi = ((octets[0] << 8) | octets[1]).toString(16);\n const lo = ((octets[2] << 8) | octets[3]).toString(16);\n addr = `${dotted[1]}${hi}:${lo}`;\n }\n\n const halves = addr.split(\"::\");\n if (halves.length > 2) return null;\n const head = halves[0] ? halves[0].split(\":\") : [];\n const tail = halves.length === 2 && halves[1] ? halves[1].split(\":\") : [];\n if (halves.length === 2 ? head.length + tail.length > 8 : head.length !== 8)\n return null;\n const groupsStr =\n halves.length === 2\n ? [\n ...head,\n ...new Array(8 - head.length - tail.length).fill(\"0\"),\n ...tail,\n ]\n : head;\n\n const groups: Array<number> = [];\n for (const group of groupsStr) {\n if (!/^[0-9a-f]{1,4}$/.test(group)) return null;\n groups.push(Number.parseInt(group, 16));\n }\n return groups;\n}\n\n/**\n * If the expanded IPv6 groups embed an IPv4 address — IPv4-mapped\n * (::ffff:0:0/96), deprecated IPv4-compatible (::/96, which also covers\n * `::`/`::1`), or the NAT64 well-known prefix (64:ff9b::/96) — return it in\n * dotted-quad form so IPv4 rules apply; otherwise return null.\n */\nfunction extractEmbeddedIpv4(groups: Array<number>): string | null {\n const [a, b, c, d, e, f, g, h] = groups;\n const zeroPrefix = a === 0 && b === 0 && c === 0 && d === 0 && e === 0;\n const mapped = zeroPrefix && f === 0xffff;\n const compat = zeroPrefix && f === 0;\n const nat64 =\n a === 0x64 && b === 0xff9b && c === 0 && d === 0 && e === 0 && f === 0;\n if (!mapped && !compat && !nat64) return null;\n return `${g >> 8}.${g & 0xff}.${h >> 8}.${h & 0xff}`;\n}\n\nfunction isBlockedIpv6(ip: string): boolean {\n // Strip zone index (e.g. fe80::1%eth0).\n const addr = ip.split(\"%\")[0];\n // Normalize to 8 groups so hex/dotted/compressed/uppercase spellings of\n // the same address are classified identically (e.g. ::ffff:7f00:1 and\n // ::ffff:127.0.0.1 are both IPv4-mapped loopback).\n const groups = expandIpv6(addr);\n if (!groups) {\n // Not a parseable IPv6 literal; block conservatively.\n return true;\n }\n // Embedded IPv4 (mapped/compat/NAT64) -> apply IPv4 rules. This also\n // covers :: (0.0.0.0) and ::1 (0.0.0.1), both within blocked 0.0.0.0/8.\n const embedded = extractEmbeddedIpv4(groups);\n if (embedded) {\n return isBlockedIpv4(embedded);\n }\n // fc00::/7 unique-local.\n if ((groups[0] & 0xfe00) === 0xfc00) return true;\n // fe80::/10 link-local.\n if ((groups[0] & 0xffc0) === 0xfe80) return true;\n return false;\n}\n",
|
|
7
|
+
"import { lookup as dnsLookup, type LookupAddress } from \"node:dns\";\nimport type { LookupFunction } from \"node:net\";\nimport { Agent } from \"undici\";\nimport { isBlockedAddress } from \"./address.js\";\n\n/** Default response-body cap (5 MiB) applied by a guarded dispatcher. */\nexport const DEFAULT_MAX_RESPONSE_BYTES = 5 * 1024 * 1024;\n\nexport interface GuardedDispatcherOptions {\n /**\n * When true, skip the private/loopback destination checks. Dev/test escape\n * hatch only — never enable on a server that accepts untrusted requests.\n */\n allowPrivateAddresses?: boolean;\n /** Cap on response body size in bytes. Defaults to 5 MiB. */\n maxResponseBytes?: number;\n}\n\ntype LookupOptions = { all?: boolean; family?: number } & Record<\n string,\n unknown\n>;\ntype LookupCallback = (\n err: NodeJS.ErrnoException | null,\n address?: string | Array<LookupAddress>,\n family?: number,\n) => void;\n\n/**\n * A DNS lookup that resolves a hostname and rejects the connection if any\n * resolved address is private/loopback/blocked. Because this runs at the\n * connection layer, it validates the address actually being connected to —\n * including on every redirect hop — closing the check-then-fetch (TOCTOU)\n * rebinding gap that a pre-fetch URL check leaves open.\n *\n * Exported for direct unit testing: the Bun test runner intercepts `fetch` and\n * ignores undici dispatchers, so the lookup decision is verified here rather\n * than through an end-to-end fetch.\n */\nexport function createGuardedLookup(\n allowPrivateAddresses: boolean,\n): LookupFunction {\n return (\n hostname: string,\n // net.connect passes a LookupOptions object; the legacy dns.lookup\n // signature allows a numeric `family`, which we preserve.\n options: LookupOptions | number,\n callback: LookupCallback,\n ): void => {\n const opts: LookupOptions =\n typeof options === \"number\" ? { family: options } : (options ?? {});\n dnsLookup(hostname, { ...opts, all: true }, (err, addresses) => {\n if (err) {\n callback(err);\n return;\n }\n if (!addresses || addresses.length === 0) {\n callback(\n Object.assign(new Error(`could not resolve ${hostname}`), {\n code: \"ENOTFOUND\",\n }),\n );\n return;\n }\n if (!allowPrivateAddresses) {\n for (const { address } of addresses) {\n if (isBlockedAddress(address)) {\n callback(\n new Error(\n `blocked non-public address ${address} for ${hostname}`,\n ),\n );\n return;\n }\n }\n }\n if (opts.all) {\n callback(null, addresses);\n } else {\n callback(null, addresses[0].address, addresses[0].family);\n }\n });\n };\n}\n\n/**\n * Build an undici {@link Agent} that defends a server fetching client-supplied\n * URLs against SSRF and memory exhaustion:\n *\n * - Every connection (including each redirect hop) resolves DNS and refuses to\n * connect to a private/loopback/link-local/metadata address (unless\n * `allowPrivateAddresses` is set).\n * - Responses larger than `maxResponseBytes` are aborted.\n *\n * Pass the returned dispatcher to `fetch(url, { dispatcher })`, or to any\n * library that forwards fetch options (e.g. open-graph-scraper's\n * `fetchOptions`).\n */\nexport function createGuardedDispatcher(\n options: GuardedDispatcherOptions = {},\n): Agent {\n const {\n allowPrivateAddresses = false,\n maxResponseBytes = DEFAULT_MAX_RESPONSE_BYTES,\n } = options;\n return new Agent({\n maxResponseSize: maxResponseBytes,\n connect: {\n lookup: createGuardedLookup(allowPrivateAddresses),\n },\n });\n}\n",
|
|
8
|
+
"/**\n * Strip any `user:password@` userinfo from a URL so it is safe to include in\n * error messages, logs, or telemetry. Best-effort for unparseable input.\n */\nexport function redactUrl(url: string): string {\n try {\n const parsed = new URL(url);\n if (parsed.username || parsed.password) {\n parsed.username = \"\";\n parsed.password = \"\";\n return parsed.toString();\n }\n return url;\n } catch {\n return url.replace(/\\/\\/[^/@\\s]*@/, \"//***@\");\n }\n}\n\n/**\n * Parse `url` and require an `http:`/`https:` scheme, returning the parsed\n * `URL`. Throws a descriptive `Error` (with credentials redacted) on a\n * malformed URL or unsupported scheme. Used to gate outbound fetches of\n * client-supplied URLs.\n */\nexport function assertHttpUrl(url: string): URL {\n let parsed: URL;\n try {\n parsed = new URL(url);\n } catch {\n throw new Error(`invalid URL: ${redactUrl(url)}`);\n }\n if (parsed.protocol !== \"http:\" && parsed.protocol !== \"https:\") {\n throw new Error(\n `unsupported scheme ${parsed.protocol} for ${redactUrl(url)}`,\n );\n }\n return parsed;\n}\n\n/** Returns true if `url` parses and uses an `http:`/`https:` scheme. */\nexport function isHttpUrl(url: string): boolean {\n try {\n assertHttpUrl(url);\n return true;\n } catch {\n return false;\n }\n}\n",
|
|
9
|
+
"import type { Agent } from \"undici\";\nimport { createGuardedDispatcher } from \"./dispatcher.js\";\nimport { assertHttpUrl, redactUrl } from \"./url.js\";\n\nexport interface SafeFetchOptions {\n /**\n * Skip the private/loopback destination checks. Dev/test escape hatch only.\n * Ignored when an explicit `dispatcher` is provided.\n */\n allowPrivateAddresses?: boolean;\n /** Response body cap in bytes. Ignored when `dispatcher` is provided. */\n maxResponseBytes?: number;\n /** Per-request timeout (applied via AbortSignal.timeout). */\n timeoutMs?: number;\n /**\n * A pre-built guarded dispatcher to reuse (for connection pooling across\n * requests). When omitted, a guarded dispatcher is created per call from\n * `allowPrivateAddresses`/`maxResponseBytes`.\n */\n dispatcher?: Agent;\n /** Extra request headers. */\n headers?: Record<string, string>;\n}\n\n/**\n * SSRF- and resource-guarded outbound fetch for client-supplied URLs:\n *\n * - rejects non-`http(s)` schemes and malformed URLs before connecting;\n * - routes the request through a guarded undici dispatcher that refuses\n * private/loopback/metadata destinations (every redirect hop) and caps the\n * response body;\n * - applies an optional timeout and throws on a non-2xx response.\n *\n * Returns the `Response` (read the body with `.text()`/`.json()`/etc.).\n */\nexport async function safeFetch(\n url: string,\n options: SafeFetchOptions = {},\n): Promise<Response> {\n assertHttpUrl(url);\n\n const dispatcher =\n options.dispatcher ??\n createGuardedDispatcher({\n allowPrivateAddresses: options.allowPrivateAddresses,\n maxResponseBytes: options.maxResponseBytes,\n });\n\n const init: RequestInit & { dispatcher?: unknown } = { dispatcher };\n if (options.timeoutMs !== undefined && options.timeoutMs > 0) {\n init.signal = AbortSignal.timeout(options.timeoutMs);\n }\n if (options.headers) {\n init.headers = options.headers;\n }\n\n const res = await fetch(url, init as RequestInit);\n if (!res.ok) {\n // Drain the unused body so the keep-alive connection is returned to the\n // pool instead of leaking until GC.\n await res.body?.cancel().catch(() => {});\n // HTTP/2 has no reason phrases, so statusText may be empty.\n const statusText = res.statusText ? ` ${res.statusText}` : \"\";\n throw new Error(\n `request failed: ${res.status}${statusText} for ${redactUrl(url)}`,\n );\n }\n return res;\n}\n"
|
|
10
|
+
],
|
|
11
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKO,SAAS,OAAO,CAAC,OAAuB;AAAA,EAC3C,IAAI,iBAAiB,OAAO;AAAA,IACxB,OAAO;AAAA,EACX;AAAA,EACA,OAAO,IAAI,MAAM,aAAa,KA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AAsB,OAAO,gBAAgB,UAAU,GAAG,GAC9D;AAAA,EACJ;AAAA,EACA,OAAO;AAAA;AAIJ,SAAS,SAAS,CAAC,KAAsB;AAAA,EAC5C,IAAI;AAAA,IACA,cAAc,GAAG;AAAA,IACjB,OAAO;AAAA,IACT,MAAM;AAAA,IACJ,OAAO;AAAA;AAAA;;;ACVf,eAAsB,SAAS,CAC3B,KACA,UAA4B,CAAC,GACZ;AAAA,EACjB,cAAc,GAAG;AAAA,EAEjB,MAAM,aACF,QAAQ,cACR,wBAAwB;AAAA,IACpB,uBAAuB,QAAQ;AAAA,IAC/B,kBAAkB,QAAQ;AAAA,EAC9B,CAAC;AAAA,EAEL,MAAM,OAA+C,EAAE,WAAW;AAAA,EAClE,IAAI,QAAQ,cAAc,aAAa,QAAQ,YAAY,GAAG;AAAA,IAC1D,KAAK,SAAS,YAAY,QAAQ,QAAQ,SAAS;AAAA,EACvD;AAAA,EACA,IAAI,QAAQ,SAAS;AAAA,IACjB,KAAK,UAAU,QAAQ;AAAA,EAC3B;AAAA,EAEA,MAAM,MAAM,MAAM,MAAM,KAAK,IAAmB;AAAA,EAChD,IAAI,CAAC,IAAI,IAAI;AAAA,IAGT,MAAM,IAAI,MAAM,OAAO,EAAE,MAAM,MAAM,EAAE;AAAA,IAEvC,MAAM,aAAa,IAAI,aAAa,IAAI,IAAI,eAAe;AAAA,IAC3D,MAAM,IAAI,MACN,mBAAmB,IAAI,SAAS,kBAAkB,UAAU,GAAG,GACnE;AAAA,EACJ;AAAA,EACA,OAAO;AAAA;",
|
|
12
|
+
"debugId": "FDAE0275EA985B4364756E2164756E21",
|
|
13
|
+
"names": []
|
|
14
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IP address classification for SSRF defense. Used to decide whether a server
|
|
3
|
+
* fetching client-supplied URLs may connect to a given resolved address.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Returns true if `ip` is a loopback, private, link-local, or otherwise
|
|
7
|
+
* non-public address that should not be reachable by a server fetching
|
|
8
|
+
* client-supplied URLs (SSRF defense). Handles IPv4, IPv6, and IPv4-mapped
|
|
9
|
+
* IPv6 addresses.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isBlockedAddress(ip: string): boolean;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { LookupFunction } from "node:net";
|
|
2
|
+
import { Agent } from "undici";
|
|
3
|
+
/** Default response-body cap (5 MiB) applied by a guarded dispatcher. */
|
|
4
|
+
export declare const DEFAULT_MAX_RESPONSE_BYTES: number;
|
|
5
|
+
export interface GuardedDispatcherOptions {
|
|
6
|
+
/**
|
|
7
|
+
* When true, skip the private/loopback destination checks. Dev/test escape
|
|
8
|
+
* hatch only — never enable on a server that accepts untrusted requests.
|
|
9
|
+
*/
|
|
10
|
+
allowPrivateAddresses?: boolean;
|
|
11
|
+
/** Cap on response body size in bytes. Defaults to 5 MiB. */
|
|
12
|
+
maxResponseBytes?: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A DNS lookup that resolves a hostname and rejects the connection if any
|
|
16
|
+
* resolved address is private/loopback/blocked. Because this runs at the
|
|
17
|
+
* connection layer, it validates the address actually being connected to —
|
|
18
|
+
* including on every redirect hop — closing the check-then-fetch (TOCTOU)
|
|
19
|
+
* rebinding gap that a pre-fetch URL check leaves open.
|
|
20
|
+
*
|
|
21
|
+
* Exported for direct unit testing: the Bun test runner intercepts `fetch` and
|
|
22
|
+
* ignores undici dispatchers, so the lookup decision is verified here rather
|
|
23
|
+
* than through an end-to-end fetch.
|
|
24
|
+
*/
|
|
25
|
+
export declare function createGuardedLookup(allowPrivateAddresses: boolean): LookupFunction;
|
|
26
|
+
/**
|
|
27
|
+
* Build an undici {@link Agent} that defends a server fetching client-supplied
|
|
28
|
+
* URLs against SSRF and memory exhaustion:
|
|
29
|
+
*
|
|
30
|
+
* - Every connection (including each redirect hop) resolves DNS and refuses to
|
|
31
|
+
* connect to a private/loopback/link-local/metadata address (unless
|
|
32
|
+
* `allowPrivateAddresses` is set).
|
|
33
|
+
* - Responses larger than `maxResponseBytes` are aborted.
|
|
34
|
+
*
|
|
35
|
+
* Pass the returned dispatcher to `fetch(url, { dispatcher })`, or to any
|
|
36
|
+
* library that forwards fetch options (e.g. open-graph-scraper's
|
|
37
|
+
* `fetchOptions`).
|
|
38
|
+
*/
|
|
39
|
+
export declare function createGuardedDispatcher(options?: GuardedDispatcherOptions): Agent;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { Agent } from "undici";
|
|
2
|
+
export interface SafeFetchOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Skip the private/loopback destination checks. Dev/test escape hatch only.
|
|
5
|
+
* Ignored when an explicit `dispatcher` is provided.
|
|
6
|
+
*/
|
|
7
|
+
allowPrivateAddresses?: boolean;
|
|
8
|
+
/** Response body cap in bytes. Ignored when `dispatcher` is provided. */
|
|
9
|
+
maxResponseBytes?: number;
|
|
10
|
+
/** Per-request timeout (applied via AbortSignal.timeout). */
|
|
11
|
+
timeoutMs?: number;
|
|
12
|
+
/**
|
|
13
|
+
* A pre-built guarded dispatcher to reuse (for connection pooling across
|
|
14
|
+
* requests). When omitted, a guarded dispatcher is created per call from
|
|
15
|
+
* `allowPrivateAddresses`/`maxResponseBytes`.
|
|
16
|
+
*/
|
|
17
|
+
dispatcher?: Agent;
|
|
18
|
+
/** Extra request headers. */
|
|
19
|
+
headers?: Record<string, string>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* SSRF- and resource-guarded outbound fetch for client-supplied URLs:
|
|
23
|
+
*
|
|
24
|
+
* - rejects non-`http(s)` schemes and malformed URLs before connecting;
|
|
25
|
+
* - routes the request through a guarded undici dispatcher that refuses
|
|
26
|
+
* private/loopback/metadata destinations (every redirect hop) and caps the
|
|
27
|
+
* response body;
|
|
28
|
+
* - applies an optional timeout and throws on a non-2xx response.
|
|
29
|
+
*
|
|
30
|
+
* Returns the `Response` (read the body with `.text()`/`.json()`/etc.).
|
|
31
|
+
*/
|
|
32
|
+
export declare function safeFetch(url: string, options?: SafeFetchOptions): Promise<Response>;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { isBlockedAddress } from "./address.js";
|
|
2
|
+
export { createGuardedDispatcher, DEFAULT_MAX_RESPONSE_BYTES, type GuardedDispatcherOptions, } from "./dispatcher.js";
|
|
3
|
+
export { type SafeFetchOptions, safeFetch } from "./fetch.js";
|
|
4
|
+
export { assertHttpUrl, isHttpUrl, redactUrl } from "./url.js";
|