@fedify/fedify 2.0.35-dev.2470 → 2.0.35-dev.2485

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 (84) hide show
  1. package/dist/{builder-CusH9K0G.mjs → builder-CKbQrOH1.mjs} +3 -3
  2. package/dist/{client-CAM_bQXx.d.cts → client-BIBzqEIp.d.cts} +0 -1
  3. package/dist/{client-BUqbea1d.mjs → client-DGNacEx_.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-r8sjhltN.d.cts → context-CnoWxuoB.d.cts} +2 -2
  9. package/dist/{context-B2iX3heK.d.ts → context-lxjaUTAI.d.ts} +2 -2
  10. package/dist/{deno-BtRoR9in.mjs → deno-DL_ifPJk.mjs} +1 -1
  11. package/dist/docloader-B0_0Fjqo.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/middleware.test.mjs +42 -23
  17. package/dist/federation/mod.cjs +1 -1
  18. package/dist/federation/mod.d.cts +2 -2
  19. package/dist/federation/mod.d.ts +2 -2
  20. package/dist/federation/mod.js +1 -1
  21. package/dist/federation/send.test.mjs +445 -70
  22. package/dist/federation/temporal.test.mjs +1 -1
  23. package/dist/federation/webfinger.test.mjs +1 -1
  24. package/dist/{http-Bs6thuCC.d.cts → http-Cd8UD1ym.d.ts} +1 -2
  25. package/dist/{http-C7d5tddX.cjs → http-CjkqnlR2.cjs} +114 -56
  26. package/dist/{http-CDupyfOc.d.ts → http-DFqAb11b.d.cts} +1 -2
  27. package/dist/{http-BTQ5wllw.js → http-DVEBwq6e.js} +113 -55
  28. package/dist/{http-CMduEDST.mjs → http-Dvsq4uCu.mjs} +136 -49
  29. package/dist/{inbox-BkvNt_Sb.mjs → inbox-D6YbZFrZ.mjs} +1 -1
  30. package/dist/{key-CDAqlyxF.mjs → key-CgcWGw6W.mjs} +1 -1
  31. package/dist/{kv-cache-Cm0DSKFo.js → kv-cache-DC_F2WSH.js} +60 -18
  32. package/dist/{kv-cache-D-xcxGwU.cjs → kv-cache-nN9RaiYp.cjs} +59 -17
  33. package/dist/{ld-Ceiqwon2.mjs → ld-CfOIwHup.mjs} +2 -2
  34. package/dist/{middleware-NVjGfqSC.js → middleware-B0eqDUmD.js} +118 -33
  35. package/dist/{middleware-Bj0R0_D7.mjs → middleware-CA7egMDR.mjs} +12 -12
  36. package/dist/{middleware-Bx0vwC0P.cjs → middleware-Cu_yKroA.cjs} +1 -1
  37. package/dist/{middleware-bv6duFIF.mjs → middleware-DEwj7jGj.mjs} +1 -1
  38. package/dist/{middleware-BWtJCObq.cjs → middleware-DZvWX5af.cjs} +118 -33
  39. package/dist/{mod-BkPB4hPe.d.cts → mod-BPDfu4Ey.d.cts} +2 -7
  40. package/dist/{mod-E7YM72JZ.d.ts → mod-BS-jg3Wx.d.ts} +1 -1
  41. package/dist/{mod-BKJyD7nk.d.ts → mod-DGP89q0u.d.ts} +2 -7
  42. package/dist/{mod-IOWUF5Yr.d.cts → mod-DVPS4yam.d.cts} +1 -1
  43. package/dist/{mod-CM_eR8hQ.d.ts → mod-I-34OMN3.d.ts} +1 -1
  44. package/dist/{mod-DDTLbfce.d.cts → mod-w1oyHseQ.d.cts} +1 -1
  45. package/dist/mod.cjs +5 -5
  46. package/dist/mod.d.cts +6 -6
  47. package/dist/mod.d.ts +6 -6
  48. package/dist/mod.js +5 -5
  49. package/dist/nodeinfo/client.test.mjs +337 -19
  50. package/dist/nodeinfo/handler.test.mjs +1 -1
  51. package/dist/nodeinfo/mod.cjs +1 -1
  52. package/dist/nodeinfo/mod.d.cts +1 -1
  53. package/dist/nodeinfo/mod.d.ts +1 -1
  54. package/dist/nodeinfo/mod.js +1 -1
  55. package/dist/otel/exporter.test.mjs +292 -30
  56. package/dist/otel/mod.cjs +36 -30
  57. package/dist/otel/mod.d.cts +13 -0
  58. package/dist/otel/mod.d.ts +13 -0
  59. package/dist/otel/mod.js +36 -30
  60. package/dist/{owner-Cmn2WKBi.mjs → owner-BvVwCoj2.mjs} +2 -2
  61. package/dist/{proof-DllVQdfR.cjs → proof-BssSZpMi.cjs} +1 -1
  62. package/dist/{proof-VZ9Ryp3F.mjs → proof-C9TBSmUR.mjs} +2 -2
  63. package/dist/{proof-ClDA_fq4.js → proof-Cp62GFJ3.js} +1 -1
  64. package/dist/{send-BiC5gxMS.mjs → send-ZWmjk2Zf.mjs} +117 -32
  65. package/dist/sig/http.test.mjs +411 -2
  66. package/dist/sig/key.test.mjs +2 -2
  67. package/dist/sig/ld.test.mjs +2 -2
  68. package/dist/sig/mod.cjs +2 -2
  69. package/dist/sig/mod.d.cts +2 -2
  70. package/dist/sig/mod.d.ts +2 -2
  71. package/dist/sig/mod.js +2 -2
  72. package/dist/sig/owner.test.mjs +1 -1
  73. package/dist/sig/proof.test.mjs +1 -1
  74. package/dist/{temporal-DXm8zGR8.mjs → temporal-BNtFi_ut.mjs} +1 -1
  75. package/dist/testing/mod.d.mts +0 -1
  76. package/dist/{types-miAYfiGO.js → types-B1_pRFE_.js} +112 -63
  77. package/dist/{types-DRPjmHVV.cjs → types-DOvyymM4.cjs} +111 -62
  78. package/dist/utils/docloader.test.mjs +316 -32
  79. package/dist/utils/mod.cjs +1 -1
  80. package/dist/utils/mod.d.cts +1 -1
  81. package/dist/utils/mod.d.ts +1 -1
  82. package/dist/utils/mod.js +1 -1
  83. package/package.json +6 -6
  84. package/dist/docloader-BF0FJHJ1.mjs +0 -82
@@ -5,9 +5,9 @@ import { t as assertEquals } from "../assert_equals-Ew3jOFa3.mjs";
5
5
  import { r as assertFalse, t as assertThrows } from "../assert_throws-D4DNWrux.mjs";
6
6
  import { n as assertInstanceOf, t as assert } from "../assert-BhQd8QHP.mjs";
7
7
  import { t as assertRejects } from "../assert_rejects-WyYSTDDX.mjs";
8
- import { i as generateCryptoKeyPair } from "../key-CDAqlyxF.mjs";
8
+ import { i as generateCryptoKeyPair } from "../key-CgcWGw6W.mjs";
9
9
  import { a as rsaPrivateKey3, c as rsaPublicKey3, i as rsaPrivateKey2, n as ed25519PrivateKey, s as rsaPublicKey2, t as ed25519Multikey } from "../keys-CCxRJhWa.mjs";
10
- import { a as compactJsonLd, d as isInvalidUrlTypeError, f as signJsonLd, h as verifySignature, i as attachSignature, m as verifyJsonLd, n as UnsafeJsonLdError, o as createSignature, s as detachSignature } from "../ld-Ceiqwon2.mjs";
10
+ import { a as compactJsonLd, d as isInvalidUrlTypeError, f as signJsonLd, h as verifySignature, i as attachSignature, m as verifyJsonLd, n as UnsafeJsonLdError, o as createSignature, s as detachSignature } from "../ld-CfOIwHup.mjs";
11
11
  import { mockDocumentLoader, test } from "@fedify/fixture";
12
12
  import { CryptographicKey } from "@fedify/vocab";
13
13
  import { encodeBase64 } from "byte-encodings/base64";
package/dist/sig/mod.cjs CHANGED
@@ -1,8 +1,8 @@
1
1
  const { Temporal } = require("@js-temporal/polyfill");
2
2
  const { URLPattern } = require("urlpattern-polyfill");
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
4
- const require_http = require("../http-C7d5tddX.cjs");
5
- const require_proof = require("../proof-DllVQdfR.cjs");
4
+ const require_http = require("../http-CjkqnlR2.cjs");
5
+ const require_proof = require("../proof-BssSZpMi.cjs");
6
6
  exports.attachSignature = require_proof.attachSignature;
7
7
  exports.createProof = require_proof.createProof;
8
8
  exports.createSignature = require_proof.createSignature;
@@ -1,5 +1,5 @@
1
1
  /// <reference lib="esnext.temporal" />
2
- import { a as signRequest, c as FetchKeyResult, d as fetchKey, f as generateCryptoKeyPair, i as VerifyRequestOptions, l as KeyCache, n as HttpMessageSignaturesSpecDeterminer, o as verifyRequest, p as importJwk, r as SignRequestOptions, s as FetchKeyOptions, t as HttpMessageSignaturesSpec, u as exportJwk } from "../http-Bs6thuCC.cjs";
2
+ import { a as signRequest, c as FetchKeyResult, d as fetchKey, f as generateCryptoKeyPair, i as VerifyRequestOptions, l as KeyCache, n as HttpMessageSignaturesSpecDeterminer, o as verifyRequest, p as importJwk, r as SignRequestOptions, s as FetchKeyOptions, t as HttpMessageSignaturesSpec, u as exportJwk } from "../http-DFqAb11b.cjs";
3
3
  import { i as getKeyOwner, n as GetKeyOwnerOptions, r as doesActorOwnKey, t as DoesActorOwnKeyOptions } from "../owner-B3hNQCG8.cjs";
4
- import { _ as verifyJsonLd, a as createProof, c as verifyProof, d as VerifyJsonLdOptions, f as VerifySignatureOptions, g as signJsonLd, h as detachSignature, i as VerifyProofOptions, l as CreateSignatureOptions, m as createSignature, n as SignObjectOptions, o as signObject, p as attachSignature, r as VerifyObjectOptions, s as verifyObject, t as CreateProofOptions, u as SignJsonLdOptions, v as verifySignature } from "../mod-IOWUF5Yr.cjs";
4
+ import { _ as verifyJsonLd, a as createProof, c as verifyProof, d as VerifyJsonLdOptions, f as VerifySignatureOptions, g as signJsonLd, h as detachSignature, i as VerifyProofOptions, l as CreateSignatureOptions, m as createSignature, n as SignObjectOptions, o as signObject, p as attachSignature, r as VerifyObjectOptions, s as verifyObject, t as CreateProofOptions, u as SignJsonLdOptions, v as verifySignature } from "../mod-DVPS4yam.cjs";
5
5
  export { CreateProofOptions, CreateSignatureOptions, DoesActorOwnKeyOptions, FetchKeyOptions, FetchKeyResult, GetKeyOwnerOptions, HttpMessageSignaturesSpec, HttpMessageSignaturesSpecDeterminer, KeyCache, SignJsonLdOptions, SignObjectOptions, SignRequestOptions, VerifyJsonLdOptions, VerifyObjectOptions, VerifyProofOptions, VerifyRequestOptions, VerifySignatureOptions, attachSignature, createProof, createSignature, detachSignature, doesActorOwnKey, exportJwk, fetchKey, generateCryptoKeyPair, getKeyOwner, importJwk, signJsonLd, signObject, signRequest, verifyJsonLd, verifyObject, verifyProof, verifyRequest, verifySignature };
package/dist/sig/mod.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /// <reference lib="esnext.temporal" />
2
- import { a as signRequest, c as FetchKeyResult, d as fetchKey, f as generateCryptoKeyPair, i as VerifyRequestOptions, l as KeyCache, n as HttpMessageSignaturesSpecDeterminer, o as verifyRequest, p as importJwk, r as SignRequestOptions, s as FetchKeyOptions, t as HttpMessageSignaturesSpec, u as exportJwk } from "../http-CDupyfOc.js";
2
+ import { a as signRequest, c as FetchKeyResult, d as fetchKey, f as generateCryptoKeyPair, i as VerifyRequestOptions, l as KeyCache, n as HttpMessageSignaturesSpecDeterminer, o as verifyRequest, p as importJwk, r as SignRequestOptions, s as FetchKeyOptions, t as HttpMessageSignaturesSpec, u as exportJwk } from "../http-Cd8UD1ym.js";
3
3
  import { i as getKeyOwner, n as GetKeyOwnerOptions, r as doesActorOwnKey, t as DoesActorOwnKeyOptions } from "../owner-CiBPcC6F.js";
4
- import { _ as verifyJsonLd, a as createProof, c as verifyProof, d as VerifyJsonLdOptions, f as VerifySignatureOptions, g as signJsonLd, h as detachSignature, i as VerifyProofOptions, l as CreateSignatureOptions, m as createSignature, n as SignObjectOptions, o as signObject, p as attachSignature, r as VerifyObjectOptions, s as verifyObject, t as CreateProofOptions, u as SignJsonLdOptions, v as verifySignature } from "../mod-E7YM72JZ.js";
4
+ import { _ as verifyJsonLd, a as createProof, c as verifyProof, d as VerifyJsonLdOptions, f as VerifySignatureOptions, g as signJsonLd, h as detachSignature, i as VerifyProofOptions, l as CreateSignatureOptions, m as createSignature, n as SignObjectOptions, o as signObject, p as attachSignature, r as VerifyObjectOptions, s as verifyObject, t as CreateProofOptions, u as SignJsonLdOptions, v as verifySignature } from "../mod-BS-jg3Wx.js";
5
5
  export { CreateProofOptions, CreateSignatureOptions, DoesActorOwnKeyOptions, FetchKeyOptions, FetchKeyResult, GetKeyOwnerOptions, HttpMessageSignaturesSpec, HttpMessageSignaturesSpecDeterminer, KeyCache, SignJsonLdOptions, SignObjectOptions, SignRequestOptions, VerifyJsonLdOptions, VerifyObjectOptions, VerifyProofOptions, VerifyRequestOptions, VerifySignatureOptions, attachSignature, createProof, createSignature, detachSignature, doesActorOwnKey, exportJwk, fetchKey, generateCryptoKeyPair, getKeyOwner, importJwk, signJsonLd, signObject, signRequest, verifyJsonLd, verifyObject, verifyProof, verifyRequest, verifySignature };
package/dist/sig/mod.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
2
  import { URLPattern } from "urlpattern-polyfill";
3
- import { c as importJwk, i as exportJwk, n as signRequest, o as fetchKey, r as verifyRequest, s as generateCryptoKeyPair } from "../http-BTQ5wllw.js";
4
- import { _ as signJsonLd, a as doesActorOwnKey, b as verifySignature, d as createSignature, f as detachSignature, i as verifyProof, l as attachSignature, n as signObject, o as getKeyOwner, r as verifyObject, t as createProof, y as verifyJsonLd } from "../proof-ClDA_fq4.js";
3
+ import { c as importJwk, i as exportJwk, n as signRequest, o as fetchKey, r as verifyRequest, s as generateCryptoKeyPair } from "../http-DVEBwq6e.js";
4
+ import { _ as signJsonLd, a as doesActorOwnKey, b as verifySignature, d as createSignature, f as detachSignature, i as verifyProof, l as attachSignature, n as signObject, o as getKeyOwner, r as verifyObject, t as createProof, y as verifyJsonLd } from "../proof-Cp62GFJ3.js";
5
5
  export { attachSignature, createProof, createSignature, detachSignature, doesActorOwnKey, exportJwk, fetchKey, generateCryptoKeyPair, getKeyOwner, importJwk, signJsonLd, signObject, signRequest, verifyJsonLd, verifyObject, verifyProof, verifyRequest, verifySignature };
@@ -6,7 +6,7 @@ import "../std__assert-DflMZha5.mjs";
6
6
  import { r as assertFalse } from "../assert_throws-D4DNWrux.mjs";
7
7
  import { t as assert } from "../assert-BhQd8QHP.mjs";
8
8
  import { o as rsaPublicKey1, s as rsaPublicKey2 } from "../keys-CCxRJhWa.mjs";
9
- import { n as getKeyOwner, t as doesActorOwnKey } from "../owner-Cmn2WKBi.mjs";
9
+ import { n as getKeyOwner, t as doesActorOwnKey } from "../owner-BvVwCoj2.mjs";
10
10
  import { createTestTracerProvider, mockDocumentLoader, test } from "@fedify/fixture";
11
11
  import { Create, CryptographicKey, lookupObject } from "@fedify/vocab";
12
12
  //#region src/sig/owner.test.ts
@@ -6,7 +6,7 @@ import "../std__assert-DflMZha5.mjs";
6
6
  import { n as assertInstanceOf } from "../assert-BhQd8QHP.mjs";
7
7
  import { t as assertRejects } from "../assert_rejects-WyYSTDDX.mjs";
8
8
  import { i as rsaPrivateKey2, n as ed25519PrivateKey, r as ed25519PublicKey, s as rsaPublicKey2, t as ed25519Multikey } from "../keys-CCxRJhWa.mjs";
9
- import { i as verifyProof, n as signObject, r as verifyObject, t as createProof } from "../proof-VZ9Ryp3F.mjs";
9
+ import { i as verifyProof, n as signObject, r as verifyObject, t as createProof } from "../proof-C9TBSmUR.mjs";
10
10
  import { mockDocumentLoader, test } from "@fedify/fixture";
11
11
  import { Create, DataIntegrityProof, Multikey, Note, Place } from "@fedify/vocab";
12
12
  import { decodeMultibase, importMultibaseKey } from "@fedify/vocab-runtime";
@@ -1,7 +1,7 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
- import { c as getNormalizationContextLoader } from "./ld-Ceiqwon2.mjs";
4
+ import { c as getNormalizationContextLoader } from "./ld-CfOIwHup.mjs";
5
5
  import jsonld from "@fedify/vocab-runtime/jsonld";
6
6
  //#region src/federation/temporal.ts
7
7
  function isPlainObject(value) {
@@ -5,7 +5,6 @@ import { Activity, Actor, Collection, CryptographicKey, Hashtag, Link, LookupObj
5
5
  import { Link as Link$1, LookupWebFingerOptions, ResourceDescriptor } from "@fedify/webfinger";
6
6
  import { TracerProvider } from "@opentelemetry/api";
7
7
  import { DocumentLoader, GetUserAgentOptions } from "@fedify/vocab-runtime";
8
-
9
8
  //#region src/nodeinfo/types.d.ts
10
9
  /**
11
10
  * The type of the result of parsing JSON.
@@ -1,7 +1,8 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
2
  import { URLPattern } from "urlpattern-polyfill";
3
3
  import { getLogger } from "@logtape/logtape";
4
- import { FetchError, UrlError, getUserAgent, validatePublicUrl } from "@fedify/vocab-runtime";
4
+ import { FetchError, UrlError, getUserAgent } from "@fedify/vocab-runtime";
5
+ import { openPublicFetch } from "@fedify/vocab-runtime/internal";
5
6
  //#region src/utils/body.ts
6
7
  /** Decoded JSON body limit: 16 MiB. @internal */
7
8
  const MAX_BODY_SIZE = 16 * 1024 * 1024;
@@ -84,23 +85,34 @@ const logger = getLogger([
84
85
  ]);
85
86
  const DEFAULT_MAX_REDIRECTION = 5;
86
87
  /**
87
- * Fetches a URL while guarding against SSRF. Before every request—including
88
- * each redirect hop—the destination is validated with
89
- * {@link validatePublicUrl}, so that loopback, link-local, and private
90
- * addresses, as well as non-HTTP(S) schemes such as `data:`, are rejected.
91
- * Redirects are followed manually with a cap, so that a public URL cannot
92
- * bounce the request to an internal address.
88
+ * Fetches a URL while guarding against SSRF. Every request—including each
89
+ * redirect hop—is made in its own session opened with `openPublicFetch()`,
90
+ * which rejects loopback, link-local, and private addresses, as well as
91
+ * non-HTTP(S) schemes such as `data:`, and connects only to the validated
92
+ * addresses where the runtime supports it. Redirects are followed manually
93
+ * with a cap, so that a public URL cannot bounce the request to an internal
94
+ * address.
95
+ *
96
+ * The final response is passed to `consume`, which must consume or cancel its
97
+ * body; the session is closed after `consume` settles. The cancellation of
98
+ * redirect response bodies is started before their sessions are closed.
93
99
  * @param url The URL to fetch.
94
100
  * @param headers The request headers.
95
- * @returns The final response, or `null` if the URL is disallowed, its
96
- * hostname cannot be resolved, or there are too many redirects.
101
+ * @param consume The function to consume the final response with.
102
+ * @param deps The dependencies.
103
+ * @returns The value returned by `consume`, or `null` if the URL is
104
+ * disallowed, its hostname cannot be resolved, or there are too many
105
+ * redirects.
106
+ * @internal
97
107
  */
98
- async function fetchPublicUrl(url, headers) {
108
+ async function fetchPublicUrl(url, headers, consume, deps = {}) {
109
+ const openHop = deps.openHop ?? ((hopUrl) => openPublicFetch(hopUrl));
99
110
  let current = url instanceof URL ? url : new URL(url);
100
111
  let redirected = 0;
101
112
  while (true) {
113
+ let session;
102
114
  try {
103
- await validatePublicUrl(current.href);
115
+ session = await openHop(current.href);
104
116
  } catch (e) {
105
117
  if (e instanceof UrlError) {
106
118
  if (e.reason === "dns") logger.error("DNS lookup failed for {url}: {error}", {
@@ -115,52 +127,89 @@ async function fetchPublicUrl(url, headers) {
115
127
  }
116
128
  throw e;
117
129
  }
118
- const response = await fetch(current, {
119
- headers,
120
- redirect: "manual"
121
- });
122
- if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
123
- redirected++;
124
- if (redirected >= DEFAULT_MAX_REDIRECTION) {
125
- logger.error("Too many redirections ({redirections}) while fetching {url}.", {
126
- redirections: redirected,
127
- url: current.href
128
- });
129
- return null;
130
- }
131
- const next = new URL(response.headers.get("Location"), response.url == null || response.url === "" ? current : response.url);
132
- if (next.protocol !== current.protocol) {
133
- logger.error("Refused a redirect to a different protocol ({protocol} to {redirectedProtocol}) while fetching NodeInfo.", {
134
- protocol: current.protocol,
135
- redirectedProtocol: next.protocol
136
- });
137
- return null;
138
- }
139
- current = next;
140
- continue;
130
+ let location;
131
+ try {
132
+ const response = await session.fetch(current, {
133
+ headers,
134
+ redirect: "manual"
135
+ });
136
+ if (response.status < 300 || response.status >= 400 || !response.headers.has("Location")) return await consume(response);
137
+ location = response.headers.get("Location");
138
+ cancelBody(response, current);
139
+ } finally {
140
+ await session.close();
141
+ }
142
+ redirected++;
143
+ if (redirected >= DEFAULT_MAX_REDIRECTION) {
144
+ logger.error("Too many redirections ({redirections}) while fetching {url}.", {
145
+ redirections: redirected,
146
+ url: current.href
147
+ });
148
+ return null;
149
+ }
150
+ const next = new URL(location, current);
151
+ if (next.protocol !== current.protocol) {
152
+ logger.error("Refused a redirect to a different protocol ({protocol} to {redirectedProtocol}) while fetching NodeInfo.", {
153
+ protocol: current.protocol,
154
+ redirectedProtocol: next.protocol
155
+ });
156
+ return null;
141
157
  }
142
- return response;
158
+ current = next;
143
159
  }
144
160
  }
161
+ /**
162
+ * Starts cancelling a response body that is not read, without waiting for it
163
+ * to settle. The cancellation of a branch of a tee (e.g., a body cloned by
164
+ * a fetch mock) settles only after the other branch is cancelled too, per
165
+ * the Streams standard, so awaiting it could block forever. A failure is
166
+ * only logged.
167
+ */
168
+ function cancelBody(response, url) {
169
+ response.body?.cancel().catch((error) => {
170
+ logger.debug("Failed to cancel the response body from {url}: {error}", {
171
+ url: url.toString(),
172
+ error
173
+ });
174
+ });
175
+ }
145
176
  async function getNodeInfo(url, options = {}) {
177
+ return await getNodeInfoInternal(url, options);
178
+ }
179
+ /**
180
+ * The implementation of {@link getNodeInfo}, which is not exported from any
181
+ * entry point.
182
+ * @param url The base URL of the server, or the URL of the NodeInfo document
183
+ * if `options.direct` is turned on.
184
+ * @param options Options for fetching the NodeInfo document.
185
+ * @param deps The dependencies.
186
+ * @returns The NodeInfo document if it could be fetched successfully.
187
+ * Otherwise, `undefined` is returned.
188
+ * @internal
189
+ */
190
+ async function getNodeInfoInternal(url, options = {}, deps = {}) {
146
191
  try {
192
+ const headers = {
193
+ Accept: "application/json",
194
+ "User-Agent": typeof options.userAgent === "string" ? options.userAgent : getUserAgent(options.userAgent)
195
+ };
147
196
  let nodeInfoUrl = url;
148
197
  if (!options.direct) {
149
198
  const wellKnownUrl = new URL("/.well-known/nodeinfo", url);
150
- const wellKnownResponse = await fetchPublicUrl(wellKnownUrl, {
151
- Accept: "application/json",
152
- "User-Agent": typeof options.userAgent === "string" ? options.userAgent : getUserAgent(options.userAgent)
153
- });
154
- if (wellKnownResponse == null) return void 0;
155
- if (!wellKnownResponse.ok) {
156
- logger.error("Failed to fetch {url}: {status} {statusText}", {
157
- url: wellKnownUrl.href,
158
- status: wellKnownResponse.status,
159
- statusText: wellKnownResponse.statusText
160
- });
161
- return;
162
- }
163
- const wellKnownRd = JSON.parse(await readBoundedText(wellKnownResponse, MAX_BODY_SIZE, wellKnownUrl));
199
+ const wellKnown = await fetchPublicUrl(wellKnownUrl, headers, async (response) => {
200
+ if (!response.ok) {
201
+ logger.error("Failed to fetch {url}: {status} {statusText}", {
202
+ url: wellKnownUrl.href,
203
+ status: response.status,
204
+ statusText: response.statusText
205
+ });
206
+ cancelBody(response, wellKnownUrl);
207
+ return;
208
+ }
209
+ return { rd: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, wellKnownUrl)) };
210
+ }, deps);
211
+ if (wellKnown == null) return void 0;
212
+ const wellKnownRd = wellKnown.rd;
164
213
  const link = wellKnownRd?.links?.find((link) => link != null && "rel" in link && (link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.0" || link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.1") && "href" in link && link.href != null);
165
214
  if (link == null || link.href == null) {
166
215
  logger.error("Failed to find a NodeInfo document link from {url}: {resourceDescriptor}", {
@@ -171,20 +220,20 @@ async function getNodeInfo(url, options = {}) {
171
220
  }
172
221
  nodeInfoUrl = link.href;
173
222
  }
174
- const response = await fetchPublicUrl(nodeInfoUrl, {
175
- Accept: "application/json",
176
- "User-Agent": typeof options.userAgent === "string" ? options.userAgent : getUserAgent(options.userAgent)
177
- });
178
- if (response == null) return void 0;
179
- if (!response.ok) {
180
- logger.error("Failed to fetch NodeInfo document from {url}: {status} {statusText}", {
181
- url: nodeInfoUrl.toString(),
182
- status: response.status,
183
- statusText: response.statusText
184
- });
185
- return;
186
- }
187
- const data = JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl));
223
+ const document = await fetchPublicUrl(nodeInfoUrl, headers, async (response) => {
224
+ if (!response.ok) {
225
+ logger.error("Failed to fetch NodeInfo document from {url}: {status} {statusText}", {
226
+ url: nodeInfoUrl.toString(),
227
+ status: response.status,
228
+ statusText: response.statusText
229
+ });
230
+ cancelBody(response, nodeInfoUrl);
231
+ return;
232
+ }
233
+ return { data: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl)) };
234
+ }, deps);
235
+ if (document == null) return void 0;
236
+ const { data } = document;
188
237
  if (options.parse === "none") return data;
189
238
  return parseNodeInfo(data, { tryBestEffort: options.parse === "best-effort" }) ?? void 0;
190
239
  } catch (error) {
@@ -3,6 +3,7 @@ const { URLPattern } = require("urlpattern-polyfill");
3
3
  require("./chunk-DDcVe30Y.cjs");
4
4
  let _logtape_logtape = require("@logtape/logtape");
5
5
  let _fedify_vocab_runtime = require("@fedify/vocab-runtime");
6
+ let _fedify_vocab_runtime_internal = require("@fedify/vocab-runtime/internal");
6
7
  //#region src/utils/body.ts
7
8
  /** Decoded JSON body limit: 16 MiB. @internal */
8
9
  const MAX_BODY_SIZE = 16 * 1024 * 1024;
@@ -85,23 +86,34 @@ const logger = (0, _logtape_logtape.getLogger)([
85
86
  ]);
86
87
  const DEFAULT_MAX_REDIRECTION = 5;
87
88
  /**
88
- * Fetches a URL while guarding against SSRF. Before every request—including
89
- * each redirect hop—the destination is validated with
90
- * {@link validatePublicUrl}, so that loopback, link-local, and private
91
- * addresses, as well as non-HTTP(S) schemes such as `data:`, are rejected.
92
- * Redirects are followed manually with a cap, so that a public URL cannot
93
- * bounce the request to an internal address.
89
+ * Fetches a URL while guarding against SSRF. Every request—including each
90
+ * redirect hop—is made in its own session opened with `openPublicFetch()`,
91
+ * which rejects loopback, link-local, and private addresses, as well as
92
+ * non-HTTP(S) schemes such as `data:`, and connects only to the validated
93
+ * addresses where the runtime supports it. Redirects are followed manually
94
+ * with a cap, so that a public URL cannot bounce the request to an internal
95
+ * address.
96
+ *
97
+ * The final response is passed to `consume`, which must consume or cancel its
98
+ * body; the session is closed after `consume` settles. The cancellation of
99
+ * redirect response bodies is started before their sessions are closed.
94
100
  * @param url The URL to fetch.
95
101
  * @param headers The request headers.
96
- * @returns The final response, or `null` if the URL is disallowed, its
97
- * hostname cannot be resolved, or there are too many redirects.
102
+ * @param consume The function to consume the final response with.
103
+ * @param deps The dependencies.
104
+ * @returns The value returned by `consume`, or `null` if the URL is
105
+ * disallowed, its hostname cannot be resolved, or there are too many
106
+ * redirects.
107
+ * @internal
98
108
  */
99
- async function fetchPublicUrl(url, headers) {
109
+ async function fetchPublicUrl(url, headers, consume, deps = {}) {
110
+ const openHop = deps.openHop ?? ((hopUrl) => (0, _fedify_vocab_runtime_internal.openPublicFetch)(hopUrl));
100
111
  let current = url instanceof URL ? url : new URL(url);
101
112
  let redirected = 0;
102
113
  while (true) {
114
+ let session;
103
115
  try {
104
- await (0, _fedify_vocab_runtime.validatePublicUrl)(current.href);
116
+ session = await openHop(current.href);
105
117
  } catch (e) {
106
118
  if (e instanceof _fedify_vocab_runtime.UrlError) {
107
119
  if (e.reason === "dns") logger.error("DNS lookup failed for {url}: {error}", {
@@ -116,52 +128,89 @@ async function fetchPublicUrl(url, headers) {
116
128
  }
117
129
  throw e;
118
130
  }
119
- const response = await fetch(current, {
120
- headers,
121
- redirect: "manual"
122
- });
123
- if (response.status >= 300 && response.status < 400 && response.headers.has("Location")) {
124
- redirected++;
125
- if (redirected >= DEFAULT_MAX_REDIRECTION) {
126
- logger.error("Too many redirections ({redirections}) while fetching {url}.", {
127
- redirections: redirected,
128
- url: current.href
129
- });
130
- return null;
131
- }
132
- const next = new URL(response.headers.get("Location"), response.url == null || response.url === "" ? current : response.url);
133
- if (next.protocol !== current.protocol) {
134
- logger.error("Refused a redirect to a different protocol ({protocol} to {redirectedProtocol}) while fetching NodeInfo.", {
135
- protocol: current.protocol,
136
- redirectedProtocol: next.protocol
137
- });
138
- return null;
139
- }
140
- current = next;
141
- continue;
131
+ let location;
132
+ try {
133
+ const response = await session.fetch(current, {
134
+ headers,
135
+ redirect: "manual"
136
+ });
137
+ if (response.status < 300 || response.status >= 400 || !response.headers.has("Location")) return await consume(response);
138
+ location = response.headers.get("Location");
139
+ cancelBody(response, current);
140
+ } finally {
141
+ await session.close();
142
+ }
143
+ redirected++;
144
+ if (redirected >= DEFAULT_MAX_REDIRECTION) {
145
+ logger.error("Too many redirections ({redirections}) while fetching {url}.", {
146
+ redirections: redirected,
147
+ url: current.href
148
+ });
149
+ return null;
150
+ }
151
+ const next = new URL(location, current);
152
+ if (next.protocol !== current.protocol) {
153
+ logger.error("Refused a redirect to a different protocol ({protocol} to {redirectedProtocol}) while fetching NodeInfo.", {
154
+ protocol: current.protocol,
155
+ redirectedProtocol: next.protocol
156
+ });
157
+ return null;
142
158
  }
143
- return response;
159
+ current = next;
144
160
  }
145
161
  }
162
+ /**
163
+ * Starts cancelling a response body that is not read, without waiting for it
164
+ * to settle. The cancellation of a branch of a tee (e.g., a body cloned by
165
+ * a fetch mock) settles only after the other branch is cancelled too, per
166
+ * the Streams standard, so awaiting it could block forever. A failure is
167
+ * only logged.
168
+ */
169
+ function cancelBody(response, url) {
170
+ response.body?.cancel().catch((error) => {
171
+ logger.debug("Failed to cancel the response body from {url}: {error}", {
172
+ url: url.toString(),
173
+ error
174
+ });
175
+ });
176
+ }
146
177
  async function getNodeInfo(url, options = {}) {
178
+ return await getNodeInfoInternal(url, options);
179
+ }
180
+ /**
181
+ * The implementation of {@link getNodeInfo}, which is not exported from any
182
+ * entry point.
183
+ * @param url The base URL of the server, or the URL of the NodeInfo document
184
+ * if `options.direct` is turned on.
185
+ * @param options Options for fetching the NodeInfo document.
186
+ * @param deps The dependencies.
187
+ * @returns The NodeInfo document if it could be fetched successfully.
188
+ * Otherwise, `undefined` is returned.
189
+ * @internal
190
+ */
191
+ async function getNodeInfoInternal(url, options = {}, deps = {}) {
147
192
  try {
193
+ const headers = {
194
+ Accept: "application/json",
195
+ "User-Agent": typeof options.userAgent === "string" ? options.userAgent : (0, _fedify_vocab_runtime.getUserAgent)(options.userAgent)
196
+ };
148
197
  let nodeInfoUrl = url;
149
198
  if (!options.direct) {
150
199
  const wellKnownUrl = new URL("/.well-known/nodeinfo", url);
151
- const wellKnownResponse = await fetchPublicUrl(wellKnownUrl, {
152
- Accept: "application/json",
153
- "User-Agent": typeof options.userAgent === "string" ? options.userAgent : (0, _fedify_vocab_runtime.getUserAgent)(options.userAgent)
154
- });
155
- if (wellKnownResponse == null) return void 0;
156
- if (!wellKnownResponse.ok) {
157
- logger.error("Failed to fetch {url}: {status} {statusText}", {
158
- url: wellKnownUrl.href,
159
- status: wellKnownResponse.status,
160
- statusText: wellKnownResponse.statusText
161
- });
162
- return;
163
- }
164
- const wellKnownRd = JSON.parse(await readBoundedText(wellKnownResponse, MAX_BODY_SIZE, wellKnownUrl));
200
+ const wellKnown = await fetchPublicUrl(wellKnownUrl, headers, async (response) => {
201
+ if (!response.ok) {
202
+ logger.error("Failed to fetch {url}: {status} {statusText}", {
203
+ url: wellKnownUrl.href,
204
+ status: response.status,
205
+ statusText: response.statusText
206
+ });
207
+ cancelBody(response, wellKnownUrl);
208
+ return;
209
+ }
210
+ return { rd: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, wellKnownUrl)) };
211
+ }, deps);
212
+ if (wellKnown == null) return void 0;
213
+ const wellKnownRd = wellKnown.rd;
165
214
  const link = wellKnownRd?.links?.find((link) => link != null && "rel" in link && (link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.0" || link.rel === "http://nodeinfo.diaspora.software/ns/schema/2.1") && "href" in link && link.href != null);
166
215
  if (link == null || link.href == null) {
167
216
  logger.error("Failed to find a NodeInfo document link from {url}: {resourceDescriptor}", {
@@ -172,20 +221,20 @@ async function getNodeInfo(url, options = {}) {
172
221
  }
173
222
  nodeInfoUrl = link.href;
174
223
  }
175
- const response = await fetchPublicUrl(nodeInfoUrl, {
176
- Accept: "application/json",
177
- "User-Agent": typeof options.userAgent === "string" ? options.userAgent : (0, _fedify_vocab_runtime.getUserAgent)(options.userAgent)
178
- });
179
- if (response == null) return void 0;
180
- if (!response.ok) {
181
- logger.error("Failed to fetch NodeInfo document from {url}: {status} {statusText}", {
182
- url: nodeInfoUrl.toString(),
183
- status: response.status,
184
- statusText: response.statusText
185
- });
186
- return;
187
- }
188
- const data = JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl));
224
+ const document = await fetchPublicUrl(nodeInfoUrl, headers, async (response) => {
225
+ if (!response.ok) {
226
+ logger.error("Failed to fetch NodeInfo document from {url}: {status} {statusText}", {
227
+ url: nodeInfoUrl.toString(),
228
+ status: response.status,
229
+ statusText: response.statusText
230
+ });
231
+ cancelBody(response, nodeInfoUrl);
232
+ return;
233
+ }
234
+ return { data: JSON.parse(await readBoundedText(response, MAX_BODY_SIZE, nodeInfoUrl)) };
235
+ }, deps);
236
+ if (document == null) return void 0;
237
+ const { data } = document;
189
238
  if (options.parse === "none") return data;
190
239
  return parseNodeInfo(data, { tryBestEffort: options.parse === "best-effort" }) ?? void 0;
191
240
  } catch (error) {