@fedify/fedify 2.2.15 → 2.2.17

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.
Files changed (62) hide show
  1. package/dist/{builder-CcReUrBQ.mjs → builder-a-kl29PE.mjs} +3 -3
  2. package/dist/compat/outgoing-jsonld.test.mjs +1 -1
  3. package/dist/compat/public-audience.test.mjs +1 -1
  4. package/dist/compat/transformers.test.mjs +2 -2
  5. package/dist/{deno-duAPwAHH.mjs → deno-Cya0rbEr.mjs} +1 -1
  6. package/dist/{docloader-DI88Lx6y.mjs → docloader-Dz0Z1Y07.mjs} +2 -2
  7. package/dist/federation/builder.test.mjs +2 -2
  8. package/dist/federation/collection.test.mjs +1 -1
  9. package/dist/federation/handler.test.mjs +7 -7
  10. package/dist/federation/idempotency.test.mjs +3 -3
  11. package/dist/federation/inbox.test.mjs +1 -1
  12. package/dist/federation/keycache.test.mjs +33 -2
  13. package/dist/federation/kv.test.mjs +1 -1
  14. package/dist/federation/middleware.test.mjs +7 -7
  15. package/dist/federation/mod.cjs +1 -1
  16. package/dist/federation/mod.js +1 -1
  17. package/dist/federation/send.test.mjs +3 -3
  18. package/dist/federation/temporal.test.mjs +1 -1
  19. package/dist/federation/webfinger.test.mjs +2 -2
  20. package/dist/{http-CJuCjxWI.cjs → http-C_aA2Zx1.cjs} +103 -9
  21. package/dist/{http-DlBFaZ7A.js → http-DkzPSl5o.js} +104 -10
  22. package/dist/{http-DytBXprv.mjs → http-dobCLGG2.mjs} +2 -2
  23. package/dist/{key-5QlWv3sx.mjs → key-BMHNd0gP.mjs} +104 -10
  24. package/dist/{keycache-C1aVprwy.mjs → keycache-Dkc_sflV.mjs} +23 -2
  25. package/dist/{kv-cache-K6z1D4HS.cjs → kv-cache-Btp4kC_U.cjs} +1 -1
  26. package/dist/{kv-cache-DJ3q30Nl.js → kv-cache-DVhfMtqE.js} +1 -1
  27. package/dist/{ld-D-oK3e4r.mjs → ld-B_m0qw8j.mjs} +2 -2
  28. package/dist/{middleware-DTIhauqW.mjs → middleware-5qLdPLro.mjs} +15 -15
  29. package/dist/{middleware-ROzi4oVa.mjs → middleware-BYxKsDHj.mjs} +1 -1
  30. package/dist/{middleware-CYAtT9KB.js → middleware-BchkzbpG.js} +25 -5
  31. package/dist/{middleware-IHStfDZ2.cjs → middleware-C7mI18WE.cjs} +26 -6
  32. package/dist/{middleware-RmEidCmR.cjs → middleware-ZKHfZ7zj.cjs} +1 -1
  33. package/dist/mod.cjs +4 -4
  34. package/dist/mod.js +4 -4
  35. package/dist/nodeinfo/client.test.mjs +1 -1
  36. package/dist/nodeinfo/handler.test.mjs +2 -2
  37. package/dist/nodeinfo/types.test.mjs +1 -1
  38. package/dist/otel/exporter.test.mjs +1 -1
  39. package/dist/{outgoing-jsonld-Dc2OXyB-.mjs → outgoing-jsonld-amccRMrV.mjs} +1 -1
  40. package/dist/{owner-CHkNw8Fk.mjs → owner-Dk85kXZz.mjs} +2 -2
  41. package/dist/{proof-BJ_pqvov.js → proof-Bz6IdT2p.js} +1 -1
  42. package/dist/{proof-DC3IlY_8.cjs → proof-ClpE9zhD.cjs} +1 -1
  43. package/dist/{proof-o8Ly9XSp.mjs → proof-DrGrnZVE.mjs} +4 -4
  44. package/dist/{send-Ck74ndlE.mjs → send-Co-E7yJn.mjs} +2 -2
  45. package/dist/sig/http.test.mjs +62 -3
  46. package/dist/sig/key.test.mjs +198 -3
  47. package/dist/sig/ld.test.mjs +2 -2
  48. package/dist/sig/mod.cjs +2 -2
  49. package/dist/sig/mod.js +2 -2
  50. package/dist/sig/owner.test.mjs +1 -1
  51. package/dist/sig/proof.test.mjs +2 -2
  52. package/dist/{temporal-8bTjf1mO.mjs → temporal-Bnwa_2NC.mjs} +1 -1
  53. package/dist/utils/docloader.test.mjs +2 -2
  54. package/dist/utils/mod.cjs +1 -1
  55. package/dist/utils/mod.js +1 -1
  56. package/package.json +5 -5
  57. /package/dist/{activity-listener-IojJ9SVl.mjs → activity-listener-ell7W1s9.mjs} +0 -0
  58. /package/dist/{client-CJ4Y0keQ.mjs → client-9F_u23dc.mjs} +0 -0
  59. /package/dist/{collection-DnoYNwBC.mjs → collection-D-HqUuA2.mjs} +0 -0
  60. /package/dist/{kv-CX8qfaad.mjs → kv-mI4Ix2EU.mjs} +0 -0
  61. /package/dist/{public-audience-CS1udGkN.mjs → public-audience-DpJqzPuh.mjs} +0 -0
  62. /package/dist/{types-s4mCTGkT.mjs → types-J53Kw7so.mjs} +0 -0
@@ -4,13 +4,13 @@ import { getLogger } from "@logtape/logtape";
4
4
  import { CryptographicKey, Object as Object$1, isActor } from "@fedify/vocab";
5
5
  import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
6
6
  import { encodeHex } from "byte-encodings/hex";
7
- import { FetchError, getDocumentLoader } from "@fedify/vocab-runtime";
7
+ import { FetchError, UrlError, getDocumentLoader } from "@fedify/vocab-runtime";
8
8
  import { Item, decodeDict, encodeDict, encodeItem } from "structured-field-values";
9
9
  import { ATTR_HTTP_REQUEST_HEADER, ATTR_HTTP_REQUEST_METHOD, ATTR_URL_FULL } from "@opentelemetry/semantic-conventions";
10
10
  import { decodeBase64, encodeBase64 } from "byte-encodings/base64";
11
11
  //#region deno.json
12
12
  var name = "@fedify/fedify";
13
- var version = "2.2.15";
13
+ var version = "2.2.17";
14
14
  //#endregion
15
15
  //#region src/sig/accept.ts
16
16
  /**
@@ -152,6 +152,64 @@ function fulfillAcceptSignature(entry, localKeyId, localAlg) {
152
152
  }
153
153
  //#endregion
154
154
  //#region src/sig/key.ts
155
+ /** Classifies decoding failures without hiding unexpected loader errors. */
156
+ function classifyKeyJsonLdError(error, seen = /* @__PURE__ */ new Set()) {
157
+ if (!(error instanceof Error) || !error.name.startsWith("jsonld.")) return null;
158
+ if (seen.has(error)) throw error;
159
+ seen.add(error);
160
+ const details = error.details;
161
+ if (typeof details?.code !== "string") return null;
162
+ if (details.code !== "loading remote context failed" || !("cause" in details) && !("cause" in error)) return "invalid";
163
+ const cause = "cause" in details ? details.cause : error.cause;
164
+ const nestedFailure = classifyKeyJsonLdError(cause, seen);
165
+ if (nestedFailure != null) return nestedFailure;
166
+ if (cause instanceof SyntaxError) return "invalid";
167
+ if (cause instanceof FetchError || cause instanceof UrlError) return "fetch";
168
+ if (cause instanceof Error && [
169
+ "AbortError",
170
+ "TimeoutError",
171
+ "NetworkError"
172
+ ].includes(cause.name)) return "fetch";
173
+ if (cause instanceof TypeError) {
174
+ const urlCode = cause.code;
175
+ if (cause.name === "InvalidContextReferenceError") return "invalid";
176
+ if (urlCode === "ERR_INVALID_URL" || cause.message === "Invalid URL string." || /^Invalid URL(?::|$)/.test(cause.message) || / cannot be parsed as a URL\.?$/.test(cause.message)) return typeof details.url === "string" && /^[A-Za-z][A-Za-z0-9+.-]*:/.test(details.url) && !URL.canParse(details.url) ? "invalid" : "fetch";
177
+ const networkCause = cause.cause;
178
+ const code = cause.code ?? networkCause?.code;
179
+ if (typeof code === "string" && [
180
+ "ECONNREFUSED",
181
+ "ECONNRESET",
182
+ "ECONNABORTED",
183
+ "ETIMEDOUT",
184
+ "EHOSTUNREACH",
185
+ "ENETUNREACH",
186
+ "ENOTFOUND",
187
+ "EAI_AGAIN",
188
+ "UND_ERR_CONNECT_TIMEOUT",
189
+ "UND_ERR_HEADERS_TIMEOUT",
190
+ "UND_ERR_BODY_TIMEOUT",
191
+ "UND_ERR_SOCKET"
192
+ ].includes(code) || /^(fetch failed|Failed to fetch|error sending request|error reading a body from connection|Unable to connect|ConnectionRefused|Network connection lost|The socket connection was closed unexpectedly)/.test(cause.message)) return "fetch";
193
+ }
194
+ throw cause;
195
+ }
196
+ /** Carries a context transport failure through the key decoding boundary. */
197
+ var KeyContextFetchError = class extends Error {
198
+ constructor(error) {
199
+ super("Failed to fetch a key's JSON-LD context.", { cause: error });
200
+ this.error = error;
201
+ }
202
+ };
203
+ function contextFetchError(error) {
204
+ const seen = /* @__PURE__ */ new Set();
205
+ while (error instanceof Error && !seen.has(error)) {
206
+ seen.add(error);
207
+ const details = error.details;
208
+ if (!error.name.startsWith("jsonld.") || details?.code !== "loading remote context failed") break;
209
+ error = "cause" in details ? details.cause : error.cause;
210
+ }
211
+ return new KeyContextFetchError(error);
212
+ }
155
213
  /**
156
214
  * Checks if the given key is valid and supported. No-op if the key is valid,
157
215
  * otherwise throws an error.
@@ -246,6 +304,9 @@ async function importJwk(jwk, type) {
246
304
  * @internal
247
305
  */
248
306
  async function fetchActorDocument(actorId, options = {}) {
307
+ return await fetchActorDocumentInternal(actorId, options, false);
308
+ }
309
+ async function fetchActorDocumentInternal(actorId, options, propagateContextFetchErrors) {
249
310
  const logger = getLogger([
250
311
  "fedify",
251
312
  "sig",
@@ -276,9 +337,12 @@ async function fetchActorDocument(actorId, options = {}) {
276
337
  baseUrl: documentUrl
277
338
  });
278
339
  } catch (error) {
279
- if (!(error instanceof TypeError)) throw error;
280
- logger.debug("The document served at {documentUrl} is not a valid object: {error}", {
340
+ const failure = classifyKeyJsonLdError(error);
341
+ if (failure === "fetch" && propagateContextFetchErrors) throw contextFetchError(error);
342
+ if (failure == null && !(error instanceof TypeError)) throw error;
343
+ logger.debug("Failed to decode the actor document at {documentUrl}: {error}", {
281
344
  documentUrl: documentUrl.href,
345
+ failure: failure ?? "invalid",
282
346
  error
283
347
  });
284
348
  return null;
@@ -317,6 +381,9 @@ async function fetchActorDocument(actorId, options = {}) {
317
381
  * @internal
318
382
  */
319
383
  async function verifyKeyOwnership(key, options = {}) {
384
+ return await verifyKeyOwnershipInternal(key, options, false);
385
+ }
386
+ async function verifyKeyOwnershipInternal(key, options, propagateContextFetchErrors) {
320
387
  const logger = getLogger([
321
388
  "fedify",
322
389
  "sig",
@@ -326,7 +393,7 @@ async function verifyKeyOwnership(key, options = {}) {
326
393
  if (keyId == null) return null;
327
394
  const claimedOwnerId = key instanceof CryptographicKey ? key.ownerId : key.controllerId;
328
395
  if (claimedOwnerId == null) return null;
329
- const owner = await fetchActorDocument(claimedOwnerId, options);
396
+ const owner = await fetchActorDocumentInternal(claimedOwnerId, options, propagateContextFetchErrors);
330
397
  if (owner == null) {
331
398
  logger.debug("The owner ({claimedOwnerId}) that key {keyId} claims could not be resolved.", {
332
399
  keyId: keyId.href,
@@ -478,6 +545,21 @@ async function resolveFetchedKey(document, cacheKey, documentUrl, keyId, cls, {
478
545
  tracerProvider
479
546
  });
480
547
  } catch (e) {
548
+ const failure = classifyKeyJsonLdError(e);
549
+ if (failure === "fetch") throw contextFetchError(e);
550
+ if (failure != null) {
551
+ logger.debug("Failed to decode key {keyId}: {error}", {
552
+ keyId,
553
+ failure,
554
+ error: e
555
+ });
556
+ await keyCache?.set(cacheKey, null);
557
+ await clearFetchErrorMetadata(cacheKey, keyCache);
558
+ return {
559
+ key: null,
560
+ cached: false
561
+ };
562
+ }
481
563
  if (!(e instanceof TypeError)) throw e;
482
564
  try {
483
565
  object = await cls.fromJsonLd(document, {
@@ -486,8 +568,14 @@ async function resolveFetchedKey(document, cacheKey, documentUrl, keyId, cls, {
486
568
  tracerProvider
487
569
  });
488
570
  } catch (e) {
489
- if (e instanceof TypeError) {
490
- logger.debug("Failed to verify; key {keyId} returned an invalid object.", { keyId });
571
+ const failure = classifyKeyJsonLdError(e);
572
+ if (failure === "fetch") throw contextFetchError(e);
573
+ if (failure != null || e instanceof TypeError) {
574
+ logger.debug("Failed to decode key {keyId}: {error}", {
575
+ keyId,
576
+ failure: failure ?? "invalid",
577
+ error: e
578
+ });
491
579
  await keyCache?.set(cacheKey, null);
492
580
  await clearFetchErrorMetadata(cacheKey, keyCache);
493
581
  return {
@@ -579,16 +667,17 @@ async function resolveFetchedKey(document, cacheKey, documentUrl, keyId, cls, {
579
667
  const claimedOwnerId = key instanceof CryptographicKey ? key.ownerId : key.controllerId;
580
668
  if (ownerDocument != null && claimedOwnerId == null) key = key instanceof CryptographicKey ? key.clone({ owner: ownerDocument.id }) : key.clone({ controller: ownerDocument.id });
581
669
  else if (claimedOwnerId != null && claimedOwnerId.href !== ownerDocument?.id?.href) {
582
- if (await verifyKeyOwnership(key, {
670
+ if (await verifyKeyOwnershipInternal(key, {
583
671
  documentLoader,
584
672
  contextLoader,
585
673
  tracerProvider
586
- }) == null) {
674
+ }, true) == null) {
587
675
  logger.debug("Failed to verify; the owner {claimedOwnerId} that key {keyId} claims does not list the key as its own.", {
588
676
  keyId,
589
677
  claimedOwnerId: claimedOwnerId.href
590
678
  });
591
679
  await keyCache?.set(cacheKey, null);
680
+ await clearFetchErrorMetadata(cacheKey, keyCache);
592
681
  return {
593
682
  key: null,
594
683
  cached: false
@@ -626,7 +715,12 @@ async function fetchKeyWithResult(cacheKey, cls, options, onCachedUnavailable, o
626
715
  } catch (error) {
627
716
  return await onFetchError(error, cacheKey, keyId, keyCache, logger);
628
717
  }
629
- return await resolveFetchedKey(document, cacheKey, documentUrl, keyId, cls, options, logger);
718
+ try {
719
+ return await resolveFetchedKey(document, cacheKey, documentUrl, keyId, cls, options, logger);
720
+ } catch (error) {
721
+ if (!(error instanceof KeyContextFetchError)) throw error;
722
+ return await onFetchError(error.error, cacheKey, keyId, keyCache, logger);
723
+ }
630
724
  }
631
725
  async function fetchKeyInternal(keyId, cls, options = {}) {
632
726
  return await fetchKeyWithResult(typeof keyId === "string" ? new URL(keyId) : keyId, cls, options, (_cacheKey, _keyId, _keyCache, _logger) => {
@@ -1,9 +1,9 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
- import { n as version, t as name } from "./deno-duAPwAHH.mjs";
4
+ import { n as version, t as name } from "./deno-Cya0rbEr.mjs";
5
5
  import { i as validateAcceptSignature, n as fulfillAcceptSignature, r as parseAcceptSignature } from "./accept-Dd__NiUL.mjs";
6
- import { i as fetchKeyDetailed, s as validateCryptoKey } from "./key-5QlWv3sx.mjs";
6
+ import { i as fetchKeyDetailed, s as validateCryptoKey } from "./key-BMHNd0gP.mjs";
7
7
  import { CryptographicKey } from "@fedify/vocab";
8
8
  import { SpanStatusCode, trace } from "@opentelemetry/api";
9
9
  import { FetchError } from "@fedify/vocab-runtime";
@@ -1,12 +1,70 @@
1
1
  import "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
- import { n as version, t as name } from "./deno-duAPwAHH.mjs";
4
+ import { n as version, t as name } from "./deno-Cya0rbEr.mjs";
5
5
  import { CryptographicKey, Object as Object$1, isActor } from "@fedify/vocab";
6
6
  import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
7
- import { FetchError, getDocumentLoader } from "@fedify/vocab-runtime";
7
+ import { FetchError, UrlError, getDocumentLoader } from "@fedify/vocab-runtime";
8
8
  import { getLogger } from "@logtape/logtape";
9
9
  //#region src/sig/key.ts
10
+ /** Classifies decoding failures without hiding unexpected loader errors. */
11
+ function classifyKeyJsonLdError(error, seen = /* @__PURE__ */ new Set()) {
12
+ if (!(error instanceof Error) || !error.name.startsWith("jsonld.")) return null;
13
+ if (seen.has(error)) throw error;
14
+ seen.add(error);
15
+ const details = error.details;
16
+ if (typeof details?.code !== "string") return null;
17
+ if (details.code !== "loading remote context failed" || !("cause" in details) && !("cause" in error)) return "invalid";
18
+ const cause = "cause" in details ? details.cause : error.cause;
19
+ const nestedFailure = classifyKeyJsonLdError(cause, seen);
20
+ if (nestedFailure != null) return nestedFailure;
21
+ if (cause instanceof SyntaxError) return "invalid";
22
+ if (cause instanceof FetchError || cause instanceof UrlError) return "fetch";
23
+ if (cause instanceof Error && [
24
+ "AbortError",
25
+ "TimeoutError",
26
+ "NetworkError"
27
+ ].includes(cause.name)) return "fetch";
28
+ if (cause instanceof TypeError) {
29
+ const urlCode = cause.code;
30
+ if (cause.name === "InvalidContextReferenceError") return "invalid";
31
+ if (urlCode === "ERR_INVALID_URL" || cause.message === "Invalid URL string." || /^Invalid URL(?::|$)/.test(cause.message) || / cannot be parsed as a URL\.?$/.test(cause.message)) return typeof details.url === "string" && /^[A-Za-z][A-Za-z0-9+.-]*:/.test(details.url) && !URL.canParse(details.url) ? "invalid" : "fetch";
32
+ const networkCause = cause.cause;
33
+ const code = cause.code ?? networkCause?.code;
34
+ if (typeof code === "string" && [
35
+ "ECONNREFUSED",
36
+ "ECONNRESET",
37
+ "ECONNABORTED",
38
+ "ETIMEDOUT",
39
+ "EHOSTUNREACH",
40
+ "ENETUNREACH",
41
+ "ENOTFOUND",
42
+ "EAI_AGAIN",
43
+ "UND_ERR_CONNECT_TIMEOUT",
44
+ "UND_ERR_HEADERS_TIMEOUT",
45
+ "UND_ERR_BODY_TIMEOUT",
46
+ "UND_ERR_SOCKET"
47
+ ].includes(code) || /^(fetch failed|Failed to fetch|error sending request|error reading a body from connection|Unable to connect|ConnectionRefused|Network connection lost|The socket connection was closed unexpectedly)/.test(cause.message)) return "fetch";
48
+ }
49
+ throw cause;
50
+ }
51
+ /** Carries a context transport failure through the key decoding boundary. */
52
+ var KeyContextFetchError = class extends Error {
53
+ constructor(error) {
54
+ super("Failed to fetch a key's JSON-LD context.", { cause: error });
55
+ this.error = error;
56
+ }
57
+ };
58
+ function contextFetchError(error) {
59
+ const seen = /* @__PURE__ */ new Set();
60
+ while (error instanceof Error && !seen.has(error)) {
61
+ seen.add(error);
62
+ const details = error.details;
63
+ if (!error.name.startsWith("jsonld.") || details?.code !== "loading remote context failed") break;
64
+ error = "cause" in details ? details.cause : error.cause;
65
+ }
66
+ return new KeyContextFetchError(error);
67
+ }
10
68
  /**
11
69
  * Checks if the given key is valid and supported. No-op if the key is valid,
12
70
  * otherwise throws an error.
@@ -101,6 +159,9 @@ async function importJwk(jwk, type) {
101
159
  * @internal
102
160
  */
103
161
  async function fetchActorDocument(actorId, options = {}) {
162
+ return await fetchActorDocumentInternal(actorId, options, false);
163
+ }
164
+ async function fetchActorDocumentInternal(actorId, options, propagateContextFetchErrors) {
104
165
  const logger = getLogger([
105
166
  "fedify",
106
167
  "sig",
@@ -131,9 +192,12 @@ async function fetchActorDocument(actorId, options = {}) {
131
192
  baseUrl: documentUrl
132
193
  });
133
194
  } catch (error) {
134
- if (!(error instanceof TypeError)) throw error;
135
- logger.debug("The document served at {documentUrl} is not a valid object: {error}", {
195
+ const failure = classifyKeyJsonLdError(error);
196
+ if (failure === "fetch" && propagateContextFetchErrors) throw contextFetchError(error);
197
+ if (failure == null && !(error instanceof TypeError)) throw error;
198
+ logger.debug("Failed to decode the actor document at {documentUrl}: {error}", {
136
199
  documentUrl: documentUrl.href,
200
+ failure: failure ?? "invalid",
137
201
  error
138
202
  });
139
203
  return null;
@@ -172,6 +236,9 @@ async function fetchActorDocument(actorId, options = {}) {
172
236
  * @internal
173
237
  */
174
238
  async function verifyKeyOwnership(key, options = {}) {
239
+ return await verifyKeyOwnershipInternal(key, options, false);
240
+ }
241
+ async function verifyKeyOwnershipInternal(key, options, propagateContextFetchErrors) {
175
242
  const logger = getLogger([
176
243
  "fedify",
177
244
  "sig",
@@ -181,7 +248,7 @@ async function verifyKeyOwnership(key, options = {}) {
181
248
  if (keyId == null) return null;
182
249
  const claimedOwnerId = key instanceof CryptographicKey ? key.ownerId : key.controllerId;
183
250
  if (claimedOwnerId == null) return null;
184
- const owner = await fetchActorDocument(claimedOwnerId, options);
251
+ const owner = await fetchActorDocumentInternal(claimedOwnerId, options, propagateContextFetchErrors);
185
252
  if (owner == null) {
186
253
  logger.debug("The owner ({claimedOwnerId}) that key {keyId} claims could not be resolved.", {
187
254
  keyId: keyId.href,
@@ -333,6 +400,21 @@ async function resolveFetchedKey(document, cacheKey, documentUrl, keyId, cls, {
333
400
  tracerProvider
334
401
  });
335
402
  } catch (e) {
403
+ const failure = classifyKeyJsonLdError(e);
404
+ if (failure === "fetch") throw contextFetchError(e);
405
+ if (failure != null) {
406
+ logger.debug("Failed to decode key {keyId}: {error}", {
407
+ keyId,
408
+ failure,
409
+ error: e
410
+ });
411
+ await keyCache?.set(cacheKey, null);
412
+ await clearFetchErrorMetadata(cacheKey, keyCache);
413
+ return {
414
+ key: null,
415
+ cached: false
416
+ };
417
+ }
336
418
  if (!(e instanceof TypeError)) throw e;
337
419
  try {
338
420
  object = await cls.fromJsonLd(document, {
@@ -341,8 +423,14 @@ async function resolveFetchedKey(document, cacheKey, documentUrl, keyId, cls, {
341
423
  tracerProvider
342
424
  });
343
425
  } catch (e) {
344
- if (e instanceof TypeError) {
345
- logger.debug("Failed to verify; key {keyId} returned an invalid object.", { keyId });
426
+ const failure = classifyKeyJsonLdError(e);
427
+ if (failure === "fetch") throw contextFetchError(e);
428
+ if (failure != null || e instanceof TypeError) {
429
+ logger.debug("Failed to decode key {keyId}: {error}", {
430
+ keyId,
431
+ failure: failure ?? "invalid",
432
+ error: e
433
+ });
346
434
  await keyCache?.set(cacheKey, null);
347
435
  await clearFetchErrorMetadata(cacheKey, keyCache);
348
436
  return {
@@ -434,16 +522,17 @@ async function resolveFetchedKey(document, cacheKey, documentUrl, keyId, cls, {
434
522
  const claimedOwnerId = key instanceof CryptographicKey ? key.ownerId : key.controllerId;
435
523
  if (ownerDocument != null && claimedOwnerId == null) key = key instanceof CryptographicKey ? key.clone({ owner: ownerDocument.id }) : key.clone({ controller: ownerDocument.id });
436
524
  else if (claimedOwnerId != null && claimedOwnerId.href !== ownerDocument?.id?.href) {
437
- if (await verifyKeyOwnership(key, {
525
+ if (await verifyKeyOwnershipInternal(key, {
438
526
  documentLoader,
439
527
  contextLoader,
440
528
  tracerProvider
441
- }) == null) {
529
+ }, true) == null) {
442
530
  logger.debug("Failed to verify; the owner {claimedOwnerId} that key {keyId} claims does not list the key as its own.", {
443
531
  keyId,
444
532
  claimedOwnerId: claimedOwnerId.href
445
533
  });
446
534
  await keyCache?.set(cacheKey, null);
535
+ await clearFetchErrorMetadata(cacheKey, keyCache);
447
536
  return {
448
537
  key: null,
449
538
  cached: false
@@ -481,7 +570,12 @@ async function fetchKeyWithResult(cacheKey, cls, options, onCachedUnavailable, o
481
570
  } catch (error) {
482
571
  return await onFetchError(error, cacheKey, keyId, keyCache, logger);
483
572
  }
484
- return await resolveFetchedKey(document, cacheKey, documentUrl, keyId, cls, options, logger);
573
+ try {
574
+ return await resolveFetchedKey(document, cacheKey, documentUrl, keyId, cls, options, logger);
575
+ } catch (error) {
576
+ if (!(error instanceof KeyContextFetchError)) throw error;
577
+ return await onFetchError(error.error, cacheKey, keyId, keyCache, logger);
578
+ }
485
579
  }
486
580
  async function fetchKeyInternal(keyId, cls, options = {}) {
487
581
  return await fetchKeyWithResult(typeof keyId === "string" ? new URL(keyId) : keyId, cls, options, (_cacheKey, _keyId, _keyCache, _logger) => {
@@ -2,6 +2,7 @@ import { Temporal } from "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
4
  import { CryptographicKey, Multikey } from "@fedify/vocab";
5
+ import { FetchError, UrlError } from "@fedify/vocab-runtime";
5
6
  //#region src/federation/keycache.ts
6
7
  const KEY_CACHE_GENERATION = "2";
7
8
  var KvKeyCache = class {
@@ -76,8 +77,15 @@ var KvKeyCache = class {
76
77
  })
77
78
  };
78
79
  else if ("errorName" in cached && typeof cached.errorName === "string" && "errorMessage" in cached && typeof cached.errorMessage === "string") {
79
- const error = new Error(cached.errorMessage);
80
+ let error;
81
+ if ("errorType" in cached && cached.errorType === "FetchError" && "errorUrl" in cached && typeof cached.errorUrl === "string" && URL.canParse(cached.errorUrl)) {
82
+ error = new FetchError(cached.errorUrl);
83
+ error.message = cached.errorMessage;
84
+ } else if ("errorType" in cached && cached.errorType === "UrlError" && "errorReason" in cached && (cached.errorReason === "dns" || cached.errorReason === "disallowed")) error = new UrlError(cached.errorMessage, { reason: cached.errorReason });
85
+ else error = new Error(cached.errorMessage);
80
86
  error.name = cached.errorName;
87
+ if ("errorCause" in cached && cached.errorCause != null && typeof cached.errorCause === "object" && "name" in cached.errorCause && typeof cached.errorCause.name === "string" && "message" in cached.errorCause && typeof cached.errorCause.message === "string") if ("isDomException" in cached.errorCause && cached.errorCause.isDomException === true) error.cause = new DOMException(cached.errorCause.message, cached.errorCause.name);
88
+ else error.cause = Object.assign(new Error(cached.errorCause.message), { name: cached.errorCause.name });
81
89
  return { error };
82
90
  }
83
91
  }
@@ -95,9 +103,22 @@ var KvKeyCache = class {
95
103
  }, { ttl: this.unavailableKeyTtl });
96
104
  return;
97
105
  }
106
+ const cause = error.error.cause;
98
107
  await this.kv.set(this.#getFetchErrorKey(keyId), {
99
108
  errorName: error.error.name,
100
- errorMessage: error.error.message
109
+ errorMessage: error.error.message,
110
+ ...error.error instanceof FetchError ? {
111
+ errorType: "FetchError",
112
+ errorUrl: error.error.url.href
113
+ } : error.error instanceof UrlError ? {
114
+ errorType: "UrlError",
115
+ errorReason: error.error.reason
116
+ } : {},
117
+ ...cause instanceof Error || cause instanceof DOMException ? { errorCause: {
118
+ name: cause.name,
119
+ message: cause.message,
120
+ isDomException: cause instanceof DOMException
121
+ } } : {}
101
122
  }, { ttl: this.unavailableKeyTtl });
102
123
  }
103
124
  };
@@ -1,7 +1,7 @@
1
1
  const { Temporal } = require("@js-temporal/polyfill");
2
2
  const { URLPattern } = require("urlpattern-polyfill");
3
3
  require("./chunk-DDcVe30Y.cjs");
4
- const require_http = require("./http-CJuCjxWI.cjs");
4
+ const require_http = require("./http-C_aA2Zx1.cjs");
5
5
  let _logtape_logtape = require("@logtape/logtape");
6
6
  let es_toolkit = require("es-toolkit");
7
7
  let _fedify_vocab_runtime = require("@fedify/vocab-runtime");
@@ -1,6 +1,6 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
2
  import { URLPattern } from "urlpattern-polyfill";
3
- import { f as validateCryptoKey, t as doubleKnock } from "./http-DlBFaZ7A.js";
3
+ import { f as validateCryptoKey, t as doubleKnock } from "./http-DkzPSl5o.js";
4
4
  import { getLogger } from "@logtape/logtape";
5
5
  import { curry } from "es-toolkit";
6
6
  import { FetchError, UrlError, createActivityPubRequest, getRemoteDocument, logRequest, preloadedContexts, validatePublicUrl } from "@fedify/vocab-runtime";
@@ -1,8 +1,8 @@
1
1
  import "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
- import { n as version, t as name } from "./deno-duAPwAHH.mjs";
5
- import { r as fetchKey, s as validateCryptoKey } from "./key-5QlWv3sx.mjs";
4
+ import { n as version, t as name } from "./deno-Cya0rbEr.mjs";
5
+ import { r as fetchKey, s as validateCryptoKey } from "./key-BMHNd0gP.mjs";
6
6
  import { Activity, CryptographicKey, Object as Object$1, getTypeId } from "@fedify/vocab";
7
7
  import { SpanStatusCode, trace } from "@opentelemetry/api";
8
8
  import { getDocumentLoader } from "@fedify/vocab-runtime";
@@ -2,25 +2,25 @@ import { Temporal } from "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
4
  import { n as RouterError } from "./router-CrMLXoOr.mjs";
5
- import { n as version, t as name } from "./deno-duAPwAHH.mjs";
5
+ import { n as version, t as name } from "./deno-Cya0rbEr.mjs";
6
6
  import { t as formatAcceptSignature } from "./accept-Dd__NiUL.mjs";
7
- import { o as importJwk, s as validateCryptoKey, t as exportJwk } from "./key-5QlWv3sx.mjs";
8
- import { l as verifyRequest, o as parseRfc9421SignatureInput, u as verifyRequestDetailed } from "./http-DytBXprv.mjs";
9
- import { t as getAuthenticatedDocumentLoader } from "./docloader-DI88Lx6y.mjs";
7
+ import { o as importJwk, s as validateCryptoKey, t as exportJwk } from "./key-BMHNd0gP.mjs";
8
+ import { l as verifyRequest, o as parseRfc9421SignatureInput, u as verifyRequestDetailed } from "./http-dobCLGG2.mjs";
9
+ import { t as getAuthenticatedDocumentLoader } from "./docloader-Dz0Z1Y07.mjs";
10
10
  import { n as kvCache } from "./kv-cache-FsT1ljhs.mjs";
11
- import { _ as wrapContextLoaderForJsonLd, a as compactJsonLd, c as getNormalizationContextLoader, d as isClearlyMalformedContextReference, f as isInvalidUrlTypeError, l as hasSignature, m as verifyCompactJsonLd, p as signJsonLd, r as assertSafeJsonLd, s as detachSignature, t as InvalidContextReferenceError, u as hasSignatureLike } from "./ld-D-oK3e4r.mjs";
12
- import { n as getKeyOwner, t as doesActorOwnKey } from "./owner-CHkNw8Fk.mjs";
13
- import { r as normalizeOutgoingActivityJsonLd } from "./outgoing-jsonld-Dc2OXyB-.mjs";
14
- import { i as verifyObject, n as hasProofLike, r as signObject } from "./proof-o8Ly9XSp.mjs";
15
- import { d as readBoundedText, l as BodyTooLargeError, t as getNodeInfo, u as MAX_BODY_SIZE } from "./client-CJ4Y0keQ.mjs";
16
- import { t as nodeInfoToJson } from "./types-s4mCTGkT.mjs";
17
- import { t as FederationBuilderImpl } from "./builder-CcReUrBQ.mjs";
18
- import { t as buildCollectionSynchronizationHeader } from "./collection-DnoYNwBC.mjs";
19
- import { t as KvKeyCache } from "./keycache-C1aVprwy.mjs";
11
+ import { t as KvKeyCache } from "./keycache-Dkc_sflV.mjs";
12
+ import { _ as wrapContextLoaderForJsonLd, a as compactJsonLd, c as getNormalizationContextLoader, d as isClearlyMalformedContextReference, f as isInvalidUrlTypeError, l as hasSignature, m as verifyCompactJsonLd, p as signJsonLd, r as assertSafeJsonLd, s as detachSignature, t as InvalidContextReferenceError, u as hasSignatureLike } from "./ld-B_m0qw8j.mjs";
13
+ import { n as getKeyOwner, t as doesActorOwnKey } from "./owner-Dk85kXZz.mjs";
14
+ import { r as normalizeOutgoingActivityJsonLd } from "./outgoing-jsonld-amccRMrV.mjs";
15
+ import { i as verifyObject, n as hasProofLike, r as signObject } from "./proof-DrGrnZVE.mjs";
16
+ import { d as readBoundedText, l as BodyTooLargeError, t as getNodeInfo, u as MAX_BODY_SIZE } from "./client-9F_u23dc.mjs";
17
+ import { t as nodeInfoToJson } from "./types-J53Kw7so.mjs";
18
+ import { t as FederationBuilderImpl } from "./builder-a-kl29PE.mjs";
19
+ import { t as buildCollectionSynchronizationHeader } from "./collection-D-HqUuA2.mjs";
20
20
  import { t as acceptsJsonLd } from "./negotiation-BHtZN6Y1.mjs";
21
- import { t as hasMalformedKnownTemporalLiteral } from "./temporal-8bTjf1mO.mjs";
21
+ import { t as hasMalformedKnownTemporalLiteral } from "./temporal-Bnwa_2NC.mjs";
22
22
  import { t as createExponentialBackoffPolicy } from "./retry-bMXBL97A.mjs";
23
- import { n as extractInboxes, r as sendActivity, t as SendActivityError } from "./send-Ck74ndlE.mjs";
23
+ import { n as extractInboxes, r as sendActivity, t as SendActivityError } from "./send-Co-E7yJn.mjs";
24
24
  import { Activity, Collection, CollectionPage, CryptographicKey, Link, Multikey, Object as Object$1, OrderedCollection, OrderedCollectionPage, Tombstone, getTypeId, lookupObject, traverseCollection } from "@fedify/vocab";
25
25
  import { lookupWebFinger } from "@fedify/webfinger";
26
26
  import { SpanKind, SpanStatusCode, context, propagation, trace } from "@opentelemetry/api";
@@ -1,5 +1,5 @@
1
1
  import "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
- import { n as FederationImpl } from "./middleware-DTIhauqW.mjs";
4
+ import { n as FederationImpl } from "./middleware-5qLdPLro.mjs";
5
5
  export { FederationImpl };
@@ -2,10 +2,10 @@ import { Temporal } from "@js-temporal/polyfill";
2
2
  import { URLPattern } from "urlpattern-polyfill";
3
3
  import { t as __exportAll } from "./chunk-CRNNMoPX.js";
4
4
  import { r as getDefaultActivityTransformers } from "./transformers-BGMIq1cs.js";
5
- import { a as verifyRequestDetailed, d as importJwk, f as validateCryptoKey, i as verifyRequest, m as formatAcceptSignature, n as parseRfc9421SignatureInput, o as exportJwk, t as doubleKnock, v as name, y as version } from "./http-DlBFaZ7A.js";
5
+ import { a as verifyRequestDetailed, d as importJwk, f as validateCryptoKey, i as verifyRequest, m as formatAcceptSignature, n as parseRfc9421SignatureInput, o as exportJwk, t as doubleKnock, v as name, y as version } from "./http-DkzPSl5o.js";
6
6
  import { a as MAX_BODY_SIZE, i as BodyTooLargeError, n as getNodeInfo, o as readBoundedText, t as nodeInfoToJson } from "./types-miAYfiGO.js";
7
- import { _ as hasSignatureLike, b as signJsonLd, c as getKeyOwner, f as compactJsonLd, g as hasSignature, h as getNormalizationContextLoader, i as verifyObject, l as InvalidContextReferenceError, m as detachSignature, n as hasProofLike, o as normalizeOutgoingActivityJsonLd, r as signObject, s as doesActorOwnKey, u as assertSafeJsonLd, v as isClearlyMalformedContextReference, w as wrapContextLoaderForJsonLd, x as verifyCompactJsonLd, y as isInvalidUrlTypeError } from "./proof-BJ_pqvov.js";
8
- import { n as getAuthenticatedDocumentLoader, t as kvCache } from "./kv-cache-DJ3q30Nl.js";
7
+ import { _ as hasSignatureLike, b as signJsonLd, c as getKeyOwner, f as compactJsonLd, g as hasSignature, h as getNormalizationContextLoader, i as verifyObject, l as InvalidContextReferenceError, m as detachSignature, n as hasProofLike, o as normalizeOutgoingActivityJsonLd, r as signObject, s as doesActorOwnKey, u as assertSafeJsonLd, v as isClearlyMalformedContextReference, w as wrapContextLoaderForJsonLd, x as verifyCompactJsonLd, y as isInvalidUrlTypeError } from "./proof-Bz6IdT2p.js";
8
+ import { n as getAuthenticatedDocumentLoader, t as kvCache } from "./kv-cache-DVhfMtqE.js";
9
9
  import { getLogger, withContext } from "@logtape/logtape";
10
10
  import { Activity, Collection, CollectionPage, CryptographicKey, Link, Multikey, Object as Object$1, OrderedCollection, OrderedCollectionPage, Tombstone, getTypeId, lookupObject, traverseCollection } from "@fedify/vocab";
11
11
  import { SpanKind, SpanStatusCode, context, propagation, trace } from "@opentelemetry/api";
@@ -995,8 +995,15 @@ var KvKeyCache = class {
995
995
  })
996
996
  };
997
997
  else if ("errorName" in cached && typeof cached.errorName === "string" && "errorMessage" in cached && typeof cached.errorMessage === "string") {
998
- const error = new Error(cached.errorMessage);
998
+ let error;
999
+ if ("errorType" in cached && cached.errorType === "FetchError" && "errorUrl" in cached && typeof cached.errorUrl === "string" && URL.canParse(cached.errorUrl)) {
1000
+ error = new FetchError(cached.errorUrl);
1001
+ error.message = cached.errorMessage;
1002
+ } else if ("errorType" in cached && cached.errorType === "UrlError" && "errorReason" in cached && (cached.errorReason === "dns" || cached.errorReason === "disallowed")) error = new UrlError(cached.errorMessage, { reason: cached.errorReason });
1003
+ else error = new Error(cached.errorMessage);
999
1004
  error.name = cached.errorName;
1005
+ if ("errorCause" in cached && cached.errorCause != null && typeof cached.errorCause === "object" && "name" in cached.errorCause && typeof cached.errorCause.name === "string" && "message" in cached.errorCause && typeof cached.errorCause.message === "string") if ("isDomException" in cached.errorCause && cached.errorCause.isDomException === true) error.cause = new DOMException(cached.errorCause.message, cached.errorCause.name);
1006
+ else error.cause = Object.assign(new Error(cached.errorCause.message), { name: cached.errorCause.name });
1000
1007
  return { error };
1001
1008
  }
1002
1009
  }
@@ -1014,9 +1021,22 @@ var KvKeyCache = class {
1014
1021
  }, { ttl: this.unavailableKeyTtl });
1015
1022
  return;
1016
1023
  }
1024
+ const cause = error.error.cause;
1017
1025
  await this.kv.set(this.#getFetchErrorKey(keyId), {
1018
1026
  errorName: error.error.name,
1019
- errorMessage: error.error.message
1027
+ errorMessage: error.error.message,
1028
+ ...error.error instanceof FetchError ? {
1029
+ errorType: "FetchError",
1030
+ errorUrl: error.error.url.href
1031
+ } : error.error instanceof UrlError ? {
1032
+ errorType: "UrlError",
1033
+ errorReason: error.error.reason
1034
+ } : {},
1035
+ ...cause instanceof Error || cause instanceof DOMException ? { errorCause: {
1036
+ name: cause.name,
1037
+ message: cause.message,
1038
+ isDomException: cause instanceof DOMException
1039
+ } } : {}
1020
1040
  }, { ttl: this.unavailableKeyTtl });
1021
1041
  }
1022
1042
  };
@@ -2,10 +2,10 @@ const { Temporal } = require("@js-temporal/polyfill");
2
2
  const { URLPattern } = require("urlpattern-polyfill");
3
3
  const require_chunk = require("./chunk-DDcVe30Y.cjs");
4
4
  const require_transformers = require("./transformers-NeAONrAq.cjs");
5
- const require_http = require("./http-CJuCjxWI.cjs");
5
+ const require_http = require("./http-C_aA2Zx1.cjs");
6
6
  const require_types = require("./types-DRPjmHVV.cjs");
7
- const require_proof = require("./proof-DC3IlY_8.cjs");
8
- const require_kv_cache = require("./kv-cache-K6z1D4HS.cjs");
7
+ const require_proof = require("./proof-ClpE9zhD.cjs");
8
+ const require_kv_cache = require("./kv-cache-Btp4kC_U.cjs");
9
9
  let _logtape_logtape = require("@logtape/logtape");
10
10
  let _fedify_vocab = require("@fedify/vocab");
11
11
  let _opentelemetry_api = require("@opentelemetry/api");
@@ -213,7 +213,7 @@ var FederationBuilderImpl = class {
213
213
  this.collectionTypeIds = {};
214
214
  }
215
215
  async build(options) {
216
- const { FederationImpl } = await Promise.resolve().then(() => require("./middleware-RmEidCmR.cjs"));
216
+ const { FederationImpl } = await Promise.resolve().then(() => require("./middleware-ZKHfZ7zj.cjs"));
217
217
  const f = new FederationImpl(options);
218
218
  const trailingSlashInsensitiveValue = f.router.trailingSlashInsensitive;
219
219
  f.router = this.router.clone();
@@ -996,8 +996,15 @@ var KvKeyCache = class {
996
996
  })
997
997
  };
998
998
  else if ("errorName" in cached && typeof cached.errorName === "string" && "errorMessage" in cached && typeof cached.errorMessage === "string") {
999
- const error = new Error(cached.errorMessage);
999
+ let error;
1000
+ if ("errorType" in cached && cached.errorType === "FetchError" && "errorUrl" in cached && typeof cached.errorUrl === "string" && URL.canParse(cached.errorUrl)) {
1001
+ error = new _fedify_vocab_runtime.FetchError(cached.errorUrl);
1002
+ error.message = cached.errorMessage;
1003
+ } else if ("errorType" in cached && cached.errorType === "UrlError" && "errorReason" in cached && (cached.errorReason === "dns" || cached.errorReason === "disallowed")) error = new _fedify_vocab_runtime.UrlError(cached.errorMessage, { reason: cached.errorReason });
1004
+ else error = new Error(cached.errorMessage);
1000
1005
  error.name = cached.errorName;
1006
+ if ("errorCause" in cached && cached.errorCause != null && typeof cached.errorCause === "object" && "name" in cached.errorCause && typeof cached.errorCause.name === "string" && "message" in cached.errorCause && typeof cached.errorCause.message === "string") if ("isDomException" in cached.errorCause && cached.errorCause.isDomException === true) error.cause = new DOMException(cached.errorCause.message, cached.errorCause.name);
1007
+ else error.cause = Object.assign(new Error(cached.errorCause.message), { name: cached.errorCause.name });
1001
1008
  return { error };
1002
1009
  }
1003
1010
  }
@@ -1015,9 +1022,22 @@ var KvKeyCache = class {
1015
1022
  }, { ttl: this.unavailableKeyTtl });
1016
1023
  return;
1017
1024
  }
1025
+ const cause = error.error.cause;
1018
1026
  await this.kv.set(this.#getFetchErrorKey(keyId), {
1019
1027
  errorName: error.error.name,
1020
- errorMessage: error.error.message
1028
+ errorMessage: error.error.message,
1029
+ ...error.error instanceof _fedify_vocab_runtime.FetchError ? {
1030
+ errorType: "FetchError",
1031
+ errorUrl: error.error.url.href
1032
+ } : error.error instanceof _fedify_vocab_runtime.UrlError ? {
1033
+ errorType: "UrlError",
1034
+ errorReason: error.error.reason
1035
+ } : {},
1036
+ ...cause instanceof Error || cause instanceof DOMException ? { errorCause: {
1037
+ name: cause.name,
1038
+ message: cause.message,
1039
+ isDomException: cause instanceof DOMException
1040
+ } } : {}
1021
1041
  }, { ttl: this.unavailableKeyTtl });
1022
1042
  }
1023
1043
  };