@fedify/fedify 2.1.29 → 2.1.31

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 (86) hide show
  1. package/dist/{builder-Mg2G3qj8.mjs → builder-isMRh_I6.mjs} +3 -3
  2. package/dist/{client-CAM_bQXx.d.cts → client-BIBzqEIp.d.cts} +0 -1
  3. package/dist/{client-CjDMsn4a.mjs → client-BZr7TGyu.mjs} +113 -64
  4. package/dist/{client-CSddvgWN.d.ts → client-jE7oGvRh.d.ts} +0 -1
  5. package/dist/compat/mod.d.cts +1 -1
  6. package/dist/compat/mod.d.ts +1 -1
  7. package/dist/compat/transformers.test.mjs +1 -1
  8. package/dist/{context-DLLaDau8.d.ts → context-C0uGVzgk.d.ts} +28 -2
  9. package/dist/{context-BNwv9Q1v.d.cts → context-CLaLxi49.d.cts} +28 -2
  10. package/dist/{deno-BiYv5RT5.mjs → deno-CiaJqTcu.mjs} +1 -1
  11. package/dist/docloader-DOrwedT7.mjs +124 -0
  12. package/dist/federation/builder.test.mjs +1 -1
  13. package/dist/federation/handler.test.mjs +6 -6
  14. package/dist/federation/idempotency.test.mjs +2 -2
  15. package/dist/federation/inbox.test.mjs +1 -1
  16. package/dist/federation/keycache.test.mjs +18 -7
  17. package/dist/federation/middleware.test.mjs +380 -24
  18. package/dist/federation/mod.cjs +1 -1
  19. package/dist/federation/mod.d.cts +2 -2
  20. package/dist/federation/mod.d.ts +2 -2
  21. package/dist/federation/mod.js +1 -1
  22. package/dist/federation/send.test.mjs +445 -70
  23. package/dist/federation/temporal.test.mjs +1 -1
  24. package/dist/federation/webfinger.test.mjs +1 -1
  25. package/dist/{http-Dl00HXr4.d.ts → http-9IFUxdkp.d.cts} +1 -2
  26. package/dist/{http-C1ga9WQu.mjs → http-A9mP2uM7.mjs} +194 -96
  27. package/dist/{http-Cginz7L0.cjs → http-CRiVlZd5.cjs} +188 -105
  28. package/dist/{http-B3rSb2GQ.js → http-Crml3gei.js} +187 -104
  29. package/dist/{http-DqZhyEso.d.cts → http-SbfezERn.d.ts} +1 -2
  30. package/dist/{inbox-BWzL0xMU.mjs → inbox-KESLudq4.mjs} +1 -1
  31. package/dist/{key-O-ATwIGS.mjs → key-Dg6-DV_O.mjs} +17 -3
  32. package/dist/{keycache-RFlZObU3.mjs → keycache-DahCaG_m.mjs} +1 -1
  33. package/dist/{kv-cache-BdylAE9w.cjs → kv-cache-Cqmm-yQa.cjs} +59 -15
  34. package/dist/{kv-cache-8Kg2CHiD.js → kv-cache-DQcyZL5T.js} +60 -16
  35. package/dist/{ld-C-fCGM-z.mjs → ld-COjDN6Nb.mjs} +2 -2
  36. package/dist/{middleware-CVRFS7PZ.cjs → middleware-BbgR7xOI.cjs} +185 -60
  37. package/dist/{middleware-VvrqPwHL.cjs → middleware-BtDQUCcz.cjs} +1 -1
  38. package/dist/{middleware-C9UCWOqw.mjs → middleware-COFlKeYn.mjs} +79 -39
  39. package/dist/{middleware-CxgFiKI3.mjs → middleware-CuQvczZd.mjs} +1 -1
  40. package/dist/{middleware-41MkquTV.js → middleware-DIW5S_5L.js} +185 -60
  41. package/dist/{mod-DNEgTI8M.d.ts → mod-BRnwh2oy.d.ts} +2 -7
  42. package/dist/{mod-BLDHhuW0.d.ts → mod-C7w-QVUv.d.ts} +1 -1
  43. package/dist/{mod-CWJNbyju.d.ts → mod-CFRocFeQ.d.ts} +1 -1
  44. package/dist/{mod-BmlfRBmU.d.cts → mod-CGUH8Ujp.d.cts} +1 -1
  45. package/dist/{mod-Cn9GJ5ex.d.cts → mod-Co7LEK9X.d.cts} +2 -7
  46. package/dist/{mod-DiuFTYUt.d.cts → mod-LOEOKbYd.d.cts} +1 -1
  47. package/dist/mod.cjs +5 -5
  48. package/dist/mod.d.cts +6 -6
  49. package/dist/mod.d.ts +6 -6
  50. package/dist/mod.js +5 -5
  51. package/dist/nodeinfo/client.test.mjs +337 -19
  52. package/dist/nodeinfo/handler.test.mjs +1 -1
  53. package/dist/nodeinfo/mod.cjs +1 -1
  54. package/dist/nodeinfo/mod.d.cts +1 -1
  55. package/dist/nodeinfo/mod.d.ts +1 -1
  56. package/dist/nodeinfo/mod.js +1 -1
  57. package/dist/otel/exporter.test.mjs +292 -30
  58. package/dist/otel/mod.cjs +36 -30
  59. package/dist/otel/mod.d.cts +13 -0
  60. package/dist/otel/mod.d.ts +13 -0
  61. package/dist/otel/mod.js +36 -30
  62. package/dist/{owner-C8IVASD3.mjs → owner-CFHq61cq.mjs} +2 -2
  63. package/dist/{proof-BbHMAkPG.cjs → proof-0JkSC_pT.cjs} +1 -1
  64. package/dist/{proof-cGfRPTiV.js → proof-DzksJFK7.js} +1 -1
  65. package/dist/{proof-CUdcBgvg.mjs → proof-KSHlbPCG.mjs} +2 -2
  66. package/dist/{send-BoV4dyVO.mjs → send-DaDe2YCb.mjs} +117 -32
  67. package/dist/sig/http.test.mjs +507 -3
  68. package/dist/sig/key.test.mjs +197 -3
  69. package/dist/sig/ld.test.mjs +47 -2
  70. package/dist/sig/mod.cjs +2 -2
  71. package/dist/sig/mod.d.cts +2 -2
  72. package/dist/sig/mod.d.ts +2 -2
  73. package/dist/sig/mod.js +2 -2
  74. package/dist/sig/owner.test.mjs +46 -1
  75. package/dist/sig/proof.test.mjs +52 -1
  76. package/dist/{temporal-BJlnku_d.mjs → temporal-DNGH8o2E.mjs} +1 -1
  77. package/dist/testing/mod.d.mts +26 -1
  78. package/dist/{types-miAYfiGO.js → types-B1_pRFE_.js} +112 -63
  79. package/dist/{types-DRPjmHVV.cjs → types-DOvyymM4.cjs} +111 -62
  80. package/dist/utils/docloader.test.mjs +380 -32
  81. package/dist/utils/mod.cjs +1 -1
  82. package/dist/utils/mod.d.cts +1 -1
  83. package/dist/utils/mod.d.ts +1 -1
  84. package/dist/utils/mod.js +1 -1
  85. package/package.json +7 -7
  86. package/dist/docloader-DrxXBFL8.mjs +0 -80
@@ -11,7 +11,7 @@ let _opentelemetry_semantic_conventions = require("@opentelemetry/semantic-conve
11
11
  let byte_encodings_base64 = require("byte-encodings/base64");
12
12
  //#region deno.json
13
13
  var name = "@fedify/fedify";
14
- var version = "2.1.29";
14
+ var version = "2.1.31";
15
15
  //#endregion
16
16
  //#region src/sig/accept.ts
17
17
  /**
@@ -292,16 +292,23 @@ async function importJwk(jwk, type) {
292
292
  /**
293
293
  * Fetches the document that the given actor URI dereferences to, and returns
294
294
  * the actor only if the host that served the document is authoritative for
295
- * the id the document claims.
295
+ * the id the document claims, and that id is the actor that was asked for.
296
296
  *
297
297
  * Only the origin that serves an actor id can speak for it. Without that
298
298
  * rule any host could serve a document describing somebody else's actor—and
299
299
  * listing its own keys as that actor's.
300
300
  *
301
+ * The returned actor always has exactly the requested `actorId` as its `id`.
302
+ * A document loader follows redirects and alternate document links, so the
303
+ * document an actor id leads to may describe a different actor altogether,
304
+ * on the same origin or on another; accepting it would attribute that other
305
+ * actor's keys to the requested one. See GHSA-8863-8468-7hq7.
306
+ *
301
307
  * @param actorId The URI of the actor to fetch.
302
308
  * @param options Options for fetching the document.
303
309
  * @returns The actor, or `null` if the document cannot be fetched, is not an
304
- * actor, or belongs to another origin.
310
+ * actor, belongs to another origin, or describes an actor other
311
+ * than the requested one.
305
312
  * @internal
306
313
  */
307
314
  async function fetchActorDocument(actorId, options = {}) {
@@ -356,6 +363,13 @@ async function fetchActorDocumentInternal(actorId, options, propagateContextFetc
356
363
  });
357
364
  return null;
358
365
  }
366
+ if (object.id.href !== actorId.href) {
367
+ logger.debug("The document fetched for the actor {actorId} is the actor {documentActorId} instead; refusing to treat it as the requested actor's document.", {
368
+ actorId: actorId.href,
369
+ documentActorId: object.id.href
370
+ });
371
+ return null;
372
+ }
359
373
  return object;
360
374
  }
361
375
  /**
@@ -1584,10 +1598,10 @@ async function createRedirectRequest(request, location, body, validateRedirect)
1584
1598
  cache: request.cache
1585
1599
  });
1586
1600
  }
1587
- async function fetchDoubleKnockRequest(request, signedRequest, signal) {
1601
+ async function fetchDoubleKnockRequest(session, request, signedRequest, signal) {
1588
1602
  const maxAttempts = request.method === "GET" || request.method === "HEAD" ? 2 : 1;
1589
1603
  for (let attempt = 1;; attempt++) try {
1590
- return await fetch(signedRequest, {
1604
+ return await session.fetch(signedRequest, {
1591
1605
  redirect: "manual",
1592
1606
  signal
1593
1607
  });
@@ -1638,117 +1652,186 @@ function getAbortReason(signal) {
1638
1652
  return signal.reason ?? new DOMException("The operation was aborted.", "AbortError");
1639
1653
  }
1640
1654
  /**
1641
- * Performs a double-knock request to the given URL. For the details of
1642
- * double-knocking, see
1643
- * <https://swicg.github.io/activitypub-http-signature/#how-to-upgrade-supported-versions>.
1655
+ * Performs a double-knock request like {@link doubleKnock}, opening a session
1656
+ * for every hop with the given function, and passes the final response to
1657
+ * the given function. The final session is closed after `consume` settles,
1658
+ * so `consume` must read or cancel the response body before it returns.
1659
+ * This is not exported from any entry point.
1644
1660
  * @param request The request to send.
1645
1661
  * @param identity The identity to use for signing the request.
1646
1662
  * @param options The options for double-knock requests.
1647
- * @returns The response to the request.
1648
- * @since 1.6.0
1663
+ * @param openHop The function to open a session for each hop. The session of
1664
+ * a hop is closed before the next hop is opened.
1665
+ * @param consume The function to consume the final response.
1666
+ * @returns The value returned by `consume`.
1649
1667
  */
1650
- async function doubleKnock(request, identity, options = {}) {
1651
- return await doubleKnockInternal(request, identity, options);
1668
+ async function doubleKnockScoped(request, identity, options, openHop, consume) {
1669
+ const { response, session } = await doubleKnockCore(request, identity, options, openHop);
1670
+ try {
1671
+ return await consume(response);
1672
+ } catch (error) {
1673
+ (0, _logtape_logtape.getLogger)([
1674
+ "fedify",
1675
+ "sig",
1676
+ "http"
1677
+ ]).debug("Failed to consume the response from {url}; closing the session: {error}", {
1678
+ url: request.url,
1679
+ error
1680
+ });
1681
+ throw error;
1682
+ } finally {
1683
+ await session.close();
1684
+ }
1652
1685
  }
1653
- async function doubleKnockInternal(request, identity, options, redirected = 0, visited = /* @__PURE__ */ new Set()) {
1654
- const { specDeterminer, log, tracerProvider, signal } = options;
1655
- visited.add(request.url);
1656
- const origin = new URL(request.url).origin;
1657
- const firstTrySpec = specDeterminer == null ? "rfc9421" : await specDeterminer.determineSpec(origin);
1658
- const body = options.body !== void 0 ? options.body : request.method !== "GET" && request.method !== "HEAD" ? await request.clone().arrayBuffer() : null;
1659
- let signedRequest = await signRequest(request, identity.privateKey, identity.keyId, {
1660
- spec: firstTrySpec,
1661
- tracerProvider,
1662
- body
1663
- });
1664
- log?.(signedRequest);
1665
- let response = await fetchDoubleKnockRequest(request, signedRequest, signal);
1666
- if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
1667
- if (redirected >= DEFAULT_MAX_REDIRECTION) throw new _fedify_vocab_runtime.FetchError(request.url, `Too many redirections (${redirected + 1})`);
1668
- const redirectRequest = await createRedirectRequest(request, response.headers.get("Location"), body, options.validateRedirect);
1669
- if (visited.has(redirectRequest.url)) throw new _fedify_vocab_runtime.FetchError(request.url, `Redirect loop detected: ${redirectRequest.url}`);
1670
- return doubleKnockInternal(redirectRequest, identity, {
1671
- ...options,
1672
- body
1673
- }, redirected + 1, visited);
1674
- } else if (response.status === 400 || response.status === 401 || response.status > 401) {
1675
- const logger = (0, _logtape_logtape.getLogger)([
1686
+ function isRedirectResponse(response) {
1687
+ return response.status >= 300 && response.status < 400 && response.headers.has("Location");
1688
+ }
1689
+ /**
1690
+ * Starts cancelling the body of a response that is not passed to the caller,
1691
+ * without waiting for it to settle. The cancellation of a branch of a tee
1692
+ * (e.g., a body cloned by a fetch mock) settles only after the other branch
1693
+ * is cancelled too, per the Streams standard, so awaiting it could block
1694
+ * forever. Failures are only logged.
1695
+ */
1696
+ function cancelResponseBody(response, url) {
1697
+ response.body?.cancel().catch((error) => {
1698
+ (0, _logtape_logtape.getLogger)([
1676
1699
  "fedify",
1677
1700
  "sig",
1678
1701
  "http"
1679
- ]);
1680
- const acceptSigHeader = response.headers.get("Accept-Signature");
1681
- if (acceptSigHeader != null) {
1682
- const entries = validateAcceptSignature(parseAcceptSignature(acceptSigHeader));
1683
- const localKeyId = identity.keyId.href;
1684
- const localAlg = "rsa-v1_5-sha256";
1685
- let fulfilled = false;
1686
- let challengeRequest;
1687
- for (const entry of entries) {
1688
- const rfc9421 = fulfillAcceptSignature(entry, localKeyId, localAlg);
1689
- if (rfc9421 == null) continue;
1690
- logger.debug("Received Accept-Signature challenge; accumulating label {label} and components {components}.", {
1691
- label: rfc9421.label,
1692
- components: rfc9421.components
1693
- });
1694
- try {
1695
- challengeRequest = await signRequest(challengeRequest ?? request, identity.privateKey, identity.keyId, {
1696
- spec: "rfc9421",
1697
- tracerProvider,
1698
- body,
1699
- rfc9421
1702
+ ]).debug("Failed to cancel the response body from {url}: {error}", {
1703
+ url,
1704
+ error
1705
+ });
1706
+ });
1707
+ }
1708
+ async function doubleKnockCore(request, identity, options, openHop) {
1709
+ const { specDeterminer, log, tracerProvider, signal } = options;
1710
+ const visited = /* @__PURE__ */ new Set();
1711
+ const body = options.body !== void 0 ? options.body : request.method !== "GET" && request.method !== "HEAD" ? await request.clone().arrayBuffer() : null;
1712
+ let session = await openHop(request.url);
1713
+ try {
1714
+ for (let redirected = 0;; redirected++) {
1715
+ visited.add(request.url);
1716
+ const origin = new URL(request.url).origin;
1717
+ const firstTrySpec = specDeterminer == null ? "rfc9421" : await specDeterminer.determineSpec(origin);
1718
+ let signedRequest = await signRequest(request, identity.privateKey, identity.keyId, {
1719
+ spec: firstTrySpec,
1720
+ tracerProvider,
1721
+ body
1722
+ });
1723
+ log?.(signedRequest);
1724
+ let response = await fetchDoubleKnockRequest(session, request, signedRequest, signal);
1725
+ const firstRedirected = isRedirectResponse(response);
1726
+ if (!firstRedirected && (response.status === 400 || response.status === 401 || response.status > 401)) {
1727
+ const logger = (0, _logtape_logtape.getLogger)([
1728
+ "fedify",
1729
+ "sig",
1730
+ "http"
1731
+ ]);
1732
+ const acceptSigHeader = response.headers.get("Accept-Signature");
1733
+ if (acceptSigHeader != null) {
1734
+ const entries = validateAcceptSignature(parseAcceptSignature(acceptSigHeader));
1735
+ const localKeyId = identity.keyId.href;
1736
+ const localAlg = "rsa-v1_5-sha256";
1737
+ let fulfilled = false;
1738
+ let challengeRequest;
1739
+ for (const entry of entries) {
1740
+ const rfc9421 = fulfillAcceptSignature(entry, localKeyId, localAlg);
1741
+ if (rfc9421 == null) continue;
1742
+ logger.debug("Received Accept-Signature challenge; accumulating label {label} and components {components}.", {
1743
+ label: rfc9421.label,
1744
+ components: rfc9421.components
1745
+ });
1746
+ try {
1747
+ challengeRequest = await signRequest(challengeRequest ?? request, identity.privateKey, identity.keyId, {
1748
+ spec: "rfc9421",
1749
+ tracerProvider,
1750
+ body,
1751
+ rfc9421
1752
+ });
1753
+ fulfilled = true;
1754
+ } catch (error) {
1755
+ logger.debug("Failed to fulfill Accept-Signature challenge entry {label}: {error}", {
1756
+ label: entry.label,
1757
+ error
1758
+ });
1759
+ }
1760
+ }
1761
+ if (fulfilled && challengeRequest != null) {
1762
+ cancelResponseBody(response, request.url);
1763
+ signedRequest = challengeRequest;
1764
+ log?.(signedRequest);
1765
+ response = await session.fetch(signedRequest, {
1766
+ redirect: "manual",
1767
+ signal
1768
+ });
1769
+ }
1770
+ if (fulfilled && !isRedirectResponse(response)) {
1771
+ if (response.status < 300) {
1772
+ await specDeterminer?.rememberSpec(origin, "rfc9421");
1773
+ return {
1774
+ response,
1775
+ session
1776
+ };
1777
+ }
1778
+ if (response.status !== 400 && response.status !== 401) return {
1779
+ response,
1780
+ session
1781
+ };
1782
+ }
1783
+ }
1784
+ if (!isRedirectResponse(response)) {
1785
+ const spec = firstTrySpec === "draft-cavage-http-signatures-12" ? "rfc9421" : "draft-cavage-http-signatures-12";
1786
+ (0, _logtape_logtape.getLogger)([
1787
+ "fedify",
1788
+ "sig",
1789
+ "http"
1790
+ ]).debug("Failed to verify with the spec {spec} ({status} {statusText}); retrying with spec {secondSpec}... (double-knocking)", {
1791
+ spec: firstTrySpec,
1792
+ secondSpec: spec,
1793
+ status: response.status,
1794
+ statusText: response.statusText
1700
1795
  });
1701
- fulfilled = true;
1702
- } catch (error) {
1703
- logger.debug("Failed to fulfill Accept-Signature challenge entry {label}: {error}", {
1704
- label: entry.label,
1705
- error
1796
+ cancelResponseBody(response, request.url);
1797
+ signedRequest = await signRequest(request, identity.privateKey, identity.keyId, {
1798
+ spec,
1799
+ tracerProvider,
1800
+ body
1706
1801
  });
1802
+ log?.(signedRequest);
1803
+ response = await fetchDoubleKnockRequest(session, request, signedRequest, signal);
1804
+ if (!isRedirectResponse(response) && response.status !== 400 && response.status !== 401) await specDeterminer?.rememberSpec(origin, spec);
1707
1805
  }
1708
- }
1709
- if (fulfilled && challengeRequest != null) {
1710
- signedRequest = challengeRequest;
1711
- log?.(signedRequest);
1712
- response = await fetch(signedRequest, {
1713
- redirect: "manual",
1714
- signal
1715
- });
1716
- if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) return doubleKnock(await createRedirectRequest(request, response.headers.get("Location"), body, options.validateRedirect), identity, {
1717
- ...options,
1718
- body
1719
- });
1720
- }
1721
- if (fulfilled && response.status < 300) {
1722
- await specDeterminer?.rememberSpec(origin, "rfc9421");
1723
- return response;
1724
- }
1725
- if (fulfilled && response.status !== 400 && response.status !== 401) return response;
1726
- }
1727
- const spec = firstTrySpec === "draft-cavage-http-signatures-12" ? "rfc9421" : "draft-cavage-http-signatures-12";
1728
- logger.debug("Failed to verify with the spec {spec} ({status} {statusText}); retrying with spec {secondSpec}... (double-knocking)", {
1729
- spec: firstTrySpec,
1730
- secondSpec: spec,
1731
- status: response.status,
1732
- statusText: response.statusText
1733
- });
1734
- signedRequest = await signRequest(request, identity.privateKey, identity.keyId, {
1735
- spec,
1736
- tracerProvider,
1737
- body
1738
- });
1739
- log?.(signedRequest);
1740
- response = await fetchDoubleKnockRequest(request, signedRequest, signal);
1741
- if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
1806
+ } else if (!firstRedirected) await specDeterminer?.rememberSpec(origin, firstTrySpec);
1807
+ if (!isRedirectResponse(response)) return {
1808
+ response,
1809
+ session
1810
+ };
1811
+ cancelResponseBody(response, request.url);
1812
+ await session.close();
1742
1813
  if (redirected >= DEFAULT_MAX_REDIRECTION) throw new _fedify_vocab_runtime.FetchError(request.url, `Too many redirections (${redirected + 1})`);
1743
- const redirectRequest = await createRedirectRequest(request, response.headers.get("Location"), body, options.validateRedirect);
1814
+ const location = response.headers.get("Location");
1815
+ const redirectRequest = await createRedirectRequest(request, location, body, options.validateRedirect);
1744
1816
  if (visited.has(redirectRequest.url)) throw new _fedify_vocab_runtime.FetchError(request.url, `Redirect loop detected: ${redirectRequest.url}`);
1745
- return doubleKnockInternal(redirectRequest, identity, {
1746
- ...options,
1747
- body
1748
- }, redirected + 1, visited);
1749
- } else if (response.status !== 400 && response.status !== 401) await specDeterminer?.rememberSpec(origin, spec);
1750
- } else await specDeterminer?.rememberSpec(origin, firstTrySpec);
1751
- return response;
1817
+ request = redirectRequest;
1818
+ session = await openHop(request.url);
1819
+ }
1820
+ } catch (error) {
1821
+ try {
1822
+ await session.close();
1823
+ } catch (closeError) {
1824
+ (0, _logtape_logtape.getLogger)([
1825
+ "fedify",
1826
+ "sig",
1827
+ "http"
1828
+ ]).debug("Failed to close the session for {url}: {error}", {
1829
+ url: request.url,
1830
+ error: closeError
1831
+ });
1832
+ }
1833
+ throw error;
1834
+ }
1752
1835
  }
1753
1836
  /**
1754
1837
  * Performs a timing-safe equality comparison between two `Uint8Array` values.
@@ -1778,10 +1861,10 @@ function timingSafeEqual(a, b) {
1778
1861
  return result === 0;
1779
1862
  }
1780
1863
  //#endregion
1781
- Object.defineProperty(exports, "doubleKnock", {
1864
+ Object.defineProperty(exports, "doubleKnockScoped", {
1782
1865
  enumerable: true,
1783
1866
  get: function() {
1784
- return doubleKnock;
1867
+ return doubleKnockScoped;
1785
1868
  }
1786
1869
  });
1787
1870
  Object.defineProperty(exports, "exportJwk", {