@fedify/webfinger 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/README.md +4 -1
- package/deno.json +1 -1
- package/dist/lookup.test.cjs +232 -11
- package/dist/lookup.test.js +232 -11
- package/dist/mod.cjs +92 -11
- package/dist/mod.d.cts +2 -1
- package/dist/mod.d.ts +2 -1
- package/dist/mod.js +92 -11
- package/package.json +2 -2
- package/src/body.ts +11 -0
- package/src/lookup-impl.ts +120 -11
- package/src/lookup.test.ts +175 -0
- package/src/lookup.ts +2 -1
package/dist/lookup.test.js
CHANGED
|
@@ -1228,7 +1228,7 @@ var esm_default = new class FetchMock {
|
|
|
1228
1228
|
//#endregion
|
|
1229
1229
|
//#region deno.json
|
|
1230
1230
|
var name = "@fedify/webfinger";
|
|
1231
|
-
var version = "2.1.
|
|
1231
|
+
var version = "2.1.32";
|
|
1232
1232
|
//#endregion
|
|
1233
1233
|
//#region src/body.ts
|
|
1234
1234
|
/** Decoded JSON body limit: 16 MiB. @internal */
|
|
@@ -1251,13 +1251,18 @@ function validateBodySizeLimit(maxBytes) {
|
|
|
1251
1251
|
* @param message The response or request to read.
|
|
1252
1252
|
* @param maxBytes The maximum number of decoded bytes to read.
|
|
1253
1253
|
* @param url The URL to include in errors and logs.
|
|
1254
|
+
* @param signal The signal that cancels an in-progress body read.
|
|
1254
1255
|
* @returns The decoded body text.
|
|
1255
1256
|
* @throws {BodyTooLargeError} If the body exceeds the limit.
|
|
1256
1257
|
* @internal
|
|
1257
1258
|
*/
|
|
1258
|
-
async function readBoundedText(message, maxBytes, url) {
|
|
1259
|
+
async function readBoundedText(message, maxBytes, url, signal) {
|
|
1259
1260
|
validateBodySizeLimit(maxBytes);
|
|
1260
1261
|
const reader = message.body?.getReader();
|
|
1262
|
+
const cancelOnAbort = () => {
|
|
1263
|
+
if (reader != null) reader.cancel(signal?.reason).catch(() => {});
|
|
1264
|
+
};
|
|
1265
|
+
signal?.addEventListener("abort", cancelOnAbort, { once: true });
|
|
1261
1266
|
const tooLarge = () => {
|
|
1262
1267
|
getLogger([
|
|
1263
1268
|
"fedify",
|
|
@@ -1270,6 +1275,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
1270
1275
|
throw new BodyTooLargeError(url, maxBytes);
|
|
1271
1276
|
};
|
|
1272
1277
|
try {
|
|
1278
|
+
signal?.throwIfAborted();
|
|
1273
1279
|
const length = message.headers.get("Content-Length");
|
|
1274
1280
|
const encoding = message.headers.get("Content-Encoding");
|
|
1275
1281
|
if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
|
|
@@ -1277,6 +1283,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
1277
1283
|
let size = 0;
|
|
1278
1284
|
const textReader = new ReadableStream({ async pull(controller) {
|
|
1279
1285
|
const { done, value } = await reader.read();
|
|
1286
|
+
signal?.throwIfAborted();
|
|
1280
1287
|
if (done) {
|
|
1281
1288
|
controller.close();
|
|
1282
1289
|
return;
|
|
@@ -1289,9 +1296,11 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
1289
1296
|
const chunks = [];
|
|
1290
1297
|
while (true) {
|
|
1291
1298
|
const { done, value } = await textReader.read();
|
|
1299
|
+
signal?.throwIfAborted();
|
|
1292
1300
|
if (done) break;
|
|
1293
1301
|
chunks.push(value);
|
|
1294
1302
|
}
|
|
1303
|
+
signal?.throwIfAborted();
|
|
1295
1304
|
return chunks.join("");
|
|
1296
1305
|
} finally {
|
|
1297
1306
|
textReader.releaseLock();
|
|
@@ -1300,6 +1309,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
1300
1309
|
if (reader != null) reader.cancel(error).catch(() => {});
|
|
1301
1310
|
throw error;
|
|
1302
1311
|
} finally {
|
|
1312
|
+
signal?.removeEventListener("abort", cancelOnAbort);
|
|
1303
1313
|
reader?.releaseLock();
|
|
1304
1314
|
}
|
|
1305
1315
|
}
|
|
@@ -1311,6 +1321,7 @@ const logger = getLogger([
|
|
|
1311
1321
|
"lookup"
|
|
1312
1322
|
]);
|
|
1313
1323
|
const DEFAULT_MAX_REDIRECTION = 5;
|
|
1324
|
+
const DEFAULT_TIMEOUT_MS = 1e4;
|
|
1314
1325
|
/**
|
|
1315
1326
|
* Looks up a WebFinger resource, opening a fetch session per hop. This is
|
|
1316
1327
|
* the implementation of {@link lookupWebFinger}, which is not exported from
|
|
@@ -1334,20 +1345,72 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
1334
1345
|
protocol = resource.protocol;
|
|
1335
1346
|
server = resource.host;
|
|
1336
1347
|
}
|
|
1337
|
-
|
|
1348
|
+
const url = new URL(`${protocol}//${server}/.well-known/webfinger`);
|
|
1338
1349
|
url.searchParams.set("resource", resource.href);
|
|
1339
|
-
const
|
|
1350
|
+
const controller = new AbortController();
|
|
1351
|
+
const signal = controller.signal;
|
|
1352
|
+
let timedOut = false;
|
|
1353
|
+
const timeoutMs = deps.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
1354
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2147483647) throw new RangeError("The WebFinger timeout must be a positive integer of at most 2147483647 ms.");
|
|
1355
|
+
const timeout = setTimeout(() => {
|
|
1356
|
+
timedOut = true;
|
|
1357
|
+
controller.abort(new DOMException("WebFinger lookup timed out", "TimeoutError"));
|
|
1358
|
+
}, timeoutMs);
|
|
1359
|
+
const abortFromCaller = () => controller.abort(options.signal?.reason);
|
|
1360
|
+
if (options.signal?.aborted) abortFromCaller();
|
|
1361
|
+
else options.signal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
1362
|
+
let stopWaiting = () => {};
|
|
1363
|
+
const aborted = new Promise((resolve) => {
|
|
1364
|
+
if (signal.aborted) resolve(null);
|
|
1365
|
+
else {
|
|
1366
|
+
stopWaiting = () => signal.removeEventListener("abort", onAbort);
|
|
1367
|
+
const onAbort = () => resolve(null);
|
|
1368
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1369
|
+
}
|
|
1370
|
+
});
|
|
1371
|
+
const work = lookupHops(url, options, deps.openHop ?? ((hopUrl, hopSignal) => options.allowPrivateAddress === true ? openDirectFetch(hopUrl) : openPublicFetch(hopUrl, { signal: hopSignal })), signal);
|
|
1372
|
+
try {
|
|
1373
|
+
const result = await Promise.race([work, aborted]);
|
|
1374
|
+
if (signal.aborted) {
|
|
1375
|
+
if (timedOut) logger.warn("WebFinger lookup timed out for {url} after {timeoutMs} ms.", {
|
|
1376
|
+
url: url.href,
|
|
1377
|
+
timeoutMs
|
|
1378
|
+
});
|
|
1379
|
+
return null;
|
|
1380
|
+
}
|
|
1381
|
+
return result;
|
|
1382
|
+
} catch (error) {
|
|
1383
|
+
if (signal.aborted) return null;
|
|
1384
|
+
throw error;
|
|
1385
|
+
} finally {
|
|
1386
|
+
clearTimeout(timeout);
|
|
1387
|
+
stopWaiting();
|
|
1388
|
+
options.signal?.removeEventListener("abort", abortFromCaller);
|
|
1389
|
+
work.catch(() => {});
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
async function lookupHops(initialUrl, options, openHop, signal) {
|
|
1393
|
+
let url = initialUrl;
|
|
1340
1394
|
let redirected = 0;
|
|
1341
1395
|
while (true) {
|
|
1396
|
+
if (signal.aborted) return null;
|
|
1342
1397
|
logger.debug("Fetching WebFinger resource descriptor from {url}...", { url: url.href });
|
|
1343
|
-
const session = await openSession(url, openHop,
|
|
1398
|
+
const session = await openSession(url, openHop, signal);
|
|
1344
1399
|
if (session == null) return null;
|
|
1345
1400
|
let hop;
|
|
1346
1401
|
try {
|
|
1347
|
-
|
|
1402
|
+
if (signal.aborted) return null;
|
|
1403
|
+
hop = await fetchHopUntilAbort(session, url, options, signal);
|
|
1348
1404
|
} finally {
|
|
1349
|
-
|
|
1405
|
+
if (signal.aborted) session.close().catch((error) => {
|
|
1406
|
+
logger.debug("Failed to close WebFinger session for {url}: {error}", {
|
|
1407
|
+
url: url.href,
|
|
1408
|
+
error
|
|
1409
|
+
});
|
|
1410
|
+
});
|
|
1411
|
+
else await session.close();
|
|
1350
1412
|
}
|
|
1413
|
+
if (signal.aborted) return null;
|
|
1351
1414
|
if ("result" in hop) return hop.result;
|
|
1352
1415
|
redirected++;
|
|
1353
1416
|
const maxRedirection = options.maxRedirection ?? DEFAULT_MAX_REDIRECTION;
|
|
@@ -1366,13 +1429,31 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
1366
1429
|
url = redirectedUrl;
|
|
1367
1430
|
}
|
|
1368
1431
|
}
|
|
1432
|
+
/** Lets the session close even when a custom fetch or body ignores abort. */
|
|
1433
|
+
async function fetchHopUntilAbort(session, url, options, signal) {
|
|
1434
|
+
const fetching = fetchHop(session, url, options, signal);
|
|
1435
|
+
let onAbort;
|
|
1436
|
+
const aborted = new Promise((resolve) => {
|
|
1437
|
+
if (signal.aborted) resolve({ result: null });
|
|
1438
|
+
else {
|
|
1439
|
+
onAbort = () => resolve({ result: null });
|
|
1440
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1441
|
+
}
|
|
1442
|
+
});
|
|
1443
|
+
try {
|
|
1444
|
+
return await Promise.race([fetching, aborted]);
|
|
1445
|
+
} finally {
|
|
1446
|
+
if (onAbort != null) signal.removeEventListener("abort", onAbort);
|
|
1447
|
+
fetching.catch(() => {});
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1369
1450
|
/**
|
|
1370
1451
|
* Opens a fetch session for a hop. A disallowed URL, a failed DNS lookup,
|
|
1371
1452
|
* or an aborted lookup yields `null` instead of an error.
|
|
1372
1453
|
*/
|
|
1373
1454
|
async function openSession(url, openHop, signal) {
|
|
1374
1455
|
try {
|
|
1375
|
-
return await openHop(url.href);
|
|
1456
|
+
return await openHop(url.href, signal);
|
|
1376
1457
|
} catch (e) {
|
|
1377
1458
|
if (e instanceof UrlError) {
|
|
1378
1459
|
if (e.reason === "dns") logger.debug("DNS lookup failed for {url}", {
|
|
@@ -1396,7 +1477,7 @@ async function openSession(url, openHop, signal) {
|
|
|
1396
1477
|
* Fetches a hop with the given session, and consumes the response body or
|
|
1397
1478
|
* starts cancelling it before returning. The caller closes the session.
|
|
1398
1479
|
*/
|
|
1399
|
-
async function fetchHop(session, url, options) {
|
|
1480
|
+
async function fetchHop(session, url, options, signal) {
|
|
1400
1481
|
let response;
|
|
1401
1482
|
try {
|
|
1402
1483
|
response = await session.fetch(url, {
|
|
@@ -1405,7 +1486,7 @@ async function fetchHop(session, url, options) {
|
|
|
1405
1486
|
"User-Agent": typeof options.userAgent === "string" ? options.userAgent : getUserAgent(options.userAgent)
|
|
1406
1487
|
},
|
|
1407
1488
|
redirect: "manual",
|
|
1408
|
-
signal
|
|
1489
|
+
signal
|
|
1409
1490
|
});
|
|
1410
1491
|
} catch (error) {
|
|
1411
1492
|
logger.debug("Failed to fetch WebFinger resource descriptor: {error}", {
|
|
@@ -1428,7 +1509,7 @@ async function fetchHop(session, url, options) {
|
|
|
1428
1509
|
return { result: null };
|
|
1429
1510
|
}
|
|
1430
1511
|
try {
|
|
1431
|
-
return { result: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url)) };
|
|
1512
|
+
return { result: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url, signal)) };
|
|
1432
1513
|
} catch (e) {
|
|
1433
1514
|
if (e instanceof BodyTooLargeError) return { result: null };
|
|
1434
1515
|
if (e instanceof SyntaxError) {
|
|
@@ -1983,5 +2064,145 @@ test("lookupWebFinger() does not wait for cancelling response bodies", async (t)
|
|
|
1983
2064
|
esm_default.hardReset();
|
|
1984
2065
|
}
|
|
1985
2066
|
});
|
|
2067
|
+
test("lookupWebFinger() caller abort releases a stalled session open", async () => {
|
|
2068
|
+
const controller = new AbortController();
|
|
2069
|
+
let finishOpen;
|
|
2070
|
+
let closed = false;
|
|
2071
|
+
const opening = new Promise((resolve) => {
|
|
2072
|
+
finishOpen = resolve;
|
|
2073
|
+
});
|
|
2074
|
+
const lookup = lookupWebFingerInternal("acct:alice@example.com", { signal: controller.signal }, { openHop: () => opening });
|
|
2075
|
+
controller.abort();
|
|
2076
|
+
deepStrictEqual(await settleWithin(lookup, 1e3), null);
|
|
2077
|
+
finishOpen({
|
|
2078
|
+
url: new URL("https://example.com"),
|
|
2079
|
+
fetch: () => {
|
|
2080
|
+
throw new Error("fetch after abort");
|
|
2081
|
+
},
|
|
2082
|
+
close: () => {
|
|
2083
|
+
closed = true;
|
|
2084
|
+
return Promise.resolve();
|
|
2085
|
+
}
|
|
2086
|
+
});
|
|
2087
|
+
await Promise.resolve();
|
|
2088
|
+
await Promise.resolve();
|
|
2089
|
+
ok(closed);
|
|
2090
|
+
});
|
|
2091
|
+
test("lookupWebFinger() deadline covers session opening", async () => {
|
|
2092
|
+
deepStrictEqual(await settleWithin(lookupWebFingerInternal("acct:alice@example.com", {}, {
|
|
2093
|
+
timeoutMs: 20,
|
|
2094
|
+
openHop: () => new Promise(() => {})
|
|
2095
|
+
}), 1e3), null);
|
|
2096
|
+
});
|
|
2097
|
+
test("lookupWebFinger() shares one deadline across redirects", async () => {
|
|
2098
|
+
const descriptor = {
|
|
2099
|
+
subject: "acct:alice@example.com",
|
|
2100
|
+
links: []
|
|
2101
|
+
};
|
|
2102
|
+
const pending = [];
|
|
2103
|
+
deepStrictEqual(await settleWithin(lookupWebFingerInternal("acct:alice@example.com", {}, {
|
|
2104
|
+
timeoutMs: 100,
|
|
2105
|
+
openHop: (url) => Promise.resolve({
|
|
2106
|
+
url: new URL(url),
|
|
2107
|
+
fetch: () => {
|
|
2108
|
+
const response = new Promise((resolve) => {
|
|
2109
|
+
setTimeout(() => {
|
|
2110
|
+
resolve(url.includes("/final") ? new Response(JSON.stringify(descriptor)) : new Response(null, {
|
|
2111
|
+
status: 302,
|
|
2112
|
+
headers: { Location: "/final" }
|
|
2113
|
+
}));
|
|
2114
|
+
}, 60);
|
|
2115
|
+
});
|
|
2116
|
+
pending.push(response);
|
|
2117
|
+
return response;
|
|
2118
|
+
},
|
|
2119
|
+
close: () => Promise.resolve()
|
|
2120
|
+
})
|
|
2121
|
+
}), 1e3), null);
|
|
2122
|
+
await Promise.all(pending);
|
|
2123
|
+
});
|
|
2124
|
+
test("lookupWebFinger() caller signal cannot extend its deadline", async () => {
|
|
2125
|
+
const signal = AbortSignal.timeout(5e3);
|
|
2126
|
+
let reason;
|
|
2127
|
+
deepStrictEqual(await settleWithin(lookupWebFingerInternal("acct:alice@example.com", { signal }, {
|
|
2128
|
+
timeoutMs: 50,
|
|
2129
|
+
openHop: (_url, effectiveSignal) => {
|
|
2130
|
+
effectiveSignal?.addEventListener("abort", () => {
|
|
2131
|
+
reason = effectiveSignal.reason;
|
|
2132
|
+
}, { once: true });
|
|
2133
|
+
return new Promise(() => {});
|
|
2134
|
+
}
|
|
2135
|
+
}), 1e3), null);
|
|
2136
|
+
ok(!signal.aborted);
|
|
2137
|
+
ok(reason instanceof DOMException);
|
|
2138
|
+
deepStrictEqual(reason.name, "TimeoutError");
|
|
2139
|
+
});
|
|
2140
|
+
test("lookupWebFinger() deadline covers response headers", async () => {
|
|
2141
|
+
let closed = false;
|
|
2142
|
+
let fetchSignal;
|
|
2143
|
+
let fetchAborted = false;
|
|
2144
|
+
deepStrictEqual(await settleWithin(lookupWebFingerInternal("acct:alice@example.com", {}, {
|
|
2145
|
+
timeoutMs: 20,
|
|
2146
|
+
openHop: () => Promise.resolve({
|
|
2147
|
+
url: new URL("https://example.com"),
|
|
2148
|
+
fetch: (_input, init) => {
|
|
2149
|
+
fetchSignal = init?.signal;
|
|
2150
|
+
fetchSignal?.addEventListener("abort", () => {
|
|
2151
|
+
fetchAborted = true;
|
|
2152
|
+
}, { once: true });
|
|
2153
|
+
return new Promise(() => {});
|
|
2154
|
+
},
|
|
2155
|
+
close: () => {
|
|
2156
|
+
closed = true;
|
|
2157
|
+
return Promise.resolve();
|
|
2158
|
+
}
|
|
2159
|
+
})
|
|
2160
|
+
}), 1e3), null);
|
|
2161
|
+
ok(fetchSignal?.aborted);
|
|
2162
|
+
ok(fetchAborted);
|
|
2163
|
+
deepStrictEqual(fetchSignal.reason.name, "TimeoutError");
|
|
2164
|
+
await Promise.resolve();
|
|
2165
|
+
ok(closed);
|
|
2166
|
+
});
|
|
2167
|
+
test("lookupWebFinger() discards completed JSON when body stalls", async () => {
|
|
2168
|
+
const descriptor = {
|
|
2169
|
+
subject: "acct:alice@example.com",
|
|
2170
|
+
links: []
|
|
2171
|
+
};
|
|
2172
|
+
let cancelled;
|
|
2173
|
+
const cancellation = new Promise((resolve) => {
|
|
2174
|
+
cancelled = resolve;
|
|
2175
|
+
});
|
|
2176
|
+
deepStrictEqual(await settleWithin(lookupWebFingerInternal("acct:alice@example.com", {}, {
|
|
2177
|
+
timeoutMs: 20,
|
|
2178
|
+
openHop: () => Promise.resolve({
|
|
2179
|
+
url: new URL("https://example.com"),
|
|
2180
|
+
fetch: () => Promise.resolve(new Response(new ReadableStream({
|
|
2181
|
+
start(controller) {
|
|
2182
|
+
controller.enqueue(new TextEncoder().encode(JSON.stringify(descriptor)));
|
|
2183
|
+
},
|
|
2184
|
+
cancel() {
|
|
2185
|
+
cancelled();
|
|
2186
|
+
}
|
|
2187
|
+
}))),
|
|
2188
|
+
close: async () => {}
|
|
2189
|
+
})
|
|
2190
|
+
}), 1e3), null);
|
|
2191
|
+
await settleWithin(cancellation, 1e3);
|
|
2192
|
+
});
|
|
2193
|
+
test("lookupWebFinger() accepts a response before its deadline", async () => {
|
|
2194
|
+
const descriptor = {
|
|
2195
|
+
subject: "acct:alice@example.com",
|
|
2196
|
+
links: []
|
|
2197
|
+
};
|
|
2198
|
+
deepStrictEqual(await lookupWebFingerInternal("acct:alice@example.com", {}, {
|
|
2199
|
+
timeoutMs: 1e3,
|
|
2200
|
+
openHop: () => Promise.resolve({
|
|
2201
|
+
url: new URL("https://example.com"),
|
|
2202
|
+
fetch: () => Promise.resolve(new Response(JSON.stringify(descriptor))),
|
|
2203
|
+
close: async () => {}
|
|
2204
|
+
})
|
|
2205
|
+
}), descriptor);
|
|
2206
|
+
});
|
|
1986
2207
|
//#endregion
|
|
1987
2208
|
export {};
|
package/dist/mod.cjs
CHANGED
|
@@ -5,7 +5,7 @@ let _fedify_vocab_runtime_internal = require("@fedify/vocab-runtime/internal");
|
|
|
5
5
|
let _logtape_logtape = require("@logtape/logtape");
|
|
6
6
|
//#region deno.json
|
|
7
7
|
var name = "@fedify/webfinger";
|
|
8
|
-
var version = "2.1.
|
|
8
|
+
var version = "2.1.32";
|
|
9
9
|
//#endregion
|
|
10
10
|
//#region src/body.ts
|
|
11
11
|
/** Decoded JSON body limit: 16 MiB. @internal */
|
|
@@ -28,13 +28,18 @@ function validateBodySizeLimit(maxBytes) {
|
|
|
28
28
|
* @param message The response or request to read.
|
|
29
29
|
* @param maxBytes The maximum number of decoded bytes to read.
|
|
30
30
|
* @param url The URL to include in errors and logs.
|
|
31
|
+
* @param signal The signal that cancels an in-progress body read.
|
|
31
32
|
* @returns The decoded body text.
|
|
32
33
|
* @throws {BodyTooLargeError} If the body exceeds the limit.
|
|
33
34
|
* @internal
|
|
34
35
|
*/
|
|
35
|
-
async function readBoundedText(message, maxBytes, url) {
|
|
36
|
+
async function readBoundedText(message, maxBytes, url, signal) {
|
|
36
37
|
validateBodySizeLimit(maxBytes);
|
|
37
38
|
const reader = message.body?.getReader();
|
|
39
|
+
const cancelOnAbort = () => {
|
|
40
|
+
if (reader != null) reader.cancel(signal?.reason).catch(() => {});
|
|
41
|
+
};
|
|
42
|
+
signal?.addEventListener("abort", cancelOnAbort, { once: true });
|
|
38
43
|
const tooLarge = () => {
|
|
39
44
|
(0, _logtape_logtape.getLogger)([
|
|
40
45
|
"fedify",
|
|
@@ -47,6 +52,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
47
52
|
throw new BodyTooLargeError(url, maxBytes);
|
|
48
53
|
};
|
|
49
54
|
try {
|
|
55
|
+
signal?.throwIfAborted();
|
|
50
56
|
const length = message.headers.get("Content-Length");
|
|
51
57
|
const encoding = message.headers.get("Content-Encoding");
|
|
52
58
|
if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
|
|
@@ -54,6 +60,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
54
60
|
let size = 0;
|
|
55
61
|
const textReader = new ReadableStream({ async pull(controller) {
|
|
56
62
|
const { done, value } = await reader.read();
|
|
63
|
+
signal?.throwIfAborted();
|
|
57
64
|
if (done) {
|
|
58
65
|
controller.close();
|
|
59
66
|
return;
|
|
@@ -66,9 +73,11 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
66
73
|
const chunks = [];
|
|
67
74
|
while (true) {
|
|
68
75
|
const { done, value } = await textReader.read();
|
|
76
|
+
signal?.throwIfAborted();
|
|
69
77
|
if (done) break;
|
|
70
78
|
chunks.push(value);
|
|
71
79
|
}
|
|
80
|
+
signal?.throwIfAborted();
|
|
72
81
|
return chunks.join("");
|
|
73
82
|
} finally {
|
|
74
83
|
textReader.releaseLock();
|
|
@@ -77,6 +86,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
77
86
|
if (reader != null) reader.cancel(error).catch(() => {});
|
|
78
87
|
throw error;
|
|
79
88
|
} finally {
|
|
89
|
+
signal?.removeEventListener("abort", cancelOnAbort);
|
|
80
90
|
reader?.releaseLock();
|
|
81
91
|
}
|
|
82
92
|
}
|
|
@@ -88,6 +98,7 @@ const logger = (0, _logtape_logtape.getLogger)([
|
|
|
88
98
|
"lookup"
|
|
89
99
|
]);
|
|
90
100
|
const DEFAULT_MAX_REDIRECTION = 5;
|
|
101
|
+
const DEFAULT_TIMEOUT_MS = 1e4;
|
|
91
102
|
/**
|
|
92
103
|
* Looks up a WebFinger resource, opening a fetch session per hop. This is
|
|
93
104
|
* the implementation of {@link lookupWebFinger}, which is not exported from
|
|
@@ -111,20 +122,72 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
111
122
|
protocol = resource.protocol;
|
|
112
123
|
server = resource.host;
|
|
113
124
|
}
|
|
114
|
-
|
|
125
|
+
const url = new URL(`${protocol}//${server}/.well-known/webfinger`);
|
|
115
126
|
url.searchParams.set("resource", resource.href);
|
|
116
|
-
const
|
|
127
|
+
const controller = new AbortController();
|
|
128
|
+
const signal = controller.signal;
|
|
129
|
+
let timedOut = false;
|
|
130
|
+
const timeoutMs = deps.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
131
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2147483647) throw new RangeError("The WebFinger timeout must be a positive integer of at most 2147483647 ms.");
|
|
132
|
+
const timeout = setTimeout(() => {
|
|
133
|
+
timedOut = true;
|
|
134
|
+
controller.abort(new DOMException("WebFinger lookup timed out", "TimeoutError"));
|
|
135
|
+
}, timeoutMs);
|
|
136
|
+
const abortFromCaller = () => controller.abort(options.signal?.reason);
|
|
137
|
+
if (options.signal?.aborted) abortFromCaller();
|
|
138
|
+
else options.signal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
139
|
+
let stopWaiting = () => {};
|
|
140
|
+
const aborted = new Promise((resolve) => {
|
|
141
|
+
if (signal.aborted) resolve(null);
|
|
142
|
+
else {
|
|
143
|
+
stopWaiting = () => signal.removeEventListener("abort", onAbort);
|
|
144
|
+
const onAbort = () => resolve(null);
|
|
145
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
const work = lookupHops(url, options, deps.openHop ?? ((hopUrl, hopSignal) => options.allowPrivateAddress === true ? (0, _fedify_vocab_runtime_internal.openDirectFetch)(hopUrl) : (0, _fedify_vocab_runtime_internal.openPublicFetch)(hopUrl, { signal: hopSignal })), signal);
|
|
149
|
+
try {
|
|
150
|
+
const result = await Promise.race([work, aborted]);
|
|
151
|
+
if (signal.aborted) {
|
|
152
|
+
if (timedOut) logger.warn("WebFinger lookup timed out for {url} after {timeoutMs} ms.", {
|
|
153
|
+
url: url.href,
|
|
154
|
+
timeoutMs
|
|
155
|
+
});
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
} catch (error) {
|
|
160
|
+
if (signal.aborted) return null;
|
|
161
|
+
throw error;
|
|
162
|
+
} finally {
|
|
163
|
+
clearTimeout(timeout);
|
|
164
|
+
stopWaiting();
|
|
165
|
+
options.signal?.removeEventListener("abort", abortFromCaller);
|
|
166
|
+
work.catch(() => {});
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
async function lookupHops(initialUrl, options, openHop, signal) {
|
|
170
|
+
let url = initialUrl;
|
|
117
171
|
let redirected = 0;
|
|
118
172
|
while (true) {
|
|
173
|
+
if (signal.aborted) return null;
|
|
119
174
|
logger.debug("Fetching WebFinger resource descriptor from {url}...", { url: url.href });
|
|
120
|
-
const session = await openSession(url, openHop,
|
|
175
|
+
const session = await openSession(url, openHop, signal);
|
|
121
176
|
if (session == null) return null;
|
|
122
177
|
let hop;
|
|
123
178
|
try {
|
|
124
|
-
|
|
179
|
+
if (signal.aborted) return null;
|
|
180
|
+
hop = await fetchHopUntilAbort(session, url, options, signal);
|
|
125
181
|
} finally {
|
|
126
|
-
|
|
182
|
+
if (signal.aborted) session.close().catch((error) => {
|
|
183
|
+
logger.debug("Failed to close WebFinger session for {url}: {error}", {
|
|
184
|
+
url: url.href,
|
|
185
|
+
error
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
else await session.close();
|
|
127
189
|
}
|
|
190
|
+
if (signal.aborted) return null;
|
|
128
191
|
if ("result" in hop) return hop.result;
|
|
129
192
|
redirected++;
|
|
130
193
|
const maxRedirection = options.maxRedirection ?? DEFAULT_MAX_REDIRECTION;
|
|
@@ -143,13 +206,31 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
143
206
|
url = redirectedUrl;
|
|
144
207
|
}
|
|
145
208
|
}
|
|
209
|
+
/** Lets the session close even when a custom fetch or body ignores abort. */
|
|
210
|
+
async function fetchHopUntilAbort(session, url, options, signal) {
|
|
211
|
+
const fetching = fetchHop(session, url, options, signal);
|
|
212
|
+
let onAbort;
|
|
213
|
+
const aborted = new Promise((resolve) => {
|
|
214
|
+
if (signal.aborted) resolve({ result: null });
|
|
215
|
+
else {
|
|
216
|
+
onAbort = () => resolve({ result: null });
|
|
217
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
try {
|
|
221
|
+
return await Promise.race([fetching, aborted]);
|
|
222
|
+
} finally {
|
|
223
|
+
if (onAbort != null) signal.removeEventListener("abort", onAbort);
|
|
224
|
+
fetching.catch(() => {});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
146
227
|
/**
|
|
147
228
|
* Opens a fetch session for a hop. A disallowed URL, a failed DNS lookup,
|
|
148
229
|
* or an aborted lookup yields `null` instead of an error.
|
|
149
230
|
*/
|
|
150
231
|
async function openSession(url, openHop, signal) {
|
|
151
232
|
try {
|
|
152
|
-
return await openHop(url.href);
|
|
233
|
+
return await openHop(url.href, signal);
|
|
153
234
|
} catch (e) {
|
|
154
235
|
if (e instanceof _fedify_vocab_runtime.UrlError) {
|
|
155
236
|
if (e.reason === "dns") logger.debug("DNS lookup failed for {url}", {
|
|
@@ -173,7 +254,7 @@ async function openSession(url, openHop, signal) {
|
|
|
173
254
|
* Fetches a hop with the given session, and consumes the response body or
|
|
174
255
|
* starts cancelling it before returning. The caller closes the session.
|
|
175
256
|
*/
|
|
176
|
-
async function fetchHop(session, url, options) {
|
|
257
|
+
async function fetchHop(session, url, options, signal) {
|
|
177
258
|
let response;
|
|
178
259
|
try {
|
|
179
260
|
response = await session.fetch(url, {
|
|
@@ -182,7 +263,7 @@ async function fetchHop(session, url, options) {
|
|
|
182
263
|
"User-Agent": typeof options.userAgent === "string" ? options.userAgent : (0, _fedify_vocab_runtime.getUserAgent)(options.userAgent)
|
|
183
264
|
},
|
|
184
265
|
redirect: "manual",
|
|
185
|
-
signal
|
|
266
|
+
signal
|
|
186
267
|
});
|
|
187
268
|
} catch (error) {
|
|
188
269
|
logger.debug("Failed to fetch WebFinger resource descriptor: {error}", {
|
|
@@ -205,7 +286,7 @@ async function fetchHop(session, url, options) {
|
|
|
205
286
|
return { result: null };
|
|
206
287
|
}
|
|
207
288
|
try {
|
|
208
|
-
return { result: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url)) };
|
|
289
|
+
return { result: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url, signal)) };
|
|
209
290
|
} catch (e) {
|
|
210
291
|
if (e instanceof BodyTooLargeError) return { result: null };
|
|
211
292
|
if (e instanceof SyntaxError) {
|
package/dist/mod.d.cts
CHANGED
|
@@ -95,7 +95,8 @@ interface LookupWebFingerOptions {
|
|
|
95
95
|
*/
|
|
96
96
|
tracerProvider?: TracerProvider;
|
|
97
97
|
/**
|
|
98
|
-
* AbortSignal for cancelling the
|
|
98
|
+
* `AbortSignal` for cancelling the lookup sooner than its default 10-second
|
|
99
|
+
* deadline. A signal cannot extend that deadline.
|
|
99
100
|
* @since 1.8.0
|
|
100
101
|
*/
|
|
101
102
|
signal?: AbortSignal;
|
package/dist/mod.d.ts
CHANGED
|
@@ -95,7 +95,8 @@ interface LookupWebFingerOptions {
|
|
|
95
95
|
*/
|
|
96
96
|
tracerProvider?: TracerProvider;
|
|
97
97
|
/**
|
|
98
|
-
* AbortSignal for cancelling the
|
|
98
|
+
* `AbortSignal` for cancelling the lookup sooner than its default 10-second
|
|
99
|
+
* deadline. A signal cannot extend that deadline.
|
|
99
100
|
* @since 1.8.0
|
|
100
101
|
*/
|
|
101
102
|
signal?: AbortSignal;
|