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
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
// src/ip.ts
|
|
2
|
+
var MAX_IP_LENGTH = 256;
|
|
3
|
+
function parseIPv4Number(part) {
|
|
4
|
+
if (part === "") return void 0;
|
|
5
|
+
let radix = 10;
|
|
6
|
+
let digits = part;
|
|
7
|
+
const c1 = part.charCodeAt(1);
|
|
8
|
+
if (part.length >= 2 && part.charCodeAt(0) === 48 && (c1 === 120 || c1 === 88)) {
|
|
9
|
+
radix = 16;
|
|
10
|
+
digits = part.slice(2);
|
|
11
|
+
if (digits === "") return 0;
|
|
12
|
+
} else if (part.length >= 2 && part.charCodeAt(0) === 48) {
|
|
13
|
+
radix = 8;
|
|
14
|
+
digits = part.slice(1);
|
|
15
|
+
}
|
|
16
|
+
for (let i = 0; i < digits.length; i++) {
|
|
17
|
+
const c = digits.charCodeAt(i);
|
|
18
|
+
const ok = radix === 16 ? c >= 48 && c <= 57 || c >= 97 && c <= 102 || c >= 65 && c <= 70 : radix === 8 ? c >= 48 && c <= 55 : c >= 48 && c <= 57;
|
|
19
|
+
if (!ok) return void 0;
|
|
20
|
+
}
|
|
21
|
+
const n = parseInt(digits, radix);
|
|
22
|
+
return Number.isFinite(n) ? n : void 0;
|
|
23
|
+
}
|
|
24
|
+
function parseIPv4(input) {
|
|
25
|
+
if (input.length === 0 || input.length > MAX_IP_LENGTH) return void 0;
|
|
26
|
+
let s = input;
|
|
27
|
+
if (s.charCodeAt(s.length - 1) === 46) s = s.slice(0, -1);
|
|
28
|
+
if (s === "") return void 0;
|
|
29
|
+
const parts = s.split(".");
|
|
30
|
+
if (parts.length > 4) return void 0;
|
|
31
|
+
const nums = [];
|
|
32
|
+
for (const p of parts) {
|
|
33
|
+
const n = parseIPv4Number(p);
|
|
34
|
+
if (n === void 0) return void 0;
|
|
35
|
+
nums.push(n);
|
|
36
|
+
}
|
|
37
|
+
const last = nums.length - 1;
|
|
38
|
+
for (let i = 0; i < last; i++) if (nums[i] > 255) return void 0;
|
|
39
|
+
if (nums[last] >= 256 ** (4 - last)) return void 0;
|
|
40
|
+
let value = nums[last];
|
|
41
|
+
for (let i = 0; i < last; i++) value += nums[i] * 256 ** (3 - i);
|
|
42
|
+
return [Math.floor(value / 16777216) & 255, value >>> 16 & 255, value >>> 8 & 255, value & 255];
|
|
43
|
+
}
|
|
44
|
+
function parseDottedQuad(s) {
|
|
45
|
+
const parts = s.split(".");
|
|
46
|
+
if (parts.length !== 4) return void 0;
|
|
47
|
+
const out = [];
|
|
48
|
+
for (const p of parts) {
|
|
49
|
+
if (p.length === 0 || p.length > 3) return void 0;
|
|
50
|
+
if (p.length > 1 && p.charCodeAt(0) === 48) return void 0;
|
|
51
|
+
for (let i = 0; i < p.length; i++) {
|
|
52
|
+
const c = p.charCodeAt(i);
|
|
53
|
+
if (c < 48 || c > 57) return void 0;
|
|
54
|
+
}
|
|
55
|
+
const n = Number(p);
|
|
56
|
+
if (n > 255) return void 0;
|
|
57
|
+
out.push(n);
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
function parseGroups(s, allowV4Tail) {
|
|
62
|
+
if (s === "") return [];
|
|
63
|
+
const groups = s.split(":");
|
|
64
|
+
const pieces = [];
|
|
65
|
+
for (let i = 0; i < groups.length; i++) {
|
|
66
|
+
const g = groups[i];
|
|
67
|
+
if (allowV4Tail && i === groups.length - 1 && g.includes(".")) {
|
|
68
|
+
const v4 = parseDottedQuad(g);
|
|
69
|
+
if (!v4) return void 0;
|
|
70
|
+
pieces.push(v4[0] << 8 | v4[1], v4[2] << 8 | v4[3]);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (g.length === 0 || g.length > 4) return void 0;
|
|
74
|
+
for (let j = 0; j < g.length; j++) {
|
|
75
|
+
const c = g.charCodeAt(j);
|
|
76
|
+
const hex = c >= 48 && c <= 57 || c >= 97 && c <= 102 || c >= 65 && c <= 70;
|
|
77
|
+
if (!hex) return void 0;
|
|
78
|
+
}
|
|
79
|
+
pieces.push(parseInt(g, 16));
|
|
80
|
+
}
|
|
81
|
+
return pieces;
|
|
82
|
+
}
|
|
83
|
+
function parseIPv6(input) {
|
|
84
|
+
if (input.length === 0 || input.length > MAX_IP_LENGTH) return void 0;
|
|
85
|
+
let s = input;
|
|
86
|
+
if (s.charCodeAt(0) === 91) {
|
|
87
|
+
if (s.charCodeAt(s.length - 1) !== 93) return void 0;
|
|
88
|
+
s = s.slice(1, -1);
|
|
89
|
+
}
|
|
90
|
+
const zone = s.indexOf("%");
|
|
91
|
+
if (zone !== -1) {
|
|
92
|
+
if (zone === s.length - 1) return void 0;
|
|
93
|
+
s = s.slice(0, zone);
|
|
94
|
+
}
|
|
95
|
+
if (!s.includes(":")) return void 0;
|
|
96
|
+
const dbl = s.indexOf("::");
|
|
97
|
+
let pieces;
|
|
98
|
+
if (dbl === -1) {
|
|
99
|
+
pieces = parseGroups(s, true);
|
|
100
|
+
if (!pieces || pieces.length !== 8) return void 0;
|
|
101
|
+
} else {
|
|
102
|
+
if (s.indexOf("::", dbl + 1) !== -1) return void 0;
|
|
103
|
+
const head = parseGroups(s.slice(0, dbl), false);
|
|
104
|
+
const tail = parseGroups(s.slice(dbl + 2), true);
|
|
105
|
+
if (!head || !tail || head.length + tail.length > 7) return void 0;
|
|
106
|
+
pieces = [...head, ...new Array(8 - head.length - tail.length).fill(0), ...tail];
|
|
107
|
+
}
|
|
108
|
+
const bytes = [];
|
|
109
|
+
for (const p of pieces) bytes.push(p >> 8, p & 255);
|
|
110
|
+
return bytes;
|
|
111
|
+
}
|
|
112
|
+
function parseIp(input) {
|
|
113
|
+
if (typeof input !== "string") return void 0;
|
|
114
|
+
const s = input.trim();
|
|
115
|
+
if (s.includes(":") || s.charCodeAt(0) === 91) {
|
|
116
|
+
const b6 = parseIPv6(s);
|
|
117
|
+
return b6 ? { version: 6, bytes: b6 } : void 0;
|
|
118
|
+
}
|
|
119
|
+
const b4 = parseIPv4(s);
|
|
120
|
+
return b4 ? { version: 4, bytes: b4 } : void 0;
|
|
121
|
+
}
|
|
122
|
+
function formatIp(ip) {
|
|
123
|
+
const b = ip.bytes;
|
|
124
|
+
if (ip.version === 4) return b.join(".");
|
|
125
|
+
const pieces = [];
|
|
126
|
+
for (let i = 0; i < 16; i += 2) pieces.push(b[i] << 8 | b[i + 1]);
|
|
127
|
+
let bestStart = -1;
|
|
128
|
+
let bestLen = 1;
|
|
129
|
+
for (let i = 0; i < 8; ) {
|
|
130
|
+
if (pieces[i] !== 0) {
|
|
131
|
+
i++;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
let j = i;
|
|
135
|
+
while (j < 8 && pieces[j] === 0) j++;
|
|
136
|
+
if (j - i > bestLen) {
|
|
137
|
+
bestStart = i;
|
|
138
|
+
bestLen = j - i;
|
|
139
|
+
}
|
|
140
|
+
i = j;
|
|
141
|
+
}
|
|
142
|
+
const hex = pieces.map((p) => p.toString(16));
|
|
143
|
+
if (bestStart === -1) return hex.join(":");
|
|
144
|
+
return hex.slice(0, bestStart).join(":") + "::" + hex.slice(bestStart + bestLen).join(":");
|
|
145
|
+
}
|
|
146
|
+
function parseCidr(input) {
|
|
147
|
+
const slash = input.lastIndexOf("/");
|
|
148
|
+
const addr = slash === -1 ? input : input.slice(0, slash);
|
|
149
|
+
const ip = parseIp(addr);
|
|
150
|
+
if (!ip) return void 0;
|
|
151
|
+
const max = ip.version === 4 ? 32 : 128;
|
|
152
|
+
let prefix = max;
|
|
153
|
+
if (slash !== -1) {
|
|
154
|
+
const p = input.slice(slash + 1);
|
|
155
|
+
if (!/^[0-9]{1,3}$/.test(p)) return void 0;
|
|
156
|
+
prefix = Number(p);
|
|
157
|
+
if (prefix > max) return void 0;
|
|
158
|
+
}
|
|
159
|
+
return { version: ip.version, bytes: ip.bytes, prefix };
|
|
160
|
+
}
|
|
161
|
+
function inCidr(ip, cidr) {
|
|
162
|
+
if (ip.version !== cidr.version) return false;
|
|
163
|
+
let bits = cidr.prefix;
|
|
164
|
+
for (let i = 0; bits > 0; i++, bits -= 8) {
|
|
165
|
+
const mask = bits >= 8 ? 255 : 255 << 8 - bits & 255;
|
|
166
|
+
if ((ip.bytes[i] & mask) !== (cidr.bytes[i] & mask)) return false;
|
|
167
|
+
}
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
function table(rows) {
|
|
171
|
+
return rows.map(([c, r]) => [parseCidr(c), r]);
|
|
172
|
+
}
|
|
173
|
+
var V4 = table([
|
|
174
|
+
["0.0.0.0/32", "unspecified"],
|
|
175
|
+
["0.0.0.0/8", "this-network"],
|
|
176
|
+
["10.0.0.0/8", "private"],
|
|
177
|
+
["100.100.100.200/32", "cloud-metadata"],
|
|
178
|
+
// Alibaba Cloud
|
|
179
|
+
["100.64.0.0/10", "carrier-grade-nat"],
|
|
180
|
+
["127.0.0.0/8", "loopback"],
|
|
181
|
+
["169.254.169.254/32", "cloud-metadata"],
|
|
182
|
+
// AWS, GCP, Azure, DigitalOcean, OpenStack...
|
|
183
|
+
["169.254.170.2/32", "cloud-metadata"],
|
|
184
|
+
// AWS ECS task metadata
|
|
185
|
+
["169.254.0.0/16", "link-local"],
|
|
186
|
+
["172.16.0.0/12", "private"],
|
|
187
|
+
["192.0.0.192/32", "cloud-metadata"],
|
|
188
|
+
// Oracle Cloud
|
|
189
|
+
["192.0.0.0/24", "reserved"],
|
|
190
|
+
["192.0.2.0/24", "documentation"],
|
|
191
|
+
["192.88.99.0/24", "reserved"],
|
|
192
|
+
// former 6to4 relay anycast
|
|
193
|
+
["192.168.0.0/16", "private"],
|
|
194
|
+
["198.18.0.0/15", "benchmarking"],
|
|
195
|
+
["198.51.100.0/24", "documentation"],
|
|
196
|
+
["203.0.113.0/24", "documentation"],
|
|
197
|
+
["224.0.0.0/4", "multicast"],
|
|
198
|
+
["255.255.255.255/32", "broadcast"],
|
|
199
|
+
["240.0.0.0/4", "reserved"]
|
|
200
|
+
]);
|
|
201
|
+
var V6 = table([
|
|
202
|
+
["::/128", "unspecified"],
|
|
203
|
+
["::1/128", "loopback"],
|
|
204
|
+
["::ffff:0:0/96", "embed-mapped"],
|
|
205
|
+
["::/96", "ipv4-compatible"],
|
|
206
|
+
["64:ff9b::/96", "embed-nat64"],
|
|
207
|
+
["64:ff9b:1::/48", "nat64"],
|
|
208
|
+
// local-use NAT64, RFC 8215
|
|
209
|
+
["::/8", "reserved"],
|
|
210
|
+
["100::/64", "discard"],
|
|
211
|
+
["2001::/32", "teredo"],
|
|
212
|
+
["2001:2::/48", "benchmarking"],
|
|
213
|
+
["2001::/23", "reserved"],
|
|
214
|
+
// IETF protocol assignments
|
|
215
|
+
["2001:db8::/32", "documentation"],
|
|
216
|
+
["2002::/16", "embed-6to4"],
|
|
217
|
+
["3fff::/20", "documentation"],
|
|
218
|
+
["5f00::/16", "reserved"],
|
|
219
|
+
// SRv6 SIDs
|
|
220
|
+
["fd00:ec2::254/128", "cloud-metadata"],
|
|
221
|
+
// AWS IMDS over IPv6
|
|
222
|
+
["fc00::/7", "unique-local"],
|
|
223
|
+
["fe80::/10", "link-local"],
|
|
224
|
+
["fec0::/10", "site-local"],
|
|
225
|
+
["ff00::/8", "multicast"]
|
|
226
|
+
]);
|
|
227
|
+
var GLOBAL_UNICAST = parseCidr("2000::/3");
|
|
228
|
+
function classifyV4(ip) {
|
|
229
|
+
for (const [cidr, range] of V4) if (inCidr(ip, cidr)) return range;
|
|
230
|
+
return "public";
|
|
231
|
+
}
|
|
232
|
+
function describeIp(ip) {
|
|
233
|
+
const text = formatIp(ip);
|
|
234
|
+
if (ip.version === 4) return { ip: text, version: 4, range: classifyV4(ip) };
|
|
235
|
+
for (const [cidr, rule] of V6) {
|
|
236
|
+
if (!inCidr(ip, cidr)) continue;
|
|
237
|
+
if (rule === "embed-mapped" || rule === "embed-nat64" || rule === "embed-6to4") {
|
|
238
|
+
const offset = rule === "embed-6to4" ? 2 : 12;
|
|
239
|
+
const v4 = { version: 4, bytes: ip.bytes.slice(offset, offset + 4) };
|
|
240
|
+
const inner = classifyV4(v4);
|
|
241
|
+
const name = rule === "embed-mapped" ? "ipv4-mapped" : rule === "embed-nat64" ? "nat64" : "6to4";
|
|
242
|
+
return {
|
|
243
|
+
ip: text,
|
|
244
|
+
version: 6,
|
|
245
|
+
range: inner === "public" ? "public" : name,
|
|
246
|
+
embedded: { ip: formatIp(v4), range: inner }
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
return { ip: text, version: 6, range: rule };
|
|
250
|
+
}
|
|
251
|
+
if (!inCidr(ip, GLOBAL_UNICAST)) return { ip: text, version: 6, range: "reserved" };
|
|
252
|
+
return { ip: text, version: 6, range: "public" };
|
|
253
|
+
}
|
|
254
|
+
function classifyIp(ip) {
|
|
255
|
+
const parsed = parseIp(ip);
|
|
256
|
+
return parsed ? describeIp(parsed).range : "invalid";
|
|
257
|
+
}
|
|
258
|
+
function isPrivateIp(ip) {
|
|
259
|
+
return classifyIp(ip) !== "public";
|
|
260
|
+
}
|
|
261
|
+
function normalizeIp(ip) {
|
|
262
|
+
const parsed = parseIp(ip);
|
|
263
|
+
return parsed ? formatIp(parsed) : void 0;
|
|
264
|
+
}
|
|
265
|
+
function isMetadataIp(info) {
|
|
266
|
+
return info.range === "cloud-metadata" || info.embedded?.range === "cloud-metadata";
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// src/check.ts
|
|
270
|
+
var METADATA_HOSTS = Object.freeze([
|
|
271
|
+
"metadata.google.internal",
|
|
272
|
+
"metadata.goog",
|
|
273
|
+
"metadata",
|
|
274
|
+
"instance-data"
|
|
275
|
+
]);
|
|
276
|
+
var INTERNAL_HOSTS = Object.freeze([
|
|
277
|
+
"localhost",
|
|
278
|
+
"*.localhost",
|
|
279
|
+
"*.local",
|
|
280
|
+
"*.localdomain",
|
|
281
|
+
"*.home.arpa",
|
|
282
|
+
"*.internal",
|
|
283
|
+
"kubernetes.default.svc",
|
|
284
|
+
"*.svc",
|
|
285
|
+
"*.cluster.local"
|
|
286
|
+
]);
|
|
287
|
+
var DEFAULT_BLOCKED_HOSTS = Object.freeze([...METADATA_HOSTS, ...INTERNAL_HOSTS]);
|
|
288
|
+
var DEFAULT_PROTOCOLS = ["http", "https"];
|
|
289
|
+
var DEFAULT_MAX_LENGTH = 8192;
|
|
290
|
+
var DEFAULT_PORTS = { "http:": 80, "https:": 443, "ws:": 80, "wss:": 443, "ftp:": 21 };
|
|
291
|
+
var RANGE_TEXT = {
|
|
292
|
+
public: "a public",
|
|
293
|
+
unspecified: "an unspecified",
|
|
294
|
+
"this-network": "a 'this network'",
|
|
295
|
+
loopback: "a loopback",
|
|
296
|
+
private: "a private",
|
|
297
|
+
"carrier-grade-nat": "a carrier-grade NAT",
|
|
298
|
+
"link-local": "a link-local",
|
|
299
|
+
"cloud-metadata": "a cloud metadata",
|
|
300
|
+
reserved: "a reserved",
|
|
301
|
+
documentation: "a documentation",
|
|
302
|
+
benchmarking: "a benchmarking",
|
|
303
|
+
multicast: "a multicast",
|
|
304
|
+
broadcast: "the broadcast",
|
|
305
|
+
"unique-local": "a unique local",
|
|
306
|
+
"site-local": "a site-local",
|
|
307
|
+
discard: "a discard-only",
|
|
308
|
+
"ipv4-mapped": "an IPv4-mapped",
|
|
309
|
+
"ipv4-compatible": "an IPv4-compatible",
|
|
310
|
+
nat64: "a NAT64",
|
|
311
|
+
"6to4": "a 6to4",
|
|
312
|
+
teredo: "a Teredo",
|
|
313
|
+
invalid: "an invalid"
|
|
314
|
+
};
|
|
315
|
+
function describeRange(info, label) {
|
|
316
|
+
let text = label && label !== info.ip ? `${label} resolves to ${info.ip}, ${RANGE_TEXT[info.range]} address` : `${info.ip} is ${RANGE_TEXT[info.range]} address`;
|
|
317
|
+
if (info.embedded && info.range !== "public") {
|
|
318
|
+
text += ` for ${info.embedded.ip}, ${RANGE_TEXT[info.embedded.range]} address`;
|
|
319
|
+
}
|
|
320
|
+
return text;
|
|
321
|
+
}
|
|
322
|
+
var ruleCache = /* @__PURE__ */ new WeakMap();
|
|
323
|
+
function normalizeName(name) {
|
|
324
|
+
let host = name.trim().toLowerCase();
|
|
325
|
+
while (host.endsWith(".")) host = host.slice(0, -1);
|
|
326
|
+
if (host === "") return host;
|
|
327
|
+
try {
|
|
328
|
+
const parsed = new URL(`http://${host}/`).hostname;
|
|
329
|
+
if (parsed) host = parsed;
|
|
330
|
+
} catch {
|
|
331
|
+
}
|
|
332
|
+
while (host.endsWith(".")) host = host.slice(0, -1);
|
|
333
|
+
return host;
|
|
334
|
+
}
|
|
335
|
+
function compileHosts(list) {
|
|
336
|
+
if (!list || list.length === 0) return void 0;
|
|
337
|
+
const cached = ruleCache.get(list);
|
|
338
|
+
if (cached) return cached;
|
|
339
|
+
const rules = { exact: /* @__PURE__ */ new Set(), suffixes: [], cidrs: [] };
|
|
340
|
+
for (const raw of list) {
|
|
341
|
+
if (typeof raw !== "string") continue;
|
|
342
|
+
const entry = raw.trim();
|
|
343
|
+
if (entry === "") continue;
|
|
344
|
+
const cidr = parseCidr(entry);
|
|
345
|
+
if (cidr) {
|
|
346
|
+
rules.cidrs.push(cidr);
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
if (entry.startsWith("*.")) {
|
|
350
|
+
const base = normalizeName(entry.slice(2));
|
|
351
|
+
if (base) rules.suffixes.push("." + base);
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
const name = normalizeName(entry);
|
|
355
|
+
if (name) rules.exact.add(name);
|
|
356
|
+
}
|
|
357
|
+
ruleCache.set(list, rules);
|
|
358
|
+
return rules;
|
|
359
|
+
}
|
|
360
|
+
function matchName(rules, name) {
|
|
361
|
+
if (!rules) return false;
|
|
362
|
+
if (rules.exact.has(name)) return true;
|
|
363
|
+
for (const s of rules.suffixes) if (name.endsWith(s)) return true;
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
function matchIp(rules, ip) {
|
|
367
|
+
if (!rules) return false;
|
|
368
|
+
for (const c of rules.cidrs) if (inCidr(ip, c)) return true;
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
371
|
+
var METADATA_RULES = compileHosts(METADATA_HOSTS);
|
|
372
|
+
var INTERNAL_RULES = compileHosts(INTERNAL_HOSTS);
|
|
373
|
+
function parseHostIp(host) {
|
|
374
|
+
if (host.charCodeAt(0) === 91 || host.includes(":")) {
|
|
375
|
+
const b6 = parseIPv6(host);
|
|
376
|
+
return b6 ? { version: 6, bytes: b6 } : void 0;
|
|
377
|
+
}
|
|
378
|
+
const b4 = parseIPv4(host);
|
|
379
|
+
return b4 ? { version: 4, bytes: b4 } : void 0;
|
|
380
|
+
}
|
|
381
|
+
function checkAddress(ip, options, label) {
|
|
382
|
+
const info = describeIp(ip);
|
|
383
|
+
const base = { ip: info.ip, range: info.range };
|
|
384
|
+
const prefix = label && label !== info.ip ? `${label} resolves to ` : "";
|
|
385
|
+
if (matchIp(compileHosts(options.blockHosts), ip)) {
|
|
386
|
+
return { safe: false, rule: "blocked-host", reason: `${prefix}${info.ip} is in blockHosts`, ...base };
|
|
387
|
+
}
|
|
388
|
+
if (matchIp(compileHosts(options.allowHosts), ip)) return { safe: true, ...base };
|
|
389
|
+
if (info.range === "public") return { safe: true, ...base };
|
|
390
|
+
if (options.allowPrivate && !isMetadataIp(info)) return { safe: true, ...base };
|
|
391
|
+
return { safe: false, rule: "private-ip", reason: describeRange(info, label), ...base };
|
|
392
|
+
}
|
|
393
|
+
function checkHostname(hostname, options = {}) {
|
|
394
|
+
if (typeof hostname !== "string" || hostname === "") {
|
|
395
|
+
return { safe: false, rule: "invalid-url", reason: "The URL has no host" };
|
|
396
|
+
}
|
|
397
|
+
const ip = parseHostIp(hostname);
|
|
398
|
+
if (ip) return checkAddress(ip, options);
|
|
399
|
+
if (hostname.charCodeAt(0) === 91) {
|
|
400
|
+
return { safe: false, rule: "invalid-url", reason: `${hostname} is not a valid IPv6 address` };
|
|
401
|
+
}
|
|
402
|
+
let name = hostname.toLowerCase();
|
|
403
|
+
while (name.endsWith(".")) name = name.slice(0, -1);
|
|
404
|
+
if (name === "") return { safe: false, rule: "invalid-url", reason: "The URL has no host" };
|
|
405
|
+
if (matchName(compileHosts(options.blockHosts), name)) {
|
|
406
|
+
return { safe: false, rule: "blocked-host", reason: `${name} is in blockHosts` };
|
|
407
|
+
}
|
|
408
|
+
if (matchName(compileHosts(options.allowHosts), name)) return { safe: true, trusted: true };
|
|
409
|
+
if (matchName(METADATA_RULES, name)) {
|
|
410
|
+
return { safe: false, rule: "blocked-host", reason: `${name} is a cloud metadata host` };
|
|
411
|
+
}
|
|
412
|
+
if (options.allowPrivate) return { safe: true };
|
|
413
|
+
if (matchName(INTERNAL_RULES, name)) {
|
|
414
|
+
return { safe: false, rule: "blocked-host", reason: `${name} is an internal hostname` };
|
|
415
|
+
}
|
|
416
|
+
if (!name.includes(".")) {
|
|
417
|
+
return {
|
|
418
|
+
safe: false,
|
|
419
|
+
rule: "single-label",
|
|
420
|
+
reason: `${name} has no dot, so it can only be an internal name (a container, a service or a machine on the local network)`
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
return { safe: true };
|
|
424
|
+
}
|
|
425
|
+
function normalizeProtocols(list) {
|
|
426
|
+
return (list ?? DEFAULT_PROTOCOLS).map((p) => {
|
|
427
|
+
const s = String(p).trim().toLowerCase();
|
|
428
|
+
return s.endsWith(":") ? s : s + ":";
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
function checkUrl(input, options = {}) {
|
|
432
|
+
let url;
|
|
433
|
+
if (input instanceof URL) {
|
|
434
|
+
url = new URL(input.href);
|
|
435
|
+
} else {
|
|
436
|
+
if (typeof input !== "string") return { safe: false, rule: "invalid-url", reason: "The URL must be a string or a URL" };
|
|
437
|
+
const max2 = options.maxLength ?? DEFAULT_MAX_LENGTH;
|
|
438
|
+
if (input.length > max2) {
|
|
439
|
+
return { safe: false, rule: "too-long", reason: `The URL is longer than ${max2} characters` };
|
|
440
|
+
}
|
|
441
|
+
try {
|
|
442
|
+
url = new URL(input);
|
|
443
|
+
} catch {
|
|
444
|
+
return { safe: false, rule: "invalid-url", reason: "The URL could not be parsed" };
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
const max = options.maxLength ?? DEFAULT_MAX_LENGTH;
|
|
448
|
+
if (url.href.length > max) {
|
|
449
|
+
return { safe: false, rule: "too-long", reason: `The URL is longer than ${max} characters`, url };
|
|
450
|
+
}
|
|
451
|
+
const protocol = url.protocol.toLowerCase();
|
|
452
|
+
if (!normalizeProtocols(options.protocols).includes(protocol)) {
|
|
453
|
+
return { safe: false, rule: "protocol", reason: `The ${protocol} scheme is not allowed`, url };
|
|
454
|
+
}
|
|
455
|
+
const host = url.hostname;
|
|
456
|
+
if (!options.allowCredentials && (url.username !== "" || url.password !== "")) {
|
|
457
|
+
return { safe: false, rule: "credentials", reason: "URLs with a username or password are not allowed", url, host };
|
|
458
|
+
}
|
|
459
|
+
const port = url.port !== "" ? Number(url.port) : DEFAULT_PORTS[protocol];
|
|
460
|
+
if (options.ports && (port === void 0 || !options.ports.includes(port))) {
|
|
461
|
+
return { safe: false, rule: "port", reason: `Port ${port ?? "(none)"} is not in the allowed ports`, url, host };
|
|
462
|
+
}
|
|
463
|
+
if (port !== void 0 && options.blockPorts?.includes(port)) {
|
|
464
|
+
return { safe: false, rule: "port", reason: `Port ${port} is blocked`, url, host };
|
|
465
|
+
}
|
|
466
|
+
const verdict = checkHostname(host, options);
|
|
467
|
+
const result = { safe: verdict.safe, url, host };
|
|
468
|
+
if (verdict.ip !== void 0) result.ip = verdict.ip;
|
|
469
|
+
if (verdict.range !== void 0) result.range = verdict.range;
|
|
470
|
+
if (!verdict.safe) {
|
|
471
|
+
result.rule = verdict.rule;
|
|
472
|
+
result.reason = verdict.reason;
|
|
473
|
+
}
|
|
474
|
+
return result;
|
|
475
|
+
}
|
|
476
|
+
function isSafeUrl(input, options) {
|
|
477
|
+
return checkUrl(input, options).safe;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// src/errors.ts
|
|
481
|
+
var SsrfError = class extends Error {
|
|
482
|
+
name = "SsrfError";
|
|
483
|
+
code = "ERR_SSRF_BLOCKED";
|
|
484
|
+
rule;
|
|
485
|
+
url;
|
|
486
|
+
host;
|
|
487
|
+
ip;
|
|
488
|
+
constructor(reason, details) {
|
|
489
|
+
super(reason);
|
|
490
|
+
this.rule = details.rule;
|
|
491
|
+
if (details.url !== void 0) this.url = details.url;
|
|
492
|
+
if (details.host !== void 0) this.host = details.host;
|
|
493
|
+
if (details.ip !== void 0) this.ip = details.ip;
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
var FetchLimitError = class extends Error {
|
|
497
|
+
name = "FetchLimitError";
|
|
498
|
+
code;
|
|
499
|
+
url;
|
|
500
|
+
constructor(message, code, url) {
|
|
501
|
+
super(message);
|
|
502
|
+
this.code = code;
|
|
503
|
+
if (url !== void 0) this.url = url;
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
export {
|
|
508
|
+
parseIp,
|
|
509
|
+
formatIp,
|
|
510
|
+
classifyIp,
|
|
511
|
+
isPrivateIp,
|
|
512
|
+
normalizeIp,
|
|
513
|
+
DEFAULT_BLOCKED_HOSTS,
|
|
514
|
+
parseHostIp,
|
|
515
|
+
checkAddress,
|
|
516
|
+
checkHostname,
|
|
517
|
+
checkUrl,
|
|
518
|
+
isSafeUrl,
|
|
519
|
+
SsrfError,
|
|
520
|
+
FetchLimitError
|
|
521
|
+
};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); 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
|
+
var _chunk4CL2OZUYcjs = require('./chunk-4CL2OZUY.cjs');
|
|
6
|
+
|
|
7
|
+
// src/shared.ts
|
|
8
|
+
var DEFAULT_MAX_REDIRECTS = 5;
|
|
9
|
+
var DEFAULT_TIMEOUT = 3e4;
|
|
10
|
+
var DEFAULT_MAX_BYTES = 10 * 1024 * 1024;
|
|
11
|
+
function assertSafeUrl(input, options, context) {
|
|
12
|
+
const result = _chunk4CL2OZUYcjs.checkUrl.call(void 0, input, options);
|
|
13
|
+
if (!result.safe || !result.url) {
|
|
14
|
+
const where = context ? `${context}: ` : "";
|
|
15
|
+
const details = {
|
|
16
|
+
rule: _nullishCoalesce(result.rule, () => ( "invalid-url")),
|
|
17
|
+
url: _nullishCoalesce(_optionalChain([result, 'access', _ => _.url, 'optionalAccess', _2 => _2.href]), () => ( String(input)))
|
|
18
|
+
};
|
|
19
|
+
if (result.host !== void 0) details.host = result.host;
|
|
20
|
+
if (result.ip !== void 0) details.ip = result.ip;
|
|
21
|
+
throw new (0, _chunk4CL2OZUYcjs.SsrfError)(`Blocked ${where}${_nullishCoalesce(result.reason, () => ( "unsafe URL"))}`, details);
|
|
22
|
+
}
|
|
23
|
+
return result.url;
|
|
24
|
+
}
|
|
25
|
+
function isRedirectStatus(status) {
|
|
26
|
+
return status === 301 || status === 302 || status === 303 || status === 307 || status === 308;
|
|
27
|
+
}
|
|
28
|
+
var BODY_HEADERS = ["content-type", "content-length", "content-encoding", "content-language", "content-location"];
|
|
29
|
+
var CREDENTIAL_HEADERS = ["authorization", "cookie", "proxy-authorization"];
|
|
30
|
+
function planRedirect(state, status, location, options) {
|
|
31
|
+
let next;
|
|
32
|
+
try {
|
|
33
|
+
next = new URL(location, state.url);
|
|
34
|
+
} catch (e) {
|
|
35
|
+
throw new (0, _chunk4CL2OZUYcjs.SsrfError)(`Blocked redirect: the Location header is not a valid URL`, {
|
|
36
|
+
rule: "invalid-url",
|
|
37
|
+
url: location
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
if (state.url.protocol === "https:" && next.protocol === "http:" && !options.allowDowngrade) {
|
|
41
|
+
throw new (0, _chunk4CL2OZUYcjs.SsrfError)(`Blocked redirect from https to http (${next.href})`, {
|
|
42
|
+
rule: "downgrade",
|
|
43
|
+
url: next.href,
|
|
44
|
+
host: next.hostname
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
const url = assertSafeUrl(next, options, "redirect");
|
|
48
|
+
let method = state.method;
|
|
49
|
+
let body = state.body;
|
|
50
|
+
const headers = new Headers(state.headers);
|
|
51
|
+
const upper = method.toUpperCase();
|
|
52
|
+
if ((status === 301 || status === 302) && upper === "POST" || status === 303 && upper !== "GET" && upper !== "HEAD") {
|
|
53
|
+
method = "GET";
|
|
54
|
+
body = null;
|
|
55
|
+
for (const h of BODY_HEADERS) headers.delete(h);
|
|
56
|
+
} else if (body !== null && state.streamed) {
|
|
57
|
+
throw new TypeError(`Can't follow a ${status} redirect: the request body is a stream and can't be sent again`);
|
|
58
|
+
}
|
|
59
|
+
if (url.origin !== state.url.origin) {
|
|
60
|
+
for (const h of CREDENTIAL_HEADERS) headers.delete(h);
|
|
61
|
+
}
|
|
62
|
+
return { url, method, headers, body, streamed: state.streamed && body !== null };
|
|
63
|
+
}
|
|
64
|
+
function tooManyRedirects(max, url) {
|
|
65
|
+
return new (0, _chunk4CL2OZUYcjs.FetchLimitError)(`Too many redirects (more than ${max})`, "ERR_MAX_REDIRECTS", url.href);
|
|
66
|
+
}
|
|
67
|
+
function tooLarge(max, url) {
|
|
68
|
+
return new (0, _chunk4CL2OZUYcjs.FetchLimitError)(`The response is larger than ${max} bytes`, "ERR_MAX_BYTES", url.href);
|
|
69
|
+
}
|
|
70
|
+
function createDeadline(timeout, userSignal) {
|
|
71
|
+
const controller = new AbortController();
|
|
72
|
+
let timer;
|
|
73
|
+
const onAbort = () => controller.abort(_optionalChain([userSignal, 'optionalAccess', _3 => _3.reason]));
|
|
74
|
+
if (userSignal) {
|
|
75
|
+
if (userSignal.aborted) controller.abort(userSignal.reason);
|
|
76
|
+
else userSignal.addEventListener("abort", onAbort, { once: true });
|
|
77
|
+
}
|
|
78
|
+
if (timeout > 0 && Number.isFinite(timeout) && !controller.signal.aborted) {
|
|
79
|
+
timer = setTimeout(() => {
|
|
80
|
+
controller.abort(new (0, _chunk4CL2OZUYcjs.FetchLimitError)(`The request took longer than ${timeout} ms`, "ERR_TIMEOUT"));
|
|
81
|
+
}, timeout);
|
|
82
|
+
}
|
|
83
|
+
let finished = false;
|
|
84
|
+
return {
|
|
85
|
+
signal: controller.signal,
|
|
86
|
+
done() {
|
|
87
|
+
if (finished) return;
|
|
88
|
+
finished = true;
|
|
89
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
90
|
+
_optionalChain([userSignal, 'optionalAccess', _4 => _4.removeEventListener, 'call', _5 => _5("abort", onAbort)]);
|
|
91
|
+
},
|
|
92
|
+
reason() {
|
|
93
|
+
const r = controller.signal.reason;
|
|
94
|
+
return _nullishCoalesce(r, () => ( new DOMException("This operation was aborted", "AbortError")));
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function limitBody(source, maxBytes, url, deadline, onEnd) {
|
|
99
|
+
const reader = source.getReader();
|
|
100
|
+
let total = 0;
|
|
101
|
+
return new ReadableStream({
|
|
102
|
+
async pull(controller) {
|
|
103
|
+
try {
|
|
104
|
+
const { value, done } = await reader.read();
|
|
105
|
+
if (done) {
|
|
106
|
+
onEnd();
|
|
107
|
+
controller.close();
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
total += value.byteLength;
|
|
111
|
+
if (total > maxBytes) {
|
|
112
|
+
const err = tooLarge(maxBytes, url);
|
|
113
|
+
reader.cancel(err).catch(() => {
|
|
114
|
+
});
|
|
115
|
+
onEnd();
|
|
116
|
+
controller.error(err);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
controller.enqueue(value);
|
|
120
|
+
} catch (err) {
|
|
121
|
+
onEnd();
|
|
122
|
+
controller.error(deadline.signal.aborted ? deadline.reason() : err);
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
cancel(reason) {
|
|
126
|
+
onEnd();
|
|
127
|
+
return reader.cancel(reason);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
function buildResponse(body, init, url, redirected) {
|
|
132
|
+
const res = new Response(body, init);
|
|
133
|
+
Object.defineProperty(res, "url", { value: url.href, enumerable: true });
|
|
134
|
+
Object.defineProperty(res, "redirected", { value: redirected, enumerable: true });
|
|
135
|
+
return res;
|
|
136
|
+
}
|
|
137
|
+
function isNullBodyStatus(status) {
|
|
138
|
+
return status === 204 || status === 205 || status === 304;
|
|
139
|
+
}
|
|
140
|
+
function isReadableStream(value) {
|
|
141
|
+
return typeof ReadableStream !== "undefined" && value instanceof ReadableStream;
|
|
142
|
+
}
|
|
143
|
+
function resolveLimits(options) {
|
|
144
|
+
return {
|
|
145
|
+
maxRedirects: _nullishCoalesce(options.maxRedirects, () => ( DEFAULT_MAX_REDIRECTS)),
|
|
146
|
+
timeout: _nullishCoalesce(options.timeout, () => ( DEFAULT_TIMEOUT)),
|
|
147
|
+
maxBytes: _nullishCoalesce(options.maxBytes, () => ( DEFAULT_MAX_BYTES))
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
exports.assertSafeUrl = assertSafeUrl; exports.isRedirectStatus = isRedirectStatus; exports.planRedirect = planRedirect; exports.tooManyRedirects = tooManyRedirects; exports.tooLarge = tooLarge; exports.createDeadline = createDeadline; exports.limitBody = limitBody; exports.buildResponse = buildResponse; exports.isNullBodyStatus = isNullBodyStatus; exports.isReadableStream = isReadableStream; exports.resolveLimits = resolveLimits;
|