@fedify/relay 2.4.0-dev.1570 → 2.4.0-dev.1573

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.
@@ -1,7 +1,7 @@
1
1
  import "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
- import { n as exportSpki, r as getDocumentLoader, t as isRelayFollowerData } from "./types-Dfpx-Pz4.js";
4
+ import { n as exportSpki, r as getDocumentLoader, t as isRelayFollowerData } from "./types-Be8NyjpQ.js";
5
5
  import { MemoryKvStore, signRequest } from "@fedify/fedify";
6
6
  import { createRelay } from "@fedify/relay";
7
7
  import { Accept, Announce, Create, Delete, Follow, Move, Note, Person, Undo, Update } from "@fedify/vocab";
@@ -1,7 +1,7 @@
1
1
  import "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
- import { n as exportSpki, r as getDocumentLoader, t as isRelayFollowerData } from "./types-Dfpx-Pz4.js";
4
+ import { n as exportSpki, r as getDocumentLoader, t as isRelayFollowerData } from "./types-Be8NyjpQ.js";
5
5
  import { MemoryKvStore, signRequest } from "@fedify/fedify";
6
6
  import { createRelay } from "@fedify/relay";
7
7
  import { Create, Delete, Follow, Move, Note, Person, Undo, Update } from "@fedify/vocab";
package/dist/mod.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  /// <reference lib="esnext.temporal" />
2
2
  import { Context, KvStore, MessageQueue } from "@fedify/fedify";
3
3
  import { Actor } from "@fedify/vocab";
4
- //#region ../vocab-runtime/dist/mod.d.ts
4
+ //#region ../vocab-runtime/dist/docloader-xRGn1azD.d.ts
5
5
  /**
6
6
  * Options for making `User-Agent` string.
7
7
  * @see {@link getUserAgent}
package/dist/mod.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /// <reference lib="esnext.temporal" />
2
2
  import { Context, KvStore, MessageQueue } from "@fedify/fedify";
3
3
  import { Actor } from "@fedify/vocab";
4
- //#region ../vocab-runtime/dist/mod.d.ts
4
+ //#region ../vocab-runtime/dist/docloader-xRGn1azD.d.ts
5
5
  /**
6
6
  * Options for making `User-Agent` string.
7
7
  * @see {@link getUserAgent}
@@ -2,10 +2,10 @@ import "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
4
  import { createRequire } from "node:module";
5
- import { getLogger } from "@logtape/logtape";
6
- import process$1 from "node:process";
7
5
  import { lookup } from "node:dns/promises";
8
6
  import { isIP } from "node:net";
7
+ import { getLogger } from "@logtape/logtape";
8
+ import process$1 from "node:process";
9
9
  //#region \0rolldown/runtime.js
10
10
  var __create = Object.create;
11
11
  var __defProp = Object.defineProperty;
@@ -30,6 +30,179 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
30
  }) : target, mod));
31
31
  var __require = /* @__PURE__ */ createRequire(import.meta.url);
32
32
  //#endregion
33
+ //#region ../vocab-runtime/dist/url-BuxPHxK2.js
34
+ var UrlError = class extends Error {
35
+ constructor(message) {
36
+ super(message);
37
+ this.name = "UrlError";
38
+ }
39
+ };
40
+ /**
41
+ * Validates a URL to prevent SSRF attacks.
42
+ */
43
+ async function validatePublicUrl(url) {
44
+ const parsed = new URL(url);
45
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new UrlError(`Unsupported protocol: ${parsed.protocol}`);
46
+ let hostname = parsed.hostname;
47
+ if (hostname.startsWith("[") && hostname.endsWith("]")) hostname = hostname.slice(1, -1);
48
+ if (hostname === "localhost") throw new UrlError("Localhost is not allowed");
49
+ const hostnameFamily = isIP(hostname);
50
+ if (hostnameFamily !== 0) {
51
+ validatePublicIpAddress(hostname, hostnameFamily);
52
+ return;
53
+ }
54
+ if ("Deno" in globalThis && !isIP(hostname)) {
55
+ if ((await Deno.permissions.query({ name: "net" })).state !== "granted") return;
56
+ }
57
+ if ("Bun" in globalThis) {
58
+ if (hostname === "example.com" || hostname.endsWith(".example.com")) return;
59
+ else if (hostname === "fedify-test.internal") throw new UrlError("Invalid or private address: fedify-test.internal");
60
+ }
61
+ let addresses;
62
+ try {
63
+ addresses = await lookup(hostname, { all: true });
64
+ } catch {
65
+ addresses = [];
66
+ }
67
+ for (const { address, family } of addresses) validatePublicIpAddress(address, family);
68
+ }
69
+ function validatePublicIpAddress(address, family) {
70
+ if (family === 4 && isValidPublicIPv4Address(address) || family === 6 && isValidPublicIPv6Address(address)) return;
71
+ throw new UrlError(`Invalid or private address: ${address}`);
72
+ }
73
+ function isValidPublicIPv4Address(address) {
74
+ const parts = parseIPv4Address(address);
75
+ if (parts == null) return false;
76
+ const value = ipv4PartsToNumber(parts);
77
+ return !nonPublicIPv4Prefixes.some(({ base, prefix }) => matchesIPv4Prefix(value, base, prefix));
78
+ }
79
+ function isValidPublicIPv6Address(address) {
80
+ const words = parseIPv6Address(address);
81
+ if (words == null) return false;
82
+ if (nonPublicIPv6Prefixes.some(({ words: prefixWords, prefix }) => matchesIPv6Prefix(words, prefixWords, prefix))) return false;
83
+ for (const { extractIPv4, prefix, words: prefixWords } of ipv6WithIPv4Prefixes) {
84
+ if (!matchesIPv6Prefix(words, prefixWords, prefix)) continue;
85
+ const ipv4Address = extractIPv4(words);
86
+ if (ipv4Address != null && !isValidPublicIPv4Address(ipv4Address)) return false;
87
+ }
88
+ return true;
89
+ }
90
+ function expandIPv6Address(address) {
91
+ address = address.toLowerCase();
92
+ const ipv4Delimiter = address.lastIndexOf(":");
93
+ if (address.includes(".") && ipv4Delimiter >= 0) {
94
+ const ipv4Parts = parseIPv4Address(address.substring(ipv4Delimiter + 1));
95
+ if (ipv4Parts == null) return address;
96
+ const high = (ipv4Parts[0] << 8) + ipv4Parts[1];
97
+ const low = (ipv4Parts[2] << 8) + ipv4Parts[3];
98
+ address = address.substring(0, ipv4Delimiter + 1) + high.toString(16) + ":" + low.toString(16);
99
+ }
100
+ if (address === "::") return "0000:0000:0000:0000:0000:0000:0000:0000";
101
+ if (address.startsWith("::")) address = "0000" + address;
102
+ if (address.endsWith("::")) address = address + "0000";
103
+ address = address.replace("::", ":0000".repeat(8 - (address.match(/:/g) || []).length) + ":");
104
+ return address.split(":").map((part) => part.padStart(4, "0")).join(":");
105
+ }
106
+ const nonPublicIPv4Prefixes = [
107
+ ipv4Prefix("0.0.0.0/8", "RFC 6890"),
108
+ ipv4Prefix("10.0.0.0/8", "RFC 1918"),
109
+ ipv4Prefix("100.64.0.0/10", "RFC 6598"),
110
+ ipv4Prefix("127.0.0.0/8", "RFC 1122"),
111
+ ipv4Prefix("169.254.0.0/16", "RFC 3927"),
112
+ ipv4Prefix("172.16.0.0/12", "RFC 1918"),
113
+ ipv4Prefix("192.0.0.0/24", "RFC 6890"),
114
+ ipv4Prefix("192.0.2.0/24", "RFC 5737"),
115
+ ipv4Prefix("192.88.99.0/24", "RFC 7526"),
116
+ ipv4Prefix("192.168.0.0/16", "RFC 1918"),
117
+ ipv4Prefix("198.18.0.0/15", "RFC 2544"),
118
+ ipv4Prefix("198.51.100.0/24", "RFC 5737"),
119
+ ipv4Prefix("203.0.113.0/24", "RFC 5737"),
120
+ ipv4Prefix("224.0.0.0/4", "RFC 5771"),
121
+ ipv4Prefix("240.0.0.0/4", "RFC 1112")
122
+ ];
123
+ const nonPublicIPv6Prefixes = [
124
+ ipv6Prefix("::/16", "RFC 4291"),
125
+ ipv6Prefix("2001::/32", "RFC 4380"),
126
+ ipv6Prefix("2002::/16", "RFC 3056"),
127
+ ipv6Prefix("64:ff9b:1::/48", "RFC 8215"),
128
+ ipv6Prefix("fc00::/7", "RFC 4193"),
129
+ ipv6Prefix("fe80::/10", "RFC 4291"),
130
+ ipv6Prefix("ff00::/8", "RFC 4291")
131
+ ];
132
+ const ipv6WithIPv4Prefixes = [{
133
+ ...ipv6Prefix("64:ff9b::/96", "RFC 6052"),
134
+ extractIPv4: (words) => ipv4FromWords(words[6], words[7])
135
+ }];
136
+ function ipv4Prefix(cidr, rfc) {
137
+ const [address, prefixText] = cidr.split("/");
138
+ const prefix = parseInt(prefixText, 10);
139
+ const parts = parseIPv4Address(address);
140
+ if (parts == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 32) throw new Error(`Invalid IPv4 prefix: ${cidr}`);
141
+ return {
142
+ cidr,
143
+ base: ipv4PartsToNumber(parts),
144
+ prefix,
145
+ rfc
146
+ };
147
+ }
148
+ function ipv6Prefix(cidr, rfc) {
149
+ const [address, prefixText] = cidr.split("/");
150
+ const prefix = parseInt(prefixText, 10);
151
+ const words = parseIPv6Address(address);
152
+ if (words == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 128) throw new Error(`Invalid IPv6 prefix: ${cidr}`);
153
+ return {
154
+ cidr,
155
+ words,
156
+ prefix,
157
+ rfc
158
+ };
159
+ }
160
+ function parseIPv4Address(address) {
161
+ const parts = address.split(".").map((part) => {
162
+ if (!/^\d+$/.test(part)) return NaN;
163
+ return parseInt(part, 10);
164
+ });
165
+ if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) return null;
166
+ return parts;
167
+ }
168
+ function parseIPv6Address(address) {
169
+ const parts = expandIPv6Address(address).split(":");
170
+ if (parts.length !== 8) return null;
171
+ const words = parts.map((part) => {
172
+ if (!/^[0-9a-f]{1,4}$/i.test(part)) return NaN;
173
+ return parseInt(part, 16);
174
+ });
175
+ if (words.some((word) => !Number.isInteger(word) || word < 0 || word > 65535)) return null;
176
+ return words;
177
+ }
178
+ function ipv4PartsToNumber(parts) {
179
+ return parts[0] * 2 ** 24 + parts[1] * 2 ** 16 + parts[2] * 2 ** 8 + parts[3];
180
+ }
181
+ function ipv4FromWords(highWord, lowWord) {
182
+ return [
183
+ highWord >> 8,
184
+ highWord & 255,
185
+ lowWord >> 8,
186
+ lowWord & 255
187
+ ].join(".");
188
+ }
189
+ function matchesIPv4Prefix(address, prefixBase, prefixLength) {
190
+ const blockSize = 2 ** (32 - prefixLength);
191
+ return Math.floor(address / blockSize) === Math.floor(prefixBase / blockSize);
192
+ }
193
+ function matchesIPv6Prefix(address, prefixWords, prefixLength) {
194
+ let remaining = prefixLength;
195
+ for (let i = 0; i < 8 && remaining > 0; i++) if (remaining >= 16) {
196
+ if (address[i] !== prefixWords[i]) return false;
197
+ remaining -= 16;
198
+ } else {
199
+ const mask = 65535 << 16 - remaining & 65535;
200
+ if ((address[i] & mask) !== (prefixWords[i] & mask)) return false;
201
+ remaining = 0;
202
+ }
203
+ return true;
204
+ }
205
+ //#endregion
33
206
  //#region ../../node_modules/.pnpm/@opentelemetry+api@1.9.1/node_modules/@opentelemetry/api/build/esm/version.js
34
207
  const VERSION = "1.9.1";
35
208
  //#endregion
@@ -25875,6 +26048,28 @@ const preloadedContexts = {
25875
26048
  "@type": "@id"
25876
26049
  }
25877
26050
  } },
26051
+ "https://w3id.org/fep/7aa9": { "@context": {
26052
+ "FeaturedCollection": "https://w3id.org/fep/7aa9#FeaturedCollection",
26053
+ "FeaturedItem": "https://w3id.org/fep/7aa9#FeaturedItem",
26054
+ "FeatureRequest": "https://w3id.org/fep/7aa9#FeatureRequest",
26055
+ "FeatureAuthorization": "https://w3id.org/fep/7aa9#FeatureAuthorization",
26056
+ "topic": {
26057
+ "@id": "https://w3id.org/fep/7aa9#topic",
26058
+ "@type": "@id"
26059
+ },
26060
+ "featuredObject": {
26061
+ "@id": "https://w3id.org/fep/7aa9#featuredObject",
26062
+ "@type": "@id"
26063
+ },
26064
+ "canFeature": {
26065
+ "@id": "https://w3id.org/fep/7aa9#canFeature",
26066
+ "@type": "@id"
26067
+ },
26068
+ "featureAuthorization": {
26069
+ "@id": "https://w3id.org/fep/7aa9#featureAuthorization",
26070
+ "@type": "@id"
26071
+ }
26072
+ } },
25878
26073
  "https://join-lemmy.org/context.json": { "@context": ["https://w3id.org/security/v1", {
25879
26074
  "as": "https://www.w3.org/ns/activitystreams#",
25880
26075
  "lemmy": "https://join-lemmy.org/ns#",
@@ -25931,7 +26126,7 @@ const preloadedContexts = {
25931
26126
  } }
25932
26127
  };
25933
26128
  var name = "@fedify/vocab-runtime";
25934
- var version = "2.4.0-dev.1570+f1b6aaa3";
26129
+ var version = "2.4.0-dev.1573+246bd0b2";
25935
26130
  const parametersNeedLowerCase = ["rel", "type"];
25936
26131
  const regexpLinkWhitespace = /[\n\r\s\t]/;
25937
26132
  function validateURI(uri) {
@@ -26179,183 +26374,72 @@ function logRequest(logger, request) {
26179
26374
  headers: Object.fromEntries(request.headers.entries())
26180
26375
  });
26181
26376
  }
26182
- var UrlError = class extends Error {
26183
- constructor(message) {
26184
- super(message);
26185
- this.name = "UrlError";
26186
- }
26187
- };
26188
- /**
26189
- * Validates a URL to prevent SSRF attacks.
26190
- */
26191
- async function validatePublicUrl(url) {
26192
- const parsed = new URL(url);
26193
- if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new UrlError(`Unsupported protocol: ${parsed.protocol}`);
26194
- let hostname = parsed.hostname;
26195
- if (hostname.startsWith("[") && hostname.endsWith("]")) hostname = hostname.slice(1, -1);
26196
- if (hostname === "localhost") throw new UrlError("Localhost is not allowed");
26197
- const hostnameFamily = isIP(hostname);
26198
- if (hostnameFamily !== 0) {
26199
- validatePublicIpAddress(hostname, hostnameFamily);
26200
- return;
26201
- }
26202
- if ("Deno" in globalThis && !isIP(hostname)) {
26203
- if ((await Deno.permissions.query({ name: "net" })).state !== "granted") return;
26204
- }
26205
- if ("Bun" in globalThis) {
26206
- if (hostname === "example.com" || hostname.endsWith(".example.com")) return;
26207
- else if (hostname === "fedify-test.internal") throw new UrlError("Invalid or private address: fedify-test.internal");
26208
- }
26209
- let addresses;
26210
- try {
26211
- addresses = await lookup(hostname, { all: true });
26212
- } catch {
26213
- addresses = [];
26214
- }
26215
- for (const { address, family } of addresses) validatePublicIpAddress(address, family);
26216
- }
26217
- function validatePublicIpAddress(address, family) {
26218
- if (family === 4 && isValidPublicIPv4Address(address) || family === 6 && isValidPublicIPv6Address(address)) return;
26219
- throw new UrlError(`Invalid or private address: ${address}`);
26220
- }
26221
- function isValidPublicIPv4Address(address) {
26222
- const parts = parseIPv4Address(address);
26223
- if (parts == null) return false;
26224
- const value = ipv4PartsToNumber(parts);
26225
- return !nonPublicIPv4Prefixes.some(({ base, prefix }) => matchesIPv4Prefix(value, base, prefix));
26226
- }
26227
- function isValidPublicIPv6Address(address) {
26228
- const words = parseIPv6Address(address);
26229
- if (words == null) return false;
26230
- if (nonPublicIPv6Prefixes.some(({ words: prefixWords, prefix }) => matchesIPv6Prefix(words, prefixWords, prefix))) return false;
26231
- for (const { extractIPv4, prefix, words: prefixWords } of ipv6WithIPv4Prefixes) {
26232
- if (!matchesIPv6Prefix(words, prefixWords, prefix)) continue;
26233
- const ipv4Address = extractIPv4(words);
26234
- if (ipv4Address != null && !isValidPublicIPv4Address(ipv4Address)) return false;
26235
- }
26236
- return true;
26237
- }
26238
- function expandIPv6Address(address) {
26239
- address = address.toLowerCase();
26240
- const ipv4Delimiter = address.lastIndexOf(":");
26241
- if (address.includes(".") && ipv4Delimiter >= 0) {
26242
- const ipv4Parts = parseIPv4Address(address.substring(ipv4Delimiter + 1));
26243
- if (ipv4Parts == null) return address;
26244
- const high = (ipv4Parts[0] << 8) + ipv4Parts[1];
26245
- const low = (ipv4Parts[2] << 8) + ipv4Parts[3];
26246
- address = address.substring(0, ipv4Delimiter + 1) + high.toString(16) + ":" + low.toString(16);
26247
- }
26248
- if (address === "::") return "0000:0000:0000:0000:0000:0000:0000:0000";
26249
- if (address.startsWith("::")) address = "0000" + address;
26250
- if (address.endsWith("::")) address = address + "0000";
26251
- address = address.replace("::", ":0000".repeat(8 - (address.match(/:/g) || []).length) + ":");
26252
- return address.split(":").map((part) => part.padStart(4, "0")).join(":");
26253
- }
26254
- const nonPublicIPv4Prefixes = [
26255
- ipv4Prefix("0.0.0.0/8", "RFC 6890"),
26256
- ipv4Prefix("10.0.0.0/8", "RFC 1918"),
26257
- ipv4Prefix("100.64.0.0/10", "RFC 6598"),
26258
- ipv4Prefix("127.0.0.0/8", "RFC 1122"),
26259
- ipv4Prefix("169.254.0.0/16", "RFC 3927"),
26260
- ipv4Prefix("172.16.0.0/12", "RFC 1918"),
26261
- ipv4Prefix("192.0.0.0/24", "RFC 6890"),
26262
- ipv4Prefix("192.0.2.0/24", "RFC 5737"),
26263
- ipv4Prefix("192.88.99.0/24", "RFC 7526"),
26264
- ipv4Prefix("192.168.0.0/16", "RFC 1918"),
26265
- ipv4Prefix("198.18.0.0/15", "RFC 2544"),
26266
- ipv4Prefix("198.51.100.0/24", "RFC 5737"),
26267
- ipv4Prefix("203.0.113.0/24", "RFC 5737"),
26268
- ipv4Prefix("224.0.0.0/4", "RFC 5771"),
26269
- ipv4Prefix("240.0.0.0/4", "RFC 1112")
26270
- ];
26271
- const nonPublicIPv6Prefixes = [
26272
- ipv6Prefix("::/16", "RFC 4291"),
26273
- ipv6Prefix("2001::/32", "RFC 4380"),
26274
- ipv6Prefix("2002::/16", "RFC 3056"),
26275
- ipv6Prefix("64:ff9b:1::/48", "RFC 8215"),
26276
- ipv6Prefix("fc00::/7", "RFC 4193"),
26277
- ipv6Prefix("fe80::/10", "RFC 4291"),
26278
- ipv6Prefix("ff00::/8", "RFC 4291")
26279
- ];
26280
- const ipv6WithIPv4Prefixes = [{
26281
- ...ipv6Prefix("64:ff9b::/96", "RFC 6052"),
26282
- extractIPv4: (words) => ipv4FromWords(words[6], words[7])
26283
- }];
26284
- function ipv4Prefix(cidr, rfc) {
26285
- const [address, prefixText] = cidr.split("/");
26286
- const prefix = parseInt(prefixText, 10);
26287
- const parts = parseIPv4Address(address);
26288
- if (parts == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 32) throw new Error(`Invalid IPv4 prefix: ${cidr}`);
26289
- return {
26290
- cidr,
26291
- base: ipv4PartsToNumber(parts),
26292
- prefix,
26293
- rfc
26294
- };
26295
- }
26296
- function ipv6Prefix(cidr, rfc) {
26297
- const [address, prefixText] = cidr.split("/");
26298
- const prefix = parseInt(prefixText, 10);
26299
- const words = parseIPv6Address(address);
26300
- if (words == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 128) throw new Error(`Invalid IPv6 prefix: ${cidr}`);
26301
- return {
26302
- cidr,
26303
- words,
26304
- prefix,
26305
- rfc
26306
- };
26307
- }
26308
- function parseIPv4Address(address) {
26309
- const parts = address.split(".").map((part) => {
26310
- if (!/^\d+$/.test(part)) return NaN;
26311
- return parseInt(part, 10);
26312
- });
26313
- if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) return null;
26314
- return parts;
26315
- }
26316
- function parseIPv6Address(address) {
26317
- const parts = expandIPv6Address(address).split(":");
26318
- if (parts.length !== 8) return null;
26319
- const words = parts.map((part) => {
26320
- if (!/^[0-9a-f]{1,4}$/i.test(part)) return NaN;
26321
- return parseInt(part, 16);
26322
- });
26323
- if (words.some((word) => !Number.isInteger(word) || word < 0 || word > 65535)) return null;
26324
- return words;
26325
- }
26326
- function ipv4PartsToNumber(parts) {
26327
- return parts[0] * 2 ** 24 + parts[1] * 2 ** 16 + parts[2] * 2 ** 8 + parts[3];
26328
- }
26329
- function ipv4FromWords(highWord, lowWord) {
26330
- return [
26331
- highWord >> 8,
26332
- highWord & 255,
26333
- lowWord >> 8,
26334
- lowWord & 255
26335
- ].join(".");
26336
- }
26337
- function matchesIPv4Prefix(address, prefixBase, prefixLength) {
26338
- const blockSize = 2 ** (32 - prefixLength);
26339
- return Math.floor(address / blockSize) === Math.floor(prefixBase / blockSize);
26340
- }
26341
- function matchesIPv6Prefix(address, prefixWords, prefixLength) {
26342
- let remaining = prefixLength;
26343
- for (let i = 0; i < 8 && remaining > 0; i++) if (remaining >= 16) {
26344
- if (address[i] !== prefixWords[i]) return false;
26345
- remaining -= 16;
26346
- } else {
26347
- const mask = 65535 << 16 - remaining & 65535;
26348
- if ((address[i] & mask) !== (prefixWords[i] & mask)) return false;
26349
- remaining = 0;
26350
- }
26351
- return true;
26352
- }
26353
26377
  const logger = getLogger([
26354
26378
  "fedify",
26355
26379
  "runtime",
26356
26380
  "docloader"
26357
26381
  ]);
26358
26382
  const DEFAULT_MAX_REDIRECTION = 20;
26383
+ const MAX_HTML_SIZE = 1024 * 1024;
26384
+ function createResponseMetadata(response) {
26385
+ return new Response(null, {
26386
+ headers: response.headers,
26387
+ status: response.status,
26388
+ statusText: response.statusText
26389
+ });
26390
+ }
26391
+ async function cancelResponseBody(response) {
26392
+ if (response.body != null) await response.body.cancel();
26393
+ }
26394
+ async function readBoundedText(response, maxBytes) {
26395
+ const contentLength = response.headers.get("Content-Length");
26396
+ if (contentLength != null) {
26397
+ const size = Number(contentLength);
26398
+ if (size > maxBytes) {
26399
+ await cancelResponseBody(response);
26400
+ return {
26401
+ text: "",
26402
+ size,
26403
+ tooLarge: true
26404
+ };
26405
+ }
26406
+ }
26407
+ if (response.body == null) return {
26408
+ text: "",
26409
+ size: 0,
26410
+ tooLarge: false
26411
+ };
26412
+ const reader = response.body.getReader();
26413
+ const decoder = new TextDecoder();
26414
+ let text = "";
26415
+ let size = 0;
26416
+ try {
26417
+ while (true) {
26418
+ const result = await reader.read();
26419
+ if (result.done) break;
26420
+ const chunkSize = result.value.byteLength;
26421
+ if (size + chunkSize > maxBytes) {
26422
+ size += chunkSize;
26423
+ await reader.cancel();
26424
+ return {
26425
+ text: "",
26426
+ size,
26427
+ tooLarge: true
26428
+ };
26429
+ }
26430
+ size += chunkSize;
26431
+ text += decoder.decode(result.value, { stream: true });
26432
+ }
26433
+ text += decoder.decode();
26434
+ return {
26435
+ text,
26436
+ size,
26437
+ tooLarge: false
26438
+ };
26439
+ } finally {
26440
+ reader.releaseLock();
26441
+ }
26442
+ }
26359
26443
  /**
26360
26444
  * Gets a {@link RemoteDocument} from the given response.
26361
26445
  * @param url The URL of the document to load.
@@ -26410,19 +26494,19 @@ async function getRemoteDocument(url, response, fetch) {
26410
26494
  }
26411
26495
  let document;
26412
26496
  if (!jsonLd && (contentType === "text/html" || contentType?.startsWith("text/html;") || contentType === "application/xhtml+xml" || contentType?.startsWith("application/xhtml+xml;"))) {
26413
- const MAX_HTML_SIZE = 1024 * 1024;
26414
- const html = await response.text();
26415
- if (html.length > MAX_HTML_SIZE) {
26497
+ const errorResponse = createResponseMetadata(response);
26498
+ const html = await readBoundedText(response, MAX_HTML_SIZE);
26499
+ if (html.tooLarge) {
26416
26500
  logger.warn("HTML response too large, skipping alternate link discovery: {url}", {
26417
26501
  url: documentUrl,
26418
- size: html.length
26502
+ size: html.size
26419
26503
  });
26420
- document = JSON.parse(html);
26504
+ throw new FetchError(documentUrl, `HTML document is too large to scan for an ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
26421
26505
  } else {
26422
26506
  const tagPattern = /<(a|link)\s+([^>]*?)\s*\/?>/gi;
26423
26507
  const attrPattern = /([a-z][a-z:_-]*)=(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
26424
26508
  let tagMatch;
26425
- while ((tagMatch = tagPattern.exec(html)) !== null) {
26509
+ while ((tagMatch = tagPattern.exec(html.text)) !== null) {
26426
26510
  const tagContent = tagMatch[2];
26427
26511
  let attrMatch;
26428
26512
  const attribs = {};
@@ -26439,7 +26523,12 @@ async function getRemoteDocument(url, response, fetch) {
26439
26523
  return await fetch(new URL(attribs.href, docUrl).href);
26440
26524
  }
26441
26525
  }
26442
- document = JSON.parse(html);
26526
+ try {
26527
+ document = JSON.parse(html.text);
26528
+ } catch (error) {
26529
+ if (!(error instanceof SyntaxError)) throw error;
26530
+ throw new FetchError(documentUrl, `HTML document has no ActivityPub alternate link (Content-Type: ${contentType})`, errorResponse);
26531
+ }
26443
26532
  }
26444
26533
  } else document = await response.json();
26445
26534
  logger.debug("Fetched document: {status} {url} {headers}", {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/relay",
3
- "version": "2.4.0-dev.1570+f1b6aaa3",
3
+ "version": "2.4.0-dev.1573+246bd0b2",
4
4
  "description": "ActivityPub relay support for Fedify",
5
5
  "keywords": [
6
6
  "Fedify",
@@ -47,14 +47,14 @@
47
47
  "dependencies": {
48
48
  "@js-temporal/polyfill": "^0.5.1",
49
49
  "@logtape/logtape": "^2.2.0",
50
- "@fedify/fedify": "^2.4.0-dev.1570+f1b6aaa3",
51
- "@fedify/vocab": "2.4.0-dev.1570+f1b6aaa3"
50
+ "@fedify/fedify": "^2.4.0-dev.1573+246bd0b2",
51
+ "@fedify/vocab": "2.4.0-dev.1573+246bd0b2"
52
52
  },
53
53
  "devDependencies": {
54
54
  "tsdown": "^0.22.0",
55
55
  "typescript": "^6.0.0",
56
56
  "urlpattern-polyfill": "^10.1.0",
57
- "@fedify/vocab-runtime": "^2.4.0-dev.1570+f1b6aaa3"
57
+ "@fedify/vocab-runtime": "^2.4.0-dev.1573+246bd0b2"
58
58
  },
59
59
  "scripts": {
60
60
  "build:self": "tsdown",