@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,236 @@
|
|
|
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/net/address.ts
|
|
36
|
+
function isBlockedAddress(ip) {
|
|
37
|
+
const addr = ip.trim().toLowerCase();
|
|
38
|
+
if (addr.includes(":")) {
|
|
39
|
+
return isBlockedIpv6(addr);
|
|
40
|
+
}
|
|
41
|
+
return isBlockedIpv4(addr);
|
|
42
|
+
}
|
|
43
|
+
function isBlockedIpv4(ip) {
|
|
44
|
+
const parts = ip.split(".").map((p) => Number(p));
|
|
45
|
+
if (parts.length !== 4 || parts.some((p) => !Number.isInteger(p) || p < 0 || p > 255)) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
const [a, b] = parts;
|
|
49
|
+
if (a === 0)
|
|
50
|
+
return true;
|
|
51
|
+
if (a === 127)
|
|
52
|
+
return true;
|
|
53
|
+
if (a === 10)
|
|
54
|
+
return true;
|
|
55
|
+
if (a === 172 && b >= 16 && b <= 31)
|
|
56
|
+
return true;
|
|
57
|
+
if (a === 192 && b === 168)
|
|
58
|
+
return true;
|
|
59
|
+
if (a === 169 && b === 254)
|
|
60
|
+
return true;
|
|
61
|
+
if (a === 100 && b >= 64 && b <= 127)
|
|
62
|
+
return true;
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
function expandIpv6(address) {
|
|
66
|
+
let addr = address;
|
|
67
|
+
const dotted = addr.match(/^(.+:)(\d{1,3}(?:\.\d{1,3}){3})$/);
|
|
68
|
+
if (dotted) {
|
|
69
|
+
const octets = dotted[2].split(".").map(Number);
|
|
70
|
+
if (octets.some((o) => !Number.isInteger(o) || o < 0 || o > 255)) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
const hi = (octets[0] << 8 | octets[1]).toString(16);
|
|
74
|
+
const lo = (octets[2] << 8 | octets[3]).toString(16);
|
|
75
|
+
addr = `${dotted[1]}${hi}:${lo}`;
|
|
76
|
+
}
|
|
77
|
+
const halves = addr.split("::");
|
|
78
|
+
if (halves.length > 2)
|
|
79
|
+
return null;
|
|
80
|
+
const head = halves[0] ? halves[0].split(":") : [];
|
|
81
|
+
const tail = halves.length === 2 && halves[1] ? halves[1].split(":") : [];
|
|
82
|
+
if (halves.length === 2 ? head.length + tail.length > 8 : head.length !== 8)
|
|
83
|
+
return null;
|
|
84
|
+
const groupsStr = halves.length === 2 ? [
|
|
85
|
+
...head,
|
|
86
|
+
...new Array(8 - head.length - tail.length).fill("0"),
|
|
87
|
+
...tail
|
|
88
|
+
] : head;
|
|
89
|
+
const groups = [];
|
|
90
|
+
for (const group of groupsStr) {
|
|
91
|
+
if (!/^[0-9a-f]{1,4}$/.test(group))
|
|
92
|
+
return null;
|
|
93
|
+
groups.push(Number.parseInt(group, 16));
|
|
94
|
+
}
|
|
95
|
+
return groups;
|
|
96
|
+
}
|
|
97
|
+
function extractEmbeddedIpv4(groups) {
|
|
98
|
+
const [a, b, c, d, e, f, g, h] = groups;
|
|
99
|
+
const zeroPrefix = a === 0 && b === 0 && c === 0 && d === 0 && e === 0;
|
|
100
|
+
const mapped = zeroPrefix && f === 65535;
|
|
101
|
+
const compat = zeroPrefix && f === 0;
|
|
102
|
+
const nat64 = a === 100 && b === 65435 && c === 0 && d === 0 && e === 0 && f === 0;
|
|
103
|
+
if (!mapped && !compat && !nat64)
|
|
104
|
+
return null;
|
|
105
|
+
return `${g >> 8}.${g & 255}.${h >> 8}.${h & 255}`;
|
|
106
|
+
}
|
|
107
|
+
function isBlockedIpv6(ip) {
|
|
108
|
+
const addr = ip.split("%")[0];
|
|
109
|
+
const groups = expandIpv6(addr);
|
|
110
|
+
if (!groups) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
const embedded = extractEmbeddedIpv4(groups);
|
|
114
|
+
if (embedded) {
|
|
115
|
+
return isBlockedIpv4(embedded);
|
|
116
|
+
}
|
|
117
|
+
if ((groups[0] & 65024) === 64512)
|
|
118
|
+
return true;
|
|
119
|
+
if ((groups[0] & 65472) === 65152)
|
|
120
|
+
return true;
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
// src/net/dispatcher.ts
|
|
124
|
+
import { lookup as dnsLookup } from "node:dns";
|
|
125
|
+
import { Agent } from "undici";
|
|
126
|
+
var DEFAULT_MAX_RESPONSE_BYTES = 5 * 1024 * 1024;
|
|
127
|
+
function createGuardedLookup(allowPrivateAddresses) {
|
|
128
|
+
return (hostname, options, callback) => {
|
|
129
|
+
const opts = typeof options === "number" ? { family: options } : options ?? {};
|
|
130
|
+
dnsLookup(hostname, { ...opts, all: true }, (err, addresses) => {
|
|
131
|
+
if (err) {
|
|
132
|
+
callback(err);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (!addresses || addresses.length === 0) {
|
|
136
|
+
callback(Object.assign(new Error(`could not resolve ${hostname}`), {
|
|
137
|
+
code: "ENOTFOUND"
|
|
138
|
+
}));
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (!allowPrivateAddresses) {
|
|
142
|
+
for (const { address } of addresses) {
|
|
143
|
+
if (isBlockedAddress(address)) {
|
|
144
|
+
callback(new Error(`blocked non-public address ${address} for ${hostname}`));
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (opts.all) {
|
|
150
|
+
callback(null, addresses);
|
|
151
|
+
} else {
|
|
152
|
+
callback(null, addresses[0].address, addresses[0].family);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function createGuardedDispatcher(options = {}) {
|
|
158
|
+
const {
|
|
159
|
+
allowPrivateAddresses = false,
|
|
160
|
+
maxResponseBytes = DEFAULT_MAX_RESPONSE_BYTES
|
|
161
|
+
} = options;
|
|
162
|
+
return new Agent({
|
|
163
|
+
maxResponseSize: maxResponseBytes,
|
|
164
|
+
connect: {
|
|
165
|
+
lookup: createGuardedLookup(allowPrivateAddresses)
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
// src/net/url.ts
|
|
170
|
+
function redactUrl(url) {
|
|
171
|
+
try {
|
|
172
|
+
const parsed = new URL(url);
|
|
173
|
+
if (parsed.username || parsed.password) {
|
|
174
|
+
parsed.username = "";
|
|
175
|
+
parsed.password = "";
|
|
176
|
+
return parsed.toString();
|
|
177
|
+
}
|
|
178
|
+
return url;
|
|
179
|
+
} catch {
|
|
180
|
+
return url.replace(/\/\/[^/@\s]*@/, "//***@");
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function assertHttpUrl(url) {
|
|
184
|
+
let parsed;
|
|
185
|
+
try {
|
|
186
|
+
parsed = new URL(url);
|
|
187
|
+
} catch {
|
|
188
|
+
throw new Error(`invalid URL: ${redactUrl(url)}`);
|
|
189
|
+
}
|
|
190
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
191
|
+
throw new Error(`unsupported scheme ${parsed.protocol} for ${redactUrl(url)}`);
|
|
192
|
+
}
|
|
193
|
+
return parsed;
|
|
194
|
+
}
|
|
195
|
+
function isHttpUrl(url) {
|
|
196
|
+
try {
|
|
197
|
+
assertHttpUrl(url);
|
|
198
|
+
return true;
|
|
199
|
+
} catch {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// src/net/fetch.ts
|
|
205
|
+
async function safeFetch(url, options = {}) {
|
|
206
|
+
assertHttpUrl(url);
|
|
207
|
+
const dispatcher = options.dispatcher ?? createGuardedDispatcher({
|
|
208
|
+
allowPrivateAddresses: options.allowPrivateAddresses,
|
|
209
|
+
maxResponseBytes: options.maxResponseBytes
|
|
210
|
+
});
|
|
211
|
+
const init = { dispatcher };
|
|
212
|
+
if (options.timeoutMs !== undefined && options.timeoutMs > 0) {
|
|
213
|
+
init.signal = AbortSignal.timeout(options.timeoutMs);
|
|
214
|
+
}
|
|
215
|
+
if (options.headers) {
|
|
216
|
+
init.headers = options.headers;
|
|
217
|
+
}
|
|
218
|
+
const res = await fetch(url, init);
|
|
219
|
+
if (!res.ok) {
|
|
220
|
+
await res.body?.cancel().catch(() => {});
|
|
221
|
+
const statusText = res.statusText ? ` ${res.statusText}` : "";
|
|
222
|
+
throw new Error(`request failed: ${res.status}${statusText} for ${redactUrl(url)}`);
|
|
223
|
+
}
|
|
224
|
+
return res;
|
|
225
|
+
}
|
|
226
|
+
export {
|
|
227
|
+
safeFetch,
|
|
228
|
+
redactUrl,
|
|
229
|
+
isHttpUrl,
|
|
230
|
+
isBlockedAddress,
|
|
231
|
+
createGuardedDispatcher,
|
|
232
|
+
assertHttpUrl,
|
|
233
|
+
DEFAULT_MAX_RESPONSE_BYTES
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
//# debugId=1EC350DEADE3199364756E2164756E21
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/net/address.ts", "../src/net/dispatcher.ts", "../src/net/url.ts", "../src/net/fetch.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"/**\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",
|
|
6
|
+
"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",
|
|
7
|
+
"/**\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",
|
|
8
|
+
"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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,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;",
|
|
11
|
+
"debugId": "1EC350DEADE3199364756E2164756E21",
|
|
12
|
+
"names": []
|
|
13
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strip any `user:password@` userinfo from a URL so it is safe to include in
|
|
3
|
+
* error messages, logs, or telemetry. Best-effort for unparseable input.
|
|
4
|
+
*/
|
|
5
|
+
export declare function redactUrl(url: string): string;
|
|
6
|
+
/**
|
|
7
|
+
* Parse `url` and require an `http:`/`https:` scheme, returning the parsed
|
|
8
|
+
* `URL`. Throws a descriptive `Error` (with credentials redacted) on a
|
|
9
|
+
* malformed URL or unsupported scheme. Used to gate outbound fetches of
|
|
10
|
+
* client-supplied URLs.
|
|
11
|
+
*/
|
|
12
|
+
export declare function assertHttpUrl(url: string): URL;
|
|
13
|
+
/** Returns true if `url` parses and uses an `http:`/`https:` scheme. */
|
|
14
|
+
export declare function isHttpUrl(url: string): boolean;
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sockethub/util",
|
|
3
|
+
"description": "Shared utilities for Sockethub (networking/SSRF helpers, etc.)",
|
|
4
|
+
"version": "1.0.0-alpha.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"private": false,
|
|
7
|
+
"author": "Nick Jennings <nick@silverbucket.net>",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./net": {
|
|
20
|
+
"types": "./dist/net/index.d.ts",
|
|
21
|
+
"import": "./dist/net/index.js",
|
|
22
|
+
"default": "./dist/net/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./error": {
|
|
25
|
+
"types": "./dist/error/index.d.ts",
|
|
26
|
+
"import": "./dist/error/index.js",
|
|
27
|
+
"default": "./dist/error/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./crypto": {
|
|
30
|
+
"types": "./dist/crypto/index.d.ts",
|
|
31
|
+
"import": "./dist/crypto/index.js",
|
|
32
|
+
"default": "./dist/crypto/index.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist/"
|
|
37
|
+
],
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=20"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"sockethub",
|
|
43
|
+
"ssrf",
|
|
44
|
+
"networking",
|
|
45
|
+
"crypto",
|
|
46
|
+
"encryption",
|
|
47
|
+
"utilities"
|
|
48
|
+
],
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/sockethub/sockethub.git",
|
|
52
|
+
"directory": "packages/util"
|
|
53
|
+
},
|
|
54
|
+
"homepage": "https://github.com/sockethub/sockethub/tree/master/packages/util",
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "bun build src/index.ts src/net/index.ts src/error/index.ts src/crypto/index.ts --outdir dist --target node --format esm --sourcemap=external --external undici",
|
|
57
|
+
"clean": "rm -rf dist",
|
|
58
|
+
"clean:deps": "rm -rf node_modules"
|
|
59
|
+
},
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"object-hash": "^3.0.0",
|
|
62
|
+
"secure-store-redis": "^4.1.0",
|
|
63
|
+
"undici": "^7.19.0"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@types/bun": "latest",
|
|
67
|
+
"@types/object-hash": "^3.0.6",
|
|
68
|
+
"@types/sinon": "^17.0.4",
|
|
69
|
+
"sinon": "^17.0.2"
|
|
70
|
+
},
|
|
71
|
+
"gitHead": "864733e5b34449ef39542eceb4ff11aa308081bc"
|
|
72
|
+
}
|