@fedify/webfinger 2.5.0-dev.2619 → 2.5.0-dev.2687

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.
@@ -48,9 +48,10 @@ function lookupMock(
48
48
  });
49
49
  }
50
50
  return lookupWebFingerWithDependencies(resource, options, {
51
- openHop: options.allowPrivateAddress === true
52
- ? openDirectFetch
53
- : openExampleDirectly,
51
+ openHop:
52
+ options.allowPrivateAddress === true
53
+ ? openDirectFetch
54
+ : openExampleDirectly,
54
55
  });
55
56
  }
56
57
 
@@ -73,8 +74,8 @@ function openExampleDirectly(url: string): Promise<PublicFetchSession> {
73
74
  return new URL(url).hostname === "example.com"
74
75
  ? openDirectFetch(url)
75
76
  : openPublicFetch(url, {
76
- lookup: lookupResolving([{ address: "93.184.215.14", family: 4 }]),
77
- });
77
+ lookup: lookupResolving([{ address: "93.184.215.14", family: 4 }]),
78
+ });
78
79
  }
79
80
 
80
81
  async function lookupResourceWithDependencies(
@@ -103,14 +104,8 @@ test({
103
104
  await lookupMock("acct:johndoe@example.com/exploit"),
104
105
  null,
105
106
  );
106
- deepStrictEqual(
107
- await lookupMock("acct:johndoe@example.com?x=1"),
108
- null,
109
- );
110
- deepStrictEqual(
111
- await lookupMock("acct:johndoe@example.com#frag"),
112
- null,
113
- );
107
+ deepStrictEqual(await lookupMock("acct:johndoe@example.com?x=1"), null);
108
+ deepStrictEqual(await lookupMock("acct:johndoe@example.com#frag"), null);
114
109
  });
115
110
 
116
111
  await t.step("connection refused", async () => {
@@ -130,16 +125,12 @@ test({
130
125
  // throws. Matches the cleanup pattern of the metrics test
131
126
  // further down in this file.
132
127
  try {
133
- fetchMock.get(
134
- "begin:https://example.com/.well-known/webfinger?",
135
- { status: 404 },
136
- );
128
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
129
+ status: 404,
130
+ });
137
131
 
138
132
  await t.step("not found", async () => {
139
- deepStrictEqual(
140
- await lookupMock("acct:johndoe@example.com"),
141
- null,
142
- );
133
+ deepStrictEqual(await lookupMock("acct:johndoe@example.com"), null);
143
134
  deepStrictEqual(await lookupMock("https://example.com/foo"), null);
144
135
  });
145
136
 
@@ -154,10 +145,7 @@ test({
154
145
  );
155
146
 
156
147
  await t.step("acct", async () => {
157
- deepStrictEqual(
158
- await lookupMock("acct:johndoe@example.com"),
159
- expected,
160
- );
148
+ deepStrictEqual(await lookupMock("acct:johndoe@example.com"), expected);
161
149
  });
162
150
 
163
151
  const expected2: ResourceDescriptor = {
@@ -171,10 +159,7 @@ test({
171
159
  );
172
160
 
173
161
  await t.step("https", async () => {
174
- deepStrictEqual(
175
- await lookupMock("https://example.com/foo"),
176
- expected2,
177
- );
162
+ deepStrictEqual(await lookupMock("https://example.com/foo"), expected2);
178
163
  });
179
164
 
180
165
  const mailtoExpected: ResourceDescriptor = {
@@ -221,32 +206,25 @@ test({
221
206
  });
222
207
 
223
208
  fetchMock.removeRoutes();
224
- fetchMock.get(
225
- "begin:https://example.com/.well-known/webfinger?",
226
- { body: "not json" },
227
- );
209
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
210
+ body: "not json",
211
+ });
228
212
 
229
213
  await t.step("invalid response", async () => {
230
- deepStrictEqual(
231
- await lookupMock("acct:johndoe@example.com"),
232
- null,
233
- );
214
+ deepStrictEqual(await lookupMock("acct:johndoe@example.com"), null);
234
215
  });
235
216
 
236
217
  fetchMock.removeRoutes();
237
- fetchMock.get(
238
- "begin:https://localhost/.well-known/webfinger?",
239
- {
240
- subject: "acct:test@localhost",
241
- links: [
242
- {
243
- rel: "self",
244
- type: "application/activity+json",
245
- href: "https://localhost/actor",
246
- },
247
- ],
248
- },
249
- );
218
+ fetchMock.get("begin:https://localhost/.well-known/webfinger?", {
219
+ subject: "acct:test@localhost",
220
+ links: [
221
+ {
222
+ rel: "self",
223
+ type: "application/activity+json",
224
+ href: "https://localhost/actor",
225
+ },
226
+ ],
227
+ });
250
228
 
251
229
  await t.step("private address", async () => {
252
230
  deepStrictEqual(await lookupMock("acct:test@localhost"), null);
@@ -268,33 +246,23 @@ test({
268
246
  });
269
247
 
270
248
  fetchMock.removeRoutes();
271
- fetchMock.get(
272
- "begin:https://example.com/.well-known/webfinger?",
273
- {
274
- status: 302,
275
- headers: { Location: "/.well-known/webfinger2" },
276
- },
277
- );
278
- fetchMock.get(
279
- "begin:https://example.com/.well-known/webfinger2",
280
- { body: expected },
281
- );
249
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
250
+ status: 302,
251
+ headers: { Location: "/.well-known/webfinger2" },
252
+ });
253
+ fetchMock.get("begin:https://example.com/.well-known/webfinger2", {
254
+ body: expected,
255
+ });
282
256
 
283
257
  await t.step("redirection", async () => {
284
- deepStrictEqual(
285
- await lookupMock("acct:johndoe@example.com"),
286
- expected,
287
- );
258
+ deepStrictEqual(await lookupMock("acct:johndoe@example.com"), expected);
288
259
  });
289
260
 
290
261
  fetchMock.removeRoutes();
291
- fetchMock.get(
292
- "begin:https://example.com/.well-known/webfinger?",
293
- {
294
- status: 302,
295
- headers: { Location: "/.well-known/webfinger" },
296
- },
297
- );
262
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
263
+ status: 302,
264
+ headers: { Location: "/.well-known/webfinger" },
265
+ });
298
266
 
299
267
  await t.step("infinite redirection", async () => {
300
268
  const result = await withTimeout(
@@ -305,54 +273,39 @@ test({
305
273
  });
306
274
 
307
275
  fetchMock.removeRoutes();
308
- fetchMock.get(
309
- "begin:https://example.com/.well-known/webfinger?",
310
- {
311
- status: 302,
312
- headers: { Location: "ftp://example.com/" },
313
- },
314
- );
276
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
277
+ status: 302,
278
+ headers: { Location: "ftp://example.com/" },
279
+ });
315
280
 
316
281
  await t.step("redirection to different protocol", async () => {
317
- deepStrictEqual(
318
- await lookupMock("acct:johndoe@example.com"),
319
- null,
320
- );
282
+ deepStrictEqual(await lookupMock("acct:johndoe@example.com"), null);
321
283
  });
322
284
 
323
285
  fetchMock.removeRoutes();
324
- fetchMock.get(
325
- "begin:https://example.com/.well-known/webfinger?",
326
- {
327
- status: 302,
328
- headers: { Location: "https://localhost/" },
329
- },
330
- );
286
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
287
+ status: 302,
288
+ headers: { Location: "https://localhost/" },
289
+ });
331
290
 
332
291
  await t.step("redirection to private address", async () => {
333
- deepStrictEqual(
334
- await lookupMock("acct:johndoe@example.com"),
335
- null,
336
- );
292
+ deepStrictEqual(await lookupMock("acct:johndoe@example.com"), null);
337
293
  });
338
294
 
339
295
  fetchMock.removeRoutes();
340
296
  let redirectCount = 0;
341
- fetchMock.get(
342
- "begin:https://example.com/.well-known/webfinger",
343
- () => {
344
- redirectCount++;
345
- if (redirectCount < 3) {
346
- return {
347
- status: 302,
348
- headers: {
349
- Location: `/.well-known/webfinger?redirect=${redirectCount}`,
350
- },
351
- };
352
- }
353
- return { body: expected };
354
- },
355
- );
297
+ fetchMock.get("begin:https://example.com/.well-known/webfinger", () => {
298
+ redirectCount++;
299
+ if (redirectCount < 3) {
300
+ return {
301
+ status: 302,
302
+ headers: {
303
+ Location: `/.well-known/webfinger?redirect=${redirectCount}`,
304
+ },
305
+ };
306
+ }
307
+ return { body: expected };
308
+ });
356
309
 
357
310
  await t.step("custom maxRedirection", async () => {
358
311
  // Test with maxRedirection: 1 (should fail; mock has 2 redirects)
@@ -385,10 +338,7 @@ test({
385
338
 
386
339
  // Test with default maxRedirection: 5 (should succeed)
387
340
  redirectCount = 0;
388
- deepStrictEqual(
389
- await lookupMock("acct:johndoe@example.com"),
390
- expected,
391
- );
341
+ deepStrictEqual(await lookupMock("acct:johndoe@example.com"), expected);
392
342
  });
393
343
 
394
344
  // Regression: `maxRedirection: 1` must allow exactly one 302 to be
@@ -409,9 +359,9 @@ test({
409
359
  count++;
410
360
  return count < 2
411
361
  ? {
412
- status: 302,
413
- headers: { Location: "/.well-known/webfinger?after=1" },
414
- }
362
+ status: 302,
363
+ headers: { Location: "/.well-known/webfinger?after=1" },
364
+ }
415
365
  : { body: expected };
416
366
  },
417
367
  );
@@ -432,11 +382,11 @@ test({
432
382
  count++;
433
383
  return count < 3
434
384
  ? {
435
- status: 302,
436
- headers: {
437
- Location: `/.well-known/webfinger?after=${count}`,
438
- },
439
- }
385
+ status: 302,
386
+ headers: {
387
+ Location: `/.well-known/webfinger?after=${count}`,
388
+ },
389
+ }
440
390
  : { body: expected };
441
391
  },
442
392
  );
@@ -476,25 +426,22 @@ test({
476
426
 
477
427
  fetchMock.removeRoutes();
478
428
  let redirectCount2 = 0;
479
- fetchMock.get(
480
- "begin:https://example.com/.well-known/webfinger",
481
- () => {
482
- redirectCount2++;
483
- if (redirectCount2 === 1) {
484
- return {
485
- status: 302,
486
- headers: { Location: "/.well-known/webfinger2" },
487
- };
488
- }
489
- return new Promise((resolve) => {
490
- const timeoutId = setTimeout(() => {
491
- resolve({ body: expected });
492
- }, 1000);
429
+ fetchMock.get("begin:https://example.com/.well-known/webfinger", () => {
430
+ redirectCount2++;
431
+ if (redirectCount2 === 1) {
432
+ return {
433
+ status: 302,
434
+ headers: { Location: "/.well-known/webfinger2" },
435
+ };
436
+ }
437
+ return new Promise((resolve) => {
438
+ const timeoutId = setTimeout(() => {
439
+ resolve({ body: expected });
440
+ }, 1000);
493
441
 
494
- responseTimers.push(timeoutId);
495
- });
496
- },
497
- );
442
+ responseTimers.push(timeoutId);
443
+ });
444
+ });
498
445
 
499
446
  await t.step("cancellation during redirection", async () => {
500
447
  // Test cancelling a request during redirection process
@@ -534,10 +481,9 @@ test({
534
481
  });
535
482
 
536
483
  fetchMock.removeRoutes();
537
- fetchMock.get(
538
- "begin:https://example.com/.well-known/webfinger?",
539
- { body: expected },
540
- );
484
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
485
+ body: expected,
486
+ });
541
487
 
542
488
  await t.step("successful request with signal", async () => {
543
489
  // Test successful request with a normal AbortController signal
@@ -555,417 +501,403 @@ test({
555
501
  },
556
502
  });
557
503
 
558
- test("lookupWebFinger() records webfinger.lookup counter and duration", {
559
- sanitizeOps: false,
560
- sanitizeResources: false,
561
- }, async (t) => {
562
- fetchMock.spyGlobal();
563
- try {
564
- const expected: ResourceDescriptor = {
565
- subject: "acct:johndoe@example.com",
566
- links: [],
567
- };
504
+ test(
505
+ "lookupWebFinger() records webfinger.lookup counter and duration",
506
+ {
507
+ sanitizeOps: false,
508
+ sanitizeResources: false,
509
+ },
510
+ async (t) => {
511
+ fetchMock.spyGlobal();
512
+ try {
513
+ const expected: ResourceDescriptor = {
514
+ subject: "acct:johndoe@example.com",
515
+ links: [],
516
+ };
517
+
518
+ await t.step(
519
+ "records result=found for a successful acct lookup",
520
+ async () => {
521
+ fetchMock.removeRoutes();
522
+ fetchMock.get(
523
+ "https://example.com/.well-known/webfinger?resource=acct%3Ajohndoe%40example.com",
524
+ { body: expected },
525
+ );
526
+ const [meterProvider, recorder] = createTestMeterProvider();
527
+ const result = await lookupMock("acct:johndoe@example.com", {
528
+ meterProvider,
529
+ });
530
+ deepStrictEqual(result, expected);
568
531
 
569
- await t.step(
570
- "records result=found for a successful acct lookup",
571
- async () => {
532
+ const counters = recorder.getMeasurements("webfinger.lookup");
533
+ deepStrictEqual(counters.length, 1);
534
+ deepStrictEqual(counters[0].type, "counter");
535
+ deepStrictEqual(counters[0].value, 1);
536
+ deepStrictEqual(
537
+ counters[0].attributes["webfinger.lookup.result"],
538
+ "found",
539
+ );
540
+ deepStrictEqual(
541
+ counters[0].attributes["webfinger.resource.scheme"],
542
+ "acct",
543
+ );
544
+ deepStrictEqual(
545
+ counters[0].attributes["activitypub.remote.host"],
546
+ "example.com",
547
+ );
548
+ deepStrictEqual(
549
+ counters[0].attributes["http.response.status_code"],
550
+ 200,
551
+ );
552
+
553
+ const durations = recorder.getMeasurements(
554
+ "webfinger.lookup.duration",
555
+ );
556
+ deepStrictEqual(durations.length, 1);
557
+ deepStrictEqual(durations[0].type, "histogram");
558
+ deepStrictEqual(
559
+ durations[0].attributes["webfinger.lookup.result"],
560
+ "found",
561
+ );
562
+ deepStrictEqual(
563
+ durations[0].attributes["webfinger.resource.scheme"],
564
+ "acct",
565
+ );
566
+ ok(typeof durations[0].value === "number" && durations[0].value >= 0);
567
+ },
568
+ );
569
+
570
+ await t.step(
571
+ "records scheme=https for an https resource lookup",
572
+ async () => {
573
+ fetchMock.removeRoutes();
574
+ fetchMock.get(
575
+ "https://example.com/.well-known/webfinger?resource=https%3A%2F%2Fexample.com%2Ffoo",
576
+ { body: { subject: "https://example.com/foo", links: [] } },
577
+ );
578
+ const [meterProvider, recorder] = createTestMeterProvider();
579
+ await lookupMock("https://example.com/foo", { meterProvider });
580
+ const counters = recorder.getMeasurements("webfinger.lookup");
581
+ deepStrictEqual(counters.length, 1);
582
+ deepStrictEqual(
583
+ counters[0].attributes["webfinger.resource.scheme"],
584
+ "https",
585
+ );
586
+ deepStrictEqual(
587
+ counters[0].attributes["webfinger.lookup.result"],
588
+ "found",
589
+ );
590
+ },
591
+ );
592
+
593
+ await t.step("records non-default ports for URL resources", async () => {
572
594
  fetchMock.removeRoutes();
573
595
  fetchMock.get(
574
- "https://example.com/.well-known/webfinger?resource=acct%3Ajohndoe%40example.com",
575
- { body: expected },
596
+ "https://example.com:8443/.well-known/webfinger?resource=https%3A%2F%2Fexample.com%3A8443%2Ffoo",
597
+ { body: { subject: "https://example.com:8443/foo", links: [] } },
576
598
  );
577
599
  const [meterProvider, recorder] = createTestMeterProvider();
600
+ await lookupMock("https://example.com:8443/foo", {
601
+ meterProvider,
602
+ });
603
+ const counter = recorder.getMeasurement("webfinger.lookup");
604
+ ok(counter != null);
605
+ deepStrictEqual(
606
+ counter.attributes["activitypub.remote.host"],
607
+ "example.com:8443",
608
+ );
609
+ });
610
+
611
+ await t.step("records result=not_found with status 404", async () => {
612
+ fetchMock.removeRoutes();
613
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
614
+ status: 404,
615
+ });
616
+ const [meterProvider, recorder] = createTestMeterProvider();
578
617
  const result = await lookupMock("acct:johndoe@example.com", {
579
618
  meterProvider,
580
619
  });
581
- deepStrictEqual(result, expected);
620
+ deepStrictEqual(result, null);
582
621
 
583
622
  const counters = recorder.getMeasurements("webfinger.lookup");
584
623
  deepStrictEqual(counters.length, 1);
585
- deepStrictEqual(counters[0].type, "counter");
586
- deepStrictEqual(counters[0].value, 1);
587
624
  deepStrictEqual(
588
625
  counters[0].attributes["webfinger.lookup.result"],
589
- "found",
626
+ "not_found",
590
627
  );
591
628
  deepStrictEqual(
592
- counters[0].attributes["webfinger.resource.scheme"],
593
- "acct",
629
+ counters[0].attributes["http.response.status_code"],
630
+ 404,
594
631
  );
595
632
  deepStrictEqual(
596
633
  counters[0].attributes["activitypub.remote.host"],
597
634
  "example.com",
598
635
  );
599
- deepStrictEqual(
600
- counters[0].attributes["http.response.status_code"],
601
- 200,
602
- );
603
636
 
604
637
  const durations = recorder.getMeasurements("webfinger.lookup.duration");
605
638
  deepStrictEqual(durations.length, 1);
606
- deepStrictEqual(durations[0].type, "histogram");
607
639
  deepStrictEqual(
608
640
  durations[0].attributes["webfinger.lookup.result"],
609
- "found",
610
- );
611
- deepStrictEqual(
612
- durations[0].attributes["webfinger.resource.scheme"],
613
- "acct",
614
- );
615
- ok(typeof durations[0].value === "number" && durations[0].value >= 0);
616
- },
617
- );
618
-
619
- await t.step(
620
- "records scheme=https for an https resource lookup",
621
- async () => {
622
- fetchMock.removeRoutes();
623
- fetchMock.get(
624
- "https://example.com/.well-known/webfinger?resource=https%3A%2F%2Fexample.com%2Ffoo",
625
- { body: { subject: "https://example.com/foo", links: [] } },
641
+ "not_found",
626
642
  );
627
- const [meterProvider, recorder] = createTestMeterProvider();
628
- await lookupMock("https://example.com/foo", { meterProvider });
629
- const counters = recorder.getMeasurements("webfinger.lookup");
630
- deepStrictEqual(counters.length, 1);
631
- deepStrictEqual(
632
- counters[0].attributes["webfinger.resource.scheme"],
633
- "https",
634
- );
635
- deepStrictEqual(
636
- counters[0].attributes["webfinger.lookup.result"],
637
- "found",
638
- );
639
- },
640
- );
643
+ });
641
644
 
642
- await t.step(
643
- "records non-default ports for URL resources",
644
- async () => {
645
+ await t.step("records result=not_found with status 410", async () => {
645
646
  fetchMock.removeRoutes();
646
- fetchMock.get(
647
- "https://example.com:8443/.well-known/webfinger?resource=https%3A%2F%2Fexample.com%3A8443%2Ffoo",
648
- { body: { subject: "https://example.com:8443/foo", links: [] } },
649
- );
650
- const [meterProvider, recorder] = createTestMeterProvider();
651
- await lookupMock("https://example.com:8443/foo", {
652
- meterProvider,
647
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
648
+ status: 410,
653
649
  });
650
+ const [meterProvider, recorder] = createTestMeterProvider();
651
+ await lookupMock("acct:johndoe@example.com", { meterProvider });
654
652
  const counter = recorder.getMeasurement("webfinger.lookup");
655
653
  ok(counter != null);
656
654
  deepStrictEqual(
657
- counter.attributes["activitypub.remote.host"],
658
- "example.com:8443",
655
+ counter.attributes["webfinger.lookup.result"],
656
+ "not_found",
659
657
  );
660
- },
661
- );
662
-
663
- await t.step("records result=not_found with status 404", async () => {
664
- fetchMock.removeRoutes();
665
- fetchMock.get(
666
- "begin:https://example.com/.well-known/webfinger?",
667
- { status: 404 },
668
- );
669
- const [meterProvider, recorder] = createTestMeterProvider();
670
- const result = await lookupMock("acct:johndoe@example.com", {
671
- meterProvider,
658
+ deepStrictEqual(counter.attributes["http.response.status_code"], 410);
672
659
  });
673
- deepStrictEqual(result, null);
674
660
 
675
- const counters = recorder.getMeasurements("webfinger.lookup");
676
- deepStrictEqual(counters.length, 1);
677
- deepStrictEqual(
678
- counters[0].attributes["webfinger.lookup.result"],
679
- "not_found",
680
- );
681
- deepStrictEqual(
682
- counters[0].attributes["http.response.status_code"],
683
- 404,
684
- );
685
- deepStrictEqual(
686
- counters[0].attributes["activitypub.remote.host"],
687
- "example.com",
661
+ await t.step(
662
+ "records result=error for non-2xx, non-404/410 HTTP responses",
663
+ async () => {
664
+ fetchMock.removeRoutes();
665
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
666
+ status: 500,
667
+ });
668
+ const [meterProvider, recorder] = createTestMeterProvider();
669
+ await lookupMock("acct:johndoe@example.com", { meterProvider });
670
+ const counter = recorder.getMeasurement("webfinger.lookup");
671
+ ok(counter != null);
672
+ deepStrictEqual(
673
+ counter.attributes["webfinger.lookup.result"],
674
+ "error",
675
+ );
676
+ deepStrictEqual(counter.attributes["http.response.status_code"], 500);
677
+ },
688
678
  );
689
679
 
690
- const durations = recorder.getMeasurements("webfinger.lookup.duration");
691
- deepStrictEqual(durations.length, 1);
692
- deepStrictEqual(
693
- durations[0].attributes["webfinger.lookup.result"],
694
- "not_found",
680
+ await t.step(
681
+ "records result=invalid for malformed JSON bodies",
682
+ async () => {
683
+ fetchMock.removeRoutes();
684
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
685
+ body: "not json",
686
+ });
687
+ const [meterProvider, recorder] = createTestMeterProvider();
688
+ await lookupMock("acct:johndoe@example.com", { meterProvider });
689
+ const counter = recorder.getMeasurement("webfinger.lookup");
690
+ ok(counter != null);
691
+ deepStrictEqual(
692
+ counter.attributes["webfinger.lookup.result"],
693
+ "invalid",
694
+ );
695
+ deepStrictEqual(counter.attributes["http.response.status_code"], 200);
696
+ },
695
697
  );
696
- });
697
698
 
698
- await t.step("records result=not_found with status 410", async () => {
699
- fetchMock.removeRoutes();
700
- fetchMock.get(
701
- "begin:https://example.com/.well-known/webfinger?",
702
- { status: 410 },
703
- );
704
- const [meterProvider, recorder] = createTestMeterProvider();
705
- await lookupMock("acct:johndoe@example.com", { meterProvider });
706
- const counter = recorder.getMeasurement("webfinger.lookup");
707
- ok(counter != null);
708
- deepStrictEqual(
709
- counter.attributes["webfinger.lookup.result"],
710
- "not_found",
699
+ await t.step(
700
+ "records result=network_error when fetch never reaches the remote",
701
+ async () => {
702
+ fetchMock.removeRoutes();
703
+ const [meterProvider, recorder] = createTestMeterProvider();
704
+ const result = await lookupMock("acct:johndoe@fedify-test.internal", {
705
+ meterProvider,
706
+ });
707
+ deepStrictEqual(result, null);
708
+ const counter = recorder.getMeasurement("webfinger.lookup");
709
+ ok(counter != null);
710
+ deepStrictEqual(
711
+ counter.attributes["webfinger.lookup.result"],
712
+ "network_error",
713
+ );
714
+ deepStrictEqual(
715
+ "http.response.status_code" in counter.attributes,
716
+ false,
717
+ "no HTTP response means no status code attribute",
718
+ );
719
+ deepStrictEqual(
720
+ counter.attributes["activitypub.remote.host"],
721
+ "fedify-test.internal",
722
+ );
723
+ },
711
724
  );
712
- deepStrictEqual(counter.attributes["http.response.status_code"], 410);
713
- });
714
725
 
715
- await t.step(
716
- "records result=error for non-2xx, non-404/410 HTTP responses",
717
- async () => {
718
- fetchMock.removeRoutes();
719
- fetchMock.get(
720
- "begin:https://example.com/.well-known/webfinger?",
721
- { status: 500 },
722
- );
723
- const [meterProvider, recorder] = createTestMeterProvider();
724
- await lookupMock("acct:johndoe@example.com", { meterProvider });
725
- const counter = recorder.getMeasurement("webfinger.lookup");
726
- ok(counter != null);
727
- deepStrictEqual(
728
- counter.attributes["webfinger.lookup.result"],
729
- "error",
730
- );
731
- deepStrictEqual(counter.attributes["http.response.status_code"], 500);
732
- },
733
- );
734
-
735
- await t.step(
736
- "records result=invalid for malformed JSON bodies",
737
- async () => {
738
- fetchMock.removeRoutes();
739
- fetchMock.get(
740
- "begin:https://example.com/.well-known/webfinger?",
741
- { body: "not json" },
742
- );
743
- const [meterProvider, recorder] = createTestMeterProvider();
744
- await lookupMock("acct:johndoe@example.com", { meterProvider });
745
- const counter = recorder.getMeasurement("webfinger.lookup");
746
- ok(counter != null);
747
- deepStrictEqual(
748
- counter.attributes["webfinger.lookup.result"],
749
- "invalid",
750
- );
751
- deepStrictEqual(counter.attributes["http.response.status_code"], 200);
752
- },
753
- );
754
-
755
- await t.step(
756
- "records result=network_error when fetch never reaches the remote",
757
- async () => {
758
- fetchMock.removeRoutes();
759
- const [meterProvider, recorder] = createTestMeterProvider();
760
- const result = await lookupMock(
761
- "acct:johndoe@fedify-test.internal",
762
- { meterProvider },
763
- );
764
- deepStrictEqual(result, null);
765
- const counter = recorder.getMeasurement("webfinger.lookup");
766
- ok(counter != null);
767
- deepStrictEqual(
768
- counter.attributes["webfinger.lookup.result"],
769
- "network_error",
770
- );
771
- deepStrictEqual(
772
- "http.response.status_code" in counter.attributes,
773
- false,
774
- "no HTTP response means no status code attribute",
775
- );
776
- deepStrictEqual(
777
- counter.attributes["activitypub.remote.host"],
778
- "fedify-test.internal",
779
- );
780
- },
781
- );
782
-
783
- await t.step(
784
- "records result=invalid for malformed acct: resources",
785
- async () => {
786
- const [meterProvider, recorder] = createTestMeterProvider();
787
- const result = await lookupMock("acct:johndoe", { meterProvider });
788
- deepStrictEqual(result, null);
789
- const counter = recorder.getMeasurement("webfinger.lookup");
790
- ok(counter != null);
791
- deepStrictEqual(
792
- counter.attributes["webfinger.lookup.result"],
793
- "invalid",
794
- );
795
- deepStrictEqual(
796
- counter.attributes["webfinger.resource.scheme"],
797
- "acct",
798
- );
799
- deepStrictEqual(
800
- "activitypub.remote.host" in counter.attributes,
801
- false,
802
- "a malformed acct resource has no usable remote host",
803
- );
804
- },
805
- );
726
+ await t.step(
727
+ "records result=invalid for malformed acct: resources",
728
+ async () => {
729
+ const [meterProvider, recorder] = createTestMeterProvider();
730
+ const result = await lookupMock("acct:johndoe", { meterProvider });
731
+ deepStrictEqual(result, null);
732
+ const counter = recorder.getMeasurement("webfinger.lookup");
733
+ ok(counter != null);
734
+ deepStrictEqual(
735
+ counter.attributes["webfinger.lookup.result"],
736
+ "invalid",
737
+ );
738
+ deepStrictEqual(
739
+ counter.attributes["webfinger.resource.scheme"],
740
+ "acct",
741
+ );
742
+ deepStrictEqual(
743
+ "activitypub.remote.host" in counter.attributes,
744
+ false,
745
+ "a malformed acct resource has no usable remote host",
746
+ );
747
+ },
748
+ );
806
749
 
807
- await t.step(
808
- "records result=invalid when the redirect chain exceeds maxRedirection",
809
- async () => {
810
- fetchMock.removeRoutes();
811
- // The redirect Location drops the original `?resource=...` query
812
- // string, so the second hop's URL no longer contains a `?`. The
813
- // route pattern omits the trailing `?` so it still matches.
814
- fetchMock.get(
815
- "begin:https://example.com/.well-known/webfinger",
816
- {
750
+ await t.step(
751
+ "records result=invalid when the redirect chain exceeds maxRedirection",
752
+ async () => {
753
+ fetchMock.removeRoutes();
754
+ // The redirect Location drops the original `?resource=...` query
755
+ // string, so the second hop's URL no longer contains a `?`. The
756
+ // route pattern omits the trailing `?` so it still matches.
757
+ fetchMock.get("begin:https://example.com/.well-known/webfinger", {
817
758
  status: 302,
818
759
  headers: { Location: "/.well-known/webfinger" },
819
- },
820
- );
821
- const [meterProvider, recorder] = createTestMeterProvider();
822
- const result = await withTimeout(
823
- () =>
824
- lookupMock("acct:johndoe@example.com", {
825
- meterProvider,
826
- maxRedirection: 3,
827
- }),
828
- 2000,
829
- );
830
- deepStrictEqual(result, null);
831
- const counter = recorder.getMeasurement("webfinger.lookup");
832
- ok(counter != null);
833
- deepStrictEqual(
834
- counter.attributes["webfinger.lookup.result"],
835
- "invalid",
836
- );
837
- deepStrictEqual(counter.attributes["http.response.status_code"], 302);
838
- deepStrictEqual(
839
- counter.attributes["activitypub.remote.host"],
840
- "example.com",
841
- );
842
- },
843
- );
760
+ });
761
+ const [meterProvider, recorder] = createTestMeterProvider();
762
+ const result = await withTimeout(
763
+ () =>
764
+ lookupMock("acct:johndoe@example.com", {
765
+ meterProvider,
766
+ maxRedirection: 3,
767
+ }),
768
+ 2000,
769
+ );
770
+ deepStrictEqual(result, null);
771
+ const counter = recorder.getMeasurement("webfinger.lookup");
772
+ ok(counter != null);
773
+ deepStrictEqual(
774
+ counter.attributes["webfinger.lookup.result"],
775
+ "invalid",
776
+ );
777
+ deepStrictEqual(counter.attributes["http.response.status_code"], 302);
778
+ deepStrictEqual(
779
+ counter.attributes["activitypub.remote.host"],
780
+ "example.com",
781
+ );
782
+ },
783
+ );
844
784
 
845
- await t.step(
846
- "records result=invalid for cross-protocol redirects",
847
- async () => {
848
- fetchMock.removeRoutes();
849
- fetchMock.get(
850
- "begin:https://example.com/.well-known/webfinger?",
851
- {
785
+ await t.step(
786
+ "records result=invalid for cross-protocol redirects",
787
+ async () => {
788
+ fetchMock.removeRoutes();
789
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
852
790
  status: 302,
853
791
  headers: { Location: "ftp://example.com/" },
854
- },
855
- );
856
- const [meterProvider, recorder] = createTestMeterProvider();
857
- await lookupMock("acct:johndoe@example.com", { meterProvider });
858
- const counter = recorder.getMeasurement("webfinger.lookup");
859
- ok(counter != null);
860
- deepStrictEqual(
861
- counter.attributes["webfinger.lookup.result"],
862
- "invalid",
863
- );
864
- deepStrictEqual(counter.attributes["http.response.status_code"], 302);
865
- },
866
- );
867
-
868
- await t.step(
869
- "records result=network_error when a redirect points to a private address",
870
- async () => {
871
- fetchMock.removeRoutes();
872
- fetchMock.get(
873
- "begin:https://example.com/.well-known/webfinger?",
874
- {
875
- status: 302,
876
- headers: { Location: "https://localhost/" },
877
- },
878
- );
879
- const [meterProvider, recorder] = createTestMeterProvider();
880
- await lookupMock("acct:johndoe@example.com", { meterProvider });
881
- const counter = recorder.getMeasurement("webfinger.lookup");
882
- ok(counter != null);
883
- deepStrictEqual(
884
- counter.attributes["webfinger.lookup.result"],
885
- "network_error",
886
- );
887
- deepStrictEqual(
888
- counter.attributes["activitypub.remote.host"],
889
- "localhost",
890
- "remote.host reflects the latest URL we attempted, even after a redirect",
891
- );
892
- },
893
- );
792
+ });
793
+ const [meterProvider, recorder] = createTestMeterProvider();
794
+ await lookupMock("acct:johndoe@example.com", { meterProvider });
795
+ const counter = recorder.getMeasurement("webfinger.lookup");
796
+ ok(counter != null);
797
+ deepStrictEqual(
798
+ counter.attributes["webfinger.lookup.result"],
799
+ "invalid",
800
+ );
801
+ deepStrictEqual(counter.attributes["http.response.status_code"], 302);
802
+ },
803
+ );
894
804
 
895
- await t.step(
896
- "records result=invalid for malformed Location headers",
897
- async () => {
898
- fetchMock.removeRoutes();
899
- fetchMock.get(
900
- "begin:https://example.com/.well-known/webfinger?",
901
- {
805
+ await t.step(
806
+ "records result=network_error when a redirect points to a private address",
807
+ async () => {
808
+ fetchMock.removeRoutes();
809
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
902
810
  status: 302,
903
- headers: { Location: "http://[bad" },
904
- },
905
- );
906
- const [meterProvider, recorder] = createTestMeterProvider();
907
- await lookupMock("acct:johndoe@example.com", { meterProvider });
908
- const counter = recorder.getMeasurement("webfinger.lookup");
909
- ok(counter != null);
910
- deepStrictEqual(
911
- counter.attributes["webfinger.lookup.result"],
912
- "invalid",
913
- );
914
- deepStrictEqual(counter.attributes["http.response.status_code"], 302);
915
- deepStrictEqual(
916
- counter.attributes["activitypub.remote.host"],
917
- "example.com",
918
- );
919
- },
920
- );
811
+ headers: { Location: "https://localhost/" },
812
+ });
813
+ const [meterProvider, recorder] = createTestMeterProvider();
814
+ await lookupMock("acct:johndoe@example.com", { meterProvider });
815
+ const counter = recorder.getMeasurement("webfinger.lookup");
816
+ ok(counter != null);
817
+ deepStrictEqual(
818
+ counter.attributes["webfinger.lookup.result"],
819
+ "network_error",
820
+ );
821
+ deepStrictEqual(
822
+ counter.attributes["activitypub.remote.host"],
823
+ "localhost",
824
+ "remote.host reflects the latest URL we attempted, even after a redirect",
825
+ );
826
+ },
827
+ );
921
828
 
922
- await t.step(
923
- "buckets unknown resource schemes as 'other' to keep metric cardinality bounded",
924
- async () => {
925
- // Lookups whose redirect chain ends on an unusual scheme (or a
926
- // resource the caller passes with a non-fediverse scheme) must
927
- // not leak that scheme into the metric attribute.
928
- fetchMock.removeRoutes();
929
- const [meterProvider, recorder] = createTestMeterProvider();
930
- // `ssh:` is not a WebFinger scheme; lookupWebFingerInternal will
931
- // attempt to build a host from the URL, fail, and return null.
932
- // The metric still records, and its scheme attribute must be
933
- // bucketed as `other`.
934
- await lookupMock("ssh://example.com/foo", { meterProvider });
935
- const counter = recorder.getMeasurement("webfinger.lookup");
936
- ok(counter != null);
937
- deepStrictEqual(
938
- counter.attributes["webfinger.resource.scheme"],
939
- "other",
940
- );
941
- },
942
- );
829
+ await t.step(
830
+ "records result=invalid for malformed Location headers",
831
+ async () => {
832
+ fetchMock.removeRoutes();
833
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
834
+ status: 302,
835
+ headers: { Location: "http://[bad" },
836
+ });
837
+ const [meterProvider, recorder] = createTestMeterProvider();
838
+ await lookupMock("acct:johndoe@example.com", { meterProvider });
839
+ const counter = recorder.getMeasurement("webfinger.lookup");
840
+ ok(counter != null);
841
+ deepStrictEqual(
842
+ counter.attributes["webfinger.lookup.result"],
843
+ "invalid",
844
+ );
845
+ deepStrictEqual(counter.attributes["http.response.status_code"], 302);
846
+ deepStrictEqual(
847
+ counter.attributes["activitypub.remote.host"],
848
+ "example.com",
849
+ );
850
+ },
851
+ );
943
852
 
944
- await t.step(
945
- "omits measurements when no meterProvider is provided",
946
- async () => {
947
- fetchMock.removeRoutes();
948
- fetchMock.get(
949
- "https://example.com/.well-known/webfinger?resource=acct%3Ajohndoe%40example.com",
950
- { body: expected },
951
- );
952
- const [_unused, recorder] = createTestMeterProvider();
953
- await lookupMock("acct:johndoe@example.com");
954
- deepStrictEqual(
955
- recorder.getMeasurements("webfinger.lookup").length,
956
- 0,
957
- );
958
- deepStrictEqual(
959
- recorder.getMeasurements("webfinger.lookup.duration").length,
960
- 0,
961
- );
962
- },
963
- );
964
- } finally {
965
- fetchMock.removeRoutes();
966
- fetchMock.hardReset();
967
- }
968
- });
853
+ await t.step(
854
+ "buckets unknown resource schemes as 'other' to keep metric cardinality bounded",
855
+ async () => {
856
+ // Lookups whose redirect chain ends on an unusual scheme (or a
857
+ // resource the caller passes with a non-fediverse scheme) must
858
+ // not leak that scheme into the metric attribute.
859
+ fetchMock.removeRoutes();
860
+ const [meterProvider, recorder] = createTestMeterProvider();
861
+ // `ssh:` is not a WebFinger scheme; lookupWebFingerInternal will
862
+ // attempt to build a host from the URL, fail, and return null.
863
+ // The metric still records, and its scheme attribute must be
864
+ // bucketed as `other`.
865
+ await lookupMock("ssh://example.com/foo", { meterProvider });
866
+ const counter = recorder.getMeasurement("webfinger.lookup");
867
+ ok(counter != null);
868
+ deepStrictEqual(
869
+ counter.attributes["webfinger.resource.scheme"],
870
+ "other",
871
+ );
872
+ },
873
+ );
874
+
875
+ await t.step(
876
+ "omits measurements when no meterProvider is provided",
877
+ async () => {
878
+ fetchMock.removeRoutes();
879
+ fetchMock.get(
880
+ "https://example.com/.well-known/webfinger?resource=acct%3Ajohndoe%40example.com",
881
+ { body: expected },
882
+ );
883
+ const [_unused, recorder] = createTestMeterProvider();
884
+ await lookupMock("acct:johndoe@example.com");
885
+ deepStrictEqual(
886
+ recorder.getMeasurements("webfinger.lookup").length,
887
+ 0,
888
+ );
889
+ deepStrictEqual(
890
+ recorder.getMeasurements("webfinger.lookup.duration").length,
891
+ 0,
892
+ );
893
+ },
894
+ );
895
+ } finally {
896
+ fetchMock.removeRoutes();
897
+ fetchMock.hardReset();
898
+ }
899
+ },
900
+ );
969
901
 
970
902
  // cSpell: ignore johndoe
971
903
 
@@ -1012,8 +944,8 @@ test("lookupWebFinger() logs DNS failures as such", async (t) => {
1012
944
  result === "throws"
1013
945
  ? Promise.reject(new Error("Resolver unavailable"))
1014
946
  : Promise.resolve(
1015
- result === "empty" ? [] : [{ address: "127.0.0.1", family: 4 }],
1016
- );
947
+ result === "empty" ? [] : [{ address: "127.0.0.1", family: 4 }],
948
+ );
1017
949
  const records: LogRecord[] = [];
1018
950
  await configure({
1019
951
  sinks: {
@@ -1036,18 +968,19 @@ test("lookupWebFinger() logs DNS failures as such", async (t) => {
1036
968
  ),
1037
969
  null,
1038
970
  );
1039
- const failures = records.filter((r) =>
1040
- r.rawMessage !==
1041
- "Fetching WebFinger resource descriptor from {url}..."
971
+ const failures = records.filter(
972
+ (r) =>
973
+ r.rawMessage !==
974
+ "Fetching WebFinger resource descriptor from {url}...",
1042
975
  );
1043
976
  deepStrictEqual(
1044
977
  failures.map((r) => [r.level, r.rawMessage]),
1045
978
  [
1046
979
  result === "private"
1047
980
  ? [
1048
- "error",
1049
- "Invalid URL for WebFinger resource descriptor: {error}",
1050
- ]
981
+ "error",
982
+ "Invalid URL for WebFinger resource descriptor: {error}",
983
+ ]
1051
984
  : ["debug", "DNS lookup failed for {url}"],
1052
985
  ],
1053
986
  );
@@ -1064,109 +997,68 @@ test("lookupWebFinger() logs DNS failures as such", async (t) => {
1064
997
  }
1065
998
  });
1066
999
 
1067
- test("lookupWebFinger() with withDiagnostics: true", {
1068
- sanitizeOps: false,
1069
- sanitizeResources: false,
1070
- }, async (t) => {
1071
- // Inject the same failed session as the connection-refused transport test,
1072
- // so the diagnostics path does not depend on real DNS or networking.
1073
- await t.step("network_error", async () => {
1074
- const outcome = await lookupMock(
1075
- "acct:johndoe@fedify-test.internal",
1076
- { withDiagnostics: true },
1077
- );
1078
- deepStrictEqual(outcome.resource, null);
1079
- deepStrictEqual(outcome.result, "network_error");
1080
- deepStrictEqual(
1081
- "statusCode" in outcome,
1082
- false,
1083
- "no HTTP response means no status code",
1084
- );
1085
- });
1086
-
1087
- fetchMock.spyGlobal();
1088
- try {
1089
- await t.step("found", async () => {
1090
- fetchMock.removeRoutes();
1091
- const expected: ResourceDescriptor = {
1092
- subject: "acct:johndoe@example.com",
1093
- links: [],
1094
- };
1095
- fetchMock.get(
1096
- "https://example.com/.well-known/webfinger?resource=acct%3Ajohndoe%40example.com",
1097
- { body: expected },
1098
- );
1099
- const outcome = await lookupMock("acct:johndoe@example.com", {
1100
- withDiagnostics: true,
1101
- });
1102
- deepStrictEqual(outcome.resource, expected);
1103
- deepStrictEqual(outcome.result, "found");
1104
- deepStrictEqual(outcome.statusCode, 200);
1105
- deepStrictEqual(outcome.remoteHost, "example.com");
1106
- });
1107
-
1108
- await t.step("not_found", async () => {
1109
- fetchMock.removeRoutes();
1110
- fetchMock.get(
1111
- "begin:https://example.com/.well-known/webfinger?",
1112
- { status: 404 },
1113
- );
1114
- const outcome = await lookupMock("acct:johndoe@example.com", {
1000
+ test(
1001
+ "lookupWebFinger() with withDiagnostics: true",
1002
+ {
1003
+ sanitizeOps: false,
1004
+ sanitizeResources: false,
1005
+ },
1006
+ async (t) => {
1007
+ // Inject the same failed session as the connection-refused transport test,
1008
+ // so the diagnostics path does not depend on real DNS or networking.
1009
+ await t.step("network_error", async () => {
1010
+ const outcome = await lookupMock("acct:johndoe@fedify-test.internal", {
1115
1011
  withDiagnostics: true,
1116
1012
  });
1117
1013
  deepStrictEqual(outcome.resource, null);
1118
- deepStrictEqual(outcome.result, "not_found");
1119
- deepStrictEqual(outcome.statusCode, 404);
1120
- deepStrictEqual(outcome.remoteHost, "example.com");
1121
- });
1122
-
1123
- await t.step("invalid", async () => {
1124
- fetchMock.removeRoutes();
1125
- fetchMock.get(
1126
- "begin:https://example.com/.well-known/webfinger?",
1127
- { body: "not json" },
1014
+ deepStrictEqual(outcome.result, "network_error");
1015
+ deepStrictEqual(
1016
+ "statusCode" in outcome,
1017
+ false,
1018
+ "no HTTP response means no status code",
1128
1019
  );
1129
- const outcome = await lookupMock("acct:johndoe@example.com", {
1130
- withDiagnostics: true,
1131
- });
1132
- deepStrictEqual(outcome.resource, null);
1133
- deepStrictEqual(outcome.result, "invalid");
1134
- deepStrictEqual(outcome.statusCode, 200);
1135
- deepStrictEqual(outcome.remoteHost, "example.com");
1136
1020
  });
1137
1021
 
1138
- await t.step(
1139
- "invalid: a literal JSON null body is not a resource descriptor",
1140
- async () => {
1141
- // JSON.parse("null") succeeds and yields the JS value `null`.
1142
- // Regression test for a server that returns 200 with a literal
1143
- // `null` body instead of an object: this must not be classified
1144
- // as "found" with a null resource.
1022
+ fetchMock.spyGlobal();
1023
+ try {
1024
+ await t.step("found", async () => {
1145
1025
  fetchMock.removeRoutes();
1026
+ const expected: ResourceDescriptor = {
1027
+ subject: "acct:johndoe@example.com",
1028
+ links: [],
1029
+ };
1146
1030
  fetchMock.get(
1147
- "begin:https://example.com/.well-known/webfinger?",
1148
- { body: "null" },
1031
+ "https://example.com/.well-known/webfinger?resource=acct%3Ajohndoe%40example.com",
1032
+ { body: expected },
1149
1033
  );
1150
1034
  const outcome = await lookupMock("acct:johndoe@example.com", {
1151
1035
  withDiagnostics: true,
1152
1036
  });
1153
- deepStrictEqual(outcome.resource, null);
1154
- deepStrictEqual(outcome.result, "invalid");
1037
+ deepStrictEqual(outcome.resource, expected);
1038
+ deepStrictEqual(outcome.result, "found");
1155
1039
  deepStrictEqual(outcome.statusCode, 200);
1156
1040
  deepStrictEqual(outcome.remoteHost, "example.com");
1157
- },
1158
- );
1041
+ });
1159
1042
 
1160
- await t.step(
1161
- "invalid: a JSON array body is not a resource descriptor",
1162
- async () => {
1163
- // typeof [] === "object" and [] !== null, so the null-only guard
1164
- // above does not catch an array body; it needs its own check.
1043
+ await t.step("not_found", async () => {
1165
1044
  fetchMock.removeRoutes();
1166
- fetchMock.get(
1167
- "begin:https://example.com/.well-known/webfinger?",
1168
- { body: "[]" },
1169
- );
1045
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
1046
+ status: 404,
1047
+ });
1048
+ const outcome = await lookupMock("acct:johndoe@example.com", {
1049
+ withDiagnostics: true,
1050
+ });
1051
+ deepStrictEqual(outcome.resource, null);
1052
+ deepStrictEqual(outcome.result, "not_found");
1053
+ deepStrictEqual(outcome.statusCode, 404);
1054
+ deepStrictEqual(outcome.remoteHost, "example.com");
1055
+ });
1056
+
1057
+ await t.step("invalid", async () => {
1058
+ fetchMock.removeRoutes();
1059
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
1060
+ body: "not json",
1061
+ });
1170
1062
  const outcome = await lookupMock("acct:johndoe@example.com", {
1171
1063
  withDiagnostics: true,
1172
1064
  });
@@ -1174,55 +1066,61 @@ test("lookupWebFinger() with withDiagnostics: true", {
1174
1066
  deepStrictEqual(outcome.result, "invalid");
1175
1067
  deepStrictEqual(outcome.statusCode, 200);
1176
1068
  deepStrictEqual(outcome.remoteHost, "example.com");
1177
- },
1178
- );
1179
-
1180
- await t.step("error", async () => {
1181
- fetchMock.removeRoutes();
1182
- fetchMock.get(
1183
- "begin:https://example.com/.well-known/webfinger?",
1184
- { status: 500 },
1185
- );
1186
- const outcome = await lookupMock("acct:johndoe@example.com", {
1187
- withDiagnostics: true,
1188
1069
  });
1189
- deepStrictEqual(outcome.resource, null);
1190
- deepStrictEqual(outcome.result, "error");
1191
- deepStrictEqual(outcome.statusCode, 500);
1192
- deepStrictEqual(outcome.remoteHost, "example.com");
1193
- });
1194
- } finally {
1195
- fetchMock.removeRoutes();
1196
- fetchMock.hardReset();
1197
- }
1198
- });
1199
1070
 
1200
- test(
1201
- "lookupWebFinger() accepts a plain LookupWebFingerOptions-typed variable",
1202
- async () => {
1203
- // Regression test: a variable typed as `LookupWebFingerOptions`, built
1204
- // separately from the call site and never mentioning `withDiagnostics`,
1205
- // must still match the default overload. This is a type-level
1206
- // concern; the point of this test is that the file compiles at all.
1207
- fetchMock.spyGlobal();
1208
- try {
1209
- const expected: ResourceDescriptor = {
1210
- subject: "acct:johndoe@example.com",
1211
- links: [],
1212
- };
1213
- fetchMock.get(
1214
- "https://example.com/.well-known/webfinger?resource=acct%3Ajohndoe%40example.com",
1215
- { body: expected },
1071
+ await t.step(
1072
+ "invalid: a literal JSON null body is not a resource descriptor",
1073
+ async () => {
1074
+ // JSON.parse("null") succeeds and yields the JS value `null`.
1075
+ // Regression test for a server that returns 200 with a literal
1076
+ // `null` body instead of an object: this must not be classified
1077
+ // as "found" with a null resource.
1078
+ fetchMock.removeRoutes();
1079
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
1080
+ body: "null",
1081
+ });
1082
+ const outcome = await lookupMock("acct:johndoe@example.com", {
1083
+ withDiagnostics: true,
1084
+ });
1085
+ deepStrictEqual(outcome.resource, null);
1086
+ deepStrictEqual(outcome.result, "invalid");
1087
+ deepStrictEqual(outcome.statusCode, 200);
1088
+ deepStrictEqual(outcome.remoteHost, "example.com");
1089
+ },
1216
1090
  );
1217
- const options: LookupWebFingerOptions = {
1218
- userAgent: "test-agent",
1219
- allowPrivateAddress: true,
1220
- };
1221
- const result = await lookupWebFinger(
1222
- "acct:johndoe@example.com",
1223
- options,
1091
+
1092
+ await t.step(
1093
+ "invalid: a JSON array body is not a resource descriptor",
1094
+ async () => {
1095
+ // typeof [] === "object" and [] !== null, so the null-only guard
1096
+ // above does not catch an array body; it needs its own check.
1097
+ fetchMock.removeRoutes();
1098
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
1099
+ body: "[]",
1100
+ });
1101
+ const outcome = await lookupMock("acct:johndoe@example.com", {
1102
+ withDiagnostics: true,
1103
+ });
1104
+ deepStrictEqual(outcome.resource, null);
1105
+ deepStrictEqual(outcome.result, "invalid");
1106
+ deepStrictEqual(outcome.statusCode, 200);
1107
+ deepStrictEqual(outcome.remoteHost, "example.com");
1108
+ },
1224
1109
  );
1225
- deepStrictEqual(result, expected);
1110
+
1111
+ await t.step("error", async () => {
1112
+ fetchMock.removeRoutes();
1113
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", {
1114
+ status: 500,
1115
+ });
1116
+ const outcome = await lookupMock("acct:johndoe@example.com", {
1117
+ withDiagnostics: true,
1118
+ });
1119
+ deepStrictEqual(outcome.resource, null);
1120
+ deepStrictEqual(outcome.result, "error");
1121
+ deepStrictEqual(outcome.statusCode, 500);
1122
+ deepStrictEqual(outcome.remoteHost, "example.com");
1123
+ });
1226
1124
  } finally {
1227
1125
  fetchMock.removeRoutes();
1228
1126
  fetchMock.hardReset();
@@ -1230,6 +1128,33 @@ test(
1230
1128
  },
1231
1129
  );
1232
1130
 
1131
+ test("lookupWebFinger() accepts a plain LookupWebFingerOptions-typed variable", async () => {
1132
+ // Regression test: a variable typed as `LookupWebFingerOptions`, built
1133
+ // separately from the call site and never mentioning `withDiagnostics`,
1134
+ // must still match the default overload. This is a type-level
1135
+ // concern; the point of this test is that the file compiles at all.
1136
+ fetchMock.spyGlobal();
1137
+ try {
1138
+ const expected: ResourceDescriptor = {
1139
+ subject: "acct:johndoe@example.com",
1140
+ links: [],
1141
+ };
1142
+ fetchMock.get(
1143
+ "https://example.com/.well-known/webfinger?resource=acct%3Ajohndoe%40example.com",
1144
+ { body: expected },
1145
+ );
1146
+ const options: LookupWebFingerOptions = {
1147
+ userAgent: "test-agent",
1148
+ allowPrivateAddress: true,
1149
+ };
1150
+ const result = await lookupWebFinger("acct:johndoe@example.com", options);
1151
+ deepStrictEqual(result, expected);
1152
+ } finally {
1153
+ fetchMock.removeRoutes();
1154
+ fetchMock.hardReset();
1155
+ }
1156
+ });
1157
+
1233
1158
  interface FakeResponse {
1234
1159
  readonly status?: number;
1235
1160
  readonly headers?: Record<string, string>;
@@ -1263,7 +1188,7 @@ function recordHops(respond: (url: URL) => FakeResponse): HopRecorder {
1263
1188
  let responses = 0;
1264
1189
  return {
1265
1190
  events,
1266
- // deno-lint-ignore require-await
1191
+ // oxlint-disable-next-line eslint/require-await
1267
1192
  async openHop(url) {
1268
1193
  opens++;
1269
1194
  events.push(`open ${url}`);
@@ -1271,7 +1196,7 @@ function recordHops(respond: (url: URL) => FakeResponse): HopRecorder {
1271
1196
  let closed = false;
1272
1197
  return {
1273
1198
  url: new URL(url),
1274
- // deno-lint-ignore require-await
1199
+ // oxlint-disable-next-line eslint/require-await
1275
1200
  async fetch(input) {
1276
1201
  if (closed) throw new TypeError("The fetch session is closed.");
1277
1202
  const target = new URL(input instanceof Request ? input.url : input);
@@ -1281,24 +1206,27 @@ function recordHops(respond: (url: URL) => FakeResponse): HopRecorder {
1281
1206
  const state = { id, settled: false };
1282
1207
  states.push(state);
1283
1208
  let sent = false;
1284
- const body = new ReadableStream<Uint8Array>({
1285
- pull(controller) {
1286
- if (!sent && fake.body != null && fake.body !== "") {
1287
- sent = true;
1288
- controller.enqueue(new TextEncoder().encode(fake.body));
1289
- return;
1290
- }
1291
- state.settled = true;
1292
- events.push(`consumed #${id}`);
1293
- controller.close();
1294
- },
1295
- cancel() {
1296
- state.settled = true;
1297
- events.push(`cancel #${id}`);
1298
- if (fake.cancelFails) throw new Error("Cancellation failed");
1299
- if (fake.cancelNeverSettles) return new Promise(() => {});
1209
+ const body = new ReadableStream<Uint8Array>(
1210
+ {
1211
+ pull(controller) {
1212
+ if (!sent && fake.body != null && fake.body !== "") {
1213
+ sent = true;
1214
+ controller.enqueue(new TextEncoder().encode(fake.body));
1215
+ return;
1216
+ }
1217
+ state.settled = true;
1218
+ events.push(`consumed #${id}`);
1219
+ controller.close();
1220
+ },
1221
+ cancel() {
1222
+ state.settled = true;
1223
+ events.push(`cancel #${id}`);
1224
+ if (fake.cancelFails) throw new Error("Cancellation failed");
1225
+ if (fake.cancelNeverSettles) return new Promise(() => {});
1226
+ },
1300
1227
  },
1301
- }, { highWaterMark: 0 });
1228
+ { highWaterMark: 0 },
1229
+ );
1302
1230
  const response = new Response(body, {
1303
1231
  status: fake.status ?? 200,
1304
1232
  headers: fake.headers,
@@ -1308,7 +1236,7 @@ function recordHops(respond: (url: URL) => FakeResponse): HopRecorder {
1308
1236
  }
1309
1237
  return response;
1310
1238
  },
1311
- // deno-lint-ignore require-await
1239
+ // oxlint-disable-next-line eslint/require-await
1312
1240
  async close() {
1313
1241
  if (closed) return;
1314
1242
  closed = true;
@@ -1340,19 +1268,23 @@ test("lookupWebFinger() closes each hop after cleaning up its body", async (t) =
1340
1268
  const recorder = recordHops((url) =>
1341
1269
  url.pathname === "/.well-known/webfinger"
1342
1270
  ? {
1343
- status: 302,
1344
- headers: { Location: "/.well-known/webfinger2" },
1345
- body: "redirecting",
1346
- // A pinned response's url has the IP address, which must not be
1347
- // used as the base of the Location:
1348
- url: url.href.replace("example.com", "93.184.215.14"),
1349
- }
1350
- : { body: JSON.stringify(descriptor) }
1271
+ status: 302,
1272
+ headers: { Location: "/.well-known/webfinger2" },
1273
+ body: "redirecting",
1274
+ // A pinned response's url has the IP address, which must not be
1275
+ // used as the base of the Location:
1276
+ url: url.href.replace("example.com", "93.184.215.14"),
1277
+ }
1278
+ : { body: JSON.stringify(descriptor) },
1351
1279
  );
1352
1280
  deepStrictEqual(
1353
- await lookupResourceWithDependencies("acct:johndoe@example.com", {}, {
1354
- openHop: recorder.openHop,
1355
- }),
1281
+ await lookupResourceWithDependencies(
1282
+ "acct:johndoe@example.com",
1283
+ {},
1284
+ {
1285
+ openHop: recorder.openHop,
1286
+ },
1287
+ ),
1356
1288
  descriptor,
1357
1289
  );
1358
1290
  deepStrictEqual(recorder.events, [
@@ -1371,9 +1303,13 @@ test("lookupWebFinger() closes each hop after cleaning up its body", async (t) =
1371
1303
  await t.step("non-2xx response", async () => {
1372
1304
  const recorder = recordHops(() => ({ status: 404, body: "not found" }));
1373
1305
  deepStrictEqual(
1374
- await lookupResourceWithDependencies("acct:johndoe@example.com", {}, {
1375
- openHop: recorder.openHop,
1376
- }),
1306
+ await lookupResourceWithDependencies(
1307
+ "acct:johndoe@example.com",
1308
+ {},
1309
+ {
1310
+ openHop: recorder.openHop,
1311
+ },
1312
+ ),
1377
1313
  null,
1378
1314
  );
1379
1315
  deepStrictEqual(recorder.events, [
@@ -1392,9 +1328,13 @@ test("lookupWebFinger() closes each hop after cleaning up its body", async (t) =
1392
1328
  body: "redirecting",
1393
1329
  }));
1394
1330
  deepStrictEqual(
1395
- await lookupResourceWithDependencies("acct:johndoe@example.com", {
1396
- maxRedirection: 1,
1397
- }, { openHop: recorder.openHop }),
1331
+ await lookupResourceWithDependencies(
1332
+ "acct:johndoe@example.com",
1333
+ {
1334
+ maxRedirection: 1,
1335
+ },
1336
+ { openHop: recorder.openHop },
1337
+ ),
1398
1338
  null,
1399
1339
  );
1400
1340
  deepStrictEqual(recorder.events, [
@@ -1414,12 +1354,12 @@ test("lookupWebFinger() closes each hop after cleaning up its body", async (t) =
1414
1354
  const recorder = recordHops((url) =>
1415
1355
  url.pathname === "/.well-known/webfinger"
1416
1356
  ? {
1417
- status: 302,
1418
- headers: { Location: "/.well-known/webfinger2" },
1419
- body: "redirecting",
1420
- cancelFails: true,
1421
- }
1422
- : { body: JSON.stringify(descriptor) }
1357
+ status: 302,
1358
+ headers: { Location: "/.well-known/webfinger2" },
1359
+ body: "redirecting",
1360
+ cancelFails: true,
1361
+ }
1362
+ : { body: JSON.stringify(descriptor) },
1423
1363
  );
1424
1364
  const records: LogRecord[] = [];
1425
1365
  await configure({
@@ -1432,14 +1372,19 @@ test("lookupWebFinger() closes each hop after cleaning up its body", async (t) =
1432
1372
  });
1433
1373
  try {
1434
1374
  deepStrictEqual(
1435
- await lookupResourceWithDependencies("acct:johndoe@example.com", {}, {
1436
- openHop: recorder.openHop,
1437
- }),
1375
+ await lookupResourceWithDependencies(
1376
+ "acct:johndoe@example.com",
1377
+ {},
1378
+ {
1379
+ openHop: recorder.openHop,
1380
+ },
1381
+ ),
1438
1382
  descriptor,
1439
1383
  );
1440
- const failures = records.filter((r) =>
1441
- r.rawMessage ===
1442
- "Failed to cancel the response body from {url}: {error}"
1384
+ const failures = records.filter(
1385
+ (r) =>
1386
+ r.rawMessage ===
1387
+ "Failed to cancel the response body from {url}: {error}",
1443
1388
  );
1444
1389
  deepStrictEqual(
1445
1390
  failures.map((r) => [r.level, r.properties.url]),
@@ -1468,18 +1413,22 @@ test("lookupWebFinger() closes each hop after cleaning up its body", async (t) =
1468
1413
  const recorder = recordHops((url) =>
1469
1414
  url.pathname === "/.well-known/webfinger"
1470
1415
  ? {
1471
- status: 302,
1472
- headers: { Location: "/.well-known/webfinger2" },
1473
- body: "redirecting",
1474
- cancelNeverSettles: true,
1475
- }
1476
- : { status: 404, body: "not found", cancelNeverSettles: true }
1416
+ status: 302,
1417
+ headers: { Location: "/.well-known/webfinger2" },
1418
+ body: "redirecting",
1419
+ cancelNeverSettles: true,
1420
+ }
1421
+ : { status: 404, body: "not found", cancelNeverSettles: true },
1477
1422
  );
1478
1423
  deepStrictEqual(
1479
1424
  await settleWithin(
1480
- lookupResourceWithDependencies("acct:johndoe@example.com", {}, {
1481
- openHop: recorder.openHop,
1482
- }),
1425
+ lookupResourceWithDependencies(
1426
+ "acct:johndoe@example.com",
1427
+ {},
1428
+ {
1429
+ openHop: recorder.openHop,
1430
+ },
1431
+ ),
1483
1432
  ),
1484
1433
  null,
1485
1434
  );
@@ -1543,13 +1492,11 @@ test("lookupWebFinger() does not wait for cancelling response bodies", async (t)
1543
1492
  try {
1544
1493
  await t.step("redirect", async () => {
1545
1494
  fetchMock.removeRoutes();
1546
- fetchMock.get(
1547
- "begin:https://example.com/.well-known/webfinger?",
1548
- () =>
1549
- neverCancellableResponse({
1550
- status: 302,
1551
- headers: { Location: "/.well-known/webfinger2" },
1552
- }),
1495
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", () =>
1496
+ neverCancellableResponse({
1497
+ status: 302,
1498
+ headers: { Location: "/.well-known/webfinger2" },
1499
+ }),
1553
1500
  );
1554
1501
  fetchMock.get(
1555
1502
  "https://example.com/.well-known/webfinger2",
@@ -1566,9 +1513,8 @@ test("lookupWebFinger() does not wait for cancelling response bodies", async (t)
1566
1513
 
1567
1514
  await t.step("non-2xx response", async () => {
1568
1515
  fetchMock.removeRoutes();
1569
- fetchMock.get(
1570
- "begin:https://example.com/.well-known/webfinger?",
1571
- () => neverCancellableResponse({ status: 404 }),
1516
+ fetchMock.get("begin:https://example.com/.well-known/webfinger?", () =>
1517
+ neverCancellableResponse({ status: 404 }),
1572
1518
  );
1573
1519
  deepStrictEqual(
1574
1520
  await settleWithin(lookupDirect("acct:johndoe@example.com")),
@@ -1583,13 +1529,11 @@ test("lookupWebFinger() does not wait for cancelling response bodies", async (t)
1583
1529
  nodeTest(
1584
1530
  "lookupWebFinger() records network_error for rejected hop addresses",
1585
1531
  async () => {
1586
- for (
1587
- const lookup of [
1588
- () => Promise.reject(new Error("DNS unavailable")),
1589
- () => Promise.resolve([]),
1590
- () => Promise.resolve([{ address: "127.0.0.1", family: 4 }]),
1591
- ]
1592
- ) {
1532
+ for (const lookup of [
1533
+ () => Promise.reject(new Error("DNS unavailable")),
1534
+ () => Promise.resolve([]),
1535
+ () => Promise.resolve([{ address: "127.0.0.1", family: 4 }]),
1536
+ ]) {
1593
1537
  const [meterProvider, recorder] = createTestMeterProvider();
1594
1538
  const result = await lookupWebFingerWithDependencies(
1595
1539
  "acct:alice@dns-failure.invalid",
@@ -1635,8 +1579,9 @@ nodeTest(
1635
1579
  );
1636
1580
  hops.assertInvariants();
1637
1581
  deepStrictEqual(
1638
- recorder.getMeasurement("webfinger.lookup")
1639
- ?.attributes["webfinger.lookup.result"],
1582
+ recorder.getMeasurement("webfinger.lookup")?.attributes[
1583
+ "webfinger.lookup.result"
1584
+ ],
1640
1585
  "invalid",
1641
1586
  );
1642
1587
  }