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