@fedify/fedify 2.1.31 → 2.1.32
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/attachment-arrays-DZ4JHJD8.mjs +111 -0
- package/dist/{builder-isMRh_I6.mjs → builder--sc-rozj.mjs} +3 -3
- package/dist/callback-BARPujV7.d.mts +2388 -0
- package/dist/{client-BIBzqEIp.d.cts → client-9DPcy_Yx.d.ts} +16 -0
- package/dist/{client-jE7oGvRh.d.ts → client-B3ruDgWg.d.cts} +16 -0
- package/dist/{client-BZr7TGyu.mjs → client-cpPaW7Ww.mjs} +86 -11
- package/dist/compat/attachment-arrays.test.d.mts +2 -0
- package/dist/compat/attachment-arrays.test.mjs +254 -0
- package/dist/compat/mod.d.cts +1 -1
- package/dist/compat/mod.d.ts +1 -1
- package/dist/compat/transformers.test.mjs +1 -1
- package/dist/{context-C0uGVzgk.d.ts → context-DE2YrQwM.d.ts} +30 -13
- package/dist/{context-CLaLxi49.d.cts → context-y6ZUQCMf.d.cts} +30 -13
- package/dist/{deno-CiaJqTcu.mjs → deno-L23b3Hba.mjs} +1 -1
- package/dist/{docloader-DOrwedT7.mjs → docloader-DUXr9k_O.mjs} +2 -2
- package/dist/federation/attachment-responses.test.d.mts +2 -0
- package/dist/federation/attachment-responses.test.mjs +182 -0
- package/dist/federation/builder.test.mjs +1 -1
- package/dist/federation/collection.test.mjs +1 -1
- package/dist/federation/delivery-attachments.test.d.mts +2 -0
- package/dist/federation/delivery-attachments.test.mjs +280 -0
- package/dist/federation/handler.test.mjs +208 -16
- package/dist/federation/idempotency.test.mjs +2 -2
- package/dist/federation/inbox.test.mjs +1 -1
- package/dist/federation/middleware.test.mjs +1185 -8
- package/dist/federation/mod.cjs +1 -1
- package/dist/federation/mod.d.cts +2 -2
- package/dist/federation/mod.d.ts +2 -2
- package/dist/federation/mod.js +1 -1
- package/dist/federation/negotiation.test.mjs +1 -1
- package/dist/federation/retry.test.mjs +1 -1
- package/dist/federation/send.test.mjs +283 -3
- package/dist/federation/temporal.test.mjs +25 -1
- package/dist/federation/webfinger.test.mjs +1 -1
- package/dist/{http-Crml3gei.js → http-BtbMAgTA.js} +6 -4
- package/dist/{http-9IFUxdkp.d.cts → http-C8qLHs_Z.d.cts} +3 -3
- package/dist/{http-CRiVlZd5.cjs → http-D5woFKA_.cjs} +6 -4
- package/dist/{http-SbfezERn.d.ts → http-D8WmcRAF.d.ts} +3 -3
- package/dist/{http-A9mP2uM7.mjs → http-HX6wVyJh.mjs} +6 -4
- package/dist/{inbox-KESLudq4.mjs → inbox-CL7A0HXT.mjs} +1 -1
- package/dist/{key-Dg6-DV_O.mjs → key-Dai3f5yI.mjs} +3 -2
- package/dist/{kv-cache-Cqmm-yQa.cjs → kv-cache-B0V_e231.cjs} +1 -1
- package/dist/{kv-cache-DQcyZL5T.js → kv-cache-BsDg-cT8.js} +1 -1
- package/dist/{ld-COjDN6Nb.mjs → ld-DN6x1iZ8.mjs} +153 -21
- package/dist/ld-signature-DoZcbsZR.mjs +23 -0
- package/dist/{middleware-BtDQUCcz.cjs → middleware-Cl6k5Hrm.cjs} +1 -1
- package/dist/{middleware-BbgR7xOI.cjs → middleware-CrfRuKCf.cjs} +561 -101
- package/dist/{middleware-COFlKeYn.mjs → middleware-DiquMgtt.mjs} +378 -89
- package/dist/{middleware-DIW5S_5L.js → middleware-DunNtbo9.js} +560 -101
- package/dist/{middleware-CuQvczZd.mjs → middleware-SnQS8eIb.mjs} +1 -1
- package/dist/{mod-LOEOKbYd.d.cts → mod-CehHJOV4.d.cts} +30 -13
- package/dist/{mod-CFRocFeQ.d.ts → mod-DLJpnqdw.d.ts} +1 -1
- package/dist/{mod-C7w-QVUv.d.ts → mod-Mh3oIuRK.d.ts} +30 -13
- package/dist/{mod-BRnwh2oy.d.ts → mod-REfljt0T.d.ts} +3 -3
- package/dist/{mod-Co7LEK9X.d.cts → mod-UvIGUHqW.d.cts} +3 -3
- package/dist/{mod-CGUH8Ujp.d.cts → mod-sTa1Zzxk.d.cts} +1 -1
- package/dist/mod.cjs +5 -5
- package/dist/mod.d.cts +8 -8
- package/dist/mod.d.ts +8 -8
- package/dist/mod.js +5 -5
- package/dist/nodeinfo/client.test.mjs +432 -5
- package/dist/nodeinfo/handler.test.mjs +1 -1
- package/dist/nodeinfo/mod.cjs +1 -1
- package/dist/nodeinfo/mod.d.cts +1 -1
- package/dist/nodeinfo/mod.d.ts +1 -1
- package/dist/nodeinfo/mod.js +1 -1
- package/dist/nodeinfo/types.test.mjs +1 -1
- package/dist/{owner-CFHq61cq.mjs → owner-DwyDorZI.mjs} +2 -2
- package/dist/{proof-0JkSC_pT.cjs → proof-Be-4K4jn.cjs} +193 -46
- package/dist/{proof-KSHlbPCG.mjs → proof-DYjmyrHh.mjs} +38 -13
- package/dist/{proof-DzksJFK7.js → proof-Du4-5SHY.js} +187 -46
- package/dist/{send-DaDe2YCb.mjs → send-BGSfQkIK.mjs} +92 -21
- package/dist/sig/http.test.mjs +2 -2
- package/dist/sig/key-types.test.d.mts +218 -0
- package/dist/sig/key-types.test.mjs +8 -0
- package/dist/sig/key.test.mjs +2 -2
- package/dist/sig/ld.test.mjs +573 -8
- package/dist/sig/mod.cjs +2 -2
- package/dist/sig/mod.d.cts +3 -3
- package/dist/sig/mod.d.ts +3 -3
- package/dist/sig/mod.js +2 -2
- package/dist/sig/owner.test.mjs +1 -1
- package/dist/sig/proof.test.mjs +95 -3
- package/dist/{temporal-DNGH8o2E.mjs → temporal-CNCchWly.mjs} +3 -5
- package/dist/testing/mod.d.mts +2 -2203
- package/dist/{types-DOvyymM4.cjs → types-B5wRhemi.cjs} +85 -10
- package/dist/{types-B1_pRFE_.js → types-CXy2lRVU.js} +86 -11
- package/dist/utils/docloader.test.mjs +2 -2
- package/dist/utils/mod.cjs +1 -1
- package/dist/utils/mod.d.cts +1 -1
- package/dist/utils/mod.d.ts +1 -1
- package/dist/utils/mod.js +1 -1
- package/package.json +6 -6
- /package/dist/{collection-BD6-SZ6O.mjs → collection-DnoYNwBC.mjs} +0 -0
- /package/dist/{negotiation-BehA2uul.mjs → negotiation-xb0QR3u_.mjs} +0 -0
- /package/dist/{retry-B_E3V_Dx.mjs → retry-CI1flGDD.mjs} +0 -0
- /package/dist/{types-DCP0WLdt.mjs → types-s4mCTGkT.mjs} +0 -0
|
@@ -25,13 +25,18 @@ function validateBodySizeLimit(maxBytes) {
|
|
|
25
25
|
* @param message The response or request to read.
|
|
26
26
|
* @param maxBytes The maximum number of decoded bytes to read.
|
|
27
27
|
* @param url The URL to include in errors and logs.
|
|
28
|
+
* @param signal An optional signal for cancelling the body read.
|
|
28
29
|
* @returns The decoded body text.
|
|
29
30
|
* @throws {BodyTooLargeError} If the body exceeds the limit.
|
|
30
31
|
* @internal
|
|
31
32
|
*/
|
|
32
|
-
async function readBoundedText(message, maxBytes, url) {
|
|
33
|
+
async function readBoundedText(message, maxBytes, url, signal) {
|
|
33
34
|
validateBodySizeLimit(maxBytes);
|
|
34
35
|
const reader = message.body?.getReader();
|
|
36
|
+
const onAbort = () => {
|
|
37
|
+
if (reader != null) reader.cancel(signal?.reason).catch(() => {});
|
|
38
|
+
};
|
|
39
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
35
40
|
const tooLarge = () => {
|
|
36
41
|
(0, _logtape_logtape.getLogger)([
|
|
37
42
|
"fedify",
|
|
@@ -44,6 +49,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
44
49
|
throw new BodyTooLargeError(url, maxBytes);
|
|
45
50
|
};
|
|
46
51
|
try {
|
|
52
|
+
signal?.throwIfAborted();
|
|
47
53
|
const length = message.headers.get("Content-Length");
|
|
48
54
|
const encoding = message.headers.get("Content-Encoding");
|
|
49
55
|
if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
|
|
@@ -51,6 +57,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
51
57
|
let size = 0;
|
|
52
58
|
const textReader = new ReadableStream({ async pull(controller) {
|
|
53
59
|
const { done, value } = await reader.read();
|
|
60
|
+
signal?.throwIfAborted();
|
|
54
61
|
if (done) {
|
|
55
62
|
controller.close();
|
|
56
63
|
return;
|
|
@@ -63,6 +70,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
63
70
|
const chunks = [];
|
|
64
71
|
while (true) {
|
|
65
72
|
const { done, value } = await textReader.read();
|
|
73
|
+
signal?.throwIfAborted();
|
|
66
74
|
if (done) break;
|
|
67
75
|
chunks.push(value);
|
|
68
76
|
}
|
|
@@ -74,6 +82,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
74
82
|
if (reader != null) reader.cancel(error).catch(() => {});
|
|
75
83
|
throw error;
|
|
76
84
|
} finally {
|
|
85
|
+
signal?.removeEventListener("abort", onAbort);
|
|
77
86
|
reader?.releaseLock();
|
|
78
87
|
}
|
|
79
88
|
}
|
|
@@ -85,6 +94,8 @@ const logger = (0, _logtape_logtape.getLogger)([
|
|
|
85
94
|
"client"
|
|
86
95
|
]);
|
|
87
96
|
const DEFAULT_MAX_REDIRECTION = 5;
|
|
97
|
+
const DEFAULT_TIMEOUT = 1e4;
|
|
98
|
+
const MAX_TIMEOUT = 2147483647;
|
|
88
99
|
/**
|
|
89
100
|
* Fetches a URL while guarding against SSRF. Every request—including each
|
|
90
101
|
* redirect hop—is made in its own session opened with `openPublicFetch()`,
|
|
@@ -101,20 +112,27 @@ const DEFAULT_MAX_REDIRECTION = 5;
|
|
|
101
112
|
* @param headers The request headers.
|
|
102
113
|
* @param consume The function to consume the final response with.
|
|
103
114
|
* @param deps The dependencies.
|
|
115
|
+
* @param signal The signal for the whole NodeInfo lookup.
|
|
104
116
|
* @returns The value returned by `consume`, or `null` if the URL is
|
|
105
117
|
* disallowed, its hostname cannot be resolved, or there are too many
|
|
106
118
|
* redirects.
|
|
107
119
|
* @internal
|
|
108
120
|
*/
|
|
109
|
-
async function fetchPublicUrl(url, headers, consume, deps = {}) {
|
|
110
|
-
const openHop = deps.openHop ?? ((hopUrl) => (0, _fedify_vocab_runtime_internal.openPublicFetch)(hopUrl))
|
|
121
|
+
async function fetchPublicUrl(url, headers, consume, deps = {}, signal) {
|
|
122
|
+
const openHop = deps.openHop ?? ((hopUrl, signal) => (0, _fedify_vocab_runtime_internal.openPublicFetch)(hopUrl, (0, _fedify_vocab_runtime_internal.detectRuntime)() === "node" ? {
|
|
123
|
+
signal,
|
|
124
|
+
lookup: (hostname) => (0, _fedify_vocab_runtime_internal.lookupAllAddressesBounded)(hostname, signal)
|
|
125
|
+
} : { signal }));
|
|
111
126
|
let current = url instanceof URL ? url : new URL(url);
|
|
112
127
|
let redirected = 0;
|
|
128
|
+
const visited = new Set([redirectKey(current)]);
|
|
113
129
|
while (true) {
|
|
130
|
+
signal?.throwIfAborted();
|
|
114
131
|
let session;
|
|
115
132
|
try {
|
|
116
|
-
session = await openHop(current.href);
|
|
133
|
+
session = await openHop(current.href, signal);
|
|
117
134
|
} catch (e) {
|
|
135
|
+
signal?.throwIfAborted();
|
|
118
136
|
if (e instanceof _fedify_vocab_runtime.UrlError) {
|
|
119
137
|
if (e.reason === "dns") logger.error("DNS lookup failed for {url}: {error}", {
|
|
120
138
|
url: current.href,
|
|
@@ -130,10 +148,16 @@ async function fetchPublicUrl(url, headers, consume, deps = {}) {
|
|
|
130
148
|
}
|
|
131
149
|
let location;
|
|
132
150
|
try {
|
|
151
|
+
signal?.throwIfAborted();
|
|
133
152
|
const response = await session.fetch(current, {
|
|
134
153
|
headers,
|
|
135
|
-
redirect: "manual"
|
|
154
|
+
redirect: "manual",
|
|
155
|
+
signal
|
|
136
156
|
});
|
|
157
|
+
if (signal?.aborted) {
|
|
158
|
+
cancelBody(response, current);
|
|
159
|
+
signal.throwIfAborted();
|
|
160
|
+
}
|
|
137
161
|
if (response.status < 300 || response.status >= 400 || !response.headers.has("Location")) return await consume(response);
|
|
138
162
|
location = response.headers.get("Location");
|
|
139
163
|
cancelBody(response, current);
|
|
@@ -156,9 +180,20 @@ async function fetchPublicUrl(url, headers, consume, deps = {}) {
|
|
|
156
180
|
});
|
|
157
181
|
return null;
|
|
158
182
|
}
|
|
183
|
+
const key = redirectKey(next);
|
|
184
|
+
if (visited.has(key)) {
|
|
185
|
+
logger.error("Redirect cycle while fetching NodeInfo: {url}.", { url: next.href });
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
visited.add(key);
|
|
159
189
|
current = next;
|
|
160
190
|
}
|
|
161
191
|
}
|
|
192
|
+
function redirectKey(url) {
|
|
193
|
+
const withoutFragment = new URL(url);
|
|
194
|
+
withoutFragment.hash = "";
|
|
195
|
+
return withoutFragment.href;
|
|
196
|
+
}
|
|
162
197
|
/**
|
|
163
198
|
* Starts cancelling a response body that is not read, without waiting for it
|
|
164
199
|
* to settle. The cancellation of a branch of a tee (e.g., a body cloned by
|
|
@@ -189,6 +224,46 @@ async function getNodeInfo(url, options = {}) {
|
|
|
189
224
|
* @internal
|
|
190
225
|
*/
|
|
191
226
|
async function getNodeInfoInternal(url, options = {}, deps = {}) {
|
|
227
|
+
const timeout = resolveTimeout(options.timeout);
|
|
228
|
+
if (timeout == null && options.signal == null) return await getNodeInfoWithSignal(url, options, deps);
|
|
229
|
+
const controller = new AbortController();
|
|
230
|
+
const timeoutReason = timeout == null ? void 0 : new DOMException(`The NodeInfo lookup timed out after ${timeout} ms.`, "TimeoutError");
|
|
231
|
+
const timer = timeout == null ? void 0 : setTimeout(() => controller.abort(timeoutReason), timeout);
|
|
232
|
+
const onCallerAbort = () => controller.abort(options.signal?.reason);
|
|
233
|
+
options.signal?.addEventListener("abort", onCallerAbort, { once: true });
|
|
234
|
+
if (options.signal?.aborted) onCallerAbort();
|
|
235
|
+
let onAbort;
|
|
236
|
+
const aborted = new Promise((_, reject) => {
|
|
237
|
+
onAbort = () => reject(controller.signal.reason);
|
|
238
|
+
controller.signal.addEventListener("abort", onAbort, { once: true });
|
|
239
|
+
});
|
|
240
|
+
const loading = getNodeInfoWithSignal(url, options, deps, controller.signal);
|
|
241
|
+
loading.catch(() => {});
|
|
242
|
+
try {
|
|
243
|
+
return await Promise.race([loading, aborted]);
|
|
244
|
+
} catch (error) {
|
|
245
|
+
if (!controller.signal.aborted) throw error;
|
|
246
|
+
if (controller.signal.reason === timeoutReason) logger.error("Timed out after {timeout} ms while fetching NodeInfo: {url}", {
|
|
247
|
+
timeout,
|
|
248
|
+
url: url.toString()
|
|
249
|
+
});
|
|
250
|
+
else logger.debug("Cancelled NodeInfo lookup for {url}.", { url: url.toString() });
|
|
251
|
+
return;
|
|
252
|
+
} finally {
|
|
253
|
+
clearTimeout(timer);
|
|
254
|
+
options.signal?.removeEventListener("abort", onCallerAbort);
|
|
255
|
+
if (onAbort != null) controller.signal.removeEventListener("abort", onAbort);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function resolveTimeout(timeout) {
|
|
259
|
+
if (timeout === void 0) return DEFAULT_TIMEOUT;
|
|
260
|
+
if (timeout === null) return null;
|
|
261
|
+
if (!Number.isFinite(timeout) || timeout <= 0) throw new RangeError(`The NodeInfo timeout must be a positive finite number of milliseconds, but got ${String(timeout)}.`);
|
|
262
|
+
const ms = Math.ceil(timeout);
|
|
263
|
+
if (ms > MAX_TIMEOUT) throw new RangeError(`The NodeInfo timeout must not be greater than ${MAX_TIMEOUT} milliseconds, but got ${timeout}.`);
|
|
264
|
+
return ms;
|
|
265
|
+
}
|
|
266
|
+
async function getNodeInfoWithSignal(url, options, deps, signal) {
|
|
192
267
|
try {
|
|
193
268
|
const headers = {
|
|
194
269
|
Accept: "application/json",
|
|
@@ -207,8 +282,8 @@ async function getNodeInfoInternal(url, options = {}, deps = {}) {
|
|
|
207
282
|
cancelBody(response, wellKnownUrl);
|
|
208
283
|
return;
|
|
209
284
|
}
|
|
210
|
-
return { rd: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, wellKnownUrl)) };
|
|
211
|
-
}, deps);
|
|
285
|
+
return { rd: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, wellKnownUrl, signal)) };
|
|
286
|
+
}, deps, signal);
|
|
212
287
|
if (wellKnown == null) return void 0;
|
|
213
288
|
const wellKnownRd = wellKnown.rd;
|
|
214
289
|
const link = wellKnownRd?.links?.find((link) => link != null && "rel" in link && (link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.0" || link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.1") && "href" in link && link.href != null);
|
|
@@ -231,14 +306,14 @@ async function getNodeInfoInternal(url, options = {}, deps = {}) {
|
|
|
231
306
|
cancelBody(response, nodeInfoUrl);
|
|
232
307
|
return;
|
|
233
308
|
}
|
|
234
|
-
return { data: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl)) };
|
|
235
|
-
}, deps);
|
|
309
|
+
return { data: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl, signal)) };
|
|
310
|
+
}, deps, signal);
|
|
236
311
|
if (document == null) return void 0;
|
|
237
312
|
const { data } = document;
|
|
238
313
|
if (options.parse === "none") return data;
|
|
239
314
|
return parseNodeInfo(data, { tryBestEffort: options.parse === "best-effort" }) ?? void 0;
|
|
240
315
|
} catch (error) {
|
|
241
|
-
logger
|
|
316
|
+
logger[signal?.aborted ? "debug" : "error"]("Failed to fetch NodeInfo document from {url}: {error}", {
|
|
242
317
|
url: url.toString(),
|
|
243
318
|
error
|
|
244
319
|
});
|
|
@@ -2,7 +2,7 @@ import { Temporal } from "@js-temporal/polyfill";
|
|
|
2
2
|
import { URLPattern } from "urlpattern-polyfill";
|
|
3
3
|
import { getLogger } from "@logtape/logtape";
|
|
4
4
|
import { FetchError, UrlError, getUserAgent } from "@fedify/vocab-runtime";
|
|
5
|
-
import { openPublicFetch } from "@fedify/vocab-runtime/internal";
|
|
5
|
+
import { detectRuntime, lookupAllAddressesBounded, openPublicFetch } from "@fedify/vocab-runtime/internal";
|
|
6
6
|
//#region src/utils/body.ts
|
|
7
7
|
/** Decoded JSON body limit: 16 MiB. @internal */
|
|
8
8
|
const MAX_BODY_SIZE = 16 * 1024 * 1024;
|
|
@@ -24,13 +24,18 @@ function validateBodySizeLimit(maxBytes) {
|
|
|
24
24
|
* @param message The response or request to read.
|
|
25
25
|
* @param maxBytes The maximum number of decoded bytes to read.
|
|
26
26
|
* @param url The URL to include in errors and logs.
|
|
27
|
+
* @param signal An optional signal for cancelling the body read.
|
|
27
28
|
* @returns The decoded body text.
|
|
28
29
|
* @throws {BodyTooLargeError} If the body exceeds the limit.
|
|
29
30
|
* @internal
|
|
30
31
|
*/
|
|
31
|
-
async function readBoundedText(message, maxBytes, url) {
|
|
32
|
+
async function readBoundedText(message, maxBytes, url, signal) {
|
|
32
33
|
validateBodySizeLimit(maxBytes);
|
|
33
34
|
const reader = message.body?.getReader();
|
|
35
|
+
const onAbort = () => {
|
|
36
|
+
if (reader != null) reader.cancel(signal?.reason).catch(() => {});
|
|
37
|
+
};
|
|
38
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
34
39
|
const tooLarge = () => {
|
|
35
40
|
getLogger([
|
|
36
41
|
"fedify",
|
|
@@ -43,6 +48,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
43
48
|
throw new BodyTooLargeError(url, maxBytes);
|
|
44
49
|
};
|
|
45
50
|
try {
|
|
51
|
+
signal?.throwIfAborted();
|
|
46
52
|
const length = message.headers.get("Content-Length");
|
|
47
53
|
const encoding = message.headers.get("Content-Encoding");
|
|
48
54
|
if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
|
|
@@ -50,6 +56,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
50
56
|
let size = 0;
|
|
51
57
|
const textReader = new ReadableStream({ async pull(controller) {
|
|
52
58
|
const { done, value } = await reader.read();
|
|
59
|
+
signal?.throwIfAborted();
|
|
53
60
|
if (done) {
|
|
54
61
|
controller.close();
|
|
55
62
|
return;
|
|
@@ -62,6 +69,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
62
69
|
const chunks = [];
|
|
63
70
|
while (true) {
|
|
64
71
|
const { done, value } = await textReader.read();
|
|
72
|
+
signal?.throwIfAborted();
|
|
65
73
|
if (done) break;
|
|
66
74
|
chunks.push(value);
|
|
67
75
|
}
|
|
@@ -73,6 +81,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
73
81
|
if (reader != null) reader.cancel(error).catch(() => {});
|
|
74
82
|
throw error;
|
|
75
83
|
} finally {
|
|
84
|
+
signal?.removeEventListener("abort", onAbort);
|
|
76
85
|
reader?.releaseLock();
|
|
77
86
|
}
|
|
78
87
|
}
|
|
@@ -84,6 +93,8 @@ const logger = getLogger([
|
|
|
84
93
|
"client"
|
|
85
94
|
]);
|
|
86
95
|
const DEFAULT_MAX_REDIRECTION = 5;
|
|
96
|
+
const DEFAULT_TIMEOUT = 1e4;
|
|
97
|
+
const MAX_TIMEOUT = 2147483647;
|
|
87
98
|
/**
|
|
88
99
|
* Fetches a URL while guarding against SSRF. Every request—including each
|
|
89
100
|
* redirect hop—is made in its own session opened with `openPublicFetch()`,
|
|
@@ -100,20 +111,27 @@ const DEFAULT_MAX_REDIRECTION = 5;
|
|
|
100
111
|
* @param headers The request headers.
|
|
101
112
|
* @param consume The function to consume the final response with.
|
|
102
113
|
* @param deps The dependencies.
|
|
114
|
+
* @param signal The signal for the whole NodeInfo lookup.
|
|
103
115
|
* @returns The value returned by `consume`, or `null` if the URL is
|
|
104
116
|
* disallowed, its hostname cannot be resolved, or there are too many
|
|
105
117
|
* redirects.
|
|
106
118
|
* @internal
|
|
107
119
|
*/
|
|
108
|
-
async function fetchPublicUrl(url, headers, consume, deps = {}) {
|
|
109
|
-
const openHop = deps.openHop ?? ((hopUrl) => openPublicFetch(hopUrl)
|
|
120
|
+
async function fetchPublicUrl(url, headers, consume, deps = {}, signal) {
|
|
121
|
+
const openHop = deps.openHop ?? ((hopUrl, signal) => openPublicFetch(hopUrl, detectRuntime() === "node" ? {
|
|
122
|
+
signal,
|
|
123
|
+
lookup: (hostname) => lookupAllAddressesBounded(hostname, signal)
|
|
124
|
+
} : { signal }));
|
|
110
125
|
let current = url instanceof URL ? url : new URL(url);
|
|
111
126
|
let redirected = 0;
|
|
127
|
+
const visited = new Set([redirectKey(current)]);
|
|
112
128
|
while (true) {
|
|
129
|
+
signal?.throwIfAborted();
|
|
113
130
|
let session;
|
|
114
131
|
try {
|
|
115
|
-
session = await openHop(current.href);
|
|
132
|
+
session = await openHop(current.href, signal);
|
|
116
133
|
} catch (e) {
|
|
134
|
+
signal?.throwIfAborted();
|
|
117
135
|
if (e instanceof UrlError) {
|
|
118
136
|
if (e.reason === "dns") logger.error("DNS lookup failed for {url}: {error}", {
|
|
119
137
|
url: current.href,
|
|
@@ -129,10 +147,16 @@ async function fetchPublicUrl(url, headers, consume, deps = {}) {
|
|
|
129
147
|
}
|
|
130
148
|
let location;
|
|
131
149
|
try {
|
|
150
|
+
signal?.throwIfAborted();
|
|
132
151
|
const response = await session.fetch(current, {
|
|
133
152
|
headers,
|
|
134
|
-
redirect: "manual"
|
|
153
|
+
redirect: "manual",
|
|
154
|
+
signal
|
|
135
155
|
});
|
|
156
|
+
if (signal?.aborted) {
|
|
157
|
+
cancelBody(response, current);
|
|
158
|
+
signal.throwIfAborted();
|
|
159
|
+
}
|
|
136
160
|
if (response.status < 300 || response.status >= 400 || !response.headers.has("Location")) return await consume(response);
|
|
137
161
|
location = response.headers.get("Location");
|
|
138
162
|
cancelBody(response, current);
|
|
@@ -155,9 +179,20 @@ async function fetchPublicUrl(url, headers, consume, deps = {}) {
|
|
|
155
179
|
});
|
|
156
180
|
return null;
|
|
157
181
|
}
|
|
182
|
+
const key = redirectKey(next);
|
|
183
|
+
if (visited.has(key)) {
|
|
184
|
+
logger.error("Redirect cycle while fetching NodeInfo: {url}.", { url: next.href });
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
visited.add(key);
|
|
158
188
|
current = next;
|
|
159
189
|
}
|
|
160
190
|
}
|
|
191
|
+
function redirectKey(url) {
|
|
192
|
+
const withoutFragment = new URL(url);
|
|
193
|
+
withoutFragment.hash = "";
|
|
194
|
+
return withoutFragment.href;
|
|
195
|
+
}
|
|
161
196
|
/**
|
|
162
197
|
* Starts cancelling a response body that is not read, without waiting for it
|
|
163
198
|
* to settle. The cancellation of a branch of a tee (e.g., a body cloned by
|
|
@@ -188,6 +223,46 @@ async function getNodeInfo(url, options = {}) {
|
|
|
188
223
|
* @internal
|
|
189
224
|
*/
|
|
190
225
|
async function getNodeInfoInternal(url, options = {}, deps = {}) {
|
|
226
|
+
const timeout = resolveTimeout(options.timeout);
|
|
227
|
+
if (timeout == null && options.signal == null) return await getNodeInfoWithSignal(url, options, deps);
|
|
228
|
+
const controller = new AbortController();
|
|
229
|
+
const timeoutReason = timeout == null ? void 0 : new DOMException(`The NodeInfo lookup timed out after ${timeout} ms.`, "TimeoutError");
|
|
230
|
+
const timer = timeout == null ? void 0 : setTimeout(() => controller.abort(timeoutReason), timeout);
|
|
231
|
+
const onCallerAbort = () => controller.abort(options.signal?.reason);
|
|
232
|
+
options.signal?.addEventListener("abort", onCallerAbort, { once: true });
|
|
233
|
+
if (options.signal?.aborted) onCallerAbort();
|
|
234
|
+
let onAbort;
|
|
235
|
+
const aborted = new Promise((_, reject) => {
|
|
236
|
+
onAbort = () => reject(controller.signal.reason);
|
|
237
|
+
controller.signal.addEventListener("abort", onAbort, { once: true });
|
|
238
|
+
});
|
|
239
|
+
const loading = getNodeInfoWithSignal(url, options, deps, controller.signal);
|
|
240
|
+
loading.catch(() => {});
|
|
241
|
+
try {
|
|
242
|
+
return await Promise.race([loading, aborted]);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
if (!controller.signal.aborted) throw error;
|
|
245
|
+
if (controller.signal.reason === timeoutReason) logger.error("Timed out after {timeout} ms while fetching NodeInfo: {url}", {
|
|
246
|
+
timeout,
|
|
247
|
+
url: url.toString()
|
|
248
|
+
});
|
|
249
|
+
else logger.debug("Cancelled NodeInfo lookup for {url}.", { url: url.toString() });
|
|
250
|
+
return;
|
|
251
|
+
} finally {
|
|
252
|
+
clearTimeout(timer);
|
|
253
|
+
options.signal?.removeEventListener("abort", onCallerAbort);
|
|
254
|
+
if (onAbort != null) controller.signal.removeEventListener("abort", onAbort);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
function resolveTimeout(timeout) {
|
|
258
|
+
if (timeout === void 0) return DEFAULT_TIMEOUT;
|
|
259
|
+
if (timeout === null) return null;
|
|
260
|
+
if (!Number.isFinite(timeout) || timeout <= 0) throw new RangeError(`The NodeInfo timeout must be a positive finite number of milliseconds, but got ${String(timeout)}.`);
|
|
261
|
+
const ms = Math.ceil(timeout);
|
|
262
|
+
if (ms > MAX_TIMEOUT) throw new RangeError(`The NodeInfo timeout must not be greater than ${MAX_TIMEOUT} milliseconds, but got ${timeout}.`);
|
|
263
|
+
return ms;
|
|
264
|
+
}
|
|
265
|
+
async function getNodeInfoWithSignal(url, options, deps, signal) {
|
|
191
266
|
try {
|
|
192
267
|
const headers = {
|
|
193
268
|
Accept: "application/json",
|
|
@@ -206,8 +281,8 @@ async function getNodeInfoInternal(url, options = {}, deps = {}) {
|
|
|
206
281
|
cancelBody(response, wellKnownUrl);
|
|
207
282
|
return;
|
|
208
283
|
}
|
|
209
|
-
return { rd: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, wellKnownUrl)) };
|
|
210
|
-
}, deps);
|
|
284
|
+
return { rd: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, wellKnownUrl, signal)) };
|
|
285
|
+
}, deps, signal);
|
|
211
286
|
if (wellKnown == null) return void 0;
|
|
212
287
|
const wellKnownRd = wellKnown.rd;
|
|
213
288
|
const link = wellKnownRd?.links?.find((link) => link != null && "rel" in link && (link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.0" || link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.1") && "href" in link && link.href != null);
|
|
@@ -230,14 +305,14 @@ async function getNodeInfoInternal(url, options = {}, deps = {}) {
|
|
|
230
305
|
cancelBody(response, nodeInfoUrl);
|
|
231
306
|
return;
|
|
232
307
|
}
|
|
233
|
-
return { data: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl)) };
|
|
234
|
-
}, deps);
|
|
308
|
+
return { data: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl, signal)) };
|
|
309
|
+
}, deps, signal);
|
|
235
310
|
if (document == null) return void 0;
|
|
236
311
|
const { data } = document;
|
|
237
312
|
if (options.parse === "none") return data;
|
|
238
313
|
return parseNodeInfo(data, { tryBestEffort: options.parse === "best-effort" }) ?? void 0;
|
|
239
314
|
} catch (error) {
|
|
240
|
-
logger
|
|
315
|
+
logger[signal?.aborted ? "debug" : "error"]("Failed to fetch NodeInfo document from {url}: {error}", {
|
|
241
316
|
url: url.toString(),
|
|
242
317
|
error
|
|
243
318
|
});
|
|
@@ -5,9 +5,9 @@ import { t as esm_default } from "../esm-DVILvP5e.mjs";
|
|
|
5
5
|
import { t as assertEquals } from "../assert_equals-Ew3jOFa3.mjs";
|
|
6
6
|
import "../std__assert-DflMZha5.mjs";
|
|
7
7
|
import { t as assertRejects } from "../assert_rejects-WyYSTDDX.mjs";
|
|
8
|
-
import { u as verifyRequest } from "../http-
|
|
8
|
+
import { u as verifyRequest } from "../http-HX6wVyJh.mjs";
|
|
9
9
|
import { i as rsaPrivateKey2 } from "../keys-CtZLJq76.mjs";
|
|
10
|
-
import { n as getAuthenticatedDocumentLoader, t as createAuthenticatedDocumentLoader } from "../docloader-
|
|
10
|
+
import { n as getAuthenticatedDocumentLoader, t as createAuthenticatedDocumentLoader } from "../docloader-DUXr9k_O.mjs";
|
|
11
11
|
import { mockDocumentLoader, test } from "@fedify/fixture";
|
|
12
12
|
import { FetchError, UrlError } from "@fedify/vocab-runtime";
|
|
13
13
|
import { openDirectFetch, openPublicFetch } from "@fedify/vocab-runtime/internal";
|
package/dist/utils/mod.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const { Temporal } = require("@js-temporal/polyfill");
|
|
2
2
|
const { URLPattern } = require("urlpattern-polyfill");
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
4
|
-
const require_kv_cache = require("../kv-cache-
|
|
4
|
+
const require_kv_cache = require("../kv-cache-B0V_e231.cjs");
|
|
5
5
|
exports.getAuthenticatedDocumentLoader = require_kv_cache.getAuthenticatedDocumentLoader;
|
|
6
6
|
exports.kvCache = require_kv_cache.kvCache;
|
package/dist/utils/mod.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
/// <reference lib="esnext.temporal" />
|
|
2
|
-
import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../mod-
|
|
2
|
+
import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../mod-UvIGUHqW.cjs";
|
|
3
3
|
export { getAuthenticatedDocumentLoader, kvCache };
|
package/dist/utils/mod.d.ts
CHANGED
package/dist/utils/mod.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Temporal } from "@js-temporal/polyfill";
|
|
2
2
|
import { URLPattern } from "urlpattern-polyfill";
|
|
3
|
-
import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../kv-cache-
|
|
3
|
+
import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../kv-cache-BsDg-cT8.js";
|
|
4
4
|
export { getAuthenticatedDocumentLoader, kvCache };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fedify/fedify",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.32",
|
|
4
4
|
"description": "An ActivityPub server framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ActivityPub",
|
|
@@ -144,9 +144,9 @@
|
|
|
144
144
|
"uri-template-router": "^1.0.0",
|
|
145
145
|
"url-template": "^3.1.1",
|
|
146
146
|
"urlpattern-polyfill": "^10.1.0",
|
|
147
|
-
"@fedify/vocab
|
|
148
|
-
"@fedify/webfinger": "2.1.
|
|
149
|
-
"@fedify/vocab": "2.1.
|
|
147
|
+
"@fedify/vocab": "2.1.32",
|
|
148
|
+
"@fedify/webfinger": "2.1.32",
|
|
149
|
+
"@fedify/vocab-runtime": "2.1.32"
|
|
150
150
|
},
|
|
151
151
|
"devDependencies": {
|
|
152
152
|
"@std/assert": "npm:@jsr/std__assert@^0.226.0",
|
|
@@ -158,8 +158,8 @@
|
|
|
158
158
|
"tsx": "^4.19.4",
|
|
159
159
|
"typescript": "^6.0.0",
|
|
160
160
|
"wrangler": "^4.17.0",
|
|
161
|
-
"@fedify/
|
|
162
|
-
"@fedify/
|
|
161
|
+
"@fedify/vocab-tools": "^2.1.32",
|
|
162
|
+
"@fedify/fixture": "2.0.0"
|
|
163
163
|
},
|
|
164
164
|
"scripts": {
|
|
165
165
|
"build:self": "tsdown",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|