@fedify/webfinger 2.2.20 → 2.2.21
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/README.md
CHANGED
|
@@ -96,11 +96,14 @@ const result = await lookupWebFinger("acct:alice@example.com", {
|
|
|
96
96
|
// Maximum redirects to follow (default: 5)
|
|
97
97
|
maxRedirection: 3,
|
|
98
98
|
|
|
99
|
-
//
|
|
99
|
+
// Cancel sooner than the default 10-second lookup deadline
|
|
100
100
|
signal: AbortSignal.timeout(5000),
|
|
101
101
|
});
|
|
102
102
|
~~~~
|
|
103
103
|
|
|
104
|
+
Each lookup has a 10-second deadline across DNS resolution, redirects, and
|
|
105
|
+
response reading. A supplied signal can end it sooner but cannot extend it.
|
|
106
|
+
|
|
104
107
|
|
|
105
108
|
API
|
|
106
109
|
---
|
package/deno.json
CHANGED
package/dist/lookup.test.cjs
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.2.
|
|
1232
|
+
var version = "2.2.21";
|
|
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
|
(0, _logtape_logtape.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 = (0, _logtape_logtape.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
|
|
@@ -1335,20 +1346,72 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
1335
1346
|
protocol = resource.protocol;
|
|
1336
1347
|
server = resource.host;
|
|
1337
1348
|
}
|
|
1338
|
-
|
|
1349
|
+
const url = new URL(`${protocol}//${server}/.well-known/webfinger`);
|
|
1339
1350
|
url.searchParams.set("resource", resource.href);
|
|
1340
|
-
const
|
|
1351
|
+
const controller = new AbortController();
|
|
1352
|
+
const signal = controller.signal;
|
|
1353
|
+
let timedOut = false;
|
|
1354
|
+
const timeoutMs = deps.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
1355
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > 2147483647) throw new RangeError("The WebFinger timeout must be a positive integer of at most 2147483647 ms.");
|
|
1356
|
+
const timeout = setTimeout(() => {
|
|
1357
|
+
timedOut = true;
|
|
1358
|
+
controller.abort(new DOMException("WebFinger lookup timed out", "TimeoutError"));
|
|
1359
|
+
}, timeoutMs);
|
|
1360
|
+
const abortFromCaller = () => controller.abort(options.signal?.reason);
|
|
1361
|
+
if (options.signal?.aborted) abortFromCaller();
|
|
1362
|
+
else options.signal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
1363
|
+
let stopWaiting = () => {};
|
|
1364
|
+
const aborted = new Promise((resolve) => {
|
|
1365
|
+
if (signal.aborted) resolve(null);
|
|
1366
|
+
else {
|
|
1367
|
+
stopWaiting = () => signal.removeEventListener("abort", onAbort);
|
|
1368
|
+
const onAbort = () => resolve(null);
|
|
1369
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1370
|
+
}
|
|
1371
|
+
});
|
|
1372
|
+
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);
|
|
1373
|
+
try {
|
|
1374
|
+
const result = await Promise.race([work, aborted]);
|
|
1375
|
+
if (signal.aborted) {
|
|
1376
|
+
if (timedOut) logger.warn("WebFinger lookup timed out for {url} after {timeoutMs} ms.", {
|
|
1377
|
+
url: url.href,
|
|
1378
|
+
timeoutMs
|
|
1379
|
+
});
|
|
1380
|
+
return null;
|
|
1381
|
+
}
|
|
1382
|
+
return result;
|
|
1383
|
+
} catch (error) {
|
|
1384
|
+
if (signal.aborted) return null;
|
|
1385
|
+
throw error;
|
|
1386
|
+
} finally {
|
|
1387
|
+
clearTimeout(timeout);
|
|
1388
|
+
stopWaiting();
|
|
1389
|
+
options.signal?.removeEventListener("abort", abortFromCaller);
|
|
1390
|
+
work.catch(() => {});
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
async function lookupHops(initialUrl, options, openHop, signal) {
|
|
1394
|
+
let url = initialUrl;
|
|
1341
1395
|
let redirected = 0;
|
|
1342
1396
|
while (true) {
|
|
1397
|
+
if (signal.aborted) return null;
|
|
1343
1398
|
logger.debug("Fetching WebFinger resource descriptor from {url}...", { url: url.href });
|
|
1344
|
-
const session = await openSession(url, openHop,
|
|
1399
|
+
const session = await openSession(url, openHop, signal);
|
|
1345
1400
|
if (session == null) return null;
|
|
1346
1401
|
let hop;
|
|
1347
1402
|
try {
|
|
1348
|
-
|
|
1403
|
+
if (signal.aborted) return null;
|
|
1404
|
+
hop = await fetchHopUntilAbort(session, url, options, signal);
|
|
1349
1405
|
} finally {
|
|
1350
|
-
|
|
1406
|
+
if (signal.aborted) session.close().catch((error) => {
|
|
1407
|
+
logger.debug("Failed to close WebFinger session for {url}: {error}", {
|
|
1408
|
+
url: url.href,
|
|
1409
|
+
error
|
|
1410
|
+
});
|
|
1411
|
+
});
|
|
1412
|
+
else await session.close();
|
|
1351
1413
|
}
|
|
1414
|
+
if (signal.aborted) return null;
|
|
1352
1415
|
if ("result" in hop) return hop.result;
|
|
1353
1416
|
redirected++;
|
|
1354
1417
|
const maxRedirection = options.maxRedirection ?? DEFAULT_MAX_REDIRECTION;
|
|
@@ -1367,13 +1430,31 @@ async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
|
1367
1430
|
url = redirectedUrl;
|
|
1368
1431
|
}
|
|
1369
1432
|
}
|
|
1433
|
+
/** Lets the session close even when a custom fetch or body ignores abort. */
|
|
1434
|
+
async function fetchHopUntilAbort(session, url, options, signal) {
|
|
1435
|
+
const fetching = fetchHop(session, url, options, signal);
|
|
1436
|
+
let onAbort;
|
|
1437
|
+
const aborted = new Promise((resolve) => {
|
|
1438
|
+
if (signal.aborted) resolve({ result: null });
|
|
1439
|
+
else {
|
|
1440
|
+
onAbort = () => resolve({ result: null });
|
|
1441
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1442
|
+
}
|
|
1443
|
+
});
|
|
1444
|
+
try {
|
|
1445
|
+
return await Promise.race([fetching, aborted]);
|
|
1446
|
+
} finally {
|
|
1447
|
+
if (onAbort != null) signal.removeEventListener("abort", onAbort);
|
|
1448
|
+
fetching.catch(() => {});
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1370
1451
|
/**
|
|
1371
1452
|
* Opens a fetch session for a hop. A disallowed URL, a failed DNS lookup,
|
|
1372
1453
|
* or an aborted lookup yields `null` instead of an error.
|
|
1373
1454
|
*/
|
|
1374
1455
|
async function openSession(url, openHop, signal) {
|
|
1375
1456
|
try {
|
|
1376
|
-
return await openHop(url.href);
|
|
1457
|
+
return await openHop(url.href, signal);
|
|
1377
1458
|
} catch (e) {
|
|
1378
1459
|
if (e instanceof _fedify_vocab_runtime.UrlError) {
|
|
1379
1460
|
if (e.reason === "dns") logger.debug("DNS lookup failed for {url}", {
|
|
@@ -1397,7 +1478,7 @@ async function openSession(url, openHop, signal) {
|
|
|
1397
1478
|
* Fetches a hop with the given session, and consumes the response body or
|
|
1398
1479
|
* starts cancelling it before returning. The caller closes the session.
|
|
1399
1480
|
*/
|
|
1400
|
-
async function fetchHop(session, url, options) {
|
|
1481
|
+
async function fetchHop(session, url, options, signal) {
|
|
1401
1482
|
let response;
|
|
1402
1483
|
try {
|
|
1403
1484
|
response = await session.fetch(url, {
|
|
@@ -1406,7 +1487,7 @@ async function fetchHop(session, url, options) {
|
|
|
1406
1487
|
"User-Agent": typeof options.userAgent === "string" ? options.userAgent : (0, _fedify_vocab_runtime.getUserAgent)(options.userAgent)
|
|
1407
1488
|
},
|
|
1408
1489
|
redirect: "manual",
|
|
1409
|
-
signal
|
|
1490
|
+
signal
|
|
1410
1491
|
});
|
|
1411
1492
|
} catch (error) {
|
|
1412
1493
|
logger.debug("Failed to fetch WebFinger resource descriptor: {error}", {
|
|
@@ -1429,7 +1510,7 @@ async function fetchHop(session, url, options) {
|
|
|
1429
1510
|
return { result: null };
|
|
1430
1511
|
}
|
|
1431
1512
|
try {
|
|
1432
|
-
return { result: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url)) };
|
|
1513
|
+
return { result: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url, signal)) };
|
|
1433
1514
|
} catch (e) {
|
|
1434
1515
|
if (e instanceof BodyTooLargeError) return { result: null };
|
|
1435
1516
|
if (e instanceof SyntaxError) {
|
|
@@ -1984,4 +2065,144 @@ function neverCancellableResponse(init) {
|
|
|
1984
2065
|
esm_default.hardReset();
|
|
1985
2066
|
}
|
|
1986
2067
|
});
|
|
2068
|
+
(0, _fedify_fixture.test)("lookupWebFinger() caller abort releases a stalled session open", async () => {
|
|
2069
|
+
const controller = new AbortController();
|
|
2070
|
+
let finishOpen;
|
|
2071
|
+
let closed = false;
|
|
2072
|
+
const opening = new Promise((resolve) => {
|
|
2073
|
+
finishOpen = resolve;
|
|
2074
|
+
});
|
|
2075
|
+
const lookup = lookupWebFingerInternal("acct:alice@example.com", { signal: controller.signal }, { openHop: () => opening });
|
|
2076
|
+
controller.abort();
|
|
2077
|
+
(0, node_assert_strict.deepStrictEqual)(await settleWithin(lookup, 1e3), null);
|
|
2078
|
+
finishOpen({
|
|
2079
|
+
url: new URL("https://example.com"),
|
|
2080
|
+
fetch: () => {
|
|
2081
|
+
throw new Error("fetch after abort");
|
|
2082
|
+
},
|
|
2083
|
+
close: () => {
|
|
2084
|
+
closed = true;
|
|
2085
|
+
return Promise.resolve();
|
|
2086
|
+
}
|
|
2087
|
+
});
|
|
2088
|
+
await Promise.resolve();
|
|
2089
|
+
await Promise.resolve();
|
|
2090
|
+
(0, node_assert_strict.ok)(closed);
|
|
2091
|
+
});
|
|
2092
|
+
(0, _fedify_fixture.test)("lookupWebFinger() deadline covers session opening", async () => {
|
|
2093
|
+
(0, node_assert_strict.deepStrictEqual)(await settleWithin(lookupWebFingerInternal("acct:alice@example.com", {}, {
|
|
2094
|
+
timeoutMs: 20,
|
|
2095
|
+
openHop: () => new Promise(() => {})
|
|
2096
|
+
}), 1e3), null);
|
|
2097
|
+
});
|
|
2098
|
+
(0, _fedify_fixture.test)("lookupWebFinger() shares one deadline across redirects", async () => {
|
|
2099
|
+
const descriptor = {
|
|
2100
|
+
subject: "acct:alice@example.com",
|
|
2101
|
+
links: []
|
|
2102
|
+
};
|
|
2103
|
+
const pending = [];
|
|
2104
|
+
(0, node_assert_strict.deepStrictEqual)(await settleWithin(lookupWebFingerInternal("acct:alice@example.com", {}, {
|
|
2105
|
+
timeoutMs: 100,
|
|
2106
|
+
openHop: (url) => Promise.resolve({
|
|
2107
|
+
url: new URL(url),
|
|
2108
|
+
fetch: () => {
|
|
2109
|
+
const response = new Promise((resolve) => {
|
|
2110
|
+
setTimeout(() => {
|
|
2111
|
+
resolve(url.includes("/final") ? new Response(JSON.stringify(descriptor)) : new Response(null, {
|
|
2112
|
+
status: 302,
|
|
2113
|
+
headers: { Location: "/final" }
|
|
2114
|
+
}));
|
|
2115
|
+
}, 60);
|
|
2116
|
+
});
|
|
2117
|
+
pending.push(response);
|
|
2118
|
+
return response;
|
|
2119
|
+
},
|
|
2120
|
+
close: () => Promise.resolve()
|
|
2121
|
+
})
|
|
2122
|
+
}), 1e3), null);
|
|
2123
|
+
await Promise.all(pending);
|
|
2124
|
+
});
|
|
2125
|
+
(0, _fedify_fixture.test)("lookupWebFinger() caller signal cannot extend its deadline", async () => {
|
|
2126
|
+
const signal = AbortSignal.timeout(5e3);
|
|
2127
|
+
let reason;
|
|
2128
|
+
(0, node_assert_strict.deepStrictEqual)(await settleWithin(lookupWebFingerInternal("acct:alice@example.com", { signal }, {
|
|
2129
|
+
timeoutMs: 50,
|
|
2130
|
+
openHop: (_url, effectiveSignal) => {
|
|
2131
|
+
effectiveSignal?.addEventListener("abort", () => {
|
|
2132
|
+
reason = effectiveSignal.reason;
|
|
2133
|
+
}, { once: true });
|
|
2134
|
+
return new Promise(() => {});
|
|
2135
|
+
}
|
|
2136
|
+
}), 1e3), null);
|
|
2137
|
+
(0, node_assert_strict.ok)(!signal.aborted);
|
|
2138
|
+
(0, node_assert_strict.ok)(reason instanceof DOMException);
|
|
2139
|
+
(0, node_assert_strict.deepStrictEqual)(reason.name, "TimeoutError");
|
|
2140
|
+
});
|
|
2141
|
+
(0, _fedify_fixture.test)("lookupWebFinger() deadline covers response headers", async () => {
|
|
2142
|
+
let closed = false;
|
|
2143
|
+
let fetchSignal;
|
|
2144
|
+
let fetchAborted = false;
|
|
2145
|
+
(0, node_assert_strict.deepStrictEqual)(await settleWithin(lookupWebFingerInternal("acct:alice@example.com", {}, {
|
|
2146
|
+
timeoutMs: 20,
|
|
2147
|
+
openHop: () => Promise.resolve({
|
|
2148
|
+
url: new URL("https://example.com"),
|
|
2149
|
+
fetch: (_input, init) => {
|
|
2150
|
+
fetchSignal = init?.signal;
|
|
2151
|
+
fetchSignal?.addEventListener("abort", () => {
|
|
2152
|
+
fetchAborted = true;
|
|
2153
|
+
}, { once: true });
|
|
2154
|
+
return new Promise(() => {});
|
|
2155
|
+
},
|
|
2156
|
+
close: () => {
|
|
2157
|
+
closed = true;
|
|
2158
|
+
return Promise.resolve();
|
|
2159
|
+
}
|
|
2160
|
+
})
|
|
2161
|
+
}), 1e3), null);
|
|
2162
|
+
(0, node_assert_strict.ok)(fetchSignal?.aborted);
|
|
2163
|
+
(0, node_assert_strict.ok)(fetchAborted);
|
|
2164
|
+
(0, node_assert_strict.deepStrictEqual)(fetchSignal.reason.name, "TimeoutError");
|
|
2165
|
+
await Promise.resolve();
|
|
2166
|
+
(0, node_assert_strict.ok)(closed);
|
|
2167
|
+
});
|
|
2168
|
+
(0, _fedify_fixture.test)("lookupWebFinger() discards completed JSON when body stalls", async () => {
|
|
2169
|
+
const descriptor = {
|
|
2170
|
+
subject: "acct:alice@example.com",
|
|
2171
|
+
links: []
|
|
2172
|
+
};
|
|
2173
|
+
let cancelled;
|
|
2174
|
+
const cancellation = new Promise((resolve) => {
|
|
2175
|
+
cancelled = resolve;
|
|
2176
|
+
});
|
|
2177
|
+
(0, node_assert_strict.deepStrictEqual)(await settleWithin(lookupWebFingerInternal("acct:alice@example.com", {}, {
|
|
2178
|
+
timeoutMs: 20,
|
|
2179
|
+
openHop: () => Promise.resolve({
|
|
2180
|
+
url: new URL("https://example.com"),
|
|
2181
|
+
fetch: () => Promise.resolve(new Response(new ReadableStream({
|
|
2182
|
+
start(controller) {
|
|
2183
|
+
controller.enqueue(new TextEncoder().encode(JSON.stringify(descriptor)));
|
|
2184
|
+
},
|
|
2185
|
+
cancel() {
|
|
2186
|
+
cancelled();
|
|
2187
|
+
}
|
|
2188
|
+
}))),
|
|
2189
|
+
close: async () => {}
|
|
2190
|
+
})
|
|
2191
|
+
}), 1e3), null);
|
|
2192
|
+
await settleWithin(cancellation, 1e3);
|
|
2193
|
+
});
|
|
2194
|
+
(0, _fedify_fixture.test)("lookupWebFinger() accepts a response before its deadline", async () => {
|
|
2195
|
+
const descriptor = {
|
|
2196
|
+
subject: "acct:alice@example.com",
|
|
2197
|
+
links: []
|
|
2198
|
+
};
|
|
2199
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupWebFingerInternal("acct:alice@example.com", {}, {
|
|
2200
|
+
timeoutMs: 1e3,
|
|
2201
|
+
openHop: () => Promise.resolve({
|
|
2202
|
+
url: new URL("https://example.com"),
|
|
2203
|
+
fetch: () => Promise.resolve(new Response(JSON.stringify(descriptor))),
|
|
2204
|
+
close: async () => {}
|
|
2205
|
+
})
|
|
2206
|
+
}), descriptor);
|
|
2207
|
+
});
|
|
1987
2208
|
//#endregion
|