@fedify/relay 2.0.35-dev.2480 → 2.0.35-dev.2485
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-D_gRT_KG.js +29 -0
- package/dist/litepub.test.js +1 -1
- package/dist/mastodon.test.js +1 -1
- package/dist/mod.d.cts +3 -1
- package/dist/mod.d.ts +3 -1
- package/dist/{types-COAZF2D5.js → types-BQoCDX2S.js} +810 -268
- package/dist/undici-CjF8_EAQ.js +22748 -0
- package/package.json +4 -4
|
@@ -1,34 +1,736 @@
|
|
|
1
1
|
import "@js-temporal/polyfill";
|
|
2
2
|
import "urlpattern-polyfill";
|
|
3
3
|
globalThis.addEventListener = () => {};
|
|
4
|
-
import {
|
|
4
|
+
import { n as __require, r as __toESM, t as __commonJSMin } from "./chunk-D_gRT_KG.js";
|
|
5
5
|
import { getLogger } from "@logtape/logtape";
|
|
6
|
-
import process$1 from "node:process";
|
|
7
6
|
import dns from "node:dns/promises";
|
|
8
7
|
import { isIP } from "node:net";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
8
|
+
import process$1 from "node:process";
|
|
9
|
+
//#region ../vocab-runtime/dist/public-fetch-Dfmc_Kfy.js
|
|
10
|
+
const grants = /* @__PURE__ */ new WeakMap();
|
|
11
|
+
/**
|
|
12
|
+
* Issues a grant for the given URL's origin and addresses. This does not
|
|
13
|
+
* validate the addresses; callers validate them before issuing, and pinners
|
|
14
|
+
* validate them again right before connecting.
|
|
15
|
+
* @param url The URL whose origin the grant is bound to.
|
|
16
|
+
* @param addresses The addresses to connect to.
|
|
17
|
+
* @returns A new grant.
|
|
18
|
+
*/
|
|
19
|
+
function issuePinGrant(url, addresses) {
|
|
20
|
+
const grant = Object.freeze(Object.create(null));
|
|
21
|
+
let hostname = url.hostname;
|
|
22
|
+
if (hostname.startsWith("[") && hostname.endsWith("]")) hostname = hostname.slice(1, -1);
|
|
23
|
+
const record = Object.freeze({
|
|
24
|
+
origin: url.origin,
|
|
25
|
+
hostname,
|
|
26
|
+
addresses: Object.freeze(addresses.map(({ address, family }) => Object.freeze({
|
|
27
|
+
address,
|
|
28
|
+
family
|
|
29
|
+
})))
|
|
30
|
+
});
|
|
31
|
+
grants.set(grant, {
|
|
32
|
+
record,
|
|
33
|
+
revoked: false
|
|
34
|
+
});
|
|
35
|
+
return grant;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Checks that the given grant was issued by {@link issuePinGrant}, has not
|
|
39
|
+
* been revoked, and was issued for the origin of the given URL.
|
|
40
|
+
* @param grant The grant to redeem.
|
|
41
|
+
* @param url The URL to connect to.
|
|
42
|
+
* @returns What the grant stands for.
|
|
43
|
+
* @throws {TypeError} If the grant cannot be used for the URL.
|
|
44
|
+
*/
|
|
45
|
+
function redeemPinGrant(grant, url) {
|
|
46
|
+
const state = typeof grant === "object" && grant !== null ? grants.get(grant) : void 0;
|
|
47
|
+
if (state == null) throw new TypeError("Invalid pin grant.");
|
|
48
|
+
if (state.revoked) throw new TypeError("The pin grant has been revoked.");
|
|
49
|
+
if (url.origin !== state.record.origin) throw new TypeError(`The pin grant for ${state.record.origin} cannot be used for ${url.origin}.`);
|
|
50
|
+
return state.record;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Revokes the given grant so that it can no longer be redeemed.
|
|
54
|
+
* @param grant The grant to revoke.
|
|
55
|
+
*/
|
|
56
|
+
function revokePinGrant(grant) {
|
|
57
|
+
const state = grants.get(grant);
|
|
58
|
+
if (state != null) state.revoked = true;
|
|
59
|
+
}
|
|
60
|
+
var UrlError = class extends Error {
|
|
61
|
+
/**
|
|
62
|
+
* The failure category. Defaults to `"disallowed"`.
|
|
63
|
+
* `"dns"` includes lookup errors and results with no usable IP addresses.
|
|
64
|
+
* @since 2.0.29
|
|
65
|
+
*/
|
|
66
|
+
reason;
|
|
67
|
+
constructor(message, options) {
|
|
68
|
+
super(message, options);
|
|
69
|
+
this.name = "UrlError";
|
|
70
|
+
this.reason = options?.reason ?? "disallowed";
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Resolves all addresses of the given hostname through `node:dns/promises`.
|
|
75
|
+
* The default `dns` object is looked up on every call so that tests can still
|
|
76
|
+
* stub `dns.lookup()`.
|
|
77
|
+
* @internal
|
|
78
|
+
*/
|
|
79
|
+
function lookupAllAddresses(hostname) {
|
|
80
|
+
return dns.lookup(hostname, { all: true });
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Checks the parts of a URL that can be validated without resolving it:
|
|
84
|
+
* the protocol, the `localhost` hostname, and IP literal hostnames.
|
|
85
|
+
* @throws {UrlError} If the URL is not allowed.
|
|
86
|
+
* @internal
|
|
87
|
+
*/
|
|
88
|
+
function parsePublicUrl(url) {
|
|
89
|
+
const parsed = new URL(url);
|
|
90
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new UrlError(`Unsupported protocol: ${parsed.protocol}`);
|
|
91
|
+
let hostname = parsed.hostname;
|
|
92
|
+
if (hostname.startsWith("[") && hostname.endsWith("]")) hostname = hostname.slice(1, -1);
|
|
93
|
+
if (hostname === "localhost") throw new UrlError("Localhost is not allowed");
|
|
94
|
+
const family = isIP(hostname);
|
|
95
|
+
if (family !== 0) validatePublicIpAddress(hostname, family);
|
|
96
|
+
return {
|
|
97
|
+
url: parsed,
|
|
98
|
+
hostname,
|
|
99
|
+
family
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Resolves the given hostname and checks that every returned IP address is
|
|
104
|
+
* public.
|
|
105
|
+
* @returns The IP addresses of the hostname, without non-IP entries.
|
|
106
|
+
* @throws {UrlError} If the lookup fails, returns no IP address, or returns
|
|
107
|
+
* any non-public address.
|
|
108
|
+
* @internal
|
|
109
|
+
*/
|
|
110
|
+
async function lookupPublicAddresses(hostname, lookup) {
|
|
111
|
+
let addresses;
|
|
112
|
+
try {
|
|
113
|
+
addresses = await lookup(hostname);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
throw new UrlError("DNS lookup failed", {
|
|
116
|
+
cause: error,
|
|
117
|
+
reason: "dns"
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
validateLookupAddresses(addresses);
|
|
121
|
+
return addresses.filter(({ address }) => isIP(address) !== 0).map(({ address }) => ({
|
|
122
|
+
address,
|
|
123
|
+
family: isIP(address)
|
|
124
|
+
}));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Checks that the given IP address is public. This is used right before
|
|
128
|
+
* connecting to an address that passed {@link lookupPublicAddresses}.
|
|
129
|
+
* @throws {UrlError} If the address is not a public IP address.
|
|
130
|
+
* @internal
|
|
131
|
+
*/
|
|
132
|
+
function assertPublicAddress(address) {
|
|
133
|
+
const family = isIP(address.address);
|
|
134
|
+
if (family === 0 || family !== address.family) throw new UrlError(`Invalid or private address: ${address.address}`);
|
|
135
|
+
validatePublicIpAddress(address.address, family);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Validates the IP addresses returned by `node:dns.lookup()`.
|
|
139
|
+
*
|
|
140
|
+
* Cloudflare Workers' `node:dns` implementation currently maps every record
|
|
141
|
+
* in a DNS-over-HTTPS `Answer` array—including CNAME records—to a
|
|
142
|
+
* `LookupAddress`, even though Node.js specifies that the `address` field must
|
|
143
|
+
* contain an IPv4 or IPv6 literal. See:
|
|
144
|
+
* https://github.com/cloudflare/workerd/issues/6886
|
|
145
|
+
*
|
|
146
|
+
* Work around that bug by ignoring non-IP entries only when the lookup also
|
|
147
|
+
* returns at least one actual IP address. This remains fail-closed: a result
|
|
148
|
+
* containing no IP addresses is rejected, and every returned IP address is
|
|
149
|
+
* still validated and must be public. This workaround can be revisited once
|
|
150
|
+
* the Workerd issue is fixed in supported Cloudflare Workers runtimes.
|
|
151
|
+
*
|
|
152
|
+
* @internal
|
|
153
|
+
*/
|
|
154
|
+
function validateLookupAddresses(addresses) {
|
|
155
|
+
let ipAddressCount = 0;
|
|
156
|
+
for (const { address } of addresses) {
|
|
157
|
+
const family = isIP(address);
|
|
158
|
+
if (family === 0) continue;
|
|
159
|
+
ipAddressCount++;
|
|
160
|
+
validatePublicIpAddress(address, family);
|
|
161
|
+
}
|
|
162
|
+
if (ipAddressCount === 0) throw new UrlError("DNS lookup did not return any IP address", { reason: "dns" });
|
|
163
|
+
}
|
|
164
|
+
function validatePublicIpAddress(address, family) {
|
|
165
|
+
if (family === 4 && isValidPublicIPv4Address(address) || family === 6 && isValidPublicIPv6Address(address)) return;
|
|
166
|
+
throw new UrlError(`Invalid or private address: ${address}`);
|
|
167
|
+
}
|
|
168
|
+
function isValidPublicIPv4Address(address) {
|
|
169
|
+
const parts = parseIPv4Address(address);
|
|
170
|
+
if (parts == null) return false;
|
|
171
|
+
const value = ipv4PartsToNumber(parts);
|
|
172
|
+
return !nonPublicIPv4Prefixes.some(({ base, prefix }) => matchesIPv4Prefix(value, base, prefix));
|
|
173
|
+
}
|
|
174
|
+
function isValidPublicIPv6Address(address) {
|
|
175
|
+
const words = parseIPv6Address(address);
|
|
176
|
+
if (words == null) return false;
|
|
177
|
+
if (nonPublicIPv6Prefixes.some(({ words: prefixWords, prefix }) => matchesIPv6Prefix(words, prefixWords, prefix))) return false;
|
|
178
|
+
for (const { extractIPv4, prefix, words: prefixWords } of ipv6WithIPv4Prefixes) {
|
|
179
|
+
if (!matchesIPv6Prefix(words, prefixWords, prefix)) continue;
|
|
180
|
+
const ipv4Address = extractIPv4(words);
|
|
181
|
+
if (ipv4Address != null && !isValidPublicIPv4Address(ipv4Address)) return false;
|
|
182
|
+
}
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
function expandIPv6Address(address) {
|
|
186
|
+
address = address.toLowerCase();
|
|
187
|
+
const ipv4Delimiter = address.lastIndexOf(":");
|
|
188
|
+
if (address.includes(".") && ipv4Delimiter >= 0) {
|
|
189
|
+
const ipv4Parts = parseIPv4Address(address.substring(ipv4Delimiter + 1));
|
|
190
|
+
if (ipv4Parts == null) return address;
|
|
191
|
+
const high = (ipv4Parts[0] << 8) + ipv4Parts[1];
|
|
192
|
+
const low = (ipv4Parts[2] << 8) + ipv4Parts[3];
|
|
193
|
+
address = address.substring(0, ipv4Delimiter + 1) + high.toString(16) + ":" + low.toString(16);
|
|
194
|
+
}
|
|
195
|
+
if (address === "::") return "0000:0000:0000:0000:0000:0000:0000:0000";
|
|
196
|
+
if (address.startsWith("::")) address = "0000" + address;
|
|
197
|
+
if (address.endsWith("::")) address = address + "0000";
|
|
198
|
+
address = address.replace("::", ":0000".repeat(8 - (address.match(/:/g) || []).length) + ":");
|
|
199
|
+
return address.split(":").map((part) => part.padStart(4, "0")).join(":");
|
|
200
|
+
}
|
|
201
|
+
const nonPublicIPv4Prefixes = [
|
|
202
|
+
ipv4Prefix("0.0.0.0/8", "RFC 6890"),
|
|
203
|
+
ipv4Prefix("10.0.0.0/8", "RFC 1918"),
|
|
204
|
+
ipv4Prefix("100.64.0.0/10", "RFC 6598"),
|
|
205
|
+
ipv4Prefix("127.0.0.0/8", "RFC 1122"),
|
|
206
|
+
ipv4Prefix("169.254.0.0/16", "RFC 3927"),
|
|
207
|
+
ipv4Prefix("172.16.0.0/12", "RFC 1918"),
|
|
208
|
+
ipv4Prefix("192.0.0.0/24", "RFC 6890"),
|
|
209
|
+
ipv4Prefix("192.0.2.0/24", "RFC 5737"),
|
|
210
|
+
ipv4Prefix("192.88.99.0/24", "RFC 7526"),
|
|
211
|
+
ipv4Prefix("192.168.0.0/16", "RFC 1918"),
|
|
212
|
+
ipv4Prefix("198.18.0.0/15", "RFC 2544"),
|
|
213
|
+
ipv4Prefix("198.51.100.0/24", "RFC 5737"),
|
|
214
|
+
ipv4Prefix("203.0.113.0/24", "RFC 5737"),
|
|
215
|
+
ipv4Prefix("224.0.0.0/4", "RFC 5771"),
|
|
216
|
+
ipv4Prefix("240.0.0.0/4", "RFC 1112")
|
|
217
|
+
];
|
|
218
|
+
const nonPublicIPv6Prefixes = [
|
|
219
|
+
ipv6Prefix("::/16", "RFC 4291"),
|
|
220
|
+
ipv6Prefix("2001::/32", "RFC 4380"),
|
|
221
|
+
ipv6Prefix("2002::/16", "RFC 3056"),
|
|
222
|
+
ipv6Prefix("64:ff9b:1::/48", "RFC 8215"),
|
|
223
|
+
ipv6Prefix("fc00::/7", "RFC 4193"),
|
|
224
|
+
ipv6Prefix("fe80::/10", "RFC 4291"),
|
|
225
|
+
ipv6Prefix("ff00::/8", "RFC 4291")
|
|
226
|
+
];
|
|
227
|
+
const ipv6WithIPv4Prefixes = [{
|
|
228
|
+
...ipv6Prefix("64:ff9b::/96", "RFC 6052"),
|
|
229
|
+
extractIPv4: (words) => ipv4FromWords(words[6], words[7])
|
|
230
|
+
}];
|
|
231
|
+
function ipv4Prefix(cidr, rfc) {
|
|
232
|
+
const [address, prefixText] = cidr.split("/");
|
|
233
|
+
const prefix = parseInt(prefixText, 10);
|
|
234
|
+
const parts = parseIPv4Address(address);
|
|
235
|
+
if (parts == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 32) throw new Error(`Invalid IPv4 prefix: ${cidr}`);
|
|
236
|
+
return {
|
|
237
|
+
cidr,
|
|
238
|
+
base: ipv4PartsToNumber(parts),
|
|
239
|
+
prefix,
|
|
240
|
+
rfc
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function ipv6Prefix(cidr, rfc) {
|
|
244
|
+
const [address, prefixText] = cidr.split("/");
|
|
245
|
+
const prefix = parseInt(prefixText, 10);
|
|
246
|
+
const words = parseIPv6Address(address);
|
|
247
|
+
if (words == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 128) throw new Error(`Invalid IPv6 prefix: ${cidr}`);
|
|
248
|
+
return {
|
|
249
|
+
cidr,
|
|
250
|
+
words,
|
|
251
|
+
prefix,
|
|
252
|
+
rfc
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
function parseIPv4Address(address) {
|
|
256
|
+
const parts = address.split(".").map((part) => {
|
|
257
|
+
if (!/^\d+$/.test(part)) return NaN;
|
|
258
|
+
return parseInt(part, 10);
|
|
259
|
+
});
|
|
260
|
+
if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) return null;
|
|
261
|
+
return parts;
|
|
262
|
+
}
|
|
263
|
+
function parseIPv6Address(address) {
|
|
264
|
+
const parts = expandIPv6Address(address).split(":");
|
|
265
|
+
if (parts.length !== 8) return null;
|
|
266
|
+
const words = parts.map((part) => {
|
|
267
|
+
if (!/^[0-9a-f]{1,4}$/i.test(part)) return NaN;
|
|
268
|
+
return parseInt(part, 16);
|
|
269
|
+
});
|
|
270
|
+
if (words.some((word) => !Number.isInteger(word) || word < 0 || word > 65535)) return null;
|
|
271
|
+
return words;
|
|
272
|
+
}
|
|
273
|
+
function ipv4PartsToNumber(parts) {
|
|
274
|
+
return parts[0] * 2 ** 24 + parts[1] * 2 ** 16 + parts[2] * 2 ** 8 + parts[3];
|
|
275
|
+
}
|
|
276
|
+
function ipv4FromWords(highWord, lowWord) {
|
|
277
|
+
return [
|
|
278
|
+
highWord >> 8,
|
|
279
|
+
highWord & 255,
|
|
280
|
+
lowWord >> 8,
|
|
281
|
+
lowWord & 255
|
|
282
|
+
].join(".");
|
|
283
|
+
}
|
|
284
|
+
function matchesIPv4Prefix(address, prefixBase, prefixLength) {
|
|
285
|
+
const blockSize = 2 ** (32 - prefixLength);
|
|
286
|
+
return Math.floor(address / blockSize) === Math.floor(prefixBase / blockSize);
|
|
287
|
+
}
|
|
288
|
+
function matchesIPv6Prefix(address, prefixWords, prefixLength) {
|
|
289
|
+
let remaining = prefixLength;
|
|
290
|
+
for (let i = 0; i < 8 && remaining > 0; i++) if (remaining >= 16) {
|
|
291
|
+
if (address[i] !== prefixWords[i]) return false;
|
|
292
|
+
remaining -= 16;
|
|
293
|
+
} else {
|
|
294
|
+
const mask = 65535 << 16 - remaining & 65535;
|
|
295
|
+
if ((address[i] & mask) !== (prefixWords[i] & mask)) return false;
|
|
296
|
+
remaining = 0;
|
|
297
|
+
}
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
const logger$1 = getLogger([
|
|
301
|
+
"fedify",
|
|
302
|
+
"runtime",
|
|
303
|
+
"fetch"
|
|
304
|
+
]);
|
|
305
|
+
/**
|
|
306
|
+
* The first Bun version that honors `proxy: false` when proxy environment
|
|
307
|
+
* variables are set at startup. Earlier versions send pinned requests
|
|
308
|
+
* through the proxy, with the validated IP address as the target.
|
|
309
|
+
*/
|
|
310
|
+
const PROXY_FALSE_MIN_VERSION = "1.4.3";
|
|
311
|
+
const PROXY_VARIABLES = [
|
|
312
|
+
"HTTP_PROXY",
|
|
313
|
+
"http_proxy",
|
|
314
|
+
"HTTPS_PROXY",
|
|
315
|
+
"https_proxy",
|
|
316
|
+
"ALL_PROXY",
|
|
317
|
+
"all_proxy"
|
|
318
|
+
];
|
|
319
|
+
const FALLBACK_ERROR_CODES = new Set([
|
|
320
|
+
"ConnectionRefused",
|
|
321
|
+
"ECONNREFUSED",
|
|
322
|
+
"EHOSTUNREACH",
|
|
323
|
+
"ENETUNREACH",
|
|
324
|
+
"ETIMEDOUT"
|
|
325
|
+
]);
|
|
326
|
+
/**
|
|
327
|
+
* Bun versions before 1.3.11 verify the server certificate against the `Host`
|
|
328
|
+
* header, including a non-default port, instead of `tls.serverName`, so
|
|
329
|
+
* pinned requests to HTTPS servers on non-default ports always fail the check
|
|
330
|
+
* there. A custom `checkServerIdentity` cannot stand in for it: these
|
|
331
|
+
* versions send the whole request before a failing custom check aborts it
|
|
332
|
+
* (and 1.3.10 even reuses the connection for the next request), so such
|
|
333
|
+
* requests are refused instead.
|
|
334
|
+
*/
|
|
335
|
+
const HOST_HEADER_IDENTITY_RANGE = "<1.3.11";
|
|
336
|
+
/**
|
|
337
|
+
* Creates a pinner for Bun. Every request is sent to an IP literal URL with
|
|
338
|
+
* the original `Host` header, `tls.serverName` set to the original hostname
|
|
339
|
+
* (so that the certificate is still verified against it), and `proxy: false`.
|
|
340
|
+
*
|
|
341
|
+
* GET and HEAD requests fall back to the next address on transport errors
|
|
342
|
+
* raised before a response arrives. Other methods use only the first
|
|
343
|
+
* address, since a transport error does not guarantee that the request did
|
|
344
|
+
* not reach the server, and retrying it could deliver an activity twice.
|
|
345
|
+
* Together with the transport retries of `doubleKnock()`, a GET request is
|
|
346
|
+
* attempted at most twice per address. Note that Bun 1.4 also reports
|
|
347
|
+
* a connection reset after the request was sent as a `TypeError`, so GET and
|
|
348
|
+
* HEAD requests may be repeated against the next address in that case, which
|
|
349
|
+
* is acceptable for these idempotent methods.
|
|
350
|
+
*
|
|
351
|
+
* Bun versions before 1.3.11 verify certificates against the `Host` header,
|
|
352
|
+
* which includes a non-default port, and cannot safely check them against
|
|
353
|
+
* the original hostname in any other way, so requests to HTTPS servers on
|
|
354
|
+
* non-default ports fail there with a `TypeError` before connecting.
|
|
355
|
+
*
|
|
356
|
+
* Bun versions before 1.4.3 ignore `proxy: false` when proxy environment
|
|
357
|
+
* variables are set, so requests go through the proxy with the validated IP
|
|
358
|
+
* address as the target. In that case a warning is logged once.
|
|
359
|
+
* @param options The pinner options.
|
|
360
|
+
* @returns The pinner.
|
|
361
|
+
*/
|
|
362
|
+
function createBunPinner({ verifyAddress }) {
|
|
363
|
+
let proxyWarningPending = ignoresProxyFalse() && hasProxyVariables();
|
|
364
|
+
return {
|
|
365
|
+
supports(url) {
|
|
366
|
+
return url.protocol === "http:" || url.protocol === "https:";
|
|
367
|
+
},
|
|
368
|
+
async fetch(grant, request, signal) {
|
|
369
|
+
const url = new URL(request.url);
|
|
370
|
+
const { hostname, addresses } = redeemPinGrant(grant, url);
|
|
371
|
+
if (proxyWarningPending) {
|
|
372
|
+
proxyWarningPending = false;
|
|
373
|
+
logger$1.warn("Bun {version} ignores proxy: false while proxy environment variables are set, so outbound requests go through the proxy instead of connecting directly to the validated addresses. Upgrade to Bun {minVersion} or later, or unset the proxy environment variables; see {url}.", {
|
|
374
|
+
version: getBun()?.version,
|
|
375
|
+
minVersion: PROXY_FALSE_MIN_VERSION,
|
|
376
|
+
url: "https://fedify.dev/manual/deploy#bun-behind-an-http-proxy"
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
const fallback = request.method === "GET" || request.method === "HEAD";
|
|
380
|
+
const candidates = fallback ? addresses : addresses.slice(0, 1);
|
|
381
|
+
const body = request.body == null ? null : await request.arrayBuffer();
|
|
382
|
+
let lastError = /* @__PURE__ */ new TypeError(`No address to connect to for ${url.host}.`);
|
|
383
|
+
for (const address of candidates) {
|
|
384
|
+
throwIfAborted(signal, request.signal);
|
|
385
|
+
verifyAddress(address);
|
|
386
|
+
const headers = new Headers(request.headers);
|
|
387
|
+
headers.set("Host", url.host);
|
|
388
|
+
const init = {
|
|
389
|
+
method: request.method,
|
|
390
|
+
headers,
|
|
391
|
+
body,
|
|
392
|
+
redirect: "manual",
|
|
393
|
+
signal: signal == null || signal === request.signal ? request.signal : anySignal(signal, request.signal),
|
|
394
|
+
proxy: false
|
|
395
|
+
};
|
|
396
|
+
if (url.protocol === "https:") init.tls = getTlsOptions(url, hostname);
|
|
397
|
+
try {
|
|
398
|
+
return await fetch(toAddressUrl(url, address), init);
|
|
399
|
+
} catch (error) {
|
|
400
|
+
throwIfAborted(signal, request.signal);
|
|
401
|
+
if (!fallback || !isFallbackError(error)) throw error;
|
|
402
|
+
lastError = error;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
throw lastError;
|
|
406
|
+
},
|
|
407
|
+
release() {}
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
function getTlsOptions(url, hostname) {
|
|
411
|
+
const bun = getBun();
|
|
412
|
+
if (url.port !== "" && bun != null && bun.semver.satisfies(bun.version, HOST_HEADER_IDENTITY_RANGE)) throw new TypeError(`Bun ${bun.version} cannot verify the certificate of ${url.host} when connecting to a validated address; upgrade to Bun 1.3.11 or later to fetch from HTTPS servers on non-default ports.`);
|
|
413
|
+
return isIP(hostname) === 0 ? { serverName: hostname } : {};
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Combines two signals without `AbortSignal.any()`, which Bun 1.1.0 lacks.
|
|
417
|
+
*/
|
|
418
|
+
function anySignal(a, b) {
|
|
419
|
+
if (a.aborted) return a;
|
|
420
|
+
if (b.aborted) return b;
|
|
421
|
+
const controller = new AbortController();
|
|
422
|
+
const abort = (event) => {
|
|
423
|
+
controller.abort(event.target.reason);
|
|
424
|
+
a.removeEventListener("abort", abort);
|
|
425
|
+
b.removeEventListener("abort", abort);
|
|
426
|
+
};
|
|
427
|
+
a.addEventListener("abort", abort);
|
|
428
|
+
b.addEventListener("abort", abort);
|
|
429
|
+
return controller.signal;
|
|
430
|
+
}
|
|
431
|
+
function toAddressUrl(url, address) {
|
|
432
|
+
const result = new URL(url);
|
|
433
|
+
result.hostname = address.family === 6 ? `[${address.address}]` : address.address;
|
|
434
|
+
return result;
|
|
435
|
+
}
|
|
436
|
+
function isFallbackError(error) {
|
|
437
|
+
if (error instanceof Error && error.name === "AbortError") return false;
|
|
438
|
+
if (error instanceof TypeError) return true;
|
|
439
|
+
const code = error?.code;
|
|
440
|
+
return typeof code === "string" && FALLBACK_ERROR_CODES.has(code);
|
|
441
|
+
}
|
|
442
|
+
function throwIfAborted(...signals) {
|
|
443
|
+
for (const signal of signals) signal?.throwIfAborted();
|
|
444
|
+
}
|
|
445
|
+
function getBun() {
|
|
446
|
+
return globalThis.Bun;
|
|
447
|
+
}
|
|
448
|
+
function ignoresProxyFalse() {
|
|
449
|
+
const bun = getBun();
|
|
450
|
+
if (bun == null) return false;
|
|
451
|
+
return !bun.semver.satisfies(bun.version, `>=${PROXY_FALSE_MIN_VERSION}`);
|
|
452
|
+
}
|
|
453
|
+
function hasProxyVariables() {
|
|
454
|
+
const env = globalThis.process?.env;
|
|
455
|
+
if (env == null) return false;
|
|
456
|
+
return PROXY_VARIABLES.some((name) => (env[name] ?? "") !== "");
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Creates a pinner for Node.js. Every request is sent through the global
|
|
460
|
+
* `fetch()` with an undici `Agent` as the `dispatcher`. The agent's
|
|
461
|
+
* `connect.lookup` answers only with the addresses of live grants for
|
|
462
|
+
* the hostname, checking each of them again right before connecting, and
|
|
463
|
+
* fails when there are none, so a connection can never be made to an address
|
|
464
|
+
* that was not validated.
|
|
465
|
+
*
|
|
466
|
+
* The agent is shared by the requests of all grants, so keep-alive
|
|
467
|
+
* connections are reused. A pooled connection was made to an address of
|
|
468
|
+
* a grant that was valid when it was connected, so reusing it from another
|
|
469
|
+
* session of the same origin still reaches a validated address. Response
|
|
470
|
+
* bodies have to be consumed or cancelled for their connections to return to
|
|
471
|
+
* the pool, which sessions do when they are closed.
|
|
472
|
+
*
|
|
473
|
+
* Since the dispatcher is passed explicitly, neither proxy environment
|
|
474
|
+
* variables nor the global dispatcher (e.g., `NODE_USE_ENV_PROXY=1`) apply.
|
|
475
|
+
* @param options The pinner options.
|
|
476
|
+
* @returns The pinner.
|
|
477
|
+
*/
|
|
478
|
+
function createNodePinner({ verifyAddress, undici }) {
|
|
479
|
+
const grants = /* @__PURE__ */ new Map();
|
|
480
|
+
function lookup(hostname, options, callback) {
|
|
481
|
+
let addresses;
|
|
482
|
+
try {
|
|
483
|
+
addresses = getGrantedAddresses(hostname, options.family);
|
|
484
|
+
} catch (error) {
|
|
485
|
+
callback(error, []);
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
if (options.all) callback(null, addresses);
|
|
489
|
+
else callback(null, addresses[0].address, addresses[0].family);
|
|
490
|
+
}
|
|
491
|
+
function getGrantedAddresses(hostname, family) {
|
|
492
|
+
const addresses = /* @__PURE__ */ new Map();
|
|
493
|
+
for (const [grant, record] of grants.get(hostname) ?? []) {
|
|
494
|
+
try {
|
|
495
|
+
redeemPinGrant(grant, new URL(record.origin));
|
|
496
|
+
} catch {
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
for (const address of record.addresses) addresses.set(address.address, address);
|
|
500
|
+
}
|
|
501
|
+
const familyNumber = family === "IPv4" ? 4 : family === "IPv6" ? 6 : typeof family === "number" ? family : 0;
|
|
502
|
+
const result = [...addresses.values()].filter((address) => familyNumber === 0 || address.family === familyNumber);
|
|
503
|
+
if (result.length < 1) throw new UrlError(`No validated address to connect to: ${hostname}`);
|
|
504
|
+
for (const address of result) verifyAddress(address);
|
|
505
|
+
return result;
|
|
506
|
+
}
|
|
507
|
+
const agent = new undici.Agent({ connect: { lookup } });
|
|
508
|
+
return {
|
|
509
|
+
supports(url) {
|
|
510
|
+
return url.protocol === "http:" || url.protocol === "https:";
|
|
511
|
+
},
|
|
512
|
+
async fetch(grant, request, signal) {
|
|
513
|
+
const record = redeemPinGrant(grant, new URL(request.url));
|
|
514
|
+
if (isIP(record.hostname) !== 0) for (const address of record.addresses) verifyAddress(address);
|
|
515
|
+
let registered = grants.get(record.hostname);
|
|
516
|
+
if (registered == null) {
|
|
517
|
+
registered = /* @__PURE__ */ new Map();
|
|
518
|
+
grants.set(record.hostname, registered);
|
|
519
|
+
}
|
|
520
|
+
registered.set(grant, record);
|
|
521
|
+
return await fetch(request, {
|
|
522
|
+
redirect: "manual",
|
|
523
|
+
signal: signal == null || signal === request.signal ? request.signal : AbortSignal.any([signal, request.signal]),
|
|
524
|
+
dispatcher: agent
|
|
525
|
+
});
|
|
526
|
+
},
|
|
527
|
+
release(grant) {
|
|
528
|
+
for (const [hostname, registered] of grants) {
|
|
529
|
+
if (!registered.delete(grant)) continue;
|
|
530
|
+
if (registered.size < 1) grants.delete(hostname);
|
|
531
|
+
break;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Detects the current runtime. Cloudflare Workers is checked first, since
|
|
538
|
+
* `process` can exist there with `nodejs_compat`, and Deno and Bun before
|
|
539
|
+
* Node.js, since both provide `process.versions.node`.
|
|
540
|
+
* @returns The current runtime.
|
|
541
|
+
*/
|
|
542
|
+
function detectRuntime() {
|
|
543
|
+
const g = globalThis;
|
|
544
|
+
if (g.navigator?.userAgent === "Cloudflare-Workers") return "cfworkers";
|
|
545
|
+
if ("Deno" in globalThis) return "deno";
|
|
546
|
+
if ("Bun" in globalThis) return "bun";
|
|
547
|
+
if (g.process?.versions?.node != null) return "node";
|
|
548
|
+
return "other";
|
|
549
|
+
}
|
|
550
|
+
let runtimePinner;
|
|
551
|
+
/**
|
|
552
|
+
* Gets the production pinner of the current runtime, which rejects
|
|
553
|
+
* non-public addresses right before connecting. Deno and Cloudflare Workers
|
|
554
|
+
* have no pinner.
|
|
555
|
+
* @returns The pinner, or `null` if the runtime has none.
|
|
556
|
+
*/
|
|
557
|
+
function getRuntimePinner() {
|
|
558
|
+
runtimePinner ??= createRuntimePinner(detectRuntime());
|
|
559
|
+
return runtimePinner;
|
|
560
|
+
}
|
|
561
|
+
async function createRuntimePinner(runtime) {
|
|
562
|
+
switch (runtime) {
|
|
563
|
+
case "bun": return createBunPinner({ verifyAddress: assertPublicAddress });
|
|
564
|
+
case "node": return createNodePinner({
|
|
565
|
+
verifyAddress: assertPublicAddress,
|
|
566
|
+
undici: await import("./undici-CjF8_EAQ.js").then((m) => /* @__PURE__ */ __toESM(m.default, 1))
|
|
567
|
+
});
|
|
568
|
+
default: return null;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
const logger$2 = getLogger([
|
|
572
|
+
"fedify",
|
|
573
|
+
"runtime",
|
|
574
|
+
"fetch"
|
|
575
|
+
]);
|
|
576
|
+
/**
|
|
577
|
+
* Creates {@link PublicFetch.openPublicFetch} and
|
|
578
|
+
* {@link PublicFetch.withPublicFetch} bound to the given pinner and lookup.
|
|
579
|
+
* This is exported only for tests; it is not exported from any entry point.
|
|
580
|
+
* @param dependencies The pinner and the default lookup function.
|
|
581
|
+
* @returns The functions.
|
|
582
|
+
*/
|
|
583
|
+
function createPublicFetch(dependencies) {
|
|
584
|
+
async function openPublicFetch(url, options = {}) {
|
|
585
|
+
const { url: parsed, hostname, family } = parsePublicUrl(url);
|
|
586
|
+
if (parsed.username !== "" || parsed.password !== "") throw new UrlError("URLs with credentials are not allowed");
|
|
587
|
+
const lookup = options.lookup ?? dependencies.lookup;
|
|
588
|
+
let addresses;
|
|
589
|
+
if (family !== 0) addresses = [{
|
|
590
|
+
address: hostname,
|
|
591
|
+
family
|
|
592
|
+
}];
|
|
593
|
+
else if (lookup === lookupAllAddresses && !await canResolve()) return createSession(parsed, globalFetch);
|
|
594
|
+
else {
|
|
595
|
+
options.signal?.throwIfAborted();
|
|
596
|
+
addresses = await lookupPublicAddresses(hostname, lookup);
|
|
597
|
+
options.signal?.throwIfAborted();
|
|
598
|
+
}
|
|
599
|
+
const pinner = await dependencies.pinner();
|
|
600
|
+
if (pinner == null || !pinner.supports(parsed)) {
|
|
601
|
+
logUnpinned(parsed);
|
|
602
|
+
return createSession(parsed, globalFetch);
|
|
603
|
+
}
|
|
604
|
+
const grant = issuePinGrant(parsed, addresses);
|
|
605
|
+
return createSession(parsed, (input, init) => pinner.fetch(grant, new Request(input, init), init.signal ?? void 0), {
|
|
606
|
+
grant,
|
|
607
|
+
pinner
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
async function withPublicFetch(url, fn, options) {
|
|
611
|
+
const session = await openPublicFetch(url, options);
|
|
612
|
+
try {
|
|
613
|
+
const result = await fn(session.fetch);
|
|
614
|
+
if (result instanceof Response) throw new TypeError("withPublicFetch() callbacks must not return a Response, since its body is cancelled when the session is closed.");
|
|
615
|
+
return result;
|
|
616
|
+
} catch (error) {
|
|
617
|
+
logger$2.debug("Failed while fetching {url}; closing the session: {error}", {
|
|
618
|
+
url: session.url.href,
|
|
619
|
+
error
|
|
620
|
+
});
|
|
621
|
+
throw error;
|
|
622
|
+
} finally {
|
|
623
|
+
await session.close();
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
return {
|
|
627
|
+
openPublicFetch,
|
|
628
|
+
withPublicFetch
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
const runtimePublicFetch = createPublicFetch({
|
|
632
|
+
pinner: getRuntimePinner,
|
|
633
|
+
lookup: lookupAllAddresses
|
|
634
|
+
});
|
|
635
|
+
/**
|
|
636
|
+
* Validates the URL, resolves its hostname, checks that every resolved
|
|
637
|
+
* address is public, and opens a session that connects only to those
|
|
638
|
+
* addresses. Connections are pinned on Node.js and Bun. On other runtimes,
|
|
639
|
+
* the session uses the global `fetch()` after the check.
|
|
640
|
+
* @param url The URL to open a session for.
|
|
641
|
+
* @param options The options.
|
|
642
|
+
* @returns The session. It must be closed.
|
|
643
|
+
* @throws {UrlError} If the URL is not allowed, or its hostname cannot be
|
|
644
|
+
* resolved (with `reason: "dns"`).
|
|
645
|
+
*/
|
|
646
|
+
const openPublicFetch = runtimePublicFetch.openPublicFetch;
|
|
647
|
+
runtimePublicFetch.withPublicFetch;
|
|
648
|
+
/**
|
|
649
|
+
* Opens a session that neither validates nor pins addresses, for callers that
|
|
650
|
+
* allow private addresses. It has the same rules as {@link openPublicFetch}
|
|
651
|
+
* sessions otherwise: requests are limited to the URL's origin, redirects are
|
|
652
|
+
* never followed, and unconsumed response bodies are cancelled on close.
|
|
653
|
+
* It grants nothing beyond calling the global `fetch()` directly.
|
|
654
|
+
* @param url The URL to open a session for.
|
|
655
|
+
* @returns The session. It must be closed.
|
|
656
|
+
*/
|
|
657
|
+
function openDirectFetch(url) {
|
|
658
|
+
return Promise.resolve(createSession(new URL(url), globalFetch));
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Starts cancelling the body of a response without waiting for it to settle.
|
|
662
|
+
* The cancellation of a branch of a tee (e.g., a body cloned by a fetch mock)
|
|
663
|
+
* settles only after the other branch is cancelled too, per the Streams
|
|
664
|
+
* standard, so awaiting it could block forever. Failures are only logged.
|
|
665
|
+
*/
|
|
666
|
+
function cancelResponseBody$1(response, url) {
|
|
667
|
+
response.body?.cancel().catch((error) => {
|
|
668
|
+
logger$2.debug("Failed to cancel a response body from {url}: {error}", {
|
|
669
|
+
url: url.href,
|
|
670
|
+
error
|
|
671
|
+
});
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
function globalFetch(input, init) {
|
|
675
|
+
return fetch(input, init);
|
|
676
|
+
}
|
|
677
|
+
function createSession(url, send, pin) {
|
|
678
|
+
const origin = url.origin;
|
|
679
|
+
const tracked = [];
|
|
680
|
+
let closed = false;
|
|
681
|
+
async function sessionFetch(input, init) {
|
|
682
|
+
if (closed) throw new TypeError("The fetch session is closed.");
|
|
683
|
+
const target = new URL(input instanceof Request ? input.url : input);
|
|
684
|
+
if (target.origin !== origin) throw new TypeError(`A fetch session for ${origin} cannot fetch ${target.origin}.`);
|
|
685
|
+
const response = await send(input, {
|
|
686
|
+
...init,
|
|
687
|
+
redirect: "manual"
|
|
23
688
|
});
|
|
689
|
+
if (closed) {
|
|
690
|
+
cancelResponseBody$1(response, url);
|
|
691
|
+
throw new TypeError("The fetch session is closed.");
|
|
692
|
+
}
|
|
693
|
+
tracked.push(response);
|
|
694
|
+
return response;
|
|
24
695
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
696
|
+
async function close() {
|
|
697
|
+
if (closed) return;
|
|
698
|
+
closed = true;
|
|
699
|
+
for (const response of tracked) {
|
|
700
|
+
if (response.body == null || response.bodyUsed) continue;
|
|
701
|
+
if (response.body.locked) {
|
|
702
|
+
logger$2.debug("Cannot cancel a locked response body from {url}.", { url: url.href });
|
|
703
|
+
continue;
|
|
704
|
+
}
|
|
705
|
+
cancelResponseBody$1(response, url);
|
|
706
|
+
}
|
|
707
|
+
if (pin == null) return;
|
|
708
|
+
revokePinGrant(pin.grant);
|
|
709
|
+
await pin.pinner.release(pin.grant);
|
|
710
|
+
}
|
|
711
|
+
return {
|
|
712
|
+
get url() {
|
|
713
|
+
return new URL(url);
|
|
714
|
+
},
|
|
715
|
+
fetch: sessionFetch,
|
|
716
|
+
close
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
async function canResolve() {
|
|
720
|
+
if (!("Deno" in globalThis)) return true;
|
|
721
|
+
return (await Deno.permissions.query({ name: "net" })).state === "granted";
|
|
722
|
+
}
|
|
723
|
+
const loggedUnpinned = /* @__PURE__ */ new Set();
|
|
724
|
+
function logUnpinned(url) {
|
|
725
|
+
const runtime = detectRuntime();
|
|
726
|
+
const key = `${runtime} ${url.protocol}`;
|
|
727
|
+
if (loggedUnpinned.has(key)) return;
|
|
728
|
+
loggedUnpinned.add(key);
|
|
729
|
+
logger$2.debug("Outbound {protocol} connections are not pinned to validated addresses on {runtime}; the hostname is resolved again when connecting.", {
|
|
730
|
+
runtime,
|
|
731
|
+
protocol: url.protocol
|
|
732
|
+
});
|
|
733
|
+
}
|
|
32
734
|
//#endregion
|
|
33
735
|
//#region ../../node_modules/.pnpm/@opentelemetry+api@1.9.0/node_modules/@opentelemetry/api/build/esm/platform/node/globalThis.js
|
|
34
736
|
/** only globals that common to node and browsers are allowed */
|
|
@@ -25710,7 +26412,7 @@ const preloadedContexts = {
|
|
|
25710
26412
|
"https://purl.archive.org/miscellany": miscellany_default
|
|
25711
26413
|
};
|
|
25712
26414
|
var name = "@fedify/vocab-runtime";
|
|
25713
|
-
var version = "2.0.35-dev.
|
|
26415
|
+
var version = "2.0.35-dev.2485+75206550";
|
|
25714
26416
|
/**
|
|
25715
26417
|
* Error thrown when fetching a JSON-LD document failed.
|
|
25716
26418
|
*/
|
|
@@ -26024,214 +26726,6 @@ var HttpHeaderLink = class HttpHeaderLink {
|
|
|
26024
26726
|
return new this(...inputs).toString();
|
|
26025
26727
|
}
|
|
26026
26728
|
};
|
|
26027
|
-
var UrlError = class extends Error {
|
|
26028
|
-
/**
|
|
26029
|
-
* The failure category. Defaults to `"disallowed"`.
|
|
26030
|
-
* `"dns"` includes lookup errors and results with no usable IP addresses.
|
|
26031
|
-
* @since 2.0.29
|
|
26032
|
-
*/
|
|
26033
|
-
reason;
|
|
26034
|
-
constructor(message, options) {
|
|
26035
|
-
super(message, options);
|
|
26036
|
-
this.name = "UrlError";
|
|
26037
|
-
this.reason = options?.reason ?? "disallowed";
|
|
26038
|
-
}
|
|
26039
|
-
};
|
|
26040
|
-
/**
|
|
26041
|
-
* Validates a URL to prevent SSRF attacks.
|
|
26042
|
-
*/
|
|
26043
|
-
async function validatePublicUrl(url) {
|
|
26044
|
-
const parsed = new URL(url);
|
|
26045
|
-
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new UrlError(`Unsupported protocol: ${parsed.protocol}`);
|
|
26046
|
-
let hostname = parsed.hostname;
|
|
26047
|
-
if (hostname.startsWith("[") && hostname.endsWith("]")) hostname = hostname.slice(1, -1);
|
|
26048
|
-
if (hostname === "localhost") throw new UrlError("Localhost is not allowed");
|
|
26049
|
-
const hostnameFamily = isIP(hostname);
|
|
26050
|
-
if (hostnameFamily !== 0) {
|
|
26051
|
-
validatePublicIpAddress(hostname, hostnameFamily);
|
|
26052
|
-
return;
|
|
26053
|
-
}
|
|
26054
|
-
if ("Deno" in globalThis && !isIP(hostname)) {
|
|
26055
|
-
if ((await Deno.permissions.query({ name: "net" })).state !== "granted") return;
|
|
26056
|
-
}
|
|
26057
|
-
if ("Bun" in globalThis) {
|
|
26058
|
-
if (hostname === "example.com" || hostname.endsWith(".example.com")) return;
|
|
26059
|
-
else if (hostname === "fedify-test.internal") throw new UrlError("Invalid or private address: fedify-test.internal");
|
|
26060
|
-
}
|
|
26061
|
-
let addresses;
|
|
26062
|
-
try {
|
|
26063
|
-
addresses = await dns.lookup(hostname, { all: true });
|
|
26064
|
-
} catch (error) {
|
|
26065
|
-
throw new UrlError("DNS lookup failed", {
|
|
26066
|
-
cause: error,
|
|
26067
|
-
reason: "dns"
|
|
26068
|
-
});
|
|
26069
|
-
}
|
|
26070
|
-
validateLookupAddresses(addresses);
|
|
26071
|
-
}
|
|
26072
|
-
/**
|
|
26073
|
-
* Validates the IP addresses returned by `node:dns.lookup()`.
|
|
26074
|
-
*
|
|
26075
|
-
* Cloudflare Workers' `node:dns` implementation currently maps every record
|
|
26076
|
-
* in a DNS-over-HTTPS `Answer` array—including CNAME records—to a
|
|
26077
|
-
* `LookupAddress`, even though Node.js specifies that the `address` field must
|
|
26078
|
-
* contain an IPv4 or IPv6 literal. See:
|
|
26079
|
-
* https://github.com/cloudflare/workerd/issues/6886
|
|
26080
|
-
*
|
|
26081
|
-
* Work around that bug by ignoring non-IP entries only when the lookup also
|
|
26082
|
-
* returns at least one actual IP address. This remains fail-closed: a result
|
|
26083
|
-
* containing no IP addresses is rejected, and every returned IP address is
|
|
26084
|
-
* still validated and must be public. This workaround can be revisited once
|
|
26085
|
-
* the Workerd issue is fixed in supported Cloudflare Workers runtimes.
|
|
26086
|
-
*
|
|
26087
|
-
* @internal
|
|
26088
|
-
*/
|
|
26089
|
-
function validateLookupAddresses(addresses) {
|
|
26090
|
-
let ipAddressCount = 0;
|
|
26091
|
-
for (const { address } of addresses) {
|
|
26092
|
-
const family = isIP(address);
|
|
26093
|
-
if (family === 0) continue;
|
|
26094
|
-
ipAddressCount++;
|
|
26095
|
-
validatePublicIpAddress(address, family);
|
|
26096
|
-
}
|
|
26097
|
-
if (ipAddressCount === 0) throw new UrlError("DNS lookup did not return any IP address", { reason: "dns" });
|
|
26098
|
-
}
|
|
26099
|
-
function validatePublicIpAddress(address, family) {
|
|
26100
|
-
if (family === 4 && isValidPublicIPv4Address(address) || family === 6 && isValidPublicIPv6Address(address)) return;
|
|
26101
|
-
throw new UrlError(`Invalid or private address: ${address}`);
|
|
26102
|
-
}
|
|
26103
|
-
function isValidPublicIPv4Address(address) {
|
|
26104
|
-
const parts = parseIPv4Address(address);
|
|
26105
|
-
if (parts == null) return false;
|
|
26106
|
-
const value = ipv4PartsToNumber(parts);
|
|
26107
|
-
return !nonPublicIPv4Prefixes.some(({ base, prefix }) => matchesIPv4Prefix(value, base, prefix));
|
|
26108
|
-
}
|
|
26109
|
-
function isValidPublicIPv6Address(address) {
|
|
26110
|
-
const words = parseIPv6Address(address);
|
|
26111
|
-
if (words == null) return false;
|
|
26112
|
-
if (nonPublicIPv6Prefixes.some(({ words: prefixWords, prefix }) => matchesIPv6Prefix(words, prefixWords, prefix))) return false;
|
|
26113
|
-
for (const { extractIPv4, prefix, words: prefixWords } of ipv6WithIPv4Prefixes) {
|
|
26114
|
-
if (!matchesIPv6Prefix(words, prefixWords, prefix)) continue;
|
|
26115
|
-
const ipv4Address = extractIPv4(words);
|
|
26116
|
-
if (ipv4Address != null && !isValidPublicIPv4Address(ipv4Address)) return false;
|
|
26117
|
-
}
|
|
26118
|
-
return true;
|
|
26119
|
-
}
|
|
26120
|
-
function expandIPv6Address(address) {
|
|
26121
|
-
address = address.toLowerCase();
|
|
26122
|
-
const ipv4Delimiter = address.lastIndexOf(":");
|
|
26123
|
-
if (address.includes(".") && ipv4Delimiter >= 0) {
|
|
26124
|
-
const ipv4Parts = parseIPv4Address(address.substring(ipv4Delimiter + 1));
|
|
26125
|
-
if (ipv4Parts == null) return address;
|
|
26126
|
-
const high = (ipv4Parts[0] << 8) + ipv4Parts[1];
|
|
26127
|
-
const low = (ipv4Parts[2] << 8) + ipv4Parts[3];
|
|
26128
|
-
address = address.substring(0, ipv4Delimiter + 1) + high.toString(16) + ":" + low.toString(16);
|
|
26129
|
-
}
|
|
26130
|
-
if (address === "::") return "0000:0000:0000:0000:0000:0000:0000:0000";
|
|
26131
|
-
if (address.startsWith("::")) address = "0000" + address;
|
|
26132
|
-
if (address.endsWith("::")) address = address + "0000";
|
|
26133
|
-
address = address.replace("::", ":0000".repeat(8 - (address.match(/:/g) || []).length) + ":");
|
|
26134
|
-
return address.split(":").map((part) => part.padStart(4, "0")).join(":");
|
|
26135
|
-
}
|
|
26136
|
-
const nonPublicIPv4Prefixes = [
|
|
26137
|
-
ipv4Prefix("0.0.0.0/8", "RFC 6890"),
|
|
26138
|
-
ipv4Prefix("10.0.0.0/8", "RFC 1918"),
|
|
26139
|
-
ipv4Prefix("100.64.0.0/10", "RFC 6598"),
|
|
26140
|
-
ipv4Prefix("127.0.0.0/8", "RFC 1122"),
|
|
26141
|
-
ipv4Prefix("169.254.0.0/16", "RFC 3927"),
|
|
26142
|
-
ipv4Prefix("172.16.0.0/12", "RFC 1918"),
|
|
26143
|
-
ipv4Prefix("192.0.0.0/24", "RFC 6890"),
|
|
26144
|
-
ipv4Prefix("192.0.2.0/24", "RFC 5737"),
|
|
26145
|
-
ipv4Prefix("192.88.99.0/24", "RFC 7526"),
|
|
26146
|
-
ipv4Prefix("192.168.0.0/16", "RFC 1918"),
|
|
26147
|
-
ipv4Prefix("198.18.0.0/15", "RFC 2544"),
|
|
26148
|
-
ipv4Prefix("198.51.100.0/24", "RFC 5737"),
|
|
26149
|
-
ipv4Prefix("203.0.113.0/24", "RFC 5737"),
|
|
26150
|
-
ipv4Prefix("224.0.0.0/4", "RFC 5771"),
|
|
26151
|
-
ipv4Prefix("240.0.0.0/4", "RFC 1112")
|
|
26152
|
-
];
|
|
26153
|
-
const nonPublicIPv6Prefixes = [
|
|
26154
|
-
ipv6Prefix("::/16", "RFC 4291"),
|
|
26155
|
-
ipv6Prefix("2001::/32", "RFC 4380"),
|
|
26156
|
-
ipv6Prefix("2002::/16", "RFC 3056"),
|
|
26157
|
-
ipv6Prefix("64:ff9b:1::/48", "RFC 8215"),
|
|
26158
|
-
ipv6Prefix("fc00::/7", "RFC 4193"),
|
|
26159
|
-
ipv6Prefix("fe80::/10", "RFC 4291"),
|
|
26160
|
-
ipv6Prefix("ff00::/8", "RFC 4291")
|
|
26161
|
-
];
|
|
26162
|
-
const ipv6WithIPv4Prefixes = [{
|
|
26163
|
-
...ipv6Prefix("64:ff9b::/96", "RFC 6052"),
|
|
26164
|
-
extractIPv4: (words) => ipv4FromWords(words[6], words[7])
|
|
26165
|
-
}];
|
|
26166
|
-
function ipv4Prefix(cidr, rfc) {
|
|
26167
|
-
const [address, prefixText] = cidr.split("/");
|
|
26168
|
-
const prefix = parseInt(prefixText, 10);
|
|
26169
|
-
const parts = parseIPv4Address(address);
|
|
26170
|
-
if (parts == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 32) throw new Error(`Invalid IPv4 prefix: ${cidr}`);
|
|
26171
|
-
return {
|
|
26172
|
-
cidr,
|
|
26173
|
-
base: ipv4PartsToNumber(parts),
|
|
26174
|
-
prefix,
|
|
26175
|
-
rfc
|
|
26176
|
-
};
|
|
26177
|
-
}
|
|
26178
|
-
function ipv6Prefix(cidr, rfc) {
|
|
26179
|
-
const [address, prefixText] = cidr.split("/");
|
|
26180
|
-
const prefix = parseInt(prefixText, 10);
|
|
26181
|
-
const words = parseIPv6Address(address);
|
|
26182
|
-
if (words == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 128) throw new Error(`Invalid IPv6 prefix: ${cidr}`);
|
|
26183
|
-
return {
|
|
26184
|
-
cidr,
|
|
26185
|
-
words,
|
|
26186
|
-
prefix,
|
|
26187
|
-
rfc
|
|
26188
|
-
};
|
|
26189
|
-
}
|
|
26190
|
-
function parseIPv4Address(address) {
|
|
26191
|
-
const parts = address.split(".").map((part) => {
|
|
26192
|
-
if (!/^\d+$/.test(part)) return NaN;
|
|
26193
|
-
return parseInt(part, 10);
|
|
26194
|
-
});
|
|
26195
|
-
if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) return null;
|
|
26196
|
-
return parts;
|
|
26197
|
-
}
|
|
26198
|
-
function parseIPv6Address(address) {
|
|
26199
|
-
const parts = expandIPv6Address(address).split(":");
|
|
26200
|
-
if (parts.length !== 8) return null;
|
|
26201
|
-
const words = parts.map((part) => {
|
|
26202
|
-
if (!/^[0-9a-f]{1,4}$/i.test(part)) return NaN;
|
|
26203
|
-
return parseInt(part, 16);
|
|
26204
|
-
});
|
|
26205
|
-
if (words.some((word) => !Number.isInteger(word) || word < 0 || word > 65535)) return null;
|
|
26206
|
-
return words;
|
|
26207
|
-
}
|
|
26208
|
-
function ipv4PartsToNumber(parts) {
|
|
26209
|
-
return parts[0] * 2 ** 24 + parts[1] * 2 ** 16 + parts[2] * 2 ** 8 + parts[3];
|
|
26210
|
-
}
|
|
26211
|
-
function ipv4FromWords(highWord, lowWord) {
|
|
26212
|
-
return [
|
|
26213
|
-
highWord >> 8,
|
|
26214
|
-
highWord & 255,
|
|
26215
|
-
lowWord >> 8,
|
|
26216
|
-
lowWord & 255
|
|
26217
|
-
].join(".");
|
|
26218
|
-
}
|
|
26219
|
-
function matchesIPv4Prefix(address, prefixBase, prefixLength) {
|
|
26220
|
-
const blockSize = 2 ** (32 - prefixLength);
|
|
26221
|
-
return Math.floor(address / blockSize) === Math.floor(prefixBase / blockSize);
|
|
26222
|
-
}
|
|
26223
|
-
function matchesIPv6Prefix(address, prefixWords, prefixLength) {
|
|
26224
|
-
let remaining = prefixLength;
|
|
26225
|
-
for (let i = 0; i < 8 && remaining > 0; i++) if (remaining >= 16) {
|
|
26226
|
-
if (address[i] !== prefixWords[i]) return false;
|
|
26227
|
-
remaining -= 16;
|
|
26228
|
-
} else {
|
|
26229
|
-
const mask = 65535 << 16 - remaining & 65535;
|
|
26230
|
-
if ((address[i] & mask) !== (prefixWords[i] & mask)) return false;
|
|
26231
|
-
remaining = 0;
|
|
26232
|
-
}
|
|
26233
|
-
return true;
|
|
26234
|
-
}
|
|
26235
26729
|
const logger = getLogger([
|
|
26236
26730
|
"fedify",
|
|
26237
26731
|
"runtime",
|
|
@@ -26239,16 +26733,34 @@ const logger = getLogger([
|
|
|
26239
26733
|
]);
|
|
26240
26734
|
const DEFAULT_MAX_REDIRECTION = 20;
|
|
26241
26735
|
/**
|
|
26242
|
-
*
|
|
26736
|
+
* Starts cancelling the body of a response without waiting for it to settle.
|
|
26737
|
+
* The cancellation of a branch of a tee (e.g., a body cloned by a fetch mock)
|
|
26738
|
+
* settles only after the other branch is cancelled too, per the Streams
|
|
26739
|
+
* standard, so awaiting it could block forever. Failures are only logged.
|
|
26740
|
+
* @param response The response whose body is cancelled.
|
|
26741
|
+
* @param url The URL of the response, for logging.
|
|
26742
|
+
*/
|
|
26743
|
+
function cancelResponseBody(response, url) {
|
|
26744
|
+
response.body?.cancel().catch((error) => {
|
|
26745
|
+
logger.debug("Failed to cancel the response body from {url}: {error}", {
|
|
26746
|
+
url,
|
|
26747
|
+
error
|
|
26748
|
+
});
|
|
26749
|
+
});
|
|
26750
|
+
}
|
|
26751
|
+
/**
|
|
26752
|
+
* Gets a {@link RemoteDocument} from the given response, or the URL of an
|
|
26753
|
+
* alternate document to load instead. When an alternate document is found,
|
|
26754
|
+
* the response body has been either read or its cancellation has been started
|
|
26755
|
+
* (without waiting for it), so that the alternate document can be loaded after
|
|
26756
|
+
* the response's session is closed.
|
|
26243
26757
|
* @param url The URL of the document to load.
|
|
26244
26758
|
* @param response The response to get the document from.
|
|
26245
|
-
* @
|
|
26246
|
-
* @returns The loaded remote document.
|
|
26759
|
+
* @returns The loaded remote document, or the alternate document URL.
|
|
26247
26760
|
* @throws {FetchError} If the response is not OK.
|
|
26248
|
-
* @internal
|
|
26249
26761
|
*/
|
|
26250
|
-
async function
|
|
26251
|
-
const documentUrl =
|
|
26762
|
+
async function parseRemoteDocument(url, response) {
|
|
26763
|
+
const documentUrl = url;
|
|
26252
26764
|
const docUrl = new URL(documentUrl);
|
|
26253
26765
|
if (!response.ok) {
|
|
26254
26766
|
logger.error("Failed to fetch document: {status} {url} {headers}", {
|
|
@@ -26285,7 +26797,8 @@ async function getRemoteDocument(url, response, fetch) {
|
|
|
26285
26797
|
alternateUrl: altUri.href,
|
|
26286
26798
|
url: documentUrl
|
|
26287
26799
|
});
|
|
26288
|
-
|
|
26800
|
+
cancelResponseBody(response, documentUrl);
|
|
26801
|
+
return { alternate: altUri.href };
|
|
26289
26802
|
}
|
|
26290
26803
|
}
|
|
26291
26804
|
}
|
|
@@ -26310,7 +26823,7 @@ async function getRemoteDocument(url, response, fetch) {
|
|
|
26310
26823
|
alternateUrl: attribs.href,
|
|
26311
26824
|
url: documentUrl
|
|
26312
26825
|
});
|
|
26313
|
-
return
|
|
26826
|
+
return { alternate: new URL(attribs.href, docUrl).href };
|
|
26314
26827
|
}
|
|
26315
26828
|
}
|
|
26316
26829
|
document = JSON.parse(html);
|
|
@@ -26320,11 +26833,11 @@ async function getRemoteDocument(url, response, fetch) {
|
|
|
26320
26833
|
url: documentUrl,
|
|
26321
26834
|
headers: Object.fromEntries(response.headers.entries())
|
|
26322
26835
|
});
|
|
26323
|
-
return {
|
|
26836
|
+
return { document: {
|
|
26324
26837
|
contextUrl,
|
|
26325
26838
|
document,
|
|
26326
26839
|
documentUrl
|
|
26327
|
-
};
|
|
26840
|
+
} };
|
|
26328
26841
|
}
|
|
26329
26842
|
/**
|
|
26330
26843
|
* Creates a JSON-LD document loader that utilizes the browser's `fetch` API.
|
|
@@ -26350,8 +26863,53 @@ async function getRemoteDocument(url, response, fetch) {
|
|
|
26350
26863
|
* @returns The document loader.
|
|
26351
26864
|
* @since 1.3.0
|
|
26352
26865
|
*/
|
|
26353
|
-
function getDocumentLoader(
|
|
26866
|
+
function getDocumentLoader(options = {}) {
|
|
26867
|
+
return createDocumentLoader(options);
|
|
26868
|
+
}
|
|
26869
|
+
/**
|
|
26870
|
+
* The implementation of {@link getDocumentLoader}, with injectable
|
|
26871
|
+
* dependencies. This is not exported from any entry point.
|
|
26872
|
+
* @param options Options for the document loader.
|
|
26873
|
+
* @param deps The dependencies.
|
|
26874
|
+
* @returns The document loader.
|
|
26875
|
+
*/
|
|
26876
|
+
function createDocumentLoader({ allowPrivateAddress, skipPreloadedContexts, userAgent } = {}, deps = {}) {
|
|
26354
26877
|
const tracer = trace.getTracerProvider().getTracer(name, version);
|
|
26878
|
+
async function openHop(url, signal) {
|
|
26879
|
+
try {
|
|
26880
|
+
if (deps.openHop != null) return await deps.openHop(url);
|
|
26881
|
+
return allowPrivateAddress ? await openDirectFetch(url) : await openPublicFetch(url, { signal });
|
|
26882
|
+
} catch (error) {
|
|
26883
|
+
if (error instanceof UrlError) if (error.reason === "dns") logger.debug("DNS lookup failed for {url}", {
|
|
26884
|
+
url,
|
|
26885
|
+
error
|
|
26886
|
+
});
|
|
26887
|
+
else logger.error("Disallowed private URL: {url}", {
|
|
26888
|
+
url,
|
|
26889
|
+
error
|
|
26890
|
+
});
|
|
26891
|
+
throw error;
|
|
26892
|
+
}
|
|
26893
|
+
}
|
|
26894
|
+
async function fetchHop(session, url, span, options) {
|
|
26895
|
+
try {
|
|
26896
|
+
const request = createActivityPubRequest(url, { userAgent });
|
|
26897
|
+
logRequest(logger, request);
|
|
26898
|
+
const response = await session.fetch(request, {
|
|
26899
|
+
redirect: "manual",
|
|
26900
|
+
signal: options?.signal
|
|
26901
|
+
});
|
|
26902
|
+
span.setAttribute("http.response.status_code", response.status);
|
|
26903
|
+
if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
|
|
26904
|
+
const location = response.headers.get("Location");
|
|
26905
|
+
cancelResponseBody(response, url);
|
|
26906
|
+
return { location };
|
|
26907
|
+
}
|
|
26908
|
+
return await parseRemoteDocument(url, response);
|
|
26909
|
+
} finally {
|
|
26910
|
+
await session.close();
|
|
26911
|
+
}
|
|
26912
|
+
}
|
|
26355
26913
|
async function load(url, options, redirected = 0, visited = /* @__PURE__ */ new Set()) {
|
|
26356
26914
|
options?.signal?.throwIfAborted();
|
|
26357
26915
|
const parsedUrl = new URL(url);
|
|
@@ -26366,33 +26924,15 @@ function getDocumentLoader({ allowPrivateAddress, skipPreloadedContexts, userAge
|
|
|
26366
26924
|
documentUrl: currentUrl
|
|
26367
26925
|
};
|
|
26368
26926
|
}
|
|
26369
|
-
|
|
26370
|
-
await validatePublicUrl(currentUrl);
|
|
26371
|
-
} catch (error) {
|
|
26372
|
-
if (error instanceof UrlError) if (error.reason === "dns") logger.debug("DNS lookup failed for {url}", {
|
|
26373
|
-
url: currentUrl,
|
|
26374
|
-
error
|
|
26375
|
-
});
|
|
26376
|
-
else logger.error("Disallowed private URL: {url}", {
|
|
26377
|
-
url: currentUrl,
|
|
26378
|
-
error
|
|
26379
|
-
});
|
|
26380
|
-
throw error;
|
|
26381
|
-
}
|
|
26927
|
+
const session = await openHop(currentUrl, options?.signal);
|
|
26382
26928
|
visited.add(currentUrl);
|
|
26383
26929
|
return await tracer.startActiveSpan("activitypub.fetch_document", {
|
|
26384
26930
|
kind: SpanKind.CLIENT,
|
|
26385
26931
|
attributes: { "url.full": currentUrl }
|
|
26386
26932
|
}, async (span) => {
|
|
26387
26933
|
try {
|
|
26388
|
-
const
|
|
26389
|
-
|
|
26390
|
-
const response = await fetch(request, {
|
|
26391
|
-
redirect: "manual",
|
|
26392
|
-
signal: options?.signal
|
|
26393
|
-
});
|
|
26394
|
-
span.setAttribute("http.response.status_code", response.status);
|
|
26395
|
-
if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
|
|
26934
|
+
const outcome = await fetchHop(session, currentUrl, span, options);
|
|
26935
|
+
if ("location" in outcome) {
|
|
26396
26936
|
if (redirected >= DEFAULT_MAX_REDIRECTION) {
|
|
26397
26937
|
logger.error("Too many redirections ({redirections}) while fetching document.", {
|
|
26398
26938
|
redirections: redirected + 1,
|
|
@@ -26400,7 +26940,7 @@ function getDocumentLoader({ allowPrivateAddress, skipPreloadedContexts, userAge
|
|
|
26400
26940
|
});
|
|
26401
26941
|
throw new FetchError(currentUrl, `Too many redirections (${redirected + 1})`);
|
|
26402
26942
|
}
|
|
26403
|
-
const redirectUrl = new URL(
|
|
26943
|
+
const redirectUrl = new URL(outcome.location, currentUrl).href;
|
|
26404
26944
|
span.setAttribute("http.redirect.url", redirectUrl);
|
|
26405
26945
|
if (visited.has(redirectUrl)) {
|
|
26406
26946
|
logger.error("Detected a redirect loop while fetching document: {url} -> {redirectUrl}", {
|
|
@@ -26411,12 +26951,14 @@ function getDocumentLoader({ allowPrivateAddress, skipPreloadedContexts, userAge
|
|
|
26411
26951
|
}
|
|
26412
26952
|
return await load(redirectUrl, options, redirected + 1, visited);
|
|
26413
26953
|
}
|
|
26414
|
-
|
|
26954
|
+
let result;
|
|
26955
|
+
if ("alternate" in outcome) {
|
|
26956
|
+
const alternateUrl = outcome.alternate;
|
|
26415
26957
|
options?.signal?.throwIfAborted();
|
|
26416
26958
|
if (redirected >= DEFAULT_MAX_REDIRECTION) throw new FetchError(currentUrl, `Too many redirections (${redirected + 1})`);
|
|
26417
26959
|
if (visited.has(alternateUrl)) throw new FetchError(currentUrl, `Redirect loop detected: ${alternateUrl}`);
|
|
26418
|
-
|
|
26419
|
-
}
|
|
26960
|
+
result = await load(alternateUrl, options, redirected + 1, visited);
|
|
26961
|
+
} else result = outcome.document;
|
|
26420
26962
|
span.setAttribute("docloader.document_url", result.documentUrl);
|
|
26421
26963
|
if (result.contextUrl != null) span.setAttribute("docloader.context_url", result.contextUrl);
|
|
26422
26964
|
return result;
|