ssrf-safe 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/LICENSE +21 -0
- package/README.md +379 -2
- package/dist/chunk-4CL2OZUY.cjs +521 -0
- package/dist/chunk-5WDHI7VU.js +521 -0
- package/dist/chunk-EDSDB6GK.cjs +163 -0
- package/dist/chunk-LI4OVIRR.js +163 -0
- package/dist/fetch.cjs +91 -0
- package/dist/fetch.d.cts +25 -0
- package/dist/fetch.d.ts +25 -0
- package/dist/fetch.js +91 -0
- package/dist/index.cjs +20 -0
- package/dist/index.d.cts +113 -0
- package/dist/index.d.ts +113 -0
- package/dist/index.js +20 -0
- package/dist/node.cjs +371 -0
- package/dist/node.d.cts +101 -0
- package/dist/node.d.ts +101 -0
- package/dist/node.js +371 -0
- package/dist/shared-BGmNa6ne.d.cts +17 -0
- package/dist/shared-BGmNa6ne.d.ts +17 -0
- package/package.json +95 -4
package/dist/node.cjs
ADDED
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
var _chunkEDSDB6GKcjs = require('./chunk-EDSDB6GK.cjs');
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
var _chunk4CL2OZUYcjs = require('./chunk-4CL2OZUY.cjs');
|
|
29
|
+
|
|
30
|
+
// src/node.ts
|
|
31
|
+
var _nodedns = require('node:dns'); var dns = _interopRequireWildcard(_nodedns);
|
|
32
|
+
var _nodehttp = require('node:http'); var http = _interopRequireWildcard(_nodehttp);
|
|
33
|
+
var _nodehttps = require('node:https'); var https = _interopRequireWildcard(_nodehttps);
|
|
34
|
+
var _nodenet = require('node:net'); var net = _interopRequireWildcard(_nodenet);
|
|
35
|
+
var _nodezlib = require('node:zlib'); var zlib = _interopRequireWildcard(_nodezlib);
|
|
36
|
+
var defaultResolver = async (hostname) => dns.promises.lookup(hostname, { all: true });
|
|
37
|
+
function toAddresses(list) {
|
|
38
|
+
const out = [];
|
|
39
|
+
for (const item of list) {
|
|
40
|
+
const raw = typeof item === "string" ? item : _optionalChain([item, 'optionalAccess', _ => _.address]);
|
|
41
|
+
const ip = typeof raw === "string" ? _chunk4CL2OZUYcjs.parseIp.call(void 0, raw) : void 0;
|
|
42
|
+
if (!ip) throw new (0, _chunk4CL2OZUYcjs.SsrfError)(`The resolver returned something that isn't an IP address: ${String(raw)}`, { rule: "dns" });
|
|
43
|
+
out.push({ address: _chunk4CL2OZUYcjs.formatIp.call(void 0, ip), family: ip.version });
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
async function resolveChecked(hostname, options) {
|
|
48
|
+
const verdict = _chunk4CL2OZUYcjs.checkHostname.call(void 0, hostname, options);
|
|
49
|
+
if (!verdict.safe) {
|
|
50
|
+
const details = { rule: verdict.rule, host: hostname };
|
|
51
|
+
if (verdict.ip !== void 0) details.ip = verdict.ip;
|
|
52
|
+
throw new (0, _chunk4CL2OZUYcjs.SsrfError)(`Blocked ${verdict.reason}`, details);
|
|
53
|
+
}
|
|
54
|
+
if (verdict.ip !== void 0) {
|
|
55
|
+
return [{ address: verdict.ip, family: verdict.ip.includes(":") ? 6 : 4 }];
|
|
56
|
+
}
|
|
57
|
+
let name = hostname;
|
|
58
|
+
while (name.endsWith(".") && name.length > 1) name = name.slice(0, -1);
|
|
59
|
+
const addresses = toAddresses(await (_nullishCoalesce(options.resolve, () => ( defaultResolver)))(name));
|
|
60
|
+
if (addresses.length === 0) {
|
|
61
|
+
throw new (0, _chunk4CL2OZUYcjs.SsrfError)(`Blocked ${hostname}: it didn't resolve to any address`, { rule: "dns", host: hostname });
|
|
62
|
+
}
|
|
63
|
+
const addressOptions = verdict.trusted ? { ...options, allowPrivate: true } : options;
|
|
64
|
+
for (const a of addresses) {
|
|
65
|
+
const check = _chunk4CL2OZUYcjs.checkAddress.call(void 0, _chunk4CL2OZUYcjs.parseIp.call(void 0, a.address), addressOptions, hostname);
|
|
66
|
+
if (!check.safe) {
|
|
67
|
+
throw new (0, _chunk4CL2OZUYcjs.SsrfError)(`Blocked ${check.reason}`, { rule: check.rule, host: hostname, ip: a.address });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return addresses;
|
|
71
|
+
}
|
|
72
|
+
async function checkHost(hostname, options = {}) {
|
|
73
|
+
try {
|
|
74
|
+
const addresses = await resolveChecked(hostname, options);
|
|
75
|
+
return { safe: true, host: hostname, addresses: addresses.map((a) => a.address) };
|
|
76
|
+
} catch (err) {
|
|
77
|
+
if (err instanceof _chunk4CL2OZUYcjs.SsrfError) {
|
|
78
|
+
return { safe: false, rule: err.rule, reason: err.message, host: hostname, addresses: err.ip ? [err.ip] : [] };
|
|
79
|
+
}
|
|
80
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
81
|
+
return { safe: false, rule: "dns", reason: `DNS lookup failed: ${message}`, host: hostname, addresses: [] };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function createLookup(options) {
|
|
85
|
+
return (hostname, opts, cb) => {
|
|
86
|
+
const callback = typeof opts === "function" ? opts : cb;
|
|
87
|
+
const lookupOpts = typeof opts === "function" ? {} : _nullishCoalesce(opts, () => ( {}));
|
|
88
|
+
resolveChecked(hostname, options).then(
|
|
89
|
+
(list) => {
|
|
90
|
+
const family = lookupOpts.family === "IPv4" ? 4 : lookupOpts.family === "IPv6" ? 6 : lookupOpts.family;
|
|
91
|
+
const addrs = family === 4 || family === 6 ? list.filter((a) => a.family === family) : list;
|
|
92
|
+
if (addrs.length === 0) {
|
|
93
|
+
const err = new Error(`getaddrinfo ENOTFOUND ${hostname}`);
|
|
94
|
+
err.code = "ENOTFOUND";
|
|
95
|
+
callback(err, "");
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (lookupOpts.all) callback(null, addrs);
|
|
99
|
+
else callback(null, addrs[0].address, addrs[0].family);
|
|
100
|
+
},
|
|
101
|
+
(err) => callback(err, "")
|
|
102
|
+
);
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
var CHECK_KEYS = [
|
|
106
|
+
"resolve",
|
|
107
|
+
"allowHosts",
|
|
108
|
+
"blockHosts",
|
|
109
|
+
"allowPrivate",
|
|
110
|
+
"ports",
|
|
111
|
+
"blockPorts",
|
|
112
|
+
"protocols",
|
|
113
|
+
"allowCredentials",
|
|
114
|
+
"maxLength"
|
|
115
|
+
];
|
|
116
|
+
function splitOptions(options) {
|
|
117
|
+
const check = {};
|
|
118
|
+
const agent = { ...options };
|
|
119
|
+
for (const key of CHECK_KEYS) {
|
|
120
|
+
if (key in agent) {
|
|
121
|
+
check[key] = agent[key];
|
|
122
|
+
delete agent[key];
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
delete agent.lookup;
|
|
126
|
+
return { check, agent };
|
|
127
|
+
}
|
|
128
|
+
function guardConnection(options, check) {
|
|
129
|
+
if (options.socketPath) {
|
|
130
|
+
return new (0, _chunk4CL2OZUYcjs.SsrfError)(`Blocked a connection to the Unix socket ${options.socketPath}`, { rule: "blocked-host" });
|
|
131
|
+
}
|
|
132
|
+
const host = String(_nullishCoalesce(_nullishCoalesce(options.host, () => ( options.hostname)), () => ( "localhost")));
|
|
133
|
+
const port = options.port === void 0 || options.port === null || options.port === "" ? void 0 : Number(options.port);
|
|
134
|
+
if (port !== void 0) {
|
|
135
|
+
if (check.ports && !check.ports.includes(port)) {
|
|
136
|
+
return new (0, _chunk4CL2OZUYcjs.SsrfError)(`Blocked port ${port}: not in the allowed ports`, { rule: "port", host });
|
|
137
|
+
}
|
|
138
|
+
if (_optionalChain([check, 'access', _2 => _2.blockPorts, 'optionalAccess', _3 => _3.includes, 'call', _4 => _4(port)])) {
|
|
139
|
+
return new (0, _chunk4CL2OZUYcjs.SsrfError)(`Blocked port ${port}`, { rule: "port", host });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const ip = _chunk4CL2OZUYcjs.parseHostIp.call(void 0, host);
|
|
143
|
+
const verdict = ip ? _chunk4CL2OZUYcjs.checkAddress.call(void 0, ip, check) : _chunk4CL2OZUYcjs.checkHostname.call(void 0, host, check);
|
|
144
|
+
if (!verdict.safe) {
|
|
145
|
+
const details = { rule: verdict.rule, host };
|
|
146
|
+
if (verdict.ip !== void 0) details.ip = verdict.ip;
|
|
147
|
+
return new (0, _chunk4CL2OZUYcjs.SsrfError)(`Blocked ${verdict.reason}`, details);
|
|
148
|
+
}
|
|
149
|
+
return void 0;
|
|
150
|
+
}
|
|
151
|
+
function failConnection(err, callback) {
|
|
152
|
+
if (typeof callback === "function") {
|
|
153
|
+
process.nextTick(callback, err);
|
|
154
|
+
return void 0;
|
|
155
|
+
}
|
|
156
|
+
const socket = new net.Socket();
|
|
157
|
+
process.nextTick(() => socket.destroy(err));
|
|
158
|
+
return socket;
|
|
159
|
+
}
|
|
160
|
+
var kCheck = /* @__PURE__ */ Symbol("ssrf-safe.check");
|
|
161
|
+
var kLookup = /* @__PURE__ */ Symbol("ssrf-safe.lookup");
|
|
162
|
+
var SafeHttpAgent = class extends http.Agent {
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
constructor(options = {}) {
|
|
166
|
+
const { check, agent } = splitOptions(options);
|
|
167
|
+
super(agent);
|
|
168
|
+
this[kCheck] = check;
|
|
169
|
+
this[kLookup] = createLookup(check);
|
|
170
|
+
}
|
|
171
|
+
createConnection(options, callback) {
|
|
172
|
+
const err = guardConnection(options, this[kCheck]);
|
|
173
|
+
if (err) return failConnection(err, callback);
|
|
174
|
+
const proto = http.Agent.prototype;
|
|
175
|
+
return proto.createConnection.call(this, { ...options, lookup: this[kLookup] }, callback);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
var SafeHttpsAgent = class extends https.Agent {
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
constructor(options = {}) {
|
|
182
|
+
const { check, agent } = splitOptions(options);
|
|
183
|
+
super(agent);
|
|
184
|
+
this[kCheck] = check;
|
|
185
|
+
this[kLookup] = createLookup(check);
|
|
186
|
+
}
|
|
187
|
+
createConnection(options, callback) {
|
|
188
|
+
const err = guardConnection(options, this[kCheck]);
|
|
189
|
+
if (err) return failConnection(err, callback);
|
|
190
|
+
const proto = https.Agent.prototype;
|
|
191
|
+
return proto.createConnection.call(this, { ...options, lookup: this[kLookup] }, callback);
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
function createSafeAgent(options = {}) {
|
|
195
|
+
return { http: new SafeHttpAgent(options), https: new SafeHttpsAgent(options) };
|
|
196
|
+
}
|
|
197
|
+
var safeHttpAgent = new SafeHttpAgent();
|
|
198
|
+
var safeHttpsAgent = new SafeHttpsAgent();
|
|
199
|
+
async function prepareBody(body, headers) {
|
|
200
|
+
if (body === null || body === void 0) return null;
|
|
201
|
+
if (_chunkEDSDB6GKcjs.isReadableStream.call(void 0, body)) return body;
|
|
202
|
+
const serialised = new Response(body);
|
|
203
|
+
const type = serialised.headers.get("content-type");
|
|
204
|
+
if (type && !headers.has("content-type")) headers.set("content-type", type);
|
|
205
|
+
return new Uint8Array(await serialised.arrayBuffer());
|
|
206
|
+
}
|
|
207
|
+
function sendRequest(url, method, headers, body, agent, signal) {
|
|
208
|
+
return new Promise((resolve, reject) => {
|
|
209
|
+
if (signal.aborted) {
|
|
210
|
+
reject(signal.reason);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const outgoing = {};
|
|
214
|
+
headers.forEach((value, key) => {
|
|
215
|
+
outgoing[key] = value;
|
|
216
|
+
});
|
|
217
|
+
if (body instanceof Uint8Array) outgoing["content-length"] = String(body.byteLength);
|
|
218
|
+
const mod = url.protocol === "https:" ? https : http;
|
|
219
|
+
const req = mod.request(url, { method, headers: outgoing, agent });
|
|
220
|
+
const onAbort = () => req.destroy(signal.reason);
|
|
221
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
222
|
+
req.on("response", (res) => {
|
|
223
|
+
signal.removeEventListener("abort", onAbort);
|
|
224
|
+
resolve(res);
|
|
225
|
+
});
|
|
226
|
+
req.on("error", (err) => {
|
|
227
|
+
signal.removeEventListener("abort", onAbort);
|
|
228
|
+
reject(signal.aborted ? signal.reason : err);
|
|
229
|
+
});
|
|
230
|
+
if (body === null) {
|
|
231
|
+
req.end();
|
|
232
|
+
} else if (body instanceof Uint8Array) {
|
|
233
|
+
req.end(body);
|
|
234
|
+
} else {
|
|
235
|
+
const reader = body.getReader();
|
|
236
|
+
const pump = async () => {
|
|
237
|
+
for (; ; ) {
|
|
238
|
+
const { value, done } = await reader.read();
|
|
239
|
+
if (done) break;
|
|
240
|
+
if (!req.write(value)) await new Promise((r) => req.once("drain", r));
|
|
241
|
+
}
|
|
242
|
+
req.end();
|
|
243
|
+
};
|
|
244
|
+
pump().catch((err) => req.destroy(err));
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
function decode(res) {
|
|
249
|
+
const encoding = String(_nullishCoalesce(res.headers["content-encoding"], () => ( ""))).trim().toLowerCase();
|
|
250
|
+
let decoder;
|
|
251
|
+
if (encoding === "gzip" || encoding === "x-gzip") decoder = zlib.createGunzip();
|
|
252
|
+
else if (encoding === "deflate") decoder = zlib.createInflate();
|
|
253
|
+
else if (encoding === "br") decoder = zlib.createBrotliDecompress();
|
|
254
|
+
if (!decoder) return res;
|
|
255
|
+
const d = decoder;
|
|
256
|
+
res.on("error", (err) => d.destroy(err));
|
|
257
|
+
d.on("close", () => res.destroy());
|
|
258
|
+
return res.pipe(d);
|
|
259
|
+
}
|
|
260
|
+
function toWebStream(readable) {
|
|
261
|
+
const iterator = readable[Symbol.asyncIterator]();
|
|
262
|
+
return new ReadableStream({
|
|
263
|
+
async pull(controller) {
|
|
264
|
+
const { value, done } = await iterator.next();
|
|
265
|
+
if (done) controller.close();
|
|
266
|
+
else controller.enqueue(new Uint8Array(value.buffer, value.byteOffset, value.byteLength));
|
|
267
|
+
},
|
|
268
|
+
cancel() {
|
|
269
|
+
readable.destroy();
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
async function safeFetch(input, init = {}, options = {}) {
|
|
274
|
+
const limits = _chunkEDSDB6GKcjs.resolveLimits.call(void 0, options);
|
|
275
|
+
const mode = _nullishCoalesce(init.redirect, () => ( "follow"));
|
|
276
|
+
const headers = new Headers(init.headers);
|
|
277
|
+
if (!headers.has("accept")) headers.set("accept", "*/*");
|
|
278
|
+
if (!headers.has("accept-encoding")) headers.set("accept-encoding", "gzip, deflate, br");
|
|
279
|
+
let state = {
|
|
280
|
+
url: _chunkEDSDB6GKcjs.assertSafeUrl.call(void 0, input, options),
|
|
281
|
+
method: _nullishCoalesce(init.method, () => ( "GET")),
|
|
282
|
+
headers,
|
|
283
|
+
body: null,
|
|
284
|
+
streamed: _chunkEDSDB6GKcjs.isReadableStream.call(void 0, init.body)
|
|
285
|
+
};
|
|
286
|
+
const upperMethod = state.method.toUpperCase();
|
|
287
|
+
if ((upperMethod === "GET" || upperMethod === "HEAD") && init.body !== null && init.body !== void 0) {
|
|
288
|
+
throw new TypeError("A GET or HEAD request can't have a body");
|
|
289
|
+
}
|
|
290
|
+
state.body = await prepareBody(init.body, headers);
|
|
291
|
+
const { agentOptions, ...rest } = options;
|
|
292
|
+
const agentConfig = { ...agentOptions, ...rest, keepAlive: false };
|
|
293
|
+
const agents = { http: new SafeHttpAgent(agentConfig), https: new SafeHttpsAgent(agentConfig) };
|
|
294
|
+
const deadline = _chunkEDSDB6GKcjs.createDeadline.call(void 0, limits.timeout, init.signal);
|
|
295
|
+
let redirects = 0;
|
|
296
|
+
try {
|
|
297
|
+
for (; ; ) {
|
|
298
|
+
if (state.url.protocol !== "http:" && state.url.protocol !== "https:") {
|
|
299
|
+
throw new (0, _chunk4CL2OZUYcjs.SsrfError)(`Blocked ${state.url.protocol} URL: ssrf-safe/node fetches http and https only`, {
|
|
300
|
+
rule: "protocol",
|
|
301
|
+
url: state.url.href
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
const agent = state.url.protocol === "https:" ? agents.https : agents.http;
|
|
305
|
+
const res = await sendRequest(state.url, state.method, state.headers, state.body, agent, deadline.signal);
|
|
306
|
+
const status = _nullishCoalesce(res.statusCode, () => ( 0));
|
|
307
|
+
const location = res.headers.location;
|
|
308
|
+
if (mode !== "manual" && _chunkEDSDB6GKcjs.isRedirectStatus.call(void 0, status) && typeof location === "string") {
|
|
309
|
+
res.destroy();
|
|
310
|
+
if (mode === "error") throw new TypeError(`Unexpected redirect to ${location} (redirect: "error")`);
|
|
311
|
+
if (redirects >= limits.maxRedirects) throw _chunkEDSDB6GKcjs.tooManyRedirects.call(void 0, limits.maxRedirects, state.url);
|
|
312
|
+
state = _chunkEDSDB6GKcjs.planRedirect.call(void 0, state, status, location, options);
|
|
313
|
+
redirects++;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
if (status < 200 || status > 599) {
|
|
317
|
+
res.destroy();
|
|
318
|
+
throw new TypeError(`Unsupported response status ${status}`);
|
|
319
|
+
}
|
|
320
|
+
const responseHeaders = new Headers();
|
|
321
|
+
const raw = res.rawHeaders;
|
|
322
|
+
for (let i = 0; i + 1 < raw.length; i += 2) {
|
|
323
|
+
try {
|
|
324
|
+
responseHeaders.append(raw[i], raw[i + 1]);
|
|
325
|
+
} catch (e) {
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const length = Number(res.headers["content-length"]);
|
|
329
|
+
if (!res.headers["content-encoding"] && length > limits.maxBytes) {
|
|
330
|
+
res.destroy();
|
|
331
|
+
throw _chunkEDSDB6GKcjs.tooLarge.call(void 0, limits.maxBytes, state.url);
|
|
332
|
+
}
|
|
333
|
+
const hasBody = !_chunkEDSDB6GKcjs.isNullBodyStatus.call(void 0, status) && state.method.toUpperCase() !== "HEAD";
|
|
334
|
+
let body = null;
|
|
335
|
+
if (hasBody) {
|
|
336
|
+
const stream = decode(res);
|
|
337
|
+
const onAbort = () => stream.destroy(deadline.reason());
|
|
338
|
+
deadline.signal.addEventListener("abort", onAbort, { once: true });
|
|
339
|
+
const onEnd = () => {
|
|
340
|
+
deadline.signal.removeEventListener("abort", onAbort);
|
|
341
|
+
deadline.done();
|
|
342
|
+
};
|
|
343
|
+
body = _chunkEDSDB6GKcjs.limitBody.call(void 0, toWebStream(stream), limits.maxBytes, state.url, deadline, onEnd);
|
|
344
|
+
} else {
|
|
345
|
+
res.resume();
|
|
346
|
+
deadline.done();
|
|
347
|
+
}
|
|
348
|
+
return _chunkEDSDB6GKcjs.buildResponse.call(void 0, body, { status, statusText: _nullishCoalesce(res.statusMessage, () => ( "")), headers: responseHeaders }, state.url, redirects > 0);
|
|
349
|
+
}
|
|
350
|
+
} catch (err) {
|
|
351
|
+
deadline.done();
|
|
352
|
+
throw deadline.signal.aborted ? deadline.reason() : err;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
exports.DEFAULT_BLOCKED_HOSTS = _chunk4CL2OZUYcjs.DEFAULT_BLOCKED_HOSTS; exports.FetchLimitError = _chunk4CL2OZUYcjs.FetchLimitError; exports.SafeHttpAgent = SafeHttpAgent; exports.SafeHttpsAgent = SafeHttpsAgent; exports.SsrfError = _chunk4CL2OZUYcjs.SsrfError; exports.checkHost = checkHost; exports.checkUrl = _chunk4CL2OZUYcjs.checkUrl; exports.classifyIp = _chunk4CL2OZUYcjs.classifyIp; exports.createSafeAgent = createSafeAgent; exports.isPrivateIp = _chunk4CL2OZUYcjs.isPrivateIp; exports.isSafeUrl = _chunk4CL2OZUYcjs.isSafeUrl; exports.normalizeIp = _chunk4CL2OZUYcjs.normalizeIp; exports.safeFetch = safeFetch; exports.safeHttpAgent = safeHttpAgent; exports.safeHttpsAgent = safeHttpsAgent;
|
package/dist/node.d.cts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import * as http from 'node:http';
|
|
2
|
+
import * as https from 'node:https';
|
|
3
|
+
import { SsrfError, CheckOptions } from './index.cjs';
|
|
4
|
+
export { BlockRule, CheckResult, DEFAULT_BLOCKED_HOSTS, FetchLimitError, IpRange, LimitCode, checkUrl, classifyIp, isPrivateIp, isSafeUrl, normalizeIp } from './index.cjs';
|
|
5
|
+
import { F as FetchLimits } from './shared-BGmNa6ne.cjs';
|
|
6
|
+
|
|
7
|
+
interface ResolvedAddress {
|
|
8
|
+
address: string;
|
|
9
|
+
family: 4 | 6;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Resolves a hostname to its addresses. Default: `dns.promises.lookup(host, { all: true })`,
|
|
13
|
+
* which uses the operating system's resolver like the rest of Node.js does.
|
|
14
|
+
* Return strings or `{ address, family }` objects.
|
|
15
|
+
*/
|
|
16
|
+
type Resolver = (hostname: string) => Promise<ReadonlyArray<string | {
|
|
17
|
+
address: string;
|
|
18
|
+
family?: number;
|
|
19
|
+
}>> | ReadonlyArray<string | {
|
|
20
|
+
address: string;
|
|
21
|
+
family?: number;
|
|
22
|
+
}>;
|
|
23
|
+
/** Options for the DNS-aware checks. */
|
|
24
|
+
interface NodeCheckOptions extends CheckOptions {
|
|
25
|
+
/** Resolve hostnames with this instead of the system resolver. For tests and custom DNS. */
|
|
26
|
+
resolve?: Resolver;
|
|
27
|
+
}
|
|
28
|
+
interface HostCheckResult {
|
|
29
|
+
safe: boolean;
|
|
30
|
+
rule?: SsrfError["rule"];
|
|
31
|
+
reason?: string;
|
|
32
|
+
host: string;
|
|
33
|
+
/** Every address the host resolved to (or the literal address). */
|
|
34
|
+
addresses: string[];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Resolves a hostname (or checks an IP literal) and reports whether every
|
|
38
|
+
* address is allowed. Never throws; DNS failures come back as `rule: "dns"`.
|
|
39
|
+
*
|
|
40
|
+
* A check followed by a separate request resolves the name twice, and the
|
|
41
|
+
* second answer can differ (DNS rebinding). To fetch, use `safeFetch` or the
|
|
42
|
+
* safe agents, which connect to the address they checked.
|
|
43
|
+
*/
|
|
44
|
+
declare function checkHost(hostname: string, options?: NodeCheckOptions): Promise<HostCheckResult>;
|
|
45
|
+
/** Options for the safe agents: the check options plus any `http.Agent` / `https.Agent` options. */
|
|
46
|
+
type SafeAgentOptions = NodeCheckOptions & https.AgentOptions;
|
|
47
|
+
interface ConnectOptions {
|
|
48
|
+
host?: string | null;
|
|
49
|
+
hostname?: string | null;
|
|
50
|
+
port?: number | string | null;
|
|
51
|
+
socketPath?: string;
|
|
52
|
+
[key: string]: unknown;
|
|
53
|
+
}
|
|
54
|
+
declare const kCheck: unique symbol;
|
|
55
|
+
declare const kLookup: unique symbol;
|
|
56
|
+
/**
|
|
57
|
+
* An `http.Agent` that refuses to connect to private, internal and metadata
|
|
58
|
+
* addresses. Hostnames are resolved, every address is checked, and the socket
|
|
59
|
+
* connects to the checked address.
|
|
60
|
+
*/
|
|
61
|
+
declare class SafeHttpAgent extends http.Agent {
|
|
62
|
+
private readonly [kCheck];
|
|
63
|
+
private readonly [kLookup];
|
|
64
|
+
constructor(options?: SafeAgentOptions);
|
|
65
|
+
createConnection(options: ConnectOptions, callback?: unknown): any;
|
|
66
|
+
}
|
|
67
|
+
/** The https version of `SafeHttpAgent`. */
|
|
68
|
+
declare class SafeHttpsAgent extends https.Agent {
|
|
69
|
+
private readonly [kCheck];
|
|
70
|
+
private readonly [kLookup];
|
|
71
|
+
constructor(options?: SafeAgentOptions);
|
|
72
|
+
createConnection(options: ConnectOptions, callback?: unknown): any;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Creates a pair of safe agents. The `{ http, https }` shape is what got's
|
|
76
|
+
* `agent` option takes; for axios, pass `httpAgent: agents.http, httpsAgent: agents.https`.
|
|
77
|
+
*/
|
|
78
|
+
declare function createSafeAgent(options?: SafeAgentOptions): {
|
|
79
|
+
http: SafeHttpAgent;
|
|
80
|
+
https: SafeHttpsAgent;
|
|
81
|
+
};
|
|
82
|
+
/** A shared `SafeHttpAgent` with the default options. */
|
|
83
|
+
declare const safeHttpAgent: SafeHttpAgent;
|
|
84
|
+
/** A shared `SafeHttpsAgent` with the default options. */
|
|
85
|
+
declare const safeHttpsAgent: SafeHttpsAgent;
|
|
86
|
+
interface NodeSafeFetchOptions extends NodeCheckOptions, FetchLimits {
|
|
87
|
+
/** Extra options for the agents, such as `ca`, `cert` and `key` for https. */
|
|
88
|
+
agentOptions?: https.AgentOptions;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* A fetch-style function for Node.js that checks the URL, resolves the host,
|
|
92
|
+
* checks every address, connects to the checked address, and checks every
|
|
93
|
+
* redirect hop the same way. Returns a standard `Response`.
|
|
94
|
+
*
|
|
95
|
+
* Request bodies: string, URLSearchParams, Blob, FormData, ArrayBuffer,
|
|
96
|
+
* typed arrays and ReadableStream. Responses compressed with gzip, deflate or
|
|
97
|
+
* br are decompressed, as fetch does.
|
|
98
|
+
*/
|
|
99
|
+
declare function safeFetch(input: string | URL, init?: RequestInit, options?: NodeSafeFetchOptions): Promise<Response>;
|
|
100
|
+
|
|
101
|
+
export { CheckOptions, FetchLimits, type HostCheckResult, type NodeCheckOptions, type NodeSafeFetchOptions, type ResolvedAddress, type Resolver, type SafeAgentOptions, SafeHttpAgent, SafeHttpsAgent, SsrfError, checkHost, createSafeAgent, safeFetch, safeHttpAgent, safeHttpsAgent };
|
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import * as http from 'node:http';
|
|
2
|
+
import * as https from 'node:https';
|
|
3
|
+
import { SsrfError, CheckOptions } from './index.js';
|
|
4
|
+
export { BlockRule, CheckResult, DEFAULT_BLOCKED_HOSTS, FetchLimitError, IpRange, LimitCode, checkUrl, classifyIp, isPrivateIp, isSafeUrl, normalizeIp } from './index.js';
|
|
5
|
+
import { F as FetchLimits } from './shared-BGmNa6ne.js';
|
|
6
|
+
|
|
7
|
+
interface ResolvedAddress {
|
|
8
|
+
address: string;
|
|
9
|
+
family: 4 | 6;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Resolves a hostname to its addresses. Default: `dns.promises.lookup(host, { all: true })`,
|
|
13
|
+
* which uses the operating system's resolver like the rest of Node.js does.
|
|
14
|
+
* Return strings or `{ address, family }` objects.
|
|
15
|
+
*/
|
|
16
|
+
type Resolver = (hostname: string) => Promise<ReadonlyArray<string | {
|
|
17
|
+
address: string;
|
|
18
|
+
family?: number;
|
|
19
|
+
}>> | ReadonlyArray<string | {
|
|
20
|
+
address: string;
|
|
21
|
+
family?: number;
|
|
22
|
+
}>;
|
|
23
|
+
/** Options for the DNS-aware checks. */
|
|
24
|
+
interface NodeCheckOptions extends CheckOptions {
|
|
25
|
+
/** Resolve hostnames with this instead of the system resolver. For tests and custom DNS. */
|
|
26
|
+
resolve?: Resolver;
|
|
27
|
+
}
|
|
28
|
+
interface HostCheckResult {
|
|
29
|
+
safe: boolean;
|
|
30
|
+
rule?: SsrfError["rule"];
|
|
31
|
+
reason?: string;
|
|
32
|
+
host: string;
|
|
33
|
+
/** Every address the host resolved to (or the literal address). */
|
|
34
|
+
addresses: string[];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Resolves a hostname (or checks an IP literal) and reports whether every
|
|
38
|
+
* address is allowed. Never throws; DNS failures come back as `rule: "dns"`.
|
|
39
|
+
*
|
|
40
|
+
* A check followed by a separate request resolves the name twice, and the
|
|
41
|
+
* second answer can differ (DNS rebinding). To fetch, use `safeFetch` or the
|
|
42
|
+
* safe agents, which connect to the address they checked.
|
|
43
|
+
*/
|
|
44
|
+
declare function checkHost(hostname: string, options?: NodeCheckOptions): Promise<HostCheckResult>;
|
|
45
|
+
/** Options for the safe agents: the check options plus any `http.Agent` / `https.Agent` options. */
|
|
46
|
+
type SafeAgentOptions = NodeCheckOptions & https.AgentOptions;
|
|
47
|
+
interface ConnectOptions {
|
|
48
|
+
host?: string | null;
|
|
49
|
+
hostname?: string | null;
|
|
50
|
+
port?: number | string | null;
|
|
51
|
+
socketPath?: string;
|
|
52
|
+
[key: string]: unknown;
|
|
53
|
+
}
|
|
54
|
+
declare const kCheck: unique symbol;
|
|
55
|
+
declare const kLookup: unique symbol;
|
|
56
|
+
/**
|
|
57
|
+
* An `http.Agent` that refuses to connect to private, internal and metadata
|
|
58
|
+
* addresses. Hostnames are resolved, every address is checked, and the socket
|
|
59
|
+
* connects to the checked address.
|
|
60
|
+
*/
|
|
61
|
+
declare class SafeHttpAgent extends http.Agent {
|
|
62
|
+
private readonly [kCheck];
|
|
63
|
+
private readonly [kLookup];
|
|
64
|
+
constructor(options?: SafeAgentOptions);
|
|
65
|
+
createConnection(options: ConnectOptions, callback?: unknown): any;
|
|
66
|
+
}
|
|
67
|
+
/** The https version of `SafeHttpAgent`. */
|
|
68
|
+
declare class SafeHttpsAgent extends https.Agent {
|
|
69
|
+
private readonly [kCheck];
|
|
70
|
+
private readonly [kLookup];
|
|
71
|
+
constructor(options?: SafeAgentOptions);
|
|
72
|
+
createConnection(options: ConnectOptions, callback?: unknown): any;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Creates a pair of safe agents. The `{ http, https }` shape is what got's
|
|
76
|
+
* `agent` option takes; for axios, pass `httpAgent: agents.http, httpsAgent: agents.https`.
|
|
77
|
+
*/
|
|
78
|
+
declare function createSafeAgent(options?: SafeAgentOptions): {
|
|
79
|
+
http: SafeHttpAgent;
|
|
80
|
+
https: SafeHttpsAgent;
|
|
81
|
+
};
|
|
82
|
+
/** A shared `SafeHttpAgent` with the default options. */
|
|
83
|
+
declare const safeHttpAgent: SafeHttpAgent;
|
|
84
|
+
/** A shared `SafeHttpsAgent` with the default options. */
|
|
85
|
+
declare const safeHttpsAgent: SafeHttpsAgent;
|
|
86
|
+
interface NodeSafeFetchOptions extends NodeCheckOptions, FetchLimits {
|
|
87
|
+
/** Extra options for the agents, such as `ca`, `cert` and `key` for https. */
|
|
88
|
+
agentOptions?: https.AgentOptions;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* A fetch-style function for Node.js that checks the URL, resolves the host,
|
|
92
|
+
* checks every address, connects to the checked address, and checks every
|
|
93
|
+
* redirect hop the same way. Returns a standard `Response`.
|
|
94
|
+
*
|
|
95
|
+
* Request bodies: string, URLSearchParams, Blob, FormData, ArrayBuffer,
|
|
96
|
+
* typed arrays and ReadableStream. Responses compressed with gzip, deflate or
|
|
97
|
+
* br are decompressed, as fetch does.
|
|
98
|
+
*/
|
|
99
|
+
declare function safeFetch(input: string | URL, init?: RequestInit, options?: NodeSafeFetchOptions): Promise<Response>;
|
|
100
|
+
|
|
101
|
+
export { CheckOptions, FetchLimits, type HostCheckResult, type NodeCheckOptions, type NodeSafeFetchOptions, type ResolvedAddress, type Resolver, type SafeAgentOptions, SafeHttpAgent, SafeHttpsAgent, SsrfError, checkHost, createSafeAgent, safeFetch, safeHttpAgent, safeHttpsAgent };
|