@fedify/webfinger 2.3.16-dev.2841 → 2.3.16
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 +259 -11
- package/dist/lookup.test.js +259 -11
- package/dist/mod.cjs +114 -11
- package/dist/mod.d.cts +5 -2
- package/dist/mod.d.ts +5 -2
- package/dist/mod.js +114 -11
- package/package.json +2 -2
- package/src/body.ts +11 -0
- package/src/lookup-impl.ts +136 -11
- package/src/lookup.test.ts +188 -0
- package/src/lookup.ts +2 -1
package/dist/lookup.test.js
CHANGED
|
@@ -1229,7 +1229,7 @@ var esm_default = new class FetchMock {
|
|
|
1229
1229
|
//#endregion
|
|
1230
1230
|
//#region deno.json
|
|
1231
1231
|
var name = "@fedify/webfinger";
|
|
1232
|
-
var version = "2.3.16
|
|
1232
|
+
var version = "2.3.16";
|
|
1233
1233
|
//#endregion
|
|
1234
1234
|
//#region src/body.ts
|
|
1235
1235
|
/** Decoded JSON body limit: 16 MiB. @internal */
|
|
@@ -1252,13 +1252,18 @@ function validateBodySizeLimit(maxBytes) {
|
|
|
1252
1252
|
* @param message The response or request to read.
|
|
1253
1253
|
* @param maxBytes The maximum number of decoded bytes to read.
|
|
1254
1254
|
* @param url The URL to include in errors and logs.
|
|
1255
|
+
* @param signal The signal that cancels an in-progress body read.
|
|
1255
1256
|
* @returns The decoded body text.
|
|
1256
1257
|
* @throws {BodyTooLargeError} If the body exceeds the limit.
|
|
1257
1258
|
* @internal
|
|
1258
1259
|
*/
|
|
1259
|
-
async function readBoundedText(message, maxBytes, url) {
|
|
1260
|
+
async function readBoundedText(message, maxBytes, url, signal) {
|
|
1260
1261
|
validateBodySizeLimit(maxBytes);
|
|
1261
1262
|
const reader = message.body?.getReader();
|
|
1263
|
+
const cancelOnAbort = () => {
|
|
1264
|
+
if (reader != null) reader.cancel(signal?.reason).catch(() => {});
|
|
1265
|
+
};
|
|
1266
|
+
signal?.addEventListener("abort", cancelOnAbort, { once: true });
|
|
1262
1267
|
const tooLarge = () => {
|
|
1263
1268
|
getLogger([
|
|
1264
1269
|
"fedify",
|
|
@@ -1271,6 +1276,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
1271
1276
|
throw new BodyTooLargeError(url, maxBytes);
|
|
1272
1277
|
};
|
|
1273
1278
|
try {
|
|
1279
|
+
signal?.throwIfAborted();
|
|
1274
1280
|
const length = message.headers.get("Content-Length");
|
|
1275
1281
|
const encoding = message.headers.get("Content-Encoding");
|
|
1276
1282
|
if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
|
|
@@ -1278,6 +1284,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
1278
1284
|
let size = 0;
|
|
1279
1285
|
const textReader = new ReadableStream({ async pull(controller) {
|
|
1280
1286
|
const { done, value } = await reader.read();
|
|
1287
|
+
signal?.throwIfAborted();
|
|
1281
1288
|
if (done) {
|
|
1282
1289
|
controller.close();
|
|
1283
1290
|
return;
|
|
@@ -1290,9 +1297,11 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
1290
1297
|
const chunks = [];
|
|
1291
1298
|
while (true) {
|
|
1292
1299
|
const { done, value } = await textReader.read();
|
|
1300
|
+
signal?.throwIfAborted();
|
|
1293
1301
|
if (done) break;
|
|
1294
1302
|
chunks.push(value);
|
|
1295
1303
|
}
|
|
1304
|
+
signal?.throwIfAborted();
|
|
1296
1305
|
return chunks.join("");
|
|
1297
1306
|
} finally {
|
|
1298
1307
|
textReader.releaseLock();
|
|
@@ -1301,6 +1310,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
1301
1310
|
if (reader != null) reader.cancel(error).catch(() => {});
|
|
1302
1311
|
throw error;
|
|
1303
1312
|
} finally {
|
|
1313
|
+
signal?.removeEventListener("abort", cancelOnAbort);
|
|
1304
1314
|
reader?.releaseLock();
|
|
1305
1315
|
}
|
|
1306
1316
|
}
|
|
@@ -1312,6 +1322,7 @@ const logger = getLogger([
|
|
|
1312
1322
|
"lookup"
|
|
1313
1323
|
]);
|
|
1314
1324
|
const DEFAULT_MAX_REDIRECTION = 5;
|
|
1325
|
+
const DEFAULT_TIMEOUT_MS = 1e4;
|
|
1315
1326
|
/**
|
|
1316
1327
|
* Looks up a WebFinger resource, opening a fetch session per hop. This is
|
|
1317
1328
|
* the implementation of {@link lookupWebFinger}, which is not exported from
|
|
@@ -1345,14 +1356,67 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
1345
1356
|
protocol = resource.protocol;
|
|
1346
1357
|
server = resource.host;
|
|
1347
1358
|
}
|
|
1348
|
-
|
|
1359
|
+
const url = new URL(`${protocol}//${server}/.well-known/webfinger`);
|
|
1349
1360
|
url.searchParams.set("resource", resource.href);
|
|
1350
|
-
const
|
|
1361
|
+
const controller = new AbortController();
|
|
1362
|
+
const signal = controller.signal;
|
|
1363
|
+
let timedOut = false;
|
|
1364
|
+
const timeoutMs = deps.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
1365
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2147483647) throw new RangeError("The WebFinger timeout must be a positive integer of at most 2147483647 ms.");
|
|
1366
|
+
const timeout = setTimeout(() => {
|
|
1367
|
+
timedOut = true;
|
|
1368
|
+
controller.abort(new DOMException("WebFinger lookup timed out", "TimeoutError"));
|
|
1369
|
+
}, timeoutMs);
|
|
1370
|
+
const abortFromCaller = () => controller.abort(options.signal?.reason);
|
|
1371
|
+
if (options.signal?.aborted) abortFromCaller();
|
|
1372
|
+
else options.signal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
1373
|
+
let stopWaiting = () => {};
|
|
1374
|
+
const cancelledOutcome = () => ({
|
|
1375
|
+
resource: null,
|
|
1376
|
+
result: "network_error",
|
|
1377
|
+
remoteHost: url.host
|
|
1378
|
+
});
|
|
1379
|
+
const aborted = new Promise((resolve) => {
|
|
1380
|
+
if (signal.aborted) resolve(cancelledOutcome());
|
|
1381
|
+
else {
|
|
1382
|
+
stopWaiting = () => signal.removeEventListener("abort", onAbort);
|
|
1383
|
+
const onAbort = () => resolve(cancelledOutcome());
|
|
1384
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1385
|
+
}
|
|
1386
|
+
});
|
|
1387
|
+
const work = lookupHops(url, options, deps.openHop ?? ((hopUrl, hopSignal) => options.allowPrivateAddress === true ? openDirectFetch(hopUrl) : openPublicFetch(hopUrl, { signal: hopSignal })), signal);
|
|
1388
|
+
try {
|
|
1389
|
+
const result = await Promise.race([work, aborted]);
|
|
1390
|
+
if (signal.aborted) {
|
|
1391
|
+
if (timedOut) logger.warn("WebFinger lookup timed out for {url} after {timeoutMs} ms.", {
|
|
1392
|
+
url: url.href,
|
|
1393
|
+
timeoutMs
|
|
1394
|
+
});
|
|
1395
|
+
return cancelledOutcome();
|
|
1396
|
+
}
|
|
1397
|
+
return result;
|
|
1398
|
+
} catch (error) {
|
|
1399
|
+
if (signal.aborted) return cancelledOutcome();
|
|
1400
|
+
throw error;
|
|
1401
|
+
} finally {
|
|
1402
|
+
clearTimeout(timeout);
|
|
1403
|
+
stopWaiting();
|
|
1404
|
+
options.signal?.removeEventListener("abort", abortFromCaller);
|
|
1405
|
+
work.catch(() => {});
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
async function lookupHops(initialUrl, options, openHop, signal) {
|
|
1409
|
+
let url = initialUrl;
|
|
1351
1410
|
let redirected = 0;
|
|
1352
1411
|
while (true) {
|
|
1412
|
+
if (signal.aborted) return {
|
|
1413
|
+
resource: null,
|
|
1414
|
+
result: "network_error",
|
|
1415
|
+
remoteHost: url.host
|
|
1416
|
+
};
|
|
1353
1417
|
logger.debug("Fetching WebFinger resource descriptor from {url}...", { url: url.href });
|
|
1354
1418
|
const remoteHost = url.host;
|
|
1355
|
-
const session = await openSession(url, openHop,
|
|
1419
|
+
const session = await openSession(url, openHop, signal);
|
|
1356
1420
|
if (session == null) return {
|
|
1357
1421
|
resource: null,
|
|
1358
1422
|
result: "network_error",
|
|
@@ -1360,10 +1424,26 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
1360
1424
|
};
|
|
1361
1425
|
let hop;
|
|
1362
1426
|
try {
|
|
1363
|
-
|
|
1427
|
+
if (signal.aborted) return {
|
|
1428
|
+
resource: null,
|
|
1429
|
+
result: "network_error",
|
|
1430
|
+
remoteHost
|
|
1431
|
+
};
|
|
1432
|
+
hop = await fetchHopUntilAbort(session, url, options, signal);
|
|
1364
1433
|
} finally {
|
|
1365
|
-
|
|
1434
|
+
if (signal.aborted) session.close().catch((error) => {
|
|
1435
|
+
logger.debug("Failed to close WebFinger session for {url}: {error}", {
|
|
1436
|
+
url: url.href,
|
|
1437
|
+
error
|
|
1438
|
+
});
|
|
1439
|
+
});
|
|
1440
|
+
else await session.close();
|
|
1366
1441
|
}
|
|
1442
|
+
if (signal.aborted) return {
|
|
1443
|
+
resource: null,
|
|
1444
|
+
result: "network_error",
|
|
1445
|
+
remoteHost
|
|
1446
|
+
};
|
|
1367
1447
|
if ("outcome" in hop) return hop.outcome;
|
|
1368
1448
|
redirected++;
|
|
1369
1449
|
const maxRedirection = options.maxRedirection ?? DEFAULT_MAX_REDIRECTION;
|
|
@@ -1406,13 +1486,36 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
1406
1486
|
url = redirectedUrl;
|
|
1407
1487
|
}
|
|
1408
1488
|
}
|
|
1489
|
+
/** Lets the session close even when a custom fetch or body ignores abort. */
|
|
1490
|
+
async function fetchHopUntilAbort(session, url, options, signal) {
|
|
1491
|
+
const fetching = fetchHop(session, url, options, signal);
|
|
1492
|
+
let onAbort;
|
|
1493
|
+
const aborted = new Promise((resolve) => {
|
|
1494
|
+
const outcome = {
|
|
1495
|
+
resource: null,
|
|
1496
|
+
result: "network_error",
|
|
1497
|
+
remoteHost: url.host
|
|
1498
|
+
};
|
|
1499
|
+
if (signal.aborted) resolve({ outcome });
|
|
1500
|
+
else {
|
|
1501
|
+
onAbort = () => resolve({ outcome });
|
|
1502
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1503
|
+
}
|
|
1504
|
+
});
|
|
1505
|
+
try {
|
|
1506
|
+
return await Promise.race([fetching, aborted]);
|
|
1507
|
+
} finally {
|
|
1508
|
+
if (onAbort != null) signal.removeEventListener("abort", onAbort);
|
|
1509
|
+
fetching.catch(() => {});
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1409
1512
|
/**
|
|
1410
1513
|
* Opens a fetch session for a hop. A disallowed URL, a failed DNS lookup,
|
|
1411
1514
|
* or an aborted lookup yields `null` instead of an error.
|
|
1412
1515
|
*/
|
|
1413
1516
|
async function openSession(url, openHop, signal) {
|
|
1414
1517
|
try {
|
|
1415
|
-
return await openHop(url.href);
|
|
1518
|
+
return await openHop(url.href, signal);
|
|
1416
1519
|
} catch (e) {
|
|
1417
1520
|
if (e instanceof UrlError) {
|
|
1418
1521
|
if (e.reason === "dns") logger.debug("DNS lookup failed for {url}", {
|
|
@@ -1436,7 +1539,7 @@ async function openSession(url, openHop, signal) {
|
|
|
1436
1539
|
* Fetches a hop with the given session, and consumes the response body or
|
|
1437
1540
|
* starts cancelling it before returning. The caller closes the session.
|
|
1438
1541
|
*/
|
|
1439
|
-
async function fetchHop(session, url, options) {
|
|
1542
|
+
async function fetchHop(session, url, options, signal) {
|
|
1440
1543
|
let response;
|
|
1441
1544
|
try {
|
|
1442
1545
|
response = await session.fetch(url, {
|
|
@@ -1445,7 +1548,7 @@ async function fetchHop(session, url, options) {
|
|
|
1445
1548
|
"User-Agent": typeof options.userAgent === "string" ? options.userAgent : getUserAgent(options.userAgent)
|
|
1446
1549
|
},
|
|
1447
1550
|
redirect: "manual",
|
|
1448
|
-
signal
|
|
1551
|
+
signal
|
|
1449
1552
|
});
|
|
1450
1553
|
} catch (error) {
|
|
1451
1554
|
logger.debug("Failed to fetch WebFinger resource descriptor: {error}", {
|
|
@@ -1481,7 +1584,7 @@ async function fetchHop(session, url, options) {
|
|
|
1481
1584
|
}
|
|
1482
1585
|
try {
|
|
1483
1586
|
return { outcome: {
|
|
1484
|
-
resource: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url)),
|
|
1587
|
+
resource: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url, signal)),
|
|
1485
1588
|
result: "found",
|
|
1486
1589
|
statusCode: response.status,
|
|
1487
1590
|
remoteHost: url.host
|
|
@@ -2372,5 +2475,150 @@ test$1("lookupWebFinger() records network_error for rejected hop addresses", asy
|
|
|
2372
2475
|
deepStrictEqual("http.response.status_code" in counter.attributes, false);
|
|
2373
2476
|
}
|
|
2374
2477
|
});
|
|
2478
|
+
test("lookupWebFinger() caller abort releases a stalled session open", async () => {
|
|
2479
|
+
const controller = new AbortController();
|
|
2480
|
+
let finishOpen;
|
|
2481
|
+
let closed = false;
|
|
2482
|
+
const opening = new Promise((resolve) => {
|
|
2483
|
+
finishOpen = resolve;
|
|
2484
|
+
});
|
|
2485
|
+
const lookup = lookupResourceWithDependencies("acct:alice@example.com", { signal: controller.signal }, { openHop: () => opening });
|
|
2486
|
+
controller.abort();
|
|
2487
|
+
deepStrictEqual(await settleWithin(lookup, 1e3), null);
|
|
2488
|
+
finishOpen({
|
|
2489
|
+
url: new URL("https://example.com"),
|
|
2490
|
+
fetch: () => {
|
|
2491
|
+
throw new Error("fetch after abort");
|
|
2492
|
+
},
|
|
2493
|
+
close: () => {
|
|
2494
|
+
closed = true;
|
|
2495
|
+
return Promise.resolve();
|
|
2496
|
+
}
|
|
2497
|
+
});
|
|
2498
|
+
await Promise.resolve();
|
|
2499
|
+
await Promise.resolve();
|
|
2500
|
+
ok(closed);
|
|
2501
|
+
});
|
|
2502
|
+
test("lookupWebFinger() deadline covers session opening", async () => {
|
|
2503
|
+
const [meterProvider, recorder] = createTestMeterProvider();
|
|
2504
|
+
deepStrictEqual(await settleWithin(lookupWebFingerWithDependencies("acct:alice@example.com", { meterProvider }, {
|
|
2505
|
+
timeoutMs: 20,
|
|
2506
|
+
openHop: () => new Promise(() => {})
|
|
2507
|
+
}), 1e3), null);
|
|
2508
|
+
const measurement = recorder.getMeasurement("webfinger.lookup");
|
|
2509
|
+
ok(measurement != null);
|
|
2510
|
+
deepStrictEqual(measurement.attributes["webfinger.lookup.result"], "network_error");
|
|
2511
|
+
deepStrictEqual(measurement.attributes["activitypub.remote.host"], "example.com");
|
|
2512
|
+
});
|
|
2513
|
+
test("lookupWebFinger() shares one deadline across redirects", async () => {
|
|
2514
|
+
const descriptor = {
|
|
2515
|
+
subject: "acct:alice@example.com",
|
|
2516
|
+
links: []
|
|
2517
|
+
};
|
|
2518
|
+
const pending = [];
|
|
2519
|
+
deepStrictEqual(await settleWithin(lookupResourceWithDependencies("acct:alice@example.com", {}, {
|
|
2520
|
+
timeoutMs: 100,
|
|
2521
|
+
openHop: (url) => Promise.resolve({
|
|
2522
|
+
url: new URL(url),
|
|
2523
|
+
fetch: () => {
|
|
2524
|
+
const response = new Promise((resolve) => {
|
|
2525
|
+
setTimeout(() => {
|
|
2526
|
+
resolve(url.includes("/final") ? new Response(JSON.stringify(descriptor)) : new Response(null, {
|
|
2527
|
+
status: 302,
|
|
2528
|
+
headers: { Location: "/final" }
|
|
2529
|
+
}));
|
|
2530
|
+
}, 60);
|
|
2531
|
+
});
|
|
2532
|
+
pending.push(response);
|
|
2533
|
+
return response;
|
|
2534
|
+
},
|
|
2535
|
+
close: () => Promise.resolve()
|
|
2536
|
+
})
|
|
2537
|
+
}), 1e3), null);
|
|
2538
|
+
await Promise.all(pending);
|
|
2539
|
+
});
|
|
2540
|
+
test("lookupWebFinger() caller signal cannot extend its deadline", async () => {
|
|
2541
|
+
const signal = AbortSignal.timeout(5e3);
|
|
2542
|
+
let reason;
|
|
2543
|
+
deepStrictEqual(await settleWithin(lookupResourceWithDependencies("acct:alice@example.com", { signal }, {
|
|
2544
|
+
timeoutMs: 50,
|
|
2545
|
+
openHop: (_url, effectiveSignal) => {
|
|
2546
|
+
effectiveSignal?.addEventListener("abort", () => {
|
|
2547
|
+
reason = effectiveSignal.reason;
|
|
2548
|
+
}, { once: true });
|
|
2549
|
+
return new Promise(() => {});
|
|
2550
|
+
}
|
|
2551
|
+
}), 1e3), null);
|
|
2552
|
+
ok(!signal.aborted);
|
|
2553
|
+
ok(reason instanceof DOMException);
|
|
2554
|
+
deepStrictEqual(reason.name, "TimeoutError");
|
|
2555
|
+
});
|
|
2556
|
+
test("lookupWebFinger() deadline covers response headers", async () => {
|
|
2557
|
+
let closed = false;
|
|
2558
|
+
let fetchSignal;
|
|
2559
|
+
let fetchAborted = false;
|
|
2560
|
+
deepStrictEqual(await settleWithin(lookupResourceWithDependencies("acct:alice@example.com", {}, {
|
|
2561
|
+
timeoutMs: 20,
|
|
2562
|
+
openHop: () => Promise.resolve({
|
|
2563
|
+
url: new URL("https://example.com"),
|
|
2564
|
+
fetch: (_input, init) => {
|
|
2565
|
+
fetchSignal = init?.signal;
|
|
2566
|
+
fetchSignal?.addEventListener("abort", () => {
|
|
2567
|
+
fetchAborted = true;
|
|
2568
|
+
}, { once: true });
|
|
2569
|
+
return new Promise(() => {});
|
|
2570
|
+
},
|
|
2571
|
+
close: () => {
|
|
2572
|
+
closed = true;
|
|
2573
|
+
return Promise.resolve();
|
|
2574
|
+
}
|
|
2575
|
+
})
|
|
2576
|
+
}), 1e3), null);
|
|
2577
|
+
ok(fetchSignal?.aborted);
|
|
2578
|
+
ok(fetchAborted);
|
|
2579
|
+
deepStrictEqual(fetchSignal.reason.name, "TimeoutError");
|
|
2580
|
+
await Promise.resolve();
|
|
2581
|
+
ok(closed);
|
|
2582
|
+
});
|
|
2583
|
+
test("lookupWebFinger() discards completed JSON when body stalls", async () => {
|
|
2584
|
+
const descriptor = {
|
|
2585
|
+
subject: "acct:alice@example.com",
|
|
2586
|
+
links: []
|
|
2587
|
+
};
|
|
2588
|
+
let cancelled;
|
|
2589
|
+
const cancellation = new Promise((resolve) => {
|
|
2590
|
+
cancelled = resolve;
|
|
2591
|
+
});
|
|
2592
|
+
deepStrictEqual(await settleWithin(lookupResourceWithDependencies("acct:alice@example.com", {}, {
|
|
2593
|
+
timeoutMs: 20,
|
|
2594
|
+
openHop: () => Promise.resolve({
|
|
2595
|
+
url: new URL("https://example.com"),
|
|
2596
|
+
fetch: () => Promise.resolve(new Response(new ReadableStream({
|
|
2597
|
+
start(controller) {
|
|
2598
|
+
controller.enqueue(new TextEncoder().encode(JSON.stringify(descriptor)));
|
|
2599
|
+
},
|
|
2600
|
+
cancel() {
|
|
2601
|
+
cancelled();
|
|
2602
|
+
}
|
|
2603
|
+
}))),
|
|
2604
|
+
close: async () => {}
|
|
2605
|
+
})
|
|
2606
|
+
}), 1e3), null);
|
|
2607
|
+
await settleWithin(cancellation, 1e3);
|
|
2608
|
+
});
|
|
2609
|
+
test("lookupWebFinger() accepts a response before its deadline", async () => {
|
|
2610
|
+
const descriptor = {
|
|
2611
|
+
subject: "acct:alice@example.com",
|
|
2612
|
+
links: []
|
|
2613
|
+
};
|
|
2614
|
+
deepStrictEqual(await lookupResourceWithDependencies("acct:alice@example.com", {}, {
|
|
2615
|
+
timeoutMs: 1e3,
|
|
2616
|
+
openHop: () => Promise.resolve({
|
|
2617
|
+
url: new URL("https://example.com"),
|
|
2618
|
+
fetch: () => Promise.resolve(new Response(JSON.stringify(descriptor))),
|
|
2619
|
+
close: async () => {}
|
|
2620
|
+
})
|
|
2621
|
+
}), descriptor);
|
|
2622
|
+
});
|
|
2375
2623
|
//#endregion
|
|
2376
2624
|
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.3.16
|
|
8
|
+
var version = "2.3.16";
|
|
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
|
|
@@ -121,14 +132,67 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
121
132
|
protocol = resource.protocol;
|
|
122
133
|
server = resource.host;
|
|
123
134
|
}
|
|
124
|
-
|
|
135
|
+
const url = new URL(`${protocol}//${server}/.well-known/webfinger`);
|
|
125
136
|
url.searchParams.set("resource", resource.href);
|
|
126
|
-
const
|
|
137
|
+
const controller = new AbortController();
|
|
138
|
+
const signal = controller.signal;
|
|
139
|
+
let timedOut = false;
|
|
140
|
+
const timeoutMs = deps.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
141
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2147483647) throw new RangeError("The WebFinger timeout must be a positive integer of at most 2147483647 ms.");
|
|
142
|
+
const timeout = setTimeout(() => {
|
|
143
|
+
timedOut = true;
|
|
144
|
+
controller.abort(new DOMException("WebFinger lookup timed out", "TimeoutError"));
|
|
145
|
+
}, timeoutMs);
|
|
146
|
+
const abortFromCaller = () => controller.abort(options.signal?.reason);
|
|
147
|
+
if (options.signal?.aborted) abortFromCaller();
|
|
148
|
+
else options.signal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
149
|
+
let stopWaiting = () => {};
|
|
150
|
+
const cancelledOutcome = () => ({
|
|
151
|
+
resource: null,
|
|
152
|
+
result: "network_error",
|
|
153
|
+
remoteHost: url.host
|
|
154
|
+
});
|
|
155
|
+
const aborted = new Promise((resolve) => {
|
|
156
|
+
if (signal.aborted) resolve(cancelledOutcome());
|
|
157
|
+
else {
|
|
158
|
+
stopWaiting = () => signal.removeEventListener("abort", onAbort);
|
|
159
|
+
const onAbort = () => resolve(cancelledOutcome());
|
|
160
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
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);
|
|
164
|
+
try {
|
|
165
|
+
const result = await Promise.race([work, aborted]);
|
|
166
|
+
if (signal.aborted) {
|
|
167
|
+
if (timedOut) logger.warn("WebFinger lookup timed out for {url} after {timeoutMs} ms.", {
|
|
168
|
+
url: url.href,
|
|
169
|
+
timeoutMs
|
|
170
|
+
});
|
|
171
|
+
return cancelledOutcome();
|
|
172
|
+
}
|
|
173
|
+
return result;
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (signal.aborted) return cancelledOutcome();
|
|
176
|
+
throw error;
|
|
177
|
+
} finally {
|
|
178
|
+
clearTimeout(timeout);
|
|
179
|
+
stopWaiting();
|
|
180
|
+
options.signal?.removeEventListener("abort", abortFromCaller);
|
|
181
|
+
work.catch(() => {});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
async function lookupHops(initialUrl, options, openHop, signal) {
|
|
185
|
+
let url = initialUrl;
|
|
127
186
|
let redirected = 0;
|
|
128
187
|
while (true) {
|
|
188
|
+
if (signal.aborted) return {
|
|
189
|
+
resource: null,
|
|
190
|
+
result: "network_error",
|
|
191
|
+
remoteHost: url.host
|
|
192
|
+
};
|
|
129
193
|
logger.debug("Fetching WebFinger resource descriptor from {url}...", { url: url.href });
|
|
130
194
|
const remoteHost = url.host;
|
|
131
|
-
const session = await openSession(url, openHop,
|
|
195
|
+
const session = await openSession(url, openHop, signal);
|
|
132
196
|
if (session == null) return {
|
|
133
197
|
resource: null,
|
|
134
198
|
result: "network_error",
|
|
@@ -136,10 +200,26 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
136
200
|
};
|
|
137
201
|
let hop;
|
|
138
202
|
try {
|
|
139
|
-
|
|
203
|
+
if (signal.aborted) return {
|
|
204
|
+
resource: null,
|
|
205
|
+
result: "network_error",
|
|
206
|
+
remoteHost
|
|
207
|
+
};
|
|
208
|
+
hop = await fetchHopUntilAbort(session, url, options, signal);
|
|
140
209
|
} finally {
|
|
141
|
-
|
|
210
|
+
if (signal.aborted) session.close().catch((error) => {
|
|
211
|
+
logger.debug("Failed to close WebFinger session for {url}: {error}", {
|
|
212
|
+
url: url.href,
|
|
213
|
+
error
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
else await session.close();
|
|
142
217
|
}
|
|
218
|
+
if (signal.aborted) return {
|
|
219
|
+
resource: null,
|
|
220
|
+
result: "network_error",
|
|
221
|
+
remoteHost
|
|
222
|
+
};
|
|
143
223
|
if ("outcome" in hop) return hop.outcome;
|
|
144
224
|
redirected++;
|
|
145
225
|
const maxRedirection = options.maxRedirection ?? DEFAULT_MAX_REDIRECTION;
|
|
@@ -182,13 +262,36 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
182
262
|
url = redirectedUrl;
|
|
183
263
|
}
|
|
184
264
|
}
|
|
265
|
+
/** Lets the session close even when a custom fetch or body ignores abort. */
|
|
266
|
+
async function fetchHopUntilAbort(session, url, options, signal) {
|
|
267
|
+
const fetching = fetchHop(session, url, options, signal);
|
|
268
|
+
let onAbort;
|
|
269
|
+
const aborted = new Promise((resolve) => {
|
|
270
|
+
const outcome = {
|
|
271
|
+
resource: null,
|
|
272
|
+
result: "network_error",
|
|
273
|
+
remoteHost: url.host
|
|
274
|
+
};
|
|
275
|
+
if (signal.aborted) resolve({ outcome });
|
|
276
|
+
else {
|
|
277
|
+
onAbort = () => resolve({ outcome });
|
|
278
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
try {
|
|
282
|
+
return await Promise.race([fetching, aborted]);
|
|
283
|
+
} finally {
|
|
284
|
+
if (onAbort != null) signal.removeEventListener("abort", onAbort);
|
|
285
|
+
fetching.catch(() => {});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
185
288
|
/**
|
|
186
289
|
* Opens a fetch session for a hop. A disallowed URL, a failed DNS lookup,
|
|
187
290
|
* or an aborted lookup yields `null` instead of an error.
|
|
188
291
|
*/
|
|
189
292
|
async function openSession(url, openHop, signal) {
|
|
190
293
|
try {
|
|
191
|
-
return await openHop(url.href);
|
|
294
|
+
return await openHop(url.href, signal);
|
|
192
295
|
} catch (e) {
|
|
193
296
|
if (e instanceof _fedify_vocab_runtime.UrlError) {
|
|
194
297
|
if (e.reason === "dns") logger.debug("DNS lookup failed for {url}", {
|
|
@@ -212,7 +315,7 @@ async function openSession(url, openHop, signal) {
|
|
|
212
315
|
* Fetches a hop with the given session, and consumes the response body or
|
|
213
316
|
* starts cancelling it before returning. The caller closes the session.
|
|
214
317
|
*/
|
|
215
|
-
async function fetchHop(session, url, options) {
|
|
318
|
+
async function fetchHop(session, url, options, signal) {
|
|
216
319
|
let response;
|
|
217
320
|
try {
|
|
218
321
|
response = await session.fetch(url, {
|
|
@@ -221,7 +324,7 @@ async function fetchHop(session, url, options) {
|
|
|
221
324
|
"User-Agent": typeof options.userAgent === "string" ? options.userAgent : (0, _fedify_vocab_runtime.getUserAgent)(options.userAgent)
|
|
222
325
|
},
|
|
223
326
|
redirect: "manual",
|
|
224
|
-
signal
|
|
327
|
+
signal
|
|
225
328
|
});
|
|
226
329
|
} catch (error) {
|
|
227
330
|
logger.debug("Failed to fetch WebFinger resource descriptor: {error}", {
|
|
@@ -257,7 +360,7 @@ async function fetchHop(session, url, options) {
|
|
|
257
360
|
}
|
|
258
361
|
try {
|
|
259
362
|
return { outcome: {
|
|
260
|
-
resource: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url)),
|
|
363
|
+
resource: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url, signal)),
|
|
261
364
|
result: "found",
|
|
262
365
|
statusCode: response.status,
|
|
263
366
|
remoteHost: url.host
|
package/dist/mod.d.cts
CHANGED
|
@@ -72,7 +72,9 @@ interface LookupWebFingerDependencies {
|
|
|
72
72
|
* Opens a fetch session for a single hop. Defaults to `openDirectFetch()`
|
|
73
73
|
* if `allowPrivateAddress` is turned on, or `openPublicFetch()` otherwise.
|
|
74
74
|
*/
|
|
75
|
-
readonly openHop?: (url: string) => Promise<PublicFetchSession>;
|
|
75
|
+
readonly openHop?: (url: string, signal?: AbortSignal) => Promise<PublicFetchSession>;
|
|
76
|
+
/** Overrides the lookup deadline for internal tests. */
|
|
77
|
+
readonly timeoutMs?: number;
|
|
76
78
|
}
|
|
77
79
|
//#endregion
|
|
78
80
|
//#region src/lookup.d.ts
|
|
@@ -146,7 +148,8 @@ interface LookupWebFingerOptions {
|
|
|
146
148
|
*/
|
|
147
149
|
meterProvider?: MeterProvider;
|
|
148
150
|
/**
|
|
149
|
-
* AbortSignal for cancelling the
|
|
151
|
+
* `AbortSignal` for cancelling the lookup sooner than its default 10-second
|
|
152
|
+
* deadline. A signal cannot extend that deadline.
|
|
150
153
|
* @since 1.8.0
|
|
151
154
|
*/
|
|
152
155
|
signal?: AbortSignal;
|
package/dist/mod.d.ts
CHANGED
|
@@ -72,7 +72,9 @@ interface LookupWebFingerDependencies {
|
|
|
72
72
|
* Opens a fetch session for a single hop. Defaults to `openDirectFetch()`
|
|
73
73
|
* if `allowPrivateAddress` is turned on, or `openPublicFetch()` otherwise.
|
|
74
74
|
*/
|
|
75
|
-
readonly openHop?: (url: string) => Promise<PublicFetchSession>;
|
|
75
|
+
readonly openHop?: (url: string, signal?: AbortSignal) => Promise<PublicFetchSession>;
|
|
76
|
+
/** Overrides the lookup deadline for internal tests. */
|
|
77
|
+
readonly timeoutMs?: number;
|
|
76
78
|
}
|
|
77
79
|
//#endregion
|
|
78
80
|
//#region src/lookup.d.ts
|
|
@@ -146,7 +148,8 @@ interface LookupWebFingerOptions {
|
|
|
146
148
|
*/
|
|
147
149
|
meterProvider?: MeterProvider;
|
|
148
150
|
/**
|
|
149
|
-
* AbortSignal for cancelling the
|
|
151
|
+
* `AbortSignal` for cancelling the lookup sooner than its default 10-second
|
|
152
|
+
* deadline. A signal cannot extend that deadline.
|
|
150
153
|
* @since 1.8.0
|
|
151
154
|
*/
|
|
152
155
|
signal?: AbortSignal;
|