@fedify/webfinger 2.2.19 → 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 +661 -109
- package/dist/lookup.test.js +662 -109
- package/dist/mod.cjs +224 -84
- package/dist/mod.d.cts +2 -1
- package/dist/mod.d.ts +3 -2
- package/dist/mod.js +225 -85
- package/package.json +3 -3
- package/src/body.ts +11 -0
- package/src/lookup-impl.ts +343 -0
- package/src/lookup.test.ts +644 -36
- package/src/lookup.ts +4 -146
package/dist/lookup.test.cjs
CHANGED
|
@@ -23,11 +23,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
23
|
//#endregion
|
|
24
24
|
let _fedify_fixture = require("@fedify/fixture");
|
|
25
25
|
let _fedify_vocab_runtime = require("@fedify/vocab-runtime");
|
|
26
|
+
let _fedify_vocab_runtime_internal = require("@fedify/vocab-runtime/internal");
|
|
26
27
|
let _logtape_logtape = require("@logtape/logtape");
|
|
27
28
|
let es_toolkit = require("es-toolkit");
|
|
28
29
|
let node_assert_strict = require("node:assert/strict");
|
|
29
|
-
let node_dns_promises = require("node:dns/promises");
|
|
30
|
-
node_dns_promises = __toESM(node_dns_promises);
|
|
31
30
|
let _opentelemetry_api = require("@opentelemetry/api");
|
|
32
31
|
//#endregion
|
|
33
32
|
//#region ../../node_modules/.pnpm/regexparam@3.0.0/node_modules/regexparam/dist/index.mjs
|
|
@@ -1230,7 +1229,7 @@ var esm_default = new class FetchMock {
|
|
|
1230
1229
|
//#endregion
|
|
1231
1230
|
//#region deno.json
|
|
1232
1231
|
var name = "@fedify/webfinger";
|
|
1233
|
-
var version = "2.2.
|
|
1232
|
+
var version = "2.2.21";
|
|
1234
1233
|
//#endregion
|
|
1235
1234
|
//#region src/body.ts
|
|
1236
1235
|
/** Decoded JSON body limit: 16 MiB. @internal */
|
|
@@ -1253,13 +1252,18 @@ function validateBodySizeLimit(maxBytes) {
|
|
|
1253
1252
|
* @param message The response or request to read.
|
|
1254
1253
|
* @param maxBytes The maximum number of decoded bytes to read.
|
|
1255
1254
|
* @param url The URL to include in errors and logs.
|
|
1255
|
+
* @param signal The signal that cancels an in-progress body read.
|
|
1256
1256
|
* @returns The decoded body text.
|
|
1257
1257
|
* @throws {BodyTooLargeError} If the body exceeds the limit.
|
|
1258
1258
|
* @internal
|
|
1259
1259
|
*/
|
|
1260
|
-
async function readBoundedText(message, maxBytes, url) {
|
|
1260
|
+
async function readBoundedText(message, maxBytes, url, signal) {
|
|
1261
1261
|
validateBodySizeLimit(maxBytes);
|
|
1262
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 });
|
|
1263
1267
|
const tooLarge = () => {
|
|
1264
1268
|
(0, _logtape_logtape.getLogger)([
|
|
1265
1269
|
"fedify",
|
|
@@ -1272,6 +1276,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
1272
1276
|
throw new BodyTooLargeError(url, maxBytes);
|
|
1273
1277
|
};
|
|
1274
1278
|
try {
|
|
1279
|
+
signal?.throwIfAborted();
|
|
1275
1280
|
const length = message.headers.get("Content-Length");
|
|
1276
1281
|
const encoding = message.headers.get("Content-Encoding");
|
|
1277
1282
|
if ((encoding == null || encoding.toLowerCase() === "identity") && length != null && /^\d+$/.test(length) && Number(length) > maxBytes) tooLarge();
|
|
@@ -1279,6 +1284,7 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
1279
1284
|
let size = 0;
|
|
1280
1285
|
const textReader = new ReadableStream({ async pull(controller) {
|
|
1281
1286
|
const { done, value } = await reader.read();
|
|
1287
|
+
signal?.throwIfAborted();
|
|
1282
1288
|
if (done) {
|
|
1283
1289
|
controller.close();
|
|
1284
1290
|
return;
|
|
@@ -1291,9 +1297,11 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
1291
1297
|
const chunks = [];
|
|
1292
1298
|
while (true) {
|
|
1293
1299
|
const { done, value } = await textReader.read();
|
|
1300
|
+
signal?.throwIfAborted();
|
|
1294
1301
|
if (done) break;
|
|
1295
1302
|
chunks.push(value);
|
|
1296
1303
|
}
|
|
1304
|
+
signal?.throwIfAborted();
|
|
1297
1305
|
return chunks.join("");
|
|
1298
1306
|
} finally {
|
|
1299
1307
|
textReader.releaseLock();
|
|
@@ -1302,48 +1310,30 @@ async function readBoundedText(message, maxBytes, url) {
|
|
|
1302
1310
|
if (reader != null) reader.cancel(error).catch(() => {});
|
|
1303
1311
|
throw error;
|
|
1304
1312
|
} finally {
|
|
1313
|
+
signal?.removeEventListener("abort", cancelOnAbort);
|
|
1305
1314
|
reader?.releaseLock();
|
|
1306
1315
|
}
|
|
1307
1316
|
}
|
|
1308
1317
|
//#endregion
|
|
1309
|
-
//#region src/lookup.ts
|
|
1318
|
+
//#region src/lookup-impl.ts
|
|
1310
1319
|
const logger = (0, _logtape_logtape.getLogger)([
|
|
1311
1320
|
"fedify",
|
|
1312
1321
|
"webfinger",
|
|
1313
1322
|
"lookup"
|
|
1314
1323
|
]);
|
|
1315
1324
|
const DEFAULT_MAX_REDIRECTION = 5;
|
|
1325
|
+
const DEFAULT_TIMEOUT_MS = 1e4;
|
|
1316
1326
|
/**
|
|
1317
|
-
* Looks up a WebFinger resource.
|
|
1327
|
+
* Looks up a WebFinger resource, opening a fetch session per hop. This is
|
|
1328
|
+
* the implementation of {@link lookupWebFinger}, which is not exported from
|
|
1329
|
+
* the package entry point.
|
|
1318
1330
|
* @param resource The resource URL to look up.
|
|
1319
1331
|
* @param options Extra options for looking up the resource.
|
|
1332
|
+
* @param deps The dependencies.
|
|
1320
1333
|
* @returns The resource descriptor, or `null` if not found.
|
|
1321
|
-
* @
|
|
1334
|
+
* @internal
|
|
1322
1335
|
*/
|
|
1323
|
-
async function
|
|
1324
|
-
return await (options.tracerProvider ?? _opentelemetry_api.trace.getTracerProvider()).getTracer(name, version).startActiveSpan("webfinger.lookup", {
|
|
1325
|
-
kind: _opentelemetry_api.SpanKind.CLIENT,
|
|
1326
|
-
attributes: {
|
|
1327
|
-
"webfinger.resource": resource.toString(),
|
|
1328
|
-
"webfinger.resource.scheme": typeof resource === "string" ? resource.replace(/:.*$/, "") : resource.protocol.replace(/:$/, "")
|
|
1329
|
-
}
|
|
1330
|
-
}, async (span) => {
|
|
1331
|
-
try {
|
|
1332
|
-
const result = await lookupWebFingerInternal(resource, options);
|
|
1333
|
-
span.setStatus({ code: result === null ? _opentelemetry_api.SpanStatusCode.ERROR : _opentelemetry_api.SpanStatusCode.OK });
|
|
1334
|
-
return result;
|
|
1335
|
-
} catch (error) {
|
|
1336
|
-
span.setStatus({
|
|
1337
|
-
code: _opentelemetry_api.SpanStatusCode.ERROR,
|
|
1338
|
-
message: String(error)
|
|
1339
|
-
});
|
|
1340
|
-
throw error;
|
|
1341
|
-
} finally {
|
|
1342
|
-
span.end();
|
|
1343
|
-
}
|
|
1344
|
-
});
|
|
1345
|
-
}
|
|
1346
|
-
async function lookupWebFingerInternal(resource, options = {}) {
|
|
1336
|
+
async function lookupWebFingerInternal(resource, options = {}, deps = {}) {
|
|
1347
1337
|
if (typeof resource === "string") resource = new URL(resource);
|
|
1348
1338
|
let protocol = "https:";
|
|
1349
1339
|
let server;
|
|
@@ -1356,86 +1346,247 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1356
1346
|
protocol = resource.protocol;
|
|
1357
1347
|
server = resource.host;
|
|
1358
1348
|
}
|
|
1359
|
-
|
|
1349
|
+
const url = new URL(`${protocol}//${server}/.well-known/webfinger`);
|
|
1360
1350
|
url.searchParams.set("resource", resource.href);
|
|
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;
|
|
1361
1395
|
let redirected = 0;
|
|
1362
1396
|
while (true) {
|
|
1397
|
+
if (signal.aborted) return null;
|
|
1363
1398
|
logger.debug("Fetching WebFinger resource descriptor from {url}...", { url: url.href });
|
|
1364
|
-
|
|
1365
|
-
if (
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
if (
|
|
1369
|
-
|
|
1399
|
+
const session = await openSession(url, openHop, signal);
|
|
1400
|
+
if (session == null) return null;
|
|
1401
|
+
let hop;
|
|
1402
|
+
try {
|
|
1403
|
+
if (signal.aborted) return null;
|
|
1404
|
+
hop = await fetchHopUntilAbort(session, url, options, signal);
|
|
1405
|
+
} finally {
|
|
1406
|
+
if (signal.aborted) session.close().catch((error) => {
|
|
1407
|
+
logger.debug("Failed to close WebFinger session for {url}: {error}", {
|
|
1370
1408
|
url: url.href,
|
|
1371
|
-
error
|
|
1409
|
+
error
|
|
1372
1410
|
});
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
}
|
|
1376
|
-
throw e;
|
|
1411
|
+
});
|
|
1412
|
+
else await session.close();
|
|
1377
1413
|
}
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1414
|
+
if (signal.aborted) return null;
|
|
1415
|
+
if ("result" in hop) return hop.result;
|
|
1416
|
+
redirected++;
|
|
1417
|
+
const maxRedirection = options.maxRedirection ?? DEFAULT_MAX_REDIRECTION;
|
|
1418
|
+
if (redirected >= maxRedirection) {
|
|
1419
|
+
logger.error("Too many redirections ({redirections}) while fetching WebFinger resource descriptor.", { redirections: redirected });
|
|
1420
|
+
return null;
|
|
1421
|
+
}
|
|
1422
|
+
const redirectedUrl = new URL(hop.redirect, url);
|
|
1423
|
+
if (redirectedUrl.protocol !== url.protocol) {
|
|
1424
|
+
logger.error("Redirected to a different protocol ({protocol} to {redirectedProtocol}) while fetching WebFinger resource descriptor.", {
|
|
1425
|
+
protocol: url.protocol,
|
|
1426
|
+
redirectedProtocol: redirectedUrl.protocol
|
|
1386
1427
|
});
|
|
1387
|
-
|
|
1388
|
-
|
|
1428
|
+
return null;
|
|
1429
|
+
}
|
|
1430
|
+
url = redirectedUrl;
|
|
1431
|
+
}
|
|
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
|
+
}
|
|
1451
|
+
/**
|
|
1452
|
+
* Opens a fetch session for a hop. A disallowed URL, a failed DNS lookup,
|
|
1453
|
+
* or an aborted lookup yields `null` instead of an error.
|
|
1454
|
+
*/
|
|
1455
|
+
async function openSession(url, openHop, signal) {
|
|
1456
|
+
try {
|
|
1457
|
+
return await openHop(url.href, signal);
|
|
1458
|
+
} catch (e) {
|
|
1459
|
+
if (e instanceof _fedify_vocab_runtime.UrlError) {
|
|
1460
|
+
if (e.reason === "dns") logger.debug("DNS lookup failed for {url}", {
|
|
1389
1461
|
url: url.href,
|
|
1390
|
-
error
|
|
1462
|
+
error: e
|
|
1391
1463
|
});
|
|
1464
|
+
else logger.error("Invalid URL for WebFinger resource descriptor: {error}", { error: e });
|
|
1392
1465
|
return null;
|
|
1393
1466
|
}
|
|
1394
|
-
if (
|
|
1395
|
-
|
|
1396
|
-
const maxRedirection = options.maxRedirection ?? DEFAULT_MAX_REDIRECTION;
|
|
1397
|
-
if (redirected >= maxRedirection) {
|
|
1398
|
-
logger.error("Too many redirections ({redirections}) while fetching WebFinger resource descriptor.", { redirections: redirected });
|
|
1399
|
-
return null;
|
|
1400
|
-
}
|
|
1401
|
-
const redirectedUrl = new URL(response.headers.get("Location"), response.url == null || response.url === "" ? url : response.url);
|
|
1402
|
-
if (redirectedUrl.protocol !== url.protocol) {
|
|
1403
|
-
logger.error("Redirected to a different protocol ({protocol} to {redirectedProtocol}) while fetching WebFinger resource descriptor.", {
|
|
1404
|
-
protocol: url.protocol,
|
|
1405
|
-
redirectedProtocol: redirectedUrl.protocol
|
|
1406
|
-
});
|
|
1407
|
-
return null;
|
|
1408
|
-
}
|
|
1409
|
-
url = redirectedUrl;
|
|
1410
|
-
continue;
|
|
1411
|
-
}
|
|
1412
|
-
if (!response.ok) {
|
|
1413
|
-
logger.debug("Failed to fetch WebFinger resource descriptor: {status} {statusText}.", {
|
|
1467
|
+
if (signal?.aborted) {
|
|
1468
|
+
logger.debug("Failed to fetch WebFinger resource descriptor: {error}", {
|
|
1414
1469
|
url: url.href,
|
|
1415
|
-
|
|
1416
|
-
statusText: response.statusText
|
|
1470
|
+
error: e
|
|
1417
1471
|
});
|
|
1418
1472
|
return null;
|
|
1419
1473
|
}
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1474
|
+
throw e;
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
/**
|
|
1478
|
+
* Fetches a hop with the given session, and consumes the response body or
|
|
1479
|
+
* starts cancelling it before returning. The caller closes the session.
|
|
1480
|
+
*/
|
|
1481
|
+
async function fetchHop(session, url, options, signal) {
|
|
1482
|
+
let response;
|
|
1483
|
+
try {
|
|
1484
|
+
response = await session.fetch(url, {
|
|
1485
|
+
headers: {
|
|
1486
|
+
Accept: "application/jrd+json",
|
|
1487
|
+
"User-Agent": typeof options.userAgent === "string" ? options.userAgent : (0, _fedify_vocab_runtime.getUserAgent)(options.userAgent)
|
|
1488
|
+
},
|
|
1489
|
+
redirect: "manual",
|
|
1490
|
+
signal
|
|
1491
|
+
});
|
|
1492
|
+
} catch (error) {
|
|
1493
|
+
logger.debug("Failed to fetch WebFinger resource descriptor: {error}", {
|
|
1494
|
+
url: url.href,
|
|
1495
|
+
error
|
|
1496
|
+
});
|
|
1497
|
+
return { result: null };
|
|
1498
|
+
}
|
|
1499
|
+
if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
|
|
1500
|
+
cancelBody(response, url);
|
|
1501
|
+
return { redirect: response.headers.get("Location") };
|
|
1502
|
+
}
|
|
1503
|
+
if (!response.ok) {
|
|
1504
|
+
logger.debug("Failed to fetch WebFinger resource descriptor: {status} {statusText}.", {
|
|
1505
|
+
url: url.href,
|
|
1506
|
+
status: response.status,
|
|
1507
|
+
statusText: response.statusText
|
|
1508
|
+
});
|
|
1509
|
+
cancelBody(response, url);
|
|
1510
|
+
return { result: null };
|
|
1511
|
+
}
|
|
1512
|
+
try {
|
|
1513
|
+
return { result: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, url, signal)) };
|
|
1514
|
+
} catch (e) {
|
|
1515
|
+
if (e instanceof BodyTooLargeError) return { result: null };
|
|
1516
|
+
if (e instanceof SyntaxError) {
|
|
1517
|
+
logger.debug("Failed to parse WebFinger resource descriptor as JSON: {error}", { error: e });
|
|
1518
|
+
return { result: null };
|
|
1429
1519
|
}
|
|
1520
|
+
throw e;
|
|
1430
1521
|
}
|
|
1431
1522
|
}
|
|
1523
|
+
/**
|
|
1524
|
+
* Starts cancelling a response body that is not read, without waiting for it
|
|
1525
|
+
* to settle. The cancellation of a branch of a tee (e.g., a body cloned by
|
|
1526
|
+
* a fetch mock) settles only after the other branch is cancelled too, per
|
|
1527
|
+
* the Streams standard, so awaiting it could block forever. A failure is
|
|
1528
|
+
* only logged.
|
|
1529
|
+
*/
|
|
1530
|
+
function cancelBody(response, url) {
|
|
1531
|
+
response.body?.cancel().catch((error) => {
|
|
1532
|
+
logger.debug("Failed to cancel the response body from {url}: {error}", {
|
|
1533
|
+
url: url.href,
|
|
1534
|
+
error
|
|
1535
|
+
});
|
|
1536
|
+
});
|
|
1537
|
+
}
|
|
1538
|
+
//#endregion
|
|
1539
|
+
//#region src/lookup.ts
|
|
1540
|
+
/**
|
|
1541
|
+
* Looks up a WebFinger resource.
|
|
1542
|
+
* @param resource The resource URL to look up.
|
|
1543
|
+
* @param options Extra options for looking up the resource.
|
|
1544
|
+
* @returns The resource descriptor, or `null` if not found.
|
|
1545
|
+
* @since 0.2.0
|
|
1546
|
+
*/
|
|
1547
|
+
async function lookupWebFinger(resource, options = {}) {
|
|
1548
|
+
return await (options.tracerProvider ?? _opentelemetry_api.trace.getTracerProvider()).getTracer(name, version).startActiveSpan("webfinger.lookup", {
|
|
1549
|
+
kind: _opentelemetry_api.SpanKind.CLIENT,
|
|
1550
|
+
attributes: {
|
|
1551
|
+
"webfinger.resource": resource.toString(),
|
|
1552
|
+
"webfinger.resource.scheme": typeof resource === "string" ? resource.replace(/:.*$/, "") : resource.protocol.replace(/:$/, "")
|
|
1553
|
+
}
|
|
1554
|
+
}, async (span) => {
|
|
1555
|
+
try {
|
|
1556
|
+
const result = await lookupWebFingerInternal(resource, options);
|
|
1557
|
+
span.setStatus({ code: result === null ? _opentelemetry_api.SpanStatusCode.ERROR : _opentelemetry_api.SpanStatusCode.OK });
|
|
1558
|
+
return result;
|
|
1559
|
+
} catch (error) {
|
|
1560
|
+
span.setStatus({
|
|
1561
|
+
code: _opentelemetry_api.SpanStatusCode.ERROR,
|
|
1562
|
+
message: String(error)
|
|
1563
|
+
});
|
|
1564
|
+
throw error;
|
|
1565
|
+
} finally {
|
|
1566
|
+
span.end();
|
|
1567
|
+
}
|
|
1568
|
+
});
|
|
1569
|
+
}
|
|
1432
1570
|
//#endregion
|
|
1433
1571
|
//#region src/lookup.test.ts
|
|
1572
|
+
function lookupDirect(resource, options) {
|
|
1573
|
+
return lookupWebFingerInternal(resource, options, { openHop: _fedify_vocab_runtime_internal.openDirectFetch });
|
|
1574
|
+
}
|
|
1575
|
+
function lookupResolving(addresses) {
|
|
1576
|
+
return () => Promise.resolve(addresses);
|
|
1577
|
+
}
|
|
1578
|
+
function openExampleDirectly(url) {
|
|
1579
|
+
return new URL(url).hostname === "example.com" ? (0, _fedify_vocab_runtime_internal.openDirectFetch)(url) : (0, _fedify_vocab_runtime_internal.openPublicFetch)(url, { lookup: lookupResolving([{
|
|
1580
|
+
address: "93.184.215.14",
|
|
1581
|
+
family: 4
|
|
1582
|
+
}]) });
|
|
1583
|
+
}
|
|
1434
1584
|
(0, _fedify_fixture.test)({
|
|
1435
1585
|
name: "lookupWebFinger()",
|
|
1436
1586
|
sanitizeOps: false,
|
|
1437
1587
|
sanitizeResources: false,
|
|
1438
1588
|
async fn(t) {
|
|
1589
|
+
const responseTimers = [];
|
|
1439
1590
|
await t.step("invalid resource", async () => {
|
|
1440
1591
|
(0, node_assert_strict.deepStrictEqual)(await lookupWebFinger("acct:johndoe"), null);
|
|
1441
1592
|
(0, node_assert_strict.deepStrictEqual)(await lookupWebFinger(new URL("acct:johndoe")), null);
|
|
@@ -1446,11 +1597,32 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1446
1597
|
(0, node_assert_strict.deepStrictEqual)(await lookupWebFinger("acct:johndoe@fedify-test.internal"), null);
|
|
1447
1598
|
(0, node_assert_strict.deepStrictEqual)(await lookupWebFinger("https://fedify-test.internal/foo"), null);
|
|
1448
1599
|
});
|
|
1600
|
+
await t.step("fedify-test.internal resolving to a private address", async () => {
|
|
1601
|
+
esm_default.mockGlobal();
|
|
1602
|
+
esm_default.catch(500);
|
|
1603
|
+
try {
|
|
1604
|
+
const hosts = [];
|
|
1605
|
+
const openHop = (url) => (0, _fedify_vocab_runtime_internal.openPublicFetch)(url, { lookup: (hostname) => {
|
|
1606
|
+
hosts.push(hostname);
|
|
1607
|
+
return Promise.resolve([{
|
|
1608
|
+
address: "10.0.0.1",
|
|
1609
|
+
family: 4
|
|
1610
|
+
}]);
|
|
1611
|
+
} });
|
|
1612
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupWebFingerInternal("acct:johndoe@fedify-test.internal", {}, { openHop }), null);
|
|
1613
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupWebFingerInternal("https://fedify-test.internal/foo", {}, { openHop }), null);
|
|
1614
|
+
(0, node_assert_strict.deepStrictEqual)(hosts, ["fedify-test.internal", "fedify-test.internal"]);
|
|
1615
|
+
(0, node_assert_strict.deepStrictEqual)(esm_default.callHistory.calls().length, 0);
|
|
1616
|
+
} finally {
|
|
1617
|
+
for (const timer of responseTimers) clearTimeout(timer);
|
|
1618
|
+
esm_default.hardReset();
|
|
1619
|
+
}
|
|
1620
|
+
});
|
|
1449
1621
|
esm_default.spyGlobal();
|
|
1450
1622
|
esm_default.get("begin:https://example.com/.well-known/webfinger?", { status: 404 });
|
|
1451
1623
|
await t.step("not found", async () => {
|
|
1452
|
-
(0, node_assert_strict.deepStrictEqual)(await
|
|
1453
|
-
(0, node_assert_strict.deepStrictEqual)(await
|
|
1624
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupDirect("acct:johndoe@example.com"), null);
|
|
1625
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupDirect("https://example.com/foo"), null);
|
|
1454
1626
|
});
|
|
1455
1627
|
const expected = {
|
|
1456
1628
|
subject: "acct:johndoe@example.com",
|
|
@@ -1459,7 +1631,7 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1459
1631
|
esm_default.removeRoutes();
|
|
1460
1632
|
esm_default.get("https://example.com/.well-known/webfinger?resource=acct%3Ajohndoe%40example.com", { body: expected });
|
|
1461
1633
|
await t.step("acct", async () => {
|
|
1462
|
-
(0, node_assert_strict.deepStrictEqual)(await
|
|
1634
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupDirect("acct:johndoe@example.com"), expected);
|
|
1463
1635
|
});
|
|
1464
1636
|
const expected2 = {
|
|
1465
1637
|
subject: "https://example.com/foo",
|
|
@@ -1468,12 +1640,12 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1468
1640
|
esm_default.removeRoutes();
|
|
1469
1641
|
esm_default.get("https://example.com/.well-known/webfinger?resource=https%3A%2F%2Fexample.com%2Ffoo", { body: expected2 });
|
|
1470
1642
|
await t.step("https", async () => {
|
|
1471
|
-
(0, node_assert_strict.deepStrictEqual)(await
|
|
1643
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupDirect("https://example.com/foo"), expected2);
|
|
1472
1644
|
});
|
|
1473
1645
|
esm_default.removeRoutes();
|
|
1474
1646
|
esm_default.get("begin:https://example.com/.well-known/webfinger?", { body: "not json" });
|
|
1475
1647
|
await t.step("invalid response", async () => {
|
|
1476
|
-
(0, node_assert_strict.deepStrictEqual)(await
|
|
1648
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupDirect("acct:johndoe@example.com"), null);
|
|
1477
1649
|
});
|
|
1478
1650
|
esm_default.removeRoutes();
|
|
1479
1651
|
esm_default.get("begin:https://localhost/.well-known/webfinger?", {
|
|
@@ -1502,7 +1674,7 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1502
1674
|
});
|
|
1503
1675
|
esm_default.get("begin:https://example.com/.well-known/webfinger2", { body: expected });
|
|
1504
1676
|
await t.step("redirection", async () => {
|
|
1505
|
-
(0, node_assert_strict.deepStrictEqual)(await
|
|
1677
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupDirect("acct:johndoe@example.com"), expected);
|
|
1506
1678
|
});
|
|
1507
1679
|
esm_default.removeRoutes();
|
|
1508
1680
|
esm_default.get("begin:https://example.com/.well-known/webfinger?", {
|
|
@@ -1510,7 +1682,7 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1510
1682
|
headers: { Location: "/.well-known/webfinger" }
|
|
1511
1683
|
});
|
|
1512
1684
|
await t.step("infinite redirection", async () => {
|
|
1513
|
-
(0, node_assert_strict.deepStrictEqual)(await (0, es_toolkit.withTimeout)(() =>
|
|
1685
|
+
(0, node_assert_strict.deepStrictEqual)(await (0, es_toolkit.withTimeout)(() => lookupDirect("acct:johndoe@example.com"), 2e3), null);
|
|
1514
1686
|
});
|
|
1515
1687
|
esm_default.removeRoutes();
|
|
1516
1688
|
esm_default.get("begin:https://example.com/.well-known/webfinger?", {
|
|
@@ -1518,7 +1690,7 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1518
1690
|
headers: { Location: "ftp://example.com/" }
|
|
1519
1691
|
});
|
|
1520
1692
|
await t.step("redirection to different protocol", async () => {
|
|
1521
|
-
(0, node_assert_strict.deepStrictEqual)(await
|
|
1693
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupDirect("acct:johndoe@example.com"), null);
|
|
1522
1694
|
});
|
|
1523
1695
|
esm_default.removeRoutes();
|
|
1524
1696
|
esm_default.get("begin:https://example.com/.well-known/webfinger?", {
|
|
@@ -1526,7 +1698,7 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1526
1698
|
headers: { Location: "https://localhost/" }
|
|
1527
1699
|
});
|
|
1528
1700
|
await t.step("redirection to private address", async () => {
|
|
1529
|
-
(0, node_assert_strict.deepStrictEqual)(await
|
|
1701
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupWebFingerInternal("acct:johndoe@example.com", {}, { openHop: openExampleDirectly }), null);
|
|
1530
1702
|
});
|
|
1531
1703
|
esm_default.removeRoutes();
|
|
1532
1704
|
let redirectCount = 0;
|
|
@@ -1540,22 +1712,22 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1540
1712
|
});
|
|
1541
1713
|
await t.step("custom maxRedirection", async () => {
|
|
1542
1714
|
redirectCount = 0;
|
|
1543
|
-
(0, node_assert_strict.deepStrictEqual)(await
|
|
1715
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupDirect("acct:johndoe@example.com", { maxRedirection: 2 }), null);
|
|
1544
1716
|
redirectCount = 0;
|
|
1545
|
-
(0, node_assert_strict.deepStrictEqual)(await
|
|
1717
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupDirect("acct:johndoe@example.com", { maxRedirection: 3 }), expected);
|
|
1546
1718
|
redirectCount = 0;
|
|
1547
|
-
(0, node_assert_strict.deepStrictEqual)(await
|
|
1719
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupDirect("acct:johndoe@example.com"), expected);
|
|
1548
1720
|
});
|
|
1549
1721
|
esm_default.removeRoutes();
|
|
1550
1722
|
esm_default.get("begin:https://example.com/.well-known/webfinger?", () => new Promise((resolve) => {
|
|
1551
1723
|
const timeoutId = setTimeout(() => {
|
|
1552
1724
|
resolve({ body: expected });
|
|
1553
1725
|
}, 1e3);
|
|
1554
|
-
|
|
1726
|
+
responseTimers.push(timeoutId);
|
|
1555
1727
|
}));
|
|
1556
1728
|
await t.step("request cancellation", async () => {
|
|
1557
1729
|
const controller = new AbortController();
|
|
1558
|
-
const promise =
|
|
1730
|
+
const promise = lookupDirect("acct:johndoe@example.com", { signal: controller.signal });
|
|
1559
1731
|
controller.abort();
|
|
1560
1732
|
(0, node_assert_strict.deepStrictEqual)(await promise, null);
|
|
1561
1733
|
});
|
|
@@ -1571,12 +1743,12 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1571
1743
|
const timeoutId = setTimeout(() => {
|
|
1572
1744
|
resolve({ body: expected });
|
|
1573
1745
|
}, 1e3);
|
|
1574
|
-
|
|
1746
|
+
responseTimers.push(timeoutId);
|
|
1575
1747
|
});
|
|
1576
1748
|
});
|
|
1577
1749
|
await t.step("cancellation during redirection", async () => {
|
|
1578
1750
|
const controller = new AbortController();
|
|
1579
|
-
const promise =
|
|
1751
|
+
const promise = lookupDirect("acct:johndoe@example.com", { signal: controller.signal });
|
|
1580
1752
|
setTimeout(() => controller.abort(), 100);
|
|
1581
1753
|
(0, node_assert_strict.deepStrictEqual)(await promise, null);
|
|
1582
1754
|
});
|
|
@@ -1585,18 +1757,20 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1585
1757
|
const timeoutId = setTimeout(() => {
|
|
1586
1758
|
resolve({ body: expected });
|
|
1587
1759
|
}, 500);
|
|
1588
|
-
|
|
1760
|
+
responseTimers.push(timeoutId);
|
|
1589
1761
|
}));
|
|
1590
1762
|
await t.step("cancellation with immediate abort", async () => {
|
|
1591
1763
|
const controller = new AbortController();
|
|
1592
1764
|
controller.abort();
|
|
1765
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupDirect("acct:johndoe@example.com", { signal: controller.signal }), null);
|
|
1593
1766
|
(0, node_assert_strict.deepStrictEqual)(await lookupWebFinger("acct:johndoe@example.com", { signal: controller.signal }), null);
|
|
1594
1767
|
});
|
|
1595
1768
|
esm_default.removeRoutes();
|
|
1596
1769
|
esm_default.get("begin:https://example.com/.well-known/webfinger?", { body: expected });
|
|
1597
1770
|
await t.step("successful request with signal", async () => {
|
|
1598
|
-
(0, node_assert_strict.deepStrictEqual)(await
|
|
1771
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupDirect("acct:johndoe@example.com", { signal: new AbortController().signal }), expected);
|
|
1599
1772
|
});
|
|
1773
|
+
for (const timer of responseTimers) clearTimeout(timer);
|
|
1600
1774
|
esm_default.hardReset();
|
|
1601
1775
|
}
|
|
1602
1776
|
});
|
|
@@ -1616,17 +1790,15 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1616
1790
|
}
|
|
1617
1791
|
});
|
|
1618
1792
|
(0, _fedify_fixture.test)("lookupWebFinger() logs DNS failures as such", async (t) => {
|
|
1619
|
-
if ("Deno" in globalThis && (await Deno.permissions.query({ name: "net" })).state !== "granted") return;
|
|
1620
1793
|
for (const result of [
|
|
1621
1794
|
"throws",
|
|
1622
1795
|
"empty",
|
|
1623
1796
|
"private"
|
|
1624
1797
|
]) await t.step(result, async () => {
|
|
1625
|
-
const
|
|
1626
|
-
node_dns_promises.default.lookup = (() => result === "throws" ? Promise.reject(/* @__PURE__ */ new Error("Resolver unavailable")) : Promise.resolve(result === "empty" ? [] : [{
|
|
1798
|
+
const lookup = () => result === "throws" ? Promise.reject(/* @__PURE__ */ new Error("Resolver unavailable")) : Promise.resolve(result === "empty" ? [] : [{
|
|
1627
1799
|
address: "127.0.0.1",
|
|
1628
1800
|
family: 4
|
|
1629
|
-
}])
|
|
1801
|
+
}]);
|
|
1630
1802
|
const records = [];
|
|
1631
1803
|
await (0, _logtape_logtape.configure)({
|
|
1632
1804
|
sinks: { buffer: (record) => records.push(record) },
|
|
@@ -1641,7 +1813,7 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1641
1813
|
reset: true
|
|
1642
1814
|
});
|
|
1643
1815
|
try {
|
|
1644
|
-
(0, node_assert_strict.deepStrictEqual)(await
|
|
1816
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupWebFingerInternal("acct:alice@dns-failure.invalid", {}, { openHop: (url) => (0, _fedify_vocab_runtime_internal.openPublicFetch)(url, { lookup }) }), null);
|
|
1645
1817
|
const failures = records.filter((r) => r.rawMessage !== "Fetching WebFinger resource descriptor from {url}...");
|
|
1646
1818
|
(0, node_assert_strict.deepStrictEqual)(failures.map((r) => [r.level, r.rawMessage]), [result === "private" ? ["error", "Invalid URL for WebFinger resource descriptor: {error}"] : ["debug", "DNS lookup failed for {url}"]]);
|
|
1647
1819
|
const { error } = failures[0].properties;
|
|
@@ -1649,8 +1821,388 @@ async function lookupWebFingerInternal(resource, options = {}) {
|
|
|
1649
1821
|
(0, node_assert_strict.deepStrictEqual)(error.reason, result === "private" ? "disallowed" : "dns");
|
|
1650
1822
|
} finally {
|
|
1651
1823
|
await (0, _logtape_logtape.reset)();
|
|
1652
|
-
node_dns_promises.default.lookup = originalLookup;
|
|
1653
1824
|
}
|
|
1654
1825
|
});
|
|
1655
1826
|
});
|
|
1827
|
+
function recordHops(respond) {
|
|
1828
|
+
const events = [];
|
|
1829
|
+
const unsettled = [];
|
|
1830
|
+
let opens = 0;
|
|
1831
|
+
let closes = 0;
|
|
1832
|
+
let responses = 0;
|
|
1833
|
+
return {
|
|
1834
|
+
events,
|
|
1835
|
+
async openHop(url) {
|
|
1836
|
+
opens++;
|
|
1837
|
+
events.push(`open ${url}`);
|
|
1838
|
+
const states = [];
|
|
1839
|
+
let closed = false;
|
|
1840
|
+
return {
|
|
1841
|
+
url: new URL(url),
|
|
1842
|
+
async fetch(input) {
|
|
1843
|
+
if (closed) throw new TypeError("The fetch session is closed.");
|
|
1844
|
+
const target = new URL(input instanceof Request ? input.url : input);
|
|
1845
|
+
const id = ++responses;
|
|
1846
|
+
events.push(`fetch ${target.href} #${id}`);
|
|
1847
|
+
const fake = respond(target);
|
|
1848
|
+
const state = {
|
|
1849
|
+
id,
|
|
1850
|
+
settled: false
|
|
1851
|
+
};
|
|
1852
|
+
states.push(state);
|
|
1853
|
+
let sent = false;
|
|
1854
|
+
const body = new ReadableStream({
|
|
1855
|
+
pull(controller) {
|
|
1856
|
+
if (!sent && fake.body != null && fake.body !== "") {
|
|
1857
|
+
sent = true;
|
|
1858
|
+
controller.enqueue(new TextEncoder().encode(fake.body));
|
|
1859
|
+
return;
|
|
1860
|
+
}
|
|
1861
|
+
state.settled = true;
|
|
1862
|
+
events.push(`consumed #${id}`);
|
|
1863
|
+
controller.close();
|
|
1864
|
+
},
|
|
1865
|
+
cancel() {
|
|
1866
|
+
state.settled = true;
|
|
1867
|
+
events.push(`cancel #${id}`);
|
|
1868
|
+
if (fake.cancelFails) throw new Error("Cancellation failed");
|
|
1869
|
+
if (fake.cancelNeverSettles) return new Promise(() => {});
|
|
1870
|
+
}
|
|
1871
|
+
}, { highWaterMark: 0 });
|
|
1872
|
+
const response = new Response(body, {
|
|
1873
|
+
status: fake.status ?? 200,
|
|
1874
|
+
headers: fake.headers
|
|
1875
|
+
});
|
|
1876
|
+
if (fake.url != null) Object.defineProperty(response, "url", { value: fake.url });
|
|
1877
|
+
return response;
|
|
1878
|
+
},
|
|
1879
|
+
async close() {
|
|
1880
|
+
if (closed) return;
|
|
1881
|
+
closed = true;
|
|
1882
|
+
closes++;
|
|
1883
|
+
for (const state of states) if (!state.settled) unsettled.push(`#${state.id}`);
|
|
1884
|
+
events.push(`close ${url}`);
|
|
1885
|
+
}
|
|
1886
|
+
};
|
|
1887
|
+
},
|
|
1888
|
+
assertInvariants() {
|
|
1889
|
+
(0, node_assert_strict.deepStrictEqual)(closes, opens);
|
|
1890
|
+
(0, node_assert_strict.deepStrictEqual)(unsettled, []);
|
|
1891
|
+
}
|
|
1892
|
+
};
|
|
1893
|
+
}
|
|
1894
|
+
(0, _fedify_fixture.test)("lookupWebFinger() closes each hop after cleaning up its body", async (t) => {
|
|
1895
|
+
const descriptor = {
|
|
1896
|
+
subject: "acct:johndoe@example.com",
|
|
1897
|
+
links: []
|
|
1898
|
+
};
|
|
1899
|
+
const first = "https://example.com/.well-known/webfinger?resource=acct%3Ajohndoe%40example.com";
|
|
1900
|
+
const second = "https://example.com/.well-known/webfinger2";
|
|
1901
|
+
await t.step("redirect", async () => {
|
|
1902
|
+
const recorder = recordHops((url) => url.pathname === "/.well-known/webfinger" ? {
|
|
1903
|
+
status: 302,
|
|
1904
|
+
headers: { Location: "/.well-known/webfinger2" },
|
|
1905
|
+
body: "redirecting",
|
|
1906
|
+
url: url.href.replace("example.com", "93.184.215.14")
|
|
1907
|
+
} : { body: JSON.stringify(descriptor) });
|
|
1908
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupWebFingerInternal("acct:johndoe@example.com", {}, { openHop: recorder.openHop }), descriptor);
|
|
1909
|
+
(0, node_assert_strict.deepStrictEqual)(recorder.events, [
|
|
1910
|
+
`open ${first}`,
|
|
1911
|
+
`fetch ${first} #1`,
|
|
1912
|
+
"cancel #1",
|
|
1913
|
+
`close ${first}`,
|
|
1914
|
+
`open ${second}`,
|
|
1915
|
+
`fetch ${second} #2`,
|
|
1916
|
+
"consumed #2",
|
|
1917
|
+
`close ${second}`
|
|
1918
|
+
]);
|
|
1919
|
+
recorder.assertInvariants();
|
|
1920
|
+
});
|
|
1921
|
+
await t.step("non-2xx response", async () => {
|
|
1922
|
+
const recorder = recordHops(() => ({
|
|
1923
|
+
status: 404,
|
|
1924
|
+
body: "not found"
|
|
1925
|
+
}));
|
|
1926
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupWebFingerInternal("acct:johndoe@example.com", {}, { openHop: recorder.openHop }), null);
|
|
1927
|
+
(0, node_assert_strict.deepStrictEqual)(recorder.events, [
|
|
1928
|
+
`open ${first}`,
|
|
1929
|
+
`fetch ${first} #1`,
|
|
1930
|
+
"cancel #1",
|
|
1931
|
+
`close ${first}`
|
|
1932
|
+
]);
|
|
1933
|
+
recorder.assertInvariants();
|
|
1934
|
+
});
|
|
1935
|
+
await t.step("too many redirections", async () => {
|
|
1936
|
+
const recorder = recordHops(() => ({
|
|
1937
|
+
status: 302,
|
|
1938
|
+
headers: { Location: "/.well-known/webfinger2" },
|
|
1939
|
+
body: "redirecting"
|
|
1940
|
+
}));
|
|
1941
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupWebFingerInternal("acct:johndoe@example.com", { maxRedirection: 2 }, { openHop: recorder.openHop }), null);
|
|
1942
|
+
(0, node_assert_strict.deepStrictEqual)(recorder.events, [
|
|
1943
|
+
`open ${first}`,
|
|
1944
|
+
`fetch ${first} #1`,
|
|
1945
|
+
"cancel #1",
|
|
1946
|
+
`close ${first}`,
|
|
1947
|
+
`open ${second}`,
|
|
1948
|
+
`fetch ${second} #2`,
|
|
1949
|
+
"cancel #2",
|
|
1950
|
+
`close ${second}`
|
|
1951
|
+
]);
|
|
1952
|
+
recorder.assertInvariants();
|
|
1953
|
+
});
|
|
1954
|
+
await t.step("failed redirect body cancellation", async () => {
|
|
1955
|
+
const recorder = recordHops((url) => url.pathname === "/.well-known/webfinger" ? {
|
|
1956
|
+
status: 302,
|
|
1957
|
+
headers: { Location: "/.well-known/webfinger2" },
|
|
1958
|
+
body: "redirecting",
|
|
1959
|
+
cancelFails: true
|
|
1960
|
+
} : { body: JSON.stringify(descriptor) });
|
|
1961
|
+
const records = [];
|
|
1962
|
+
await (0, _logtape_logtape.configure)({
|
|
1963
|
+
sinks: { buffer: (record) => records.push(record) },
|
|
1964
|
+
loggers: [{
|
|
1965
|
+
category: "fedify",
|
|
1966
|
+
sinks: ["buffer"],
|
|
1967
|
+
lowestLevel: "debug"
|
|
1968
|
+
}, {
|
|
1969
|
+
category: ["logtape", "meta"],
|
|
1970
|
+
sinks: []
|
|
1971
|
+
}],
|
|
1972
|
+
reset: true
|
|
1973
|
+
});
|
|
1974
|
+
try {
|
|
1975
|
+
(0, node_assert_strict.deepStrictEqual)(await lookupWebFingerInternal("acct:johndoe@example.com", {}, { openHop: recorder.openHop }), descriptor);
|
|
1976
|
+
const failures = records.filter((r) => r.rawMessage === "Failed to cancel the response body from {url}: {error}");
|
|
1977
|
+
(0, node_assert_strict.deepStrictEqual)(failures.map((r) => [r.level, r.properties.url]), [["debug", first]]);
|
|
1978
|
+
const { error } = failures[0].properties;
|
|
1979
|
+
(0, node_assert_strict.ok)(error instanceof Error);
|
|
1980
|
+
(0, node_assert_strict.deepStrictEqual)(error.message, "Cancellation failed");
|
|
1981
|
+
} finally {
|
|
1982
|
+
await (0, _logtape_logtape.reset)();
|
|
1983
|
+
}
|
|
1984
|
+
(0, node_assert_strict.deepStrictEqual)(recorder.events, [
|
|
1985
|
+
`open ${first}`,
|
|
1986
|
+
`fetch ${first} #1`,
|
|
1987
|
+
"cancel #1",
|
|
1988
|
+
`close ${first}`,
|
|
1989
|
+
`open ${second}`,
|
|
1990
|
+
`fetch ${second} #2`,
|
|
1991
|
+
"consumed #2",
|
|
1992
|
+
`close ${second}`
|
|
1993
|
+
]);
|
|
1994
|
+
recorder.assertInvariants();
|
|
1995
|
+
});
|
|
1996
|
+
await t.step("never-settling body cancellation", async () => {
|
|
1997
|
+
const recorder = recordHops((url) => url.pathname === "/.well-known/webfinger" ? {
|
|
1998
|
+
status: 302,
|
|
1999
|
+
headers: { Location: "/.well-known/webfinger2" },
|
|
2000
|
+
body: "redirecting",
|
|
2001
|
+
cancelNeverSettles: true
|
|
2002
|
+
} : {
|
|
2003
|
+
status: 404,
|
|
2004
|
+
body: "not found",
|
|
2005
|
+
cancelNeverSettles: true
|
|
2006
|
+
});
|
|
2007
|
+
(0, node_assert_strict.deepStrictEqual)(await settleWithin(lookupWebFingerInternal("acct:johndoe@example.com", {}, { openHop: recorder.openHop })), null);
|
|
2008
|
+
(0, node_assert_strict.deepStrictEqual)(recorder.events, [
|
|
2009
|
+
`open ${first}`,
|
|
2010
|
+
`fetch ${first} #1`,
|
|
2011
|
+
"cancel #1",
|
|
2012
|
+
`close ${first}`,
|
|
2013
|
+
`open ${second}`,
|
|
2014
|
+
`fetch ${second} #2`,
|
|
2015
|
+
"cancel #2",
|
|
2016
|
+
`close ${second}`
|
|
2017
|
+
]);
|
|
2018
|
+
recorder.assertInvariants();
|
|
2019
|
+
});
|
|
2020
|
+
});
|
|
2021
|
+
/**
|
|
2022
|
+
* Rejects if the given promise does not settle within the timeout, so that
|
|
2023
|
+
* a hang fails the test instead of blocking it.
|
|
2024
|
+
*/
|
|
2025
|
+
async function settleWithin(promise, ms = 5e3) {
|
|
2026
|
+
let timer;
|
|
2027
|
+
try {
|
|
2028
|
+
return await Promise.race([promise, new Promise((_, reject) => {
|
|
2029
|
+
timer = setTimeout(() => reject(/* @__PURE__ */ new Error(`Did not settle within ${ms} ms.`)), ms);
|
|
2030
|
+
})]);
|
|
2031
|
+
} finally {
|
|
2032
|
+
clearTimeout(timer);
|
|
2033
|
+
}
|
|
2034
|
+
}
|
|
2035
|
+
function neverCancellableResponse(init) {
|
|
2036
|
+
return new Response(new ReadableStream({
|
|
2037
|
+
start(controller) {
|
|
2038
|
+
controller.enqueue(new TextEncoder().encode("never cancelled"));
|
|
2039
|
+
},
|
|
2040
|
+
cancel: () => new Promise(() => {})
|
|
2041
|
+
}), init);
|
|
2042
|
+
}
|
|
2043
|
+
(0, _fedify_fixture.test)("lookupWebFinger() does not wait for cancelling response bodies", async (t) => {
|
|
2044
|
+
const descriptor = {
|
|
2045
|
+
subject: "acct:johndoe@example.com",
|
|
2046
|
+
links: []
|
|
2047
|
+
};
|
|
2048
|
+
esm_default.mockGlobal();
|
|
2049
|
+
try {
|
|
2050
|
+
await t.step("redirect", async () => {
|
|
2051
|
+
esm_default.removeRoutes();
|
|
2052
|
+
esm_default.get("begin:https://example.com/.well-known/webfinger?", () => neverCancellableResponse({
|
|
2053
|
+
status: 302,
|
|
2054
|
+
headers: { Location: "/.well-known/webfinger2" }
|
|
2055
|
+
}));
|
|
2056
|
+
esm_default.get("https://example.com/.well-known/webfinger2", () => new Response(JSON.stringify(descriptor), { headers: { "Content-Type": "application/jrd+json" } }));
|
|
2057
|
+
(0, node_assert_strict.deepStrictEqual)(await settleWithin(lookupDirect("acct:johndoe@example.com")), descriptor);
|
|
2058
|
+
});
|
|
2059
|
+
await t.step("non-2xx response", async () => {
|
|
2060
|
+
esm_default.removeRoutes();
|
|
2061
|
+
esm_default.get("begin:https://example.com/.well-known/webfinger?", () => neverCancellableResponse({ status: 404 }));
|
|
2062
|
+
(0, node_assert_strict.deepStrictEqual)(await settleWithin(lookupDirect("acct:johndoe@example.com")), null);
|
|
2063
|
+
});
|
|
2064
|
+
} finally {
|
|
2065
|
+
esm_default.hardReset();
|
|
2066
|
+
}
|
|
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
|
+
});
|
|
1656
2208
|
//#endregion
|