@fedify/vocab-runtime 2.4.0-dev.1564 → 2.4.0-dev.1570
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.
- package/deno.json +1 -2
- package/dist/mod.cjs +190 -107
- package/dist/mod.d.cts +200 -18
- package/dist/mod.d.ts +200 -18
- package/dist/mod.js +184 -97
- package/dist/tests/decimal.test.cjs +2 -2
- package/dist/tests/decimal.test.mjs +2 -2
- package/dist/tests/{docloader-CYQvKbtL.mjs → docloader-C76ldE5C.mjs} +10 -97
- package/dist/tests/{docloader-UAdXnDwt.cjs → docloader-mvgIWKI7.cjs} +9 -102
- package/dist/tests/docloader.test.cjs +6 -58
- package/dist/tests/docloader.test.mjs +6 -58
- package/dist/tests/{request-DgAlI7RF.mjs → request-BEXkv1ul.mjs} +1 -1
- package/dist/tests/{request-DnzhAfki.cjs → request-SworbvLc.cjs} +1 -1
- package/dist/tests/request.test.cjs +1 -1
- package/dist/tests/request.test.mjs +1 -1
- package/dist/tests/{url-2XwVbUS_.cjs → url-C20FhC7p.cjs} +0 -108
- package/dist/tests/{url-YWJbnRlf.mjs → url-m9Qzxy-Y.mjs} +1 -85
- package/dist/tests/url.test.cjs +1 -104
- package/dist/tests/url.test.mjs +2 -105
- package/package.json +1 -11
- package/src/contexts.ts +0 -2
- package/src/docloader.test.ts +6 -102
- package/src/docloader.ts +8 -76
- package/src/mod.ts +0 -4
- package/src/url.test.ts +1 -252
- package/src/url.ts +0 -141
- package/tsdown.config.ts +1 -6
- package/dist/docloader-DnUMWHaJ.d.cts +0 -202
- package/dist/docloader-xRGn1azD.d.ts +0 -202
- package/dist/internal/jsonld-cache.cjs +0 -279
- package/dist/internal/jsonld-cache.d.cts +0 -48
- package/dist/internal/jsonld-cache.d.ts +0 -48
- package/dist/internal/jsonld-cache.js +0 -275
- package/dist/tests/jsonld-cache.test.cjs +0 -652
- package/dist/tests/jsonld-cache.test.d.cts +0 -1
- package/dist/tests/jsonld-cache.test.d.mts +0 -1
- package/dist/tests/jsonld-cache.test.mjs +0 -651
- package/dist/url-BAdyyqAa.cjs +0 -315
- package/dist/url-BuxPHxK2.js +0 -261
- package/src/contexts/fep-7aa9.json +0 -24
- package/src/internal/jsonld-cache.ts +0 -538
- package/src/jsonld-cache.test.ts +0 -554
package/dist/url-BAdyyqAa.cjs
DELETED
|
@@ -1,315 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
require("./chunk-M78iaK0I.cjs");
|
|
3
|
-
let node_dns_promises = require("node:dns/promises");
|
|
4
|
-
let node_net = require("node:net");
|
|
5
|
-
//#region src/url.ts
|
|
6
|
-
var UrlError = class extends Error {
|
|
7
|
-
constructor(message) {
|
|
8
|
-
super(message);
|
|
9
|
-
this.name = "UrlError";
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
const PORTABLE_IRI_PATTERN = /^(ap|ap\+ef61):\/\/([^/?#]*)([^?#]*)(\?[^#]*)?(#.*)?$/i;
|
|
13
|
-
const INVALID_PERCENT_ENCODING_PATTERN = /%(?![0-9A-Fa-f]{2})/;
|
|
14
|
-
const DID_SCHEME_PATTERN = /^did:/i;
|
|
15
|
-
const DID_PATTERN = /^did:[a-z0-9]+:[-A-Za-z0-9._%]+(?::[-A-Za-z0-9._%]+)*$/i;
|
|
16
|
-
/**
|
|
17
|
-
* Parses a JSON-LD `@id` value as an IRI.
|
|
18
|
-
*/
|
|
19
|
-
function parseJsonLdId(id, base) {
|
|
20
|
-
if (id == null || id.startsWith("_:")) return void 0;
|
|
21
|
-
try {
|
|
22
|
-
return parseIri(id, base);
|
|
23
|
-
} catch {
|
|
24
|
-
throw new TypeError("Invalid @id: " + id);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Parses an IRI as a URL, including FEP-ef61 portable ActivityPub IRIs.
|
|
29
|
-
*/
|
|
30
|
-
function parseIri(iri, base) {
|
|
31
|
-
if (iri instanceof URL) return normalizePortableUrl(iri) ?? new URL(iri.href);
|
|
32
|
-
const portable = parsePortableIri(iri);
|
|
33
|
-
if (portable != null) return portable;
|
|
34
|
-
base = normalizeBaseIri(base);
|
|
35
|
-
if (!URL.canParse(iri, base) && iri.startsWith("at://")) return parseAtUri(iri);
|
|
36
|
-
const parsed = new URL(iri, base);
|
|
37
|
-
return normalizePortableUrl(parsed) ?? parsed;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Formats a URL as an IRI, including FEP-ef61 portable ActivityPub IRIs.
|
|
41
|
-
*/
|
|
42
|
-
function formatIri(iri) {
|
|
43
|
-
const parsed = parsePortableIri(iri instanceof URL ? iri.href : iri);
|
|
44
|
-
if (parsed == null) return iri instanceof URL ? iri.href : URL.canParse(iri) ? new URL(iri).href : iri;
|
|
45
|
-
return `ap+ef61://${decodePortableAuthority(parsed.host)}${parsed.pathname}${parsed.search}${parsed.hash}`;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Checks whether two IRIs have the same origin.
|
|
49
|
-
*/
|
|
50
|
-
function haveSameIriOrigin(left, right) {
|
|
51
|
-
return getComparableIriOrigin(left) === getComparableIriOrigin(right);
|
|
52
|
-
}
|
|
53
|
-
function getComparableIriOrigin(iri) {
|
|
54
|
-
iri = normalizePortableUrl(iri) ?? iri;
|
|
55
|
-
if (iri.origin !== "null") return iri.origin;
|
|
56
|
-
if (iri.host !== "") {
|
|
57
|
-
const host = iri.protocol === "ap+ef61:" ? encodeURIComponent(decodePortableAuthority(iri.host).replace(DID_SCHEME_PATTERN, "did:")) : iri.host;
|
|
58
|
-
return `${iri.protocol}//${host}`;
|
|
59
|
-
}
|
|
60
|
-
return iri.href;
|
|
61
|
-
}
|
|
62
|
-
function parsePortableIri(iri) {
|
|
63
|
-
const match = iri.match(PORTABLE_IRI_PATTERN);
|
|
64
|
-
if (match == null) return null;
|
|
65
|
-
const authority = decodePortableAuthority(match[2]);
|
|
66
|
-
if (!DID_PATTERN.test(authority)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
|
|
67
|
-
if (match[3] === "") throw new TypeError("Invalid portable ActivityPub IRI path.");
|
|
68
|
-
return new URL(`ap+ef61://${encodeURIComponent(authority)}${match[3]}${match[4] ?? ""}${match[5] ?? ""}`);
|
|
69
|
-
}
|
|
70
|
-
function normalizePortableUrl(iri) {
|
|
71
|
-
if (iri.protocol !== "ap:" && iri.protocol !== "ap+ef61:") return null;
|
|
72
|
-
return parsePortableIri(`ap+ef61://${iri.host}${iri.pathname}${iri.search}${iri.hash}`);
|
|
73
|
-
}
|
|
74
|
-
function normalizeBaseIri(base) {
|
|
75
|
-
if (base == null) return void 0;
|
|
76
|
-
if (base instanceof URL) return normalizePortableUrl(base) ?? base;
|
|
77
|
-
return parsePortableIri(base) ?? (base.startsWith("at://") && !URL.canParse(".", base) ? parseAtUri(base) : base);
|
|
78
|
-
}
|
|
79
|
-
function decodePortableAuthority(authority) {
|
|
80
|
-
if (INVALID_PERCENT_ENCODING_PATTERN.test(authority)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
|
|
81
|
-
if (DID_SCHEME_PATTERN.test(authority)) {
|
|
82
|
-
const decoded = authority.replace(/%25/gi, "%");
|
|
83
|
-
if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
|
|
84
|
-
return decoded;
|
|
85
|
-
}
|
|
86
|
-
const decoded = authority.replace(/%(25|3A)/gi, (match) => match.toLowerCase() === "%3a" ? ":" : "%");
|
|
87
|
-
if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
|
|
88
|
-
return decoded;
|
|
89
|
-
}
|
|
90
|
-
function parseAtUri(uri) {
|
|
91
|
-
const index = uri.indexOf("/", 5);
|
|
92
|
-
const authority = index >= 0 ? uri.slice(5, index) : uri.slice(5);
|
|
93
|
-
const path = index >= 0 ? uri.slice(index) : "";
|
|
94
|
-
return new URL("at://" + encodeURIComponent(authority) + path);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Validates a URL to prevent SSRF attacks.
|
|
98
|
-
*/
|
|
99
|
-
async function validatePublicUrl(url) {
|
|
100
|
-
const parsed = new URL(url);
|
|
101
|
-
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new UrlError(`Unsupported protocol: ${parsed.protocol}`);
|
|
102
|
-
let hostname = parsed.hostname;
|
|
103
|
-
if (hostname.startsWith("[") && hostname.endsWith("]")) hostname = hostname.slice(1, -1);
|
|
104
|
-
if (hostname === "localhost") throw new UrlError("Localhost is not allowed");
|
|
105
|
-
const hostnameFamily = (0, node_net.isIP)(hostname);
|
|
106
|
-
if (hostnameFamily !== 0) {
|
|
107
|
-
validatePublicIpAddress(hostname, hostnameFamily);
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
if ("Deno" in globalThis && !(0, node_net.isIP)(hostname)) {
|
|
111
|
-
if ((await Deno.permissions.query({ name: "net" })).state !== "granted") return;
|
|
112
|
-
}
|
|
113
|
-
if ("Bun" in globalThis) {
|
|
114
|
-
if (hostname === "example.com" || hostname.endsWith(".example.com")) return;
|
|
115
|
-
else if (hostname === "fedify-test.internal") throw new UrlError("Invalid or private address: fedify-test.internal");
|
|
116
|
-
}
|
|
117
|
-
let addresses;
|
|
118
|
-
try {
|
|
119
|
-
addresses = await (0, node_dns_promises.lookup)(hostname, { all: true });
|
|
120
|
-
} catch {
|
|
121
|
-
addresses = [];
|
|
122
|
-
}
|
|
123
|
-
for (const { address, family } of addresses) validatePublicIpAddress(address, family);
|
|
124
|
-
}
|
|
125
|
-
function validatePublicIpAddress(address, family) {
|
|
126
|
-
if (family === 4 && isValidPublicIPv4Address(address) || family === 6 && isValidPublicIPv6Address(address)) return;
|
|
127
|
-
throw new UrlError(`Invalid or private address: ${address}`);
|
|
128
|
-
}
|
|
129
|
-
function isValidPublicIPv4Address(address) {
|
|
130
|
-
const parts = parseIPv4Address(address);
|
|
131
|
-
if (parts == null) return false;
|
|
132
|
-
const value = ipv4PartsToNumber(parts);
|
|
133
|
-
return !nonPublicIPv4Prefixes.some(({ base, prefix }) => matchesIPv4Prefix(value, base, prefix));
|
|
134
|
-
}
|
|
135
|
-
function isValidPublicIPv6Address(address) {
|
|
136
|
-
const words = parseIPv6Address(address);
|
|
137
|
-
if (words == null) return false;
|
|
138
|
-
if (nonPublicIPv6Prefixes.some(({ words: prefixWords, prefix }) => matchesIPv6Prefix(words, prefixWords, prefix))) return false;
|
|
139
|
-
for (const { extractIPv4, prefix, words: prefixWords } of ipv6WithIPv4Prefixes) {
|
|
140
|
-
if (!matchesIPv6Prefix(words, prefixWords, prefix)) continue;
|
|
141
|
-
const ipv4Address = extractIPv4(words);
|
|
142
|
-
if (ipv4Address != null && !isValidPublicIPv4Address(ipv4Address)) return false;
|
|
143
|
-
}
|
|
144
|
-
return true;
|
|
145
|
-
}
|
|
146
|
-
function expandIPv6Address(address) {
|
|
147
|
-
address = address.toLowerCase();
|
|
148
|
-
const ipv4Delimiter = address.lastIndexOf(":");
|
|
149
|
-
if (address.includes(".") && ipv4Delimiter >= 0) {
|
|
150
|
-
const ipv4Parts = parseIPv4Address(address.substring(ipv4Delimiter + 1));
|
|
151
|
-
if (ipv4Parts == null) return address;
|
|
152
|
-
const high = (ipv4Parts[0] << 8) + ipv4Parts[1];
|
|
153
|
-
const low = (ipv4Parts[2] << 8) + ipv4Parts[3];
|
|
154
|
-
address = address.substring(0, ipv4Delimiter + 1) + high.toString(16) + ":" + low.toString(16);
|
|
155
|
-
}
|
|
156
|
-
if (address === "::") return "0000:0000:0000:0000:0000:0000:0000:0000";
|
|
157
|
-
if (address.startsWith("::")) address = "0000" + address;
|
|
158
|
-
if (address.endsWith("::")) address = address + "0000";
|
|
159
|
-
address = address.replace("::", ":0000".repeat(8 - (address.match(/:/g) || []).length) + ":");
|
|
160
|
-
return address.split(":").map((part) => part.padStart(4, "0")).join(":");
|
|
161
|
-
}
|
|
162
|
-
const nonPublicIPv4Prefixes = [
|
|
163
|
-
ipv4Prefix("0.0.0.0/8", "RFC 6890"),
|
|
164
|
-
ipv4Prefix("10.0.0.0/8", "RFC 1918"),
|
|
165
|
-
ipv4Prefix("100.64.0.0/10", "RFC 6598"),
|
|
166
|
-
ipv4Prefix("127.0.0.0/8", "RFC 1122"),
|
|
167
|
-
ipv4Prefix("169.254.0.0/16", "RFC 3927"),
|
|
168
|
-
ipv4Prefix("172.16.0.0/12", "RFC 1918"),
|
|
169
|
-
ipv4Prefix("192.0.0.0/24", "RFC 6890"),
|
|
170
|
-
ipv4Prefix("192.0.2.0/24", "RFC 5737"),
|
|
171
|
-
ipv4Prefix("192.88.99.0/24", "RFC 7526"),
|
|
172
|
-
ipv4Prefix("192.168.0.0/16", "RFC 1918"),
|
|
173
|
-
ipv4Prefix("198.18.0.0/15", "RFC 2544"),
|
|
174
|
-
ipv4Prefix("198.51.100.0/24", "RFC 5737"),
|
|
175
|
-
ipv4Prefix("203.0.113.0/24", "RFC 5737"),
|
|
176
|
-
ipv4Prefix("224.0.0.0/4", "RFC 5771"),
|
|
177
|
-
ipv4Prefix("240.0.0.0/4", "RFC 1112")
|
|
178
|
-
];
|
|
179
|
-
const nonPublicIPv6Prefixes = [
|
|
180
|
-
ipv6Prefix("::/16", "RFC 4291"),
|
|
181
|
-
ipv6Prefix("2001::/32", "RFC 4380"),
|
|
182
|
-
ipv6Prefix("2002::/16", "RFC 3056"),
|
|
183
|
-
ipv6Prefix("64:ff9b:1::/48", "RFC 8215"),
|
|
184
|
-
ipv6Prefix("fc00::/7", "RFC 4193"),
|
|
185
|
-
ipv6Prefix("fe80::/10", "RFC 4291"),
|
|
186
|
-
ipv6Prefix("ff00::/8", "RFC 4291")
|
|
187
|
-
];
|
|
188
|
-
const ipv6WithIPv4Prefixes = [{
|
|
189
|
-
...ipv6Prefix("64:ff9b::/96", "RFC 6052"),
|
|
190
|
-
extractIPv4: (words) => ipv4FromWords(words[6], words[7])
|
|
191
|
-
}];
|
|
192
|
-
function ipv4Prefix(cidr, rfc) {
|
|
193
|
-
const [address, prefixText] = cidr.split("/");
|
|
194
|
-
const prefix = parseInt(prefixText, 10);
|
|
195
|
-
const parts = parseIPv4Address(address);
|
|
196
|
-
if (parts == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 32) throw new Error(`Invalid IPv4 prefix: ${cidr}`);
|
|
197
|
-
return {
|
|
198
|
-
cidr,
|
|
199
|
-
base: ipv4PartsToNumber(parts),
|
|
200
|
-
prefix,
|
|
201
|
-
rfc
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
function ipv6Prefix(cidr, rfc) {
|
|
205
|
-
const [address, prefixText] = cidr.split("/");
|
|
206
|
-
const prefix = parseInt(prefixText, 10);
|
|
207
|
-
const words = parseIPv6Address(address);
|
|
208
|
-
if (words == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 128) throw new Error(`Invalid IPv6 prefix: ${cidr}`);
|
|
209
|
-
return {
|
|
210
|
-
cidr,
|
|
211
|
-
words,
|
|
212
|
-
prefix,
|
|
213
|
-
rfc
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
function parseIPv4Address(address) {
|
|
217
|
-
const parts = address.split(".").map((part) => {
|
|
218
|
-
if (!/^\d+$/.test(part)) return NaN;
|
|
219
|
-
return parseInt(part, 10);
|
|
220
|
-
});
|
|
221
|
-
if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) return null;
|
|
222
|
-
return parts;
|
|
223
|
-
}
|
|
224
|
-
function parseIPv6Address(address) {
|
|
225
|
-
const parts = expandIPv6Address(address).split(":");
|
|
226
|
-
if (parts.length !== 8) return null;
|
|
227
|
-
const words = parts.map((part) => {
|
|
228
|
-
if (!/^[0-9a-f]{1,4}$/i.test(part)) return NaN;
|
|
229
|
-
return parseInt(part, 16);
|
|
230
|
-
});
|
|
231
|
-
if (words.some((word) => !Number.isInteger(word) || word < 0 || word > 65535)) return null;
|
|
232
|
-
return words;
|
|
233
|
-
}
|
|
234
|
-
function ipv4PartsToNumber(parts) {
|
|
235
|
-
return parts[0] * 2 ** 24 + parts[1] * 2 ** 16 + parts[2] * 2 ** 8 + parts[3];
|
|
236
|
-
}
|
|
237
|
-
function ipv4FromWords(highWord, lowWord) {
|
|
238
|
-
return [
|
|
239
|
-
highWord >> 8,
|
|
240
|
-
highWord & 255,
|
|
241
|
-
lowWord >> 8,
|
|
242
|
-
lowWord & 255
|
|
243
|
-
].join(".");
|
|
244
|
-
}
|
|
245
|
-
function matchesIPv4Prefix(address, prefixBase, prefixLength) {
|
|
246
|
-
const blockSize = 2 ** (32 - prefixLength);
|
|
247
|
-
return Math.floor(address / blockSize) === Math.floor(prefixBase / blockSize);
|
|
248
|
-
}
|
|
249
|
-
function matchesIPv6Prefix(address, prefixWords, prefixLength) {
|
|
250
|
-
let remaining = prefixLength;
|
|
251
|
-
for (let i = 0; i < 8 && remaining > 0; i++) if (remaining >= 16) {
|
|
252
|
-
if (address[i] !== prefixWords[i]) return false;
|
|
253
|
-
remaining -= 16;
|
|
254
|
-
} else {
|
|
255
|
-
const mask = 65535 << 16 - remaining & 65535;
|
|
256
|
-
if ((address[i] & mask) !== (prefixWords[i] & mask)) return false;
|
|
257
|
-
remaining = 0;
|
|
258
|
-
}
|
|
259
|
-
return true;
|
|
260
|
-
}
|
|
261
|
-
//#endregion
|
|
262
|
-
Object.defineProperty(exports, "UrlError", {
|
|
263
|
-
enumerable: true,
|
|
264
|
-
get: function() {
|
|
265
|
-
return UrlError;
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
Object.defineProperty(exports, "expandIPv6Address", {
|
|
269
|
-
enumerable: true,
|
|
270
|
-
get: function() {
|
|
271
|
-
return expandIPv6Address;
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
Object.defineProperty(exports, "formatIri", {
|
|
275
|
-
enumerable: true,
|
|
276
|
-
get: function() {
|
|
277
|
-
return formatIri;
|
|
278
|
-
}
|
|
279
|
-
});
|
|
280
|
-
Object.defineProperty(exports, "haveSameIriOrigin", {
|
|
281
|
-
enumerable: true,
|
|
282
|
-
get: function() {
|
|
283
|
-
return haveSameIriOrigin;
|
|
284
|
-
}
|
|
285
|
-
});
|
|
286
|
-
Object.defineProperty(exports, "isValidPublicIPv4Address", {
|
|
287
|
-
enumerable: true,
|
|
288
|
-
get: function() {
|
|
289
|
-
return isValidPublicIPv4Address;
|
|
290
|
-
}
|
|
291
|
-
});
|
|
292
|
-
Object.defineProperty(exports, "isValidPublicIPv6Address", {
|
|
293
|
-
enumerable: true,
|
|
294
|
-
get: function() {
|
|
295
|
-
return isValidPublicIPv6Address;
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
Object.defineProperty(exports, "parseIri", {
|
|
299
|
-
enumerable: true,
|
|
300
|
-
get: function() {
|
|
301
|
-
return parseIri;
|
|
302
|
-
}
|
|
303
|
-
});
|
|
304
|
-
Object.defineProperty(exports, "parseJsonLdId", {
|
|
305
|
-
enumerable: true,
|
|
306
|
-
get: function() {
|
|
307
|
-
return parseJsonLdId;
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
Object.defineProperty(exports, "validatePublicUrl", {
|
|
311
|
-
enumerable: true,
|
|
312
|
-
get: function() {
|
|
313
|
-
return validatePublicUrl;
|
|
314
|
-
}
|
|
315
|
-
});
|
package/dist/url-BuxPHxK2.js
DELETED
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import { lookup } from "node:dns/promises";
|
|
3
|
-
import { isIP } from "node:net";
|
|
4
|
-
//#region src/url.ts
|
|
5
|
-
var UrlError = class extends Error {
|
|
6
|
-
constructor(message) {
|
|
7
|
-
super(message);
|
|
8
|
-
this.name = "UrlError";
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
const PORTABLE_IRI_PATTERN = /^(ap|ap\+ef61):\/\/([^/?#]*)([^?#]*)(\?[^#]*)?(#.*)?$/i;
|
|
12
|
-
const INVALID_PERCENT_ENCODING_PATTERN = /%(?![0-9A-Fa-f]{2})/;
|
|
13
|
-
const DID_SCHEME_PATTERN = /^did:/i;
|
|
14
|
-
const DID_PATTERN = /^did:[a-z0-9]+:[-A-Za-z0-9._%]+(?::[-A-Za-z0-9._%]+)*$/i;
|
|
15
|
-
/**
|
|
16
|
-
* Parses a JSON-LD `@id` value as an IRI.
|
|
17
|
-
*/
|
|
18
|
-
function parseJsonLdId(id, base) {
|
|
19
|
-
if (id == null || id.startsWith("_:")) return void 0;
|
|
20
|
-
try {
|
|
21
|
-
return parseIri(id, base);
|
|
22
|
-
} catch {
|
|
23
|
-
throw new TypeError("Invalid @id: " + id);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Parses an IRI as a URL, including FEP-ef61 portable ActivityPub IRIs.
|
|
28
|
-
*/
|
|
29
|
-
function parseIri(iri, base) {
|
|
30
|
-
if (iri instanceof URL) return normalizePortableUrl(iri) ?? new URL(iri.href);
|
|
31
|
-
const portable = parsePortableIri(iri);
|
|
32
|
-
if (portable != null) return portable;
|
|
33
|
-
base = normalizeBaseIri(base);
|
|
34
|
-
if (!URL.canParse(iri, base) && iri.startsWith("at://")) return parseAtUri(iri);
|
|
35
|
-
const parsed = new URL(iri, base);
|
|
36
|
-
return normalizePortableUrl(parsed) ?? parsed;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Formats a URL as an IRI, including FEP-ef61 portable ActivityPub IRIs.
|
|
40
|
-
*/
|
|
41
|
-
function formatIri(iri) {
|
|
42
|
-
const parsed = parsePortableIri(iri instanceof URL ? iri.href : iri);
|
|
43
|
-
if (parsed == null) return iri instanceof URL ? iri.href : URL.canParse(iri) ? new URL(iri).href : iri;
|
|
44
|
-
return `ap+ef61://${decodePortableAuthority(parsed.host)}${parsed.pathname}${parsed.search}${parsed.hash}`;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Checks whether two IRIs have the same origin.
|
|
48
|
-
*/
|
|
49
|
-
function haveSameIriOrigin(left, right) {
|
|
50
|
-
return getComparableIriOrigin(left) === getComparableIriOrigin(right);
|
|
51
|
-
}
|
|
52
|
-
function getComparableIriOrigin(iri) {
|
|
53
|
-
iri = normalizePortableUrl(iri) ?? iri;
|
|
54
|
-
if (iri.origin !== "null") return iri.origin;
|
|
55
|
-
if (iri.host !== "") {
|
|
56
|
-
const host = iri.protocol === "ap+ef61:" ? encodeURIComponent(decodePortableAuthority(iri.host).replace(DID_SCHEME_PATTERN, "did:")) : iri.host;
|
|
57
|
-
return `${iri.protocol}//${host}`;
|
|
58
|
-
}
|
|
59
|
-
return iri.href;
|
|
60
|
-
}
|
|
61
|
-
function parsePortableIri(iri) {
|
|
62
|
-
const match = iri.match(PORTABLE_IRI_PATTERN);
|
|
63
|
-
if (match == null) return null;
|
|
64
|
-
const authority = decodePortableAuthority(match[2]);
|
|
65
|
-
if (!DID_PATTERN.test(authority)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
|
|
66
|
-
if (match[3] === "") throw new TypeError("Invalid portable ActivityPub IRI path.");
|
|
67
|
-
return new URL(`ap+ef61://${encodeURIComponent(authority)}${match[3]}${match[4] ?? ""}${match[5] ?? ""}`);
|
|
68
|
-
}
|
|
69
|
-
function normalizePortableUrl(iri) {
|
|
70
|
-
if (iri.protocol !== "ap:" && iri.protocol !== "ap+ef61:") return null;
|
|
71
|
-
return parsePortableIri(`ap+ef61://${iri.host}${iri.pathname}${iri.search}${iri.hash}`);
|
|
72
|
-
}
|
|
73
|
-
function normalizeBaseIri(base) {
|
|
74
|
-
if (base == null) return void 0;
|
|
75
|
-
if (base instanceof URL) return normalizePortableUrl(base) ?? base;
|
|
76
|
-
return parsePortableIri(base) ?? (base.startsWith("at://") && !URL.canParse(".", base) ? parseAtUri(base) : base);
|
|
77
|
-
}
|
|
78
|
-
function decodePortableAuthority(authority) {
|
|
79
|
-
if (INVALID_PERCENT_ENCODING_PATTERN.test(authority)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
|
|
80
|
-
if (DID_SCHEME_PATTERN.test(authority)) {
|
|
81
|
-
const decoded = authority.replace(/%25/gi, "%");
|
|
82
|
-
if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
|
|
83
|
-
return decoded;
|
|
84
|
-
}
|
|
85
|
-
const decoded = authority.replace(/%(25|3A)/gi, (match) => match.toLowerCase() === "%3a" ? ":" : "%");
|
|
86
|
-
if (INVALID_PERCENT_ENCODING_PATTERN.test(decoded)) throw new TypeError("Invalid portable ActivityPub IRI authority.");
|
|
87
|
-
return decoded;
|
|
88
|
-
}
|
|
89
|
-
function parseAtUri(uri) {
|
|
90
|
-
const index = uri.indexOf("/", 5);
|
|
91
|
-
const authority = index >= 0 ? uri.slice(5, index) : uri.slice(5);
|
|
92
|
-
const path = index >= 0 ? uri.slice(index) : "";
|
|
93
|
-
return new URL("at://" + encodeURIComponent(authority) + path);
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Validates a URL to prevent SSRF attacks.
|
|
97
|
-
*/
|
|
98
|
-
async function validatePublicUrl(url) {
|
|
99
|
-
const parsed = new URL(url);
|
|
100
|
-
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new UrlError(`Unsupported protocol: ${parsed.protocol}`);
|
|
101
|
-
let hostname = parsed.hostname;
|
|
102
|
-
if (hostname.startsWith("[") && hostname.endsWith("]")) hostname = hostname.slice(1, -1);
|
|
103
|
-
if (hostname === "localhost") throw new UrlError("Localhost is not allowed");
|
|
104
|
-
const hostnameFamily = isIP(hostname);
|
|
105
|
-
if (hostnameFamily !== 0) {
|
|
106
|
-
validatePublicIpAddress(hostname, hostnameFamily);
|
|
107
|
-
return;
|
|
108
|
-
}
|
|
109
|
-
if ("Deno" in globalThis && !isIP(hostname)) {
|
|
110
|
-
if ((await Deno.permissions.query({ name: "net" })).state !== "granted") return;
|
|
111
|
-
}
|
|
112
|
-
if ("Bun" in globalThis) {
|
|
113
|
-
if (hostname === "example.com" || hostname.endsWith(".example.com")) return;
|
|
114
|
-
else if (hostname === "fedify-test.internal") throw new UrlError("Invalid or private address: fedify-test.internal");
|
|
115
|
-
}
|
|
116
|
-
let addresses;
|
|
117
|
-
try {
|
|
118
|
-
addresses = await lookup(hostname, { all: true });
|
|
119
|
-
} catch {
|
|
120
|
-
addresses = [];
|
|
121
|
-
}
|
|
122
|
-
for (const { address, family } of addresses) validatePublicIpAddress(address, family);
|
|
123
|
-
}
|
|
124
|
-
function validatePublicIpAddress(address, family) {
|
|
125
|
-
if (family === 4 && isValidPublicIPv4Address(address) || family === 6 && isValidPublicIPv6Address(address)) return;
|
|
126
|
-
throw new UrlError(`Invalid or private address: ${address}`);
|
|
127
|
-
}
|
|
128
|
-
function isValidPublicIPv4Address(address) {
|
|
129
|
-
const parts = parseIPv4Address(address);
|
|
130
|
-
if (parts == null) return false;
|
|
131
|
-
const value = ipv4PartsToNumber(parts);
|
|
132
|
-
return !nonPublicIPv4Prefixes.some(({ base, prefix }) => matchesIPv4Prefix(value, base, prefix));
|
|
133
|
-
}
|
|
134
|
-
function isValidPublicIPv6Address(address) {
|
|
135
|
-
const words = parseIPv6Address(address);
|
|
136
|
-
if (words == null) return false;
|
|
137
|
-
if (nonPublicIPv6Prefixes.some(({ words: prefixWords, prefix }) => matchesIPv6Prefix(words, prefixWords, prefix))) return false;
|
|
138
|
-
for (const { extractIPv4, prefix, words: prefixWords } of ipv6WithIPv4Prefixes) {
|
|
139
|
-
if (!matchesIPv6Prefix(words, prefixWords, prefix)) continue;
|
|
140
|
-
const ipv4Address = extractIPv4(words);
|
|
141
|
-
if (ipv4Address != null && !isValidPublicIPv4Address(ipv4Address)) return false;
|
|
142
|
-
}
|
|
143
|
-
return true;
|
|
144
|
-
}
|
|
145
|
-
function expandIPv6Address(address) {
|
|
146
|
-
address = address.toLowerCase();
|
|
147
|
-
const ipv4Delimiter = address.lastIndexOf(":");
|
|
148
|
-
if (address.includes(".") && ipv4Delimiter >= 0) {
|
|
149
|
-
const ipv4Parts = parseIPv4Address(address.substring(ipv4Delimiter + 1));
|
|
150
|
-
if (ipv4Parts == null) return address;
|
|
151
|
-
const high = (ipv4Parts[0] << 8) + ipv4Parts[1];
|
|
152
|
-
const low = (ipv4Parts[2] << 8) + ipv4Parts[3];
|
|
153
|
-
address = address.substring(0, ipv4Delimiter + 1) + high.toString(16) + ":" + low.toString(16);
|
|
154
|
-
}
|
|
155
|
-
if (address === "::") return "0000:0000:0000:0000:0000:0000:0000:0000";
|
|
156
|
-
if (address.startsWith("::")) address = "0000" + address;
|
|
157
|
-
if (address.endsWith("::")) address = address + "0000";
|
|
158
|
-
address = address.replace("::", ":0000".repeat(8 - (address.match(/:/g) || []).length) + ":");
|
|
159
|
-
return address.split(":").map((part) => part.padStart(4, "0")).join(":");
|
|
160
|
-
}
|
|
161
|
-
const nonPublicIPv4Prefixes = [
|
|
162
|
-
ipv4Prefix("0.0.0.0/8", "RFC 6890"),
|
|
163
|
-
ipv4Prefix("10.0.0.0/8", "RFC 1918"),
|
|
164
|
-
ipv4Prefix("100.64.0.0/10", "RFC 6598"),
|
|
165
|
-
ipv4Prefix("127.0.0.0/8", "RFC 1122"),
|
|
166
|
-
ipv4Prefix("169.254.0.0/16", "RFC 3927"),
|
|
167
|
-
ipv4Prefix("172.16.0.0/12", "RFC 1918"),
|
|
168
|
-
ipv4Prefix("192.0.0.0/24", "RFC 6890"),
|
|
169
|
-
ipv4Prefix("192.0.2.0/24", "RFC 5737"),
|
|
170
|
-
ipv4Prefix("192.88.99.0/24", "RFC 7526"),
|
|
171
|
-
ipv4Prefix("192.168.0.0/16", "RFC 1918"),
|
|
172
|
-
ipv4Prefix("198.18.0.0/15", "RFC 2544"),
|
|
173
|
-
ipv4Prefix("198.51.100.0/24", "RFC 5737"),
|
|
174
|
-
ipv4Prefix("203.0.113.0/24", "RFC 5737"),
|
|
175
|
-
ipv4Prefix("224.0.0.0/4", "RFC 5771"),
|
|
176
|
-
ipv4Prefix("240.0.0.0/4", "RFC 1112")
|
|
177
|
-
];
|
|
178
|
-
const nonPublicIPv6Prefixes = [
|
|
179
|
-
ipv6Prefix("::/16", "RFC 4291"),
|
|
180
|
-
ipv6Prefix("2001::/32", "RFC 4380"),
|
|
181
|
-
ipv6Prefix("2002::/16", "RFC 3056"),
|
|
182
|
-
ipv6Prefix("64:ff9b:1::/48", "RFC 8215"),
|
|
183
|
-
ipv6Prefix("fc00::/7", "RFC 4193"),
|
|
184
|
-
ipv6Prefix("fe80::/10", "RFC 4291"),
|
|
185
|
-
ipv6Prefix("ff00::/8", "RFC 4291")
|
|
186
|
-
];
|
|
187
|
-
const ipv6WithIPv4Prefixes = [{
|
|
188
|
-
...ipv6Prefix("64:ff9b::/96", "RFC 6052"),
|
|
189
|
-
extractIPv4: (words) => ipv4FromWords(words[6], words[7])
|
|
190
|
-
}];
|
|
191
|
-
function ipv4Prefix(cidr, rfc) {
|
|
192
|
-
const [address, prefixText] = cidr.split("/");
|
|
193
|
-
const prefix = parseInt(prefixText, 10);
|
|
194
|
-
const parts = parseIPv4Address(address);
|
|
195
|
-
if (parts == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 32) throw new Error(`Invalid IPv4 prefix: ${cidr}`);
|
|
196
|
-
return {
|
|
197
|
-
cidr,
|
|
198
|
-
base: ipv4PartsToNumber(parts),
|
|
199
|
-
prefix,
|
|
200
|
-
rfc
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
function ipv6Prefix(cidr, rfc) {
|
|
204
|
-
const [address, prefixText] = cidr.split("/");
|
|
205
|
-
const prefix = parseInt(prefixText, 10);
|
|
206
|
-
const words = parseIPv6Address(address);
|
|
207
|
-
if (words == null || !Number.isInteger(prefix) || prefix < 0 || prefix > 128) throw new Error(`Invalid IPv6 prefix: ${cidr}`);
|
|
208
|
-
return {
|
|
209
|
-
cidr,
|
|
210
|
-
words,
|
|
211
|
-
prefix,
|
|
212
|
-
rfc
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
function parseIPv4Address(address) {
|
|
216
|
-
const parts = address.split(".").map((part) => {
|
|
217
|
-
if (!/^\d+$/.test(part)) return NaN;
|
|
218
|
-
return parseInt(part, 10);
|
|
219
|
-
});
|
|
220
|
-
if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) return null;
|
|
221
|
-
return parts;
|
|
222
|
-
}
|
|
223
|
-
function parseIPv6Address(address) {
|
|
224
|
-
const parts = expandIPv6Address(address).split(":");
|
|
225
|
-
if (parts.length !== 8) return null;
|
|
226
|
-
const words = parts.map((part) => {
|
|
227
|
-
if (!/^[0-9a-f]{1,4}$/i.test(part)) return NaN;
|
|
228
|
-
return parseInt(part, 16);
|
|
229
|
-
});
|
|
230
|
-
if (words.some((word) => !Number.isInteger(word) || word < 0 || word > 65535)) return null;
|
|
231
|
-
return words;
|
|
232
|
-
}
|
|
233
|
-
function ipv4PartsToNumber(parts) {
|
|
234
|
-
return parts[0] * 2 ** 24 + parts[1] * 2 ** 16 + parts[2] * 2 ** 8 + parts[3];
|
|
235
|
-
}
|
|
236
|
-
function ipv4FromWords(highWord, lowWord) {
|
|
237
|
-
return [
|
|
238
|
-
highWord >> 8,
|
|
239
|
-
highWord & 255,
|
|
240
|
-
lowWord >> 8,
|
|
241
|
-
lowWord & 255
|
|
242
|
-
].join(".");
|
|
243
|
-
}
|
|
244
|
-
function matchesIPv4Prefix(address, prefixBase, prefixLength) {
|
|
245
|
-
const blockSize = 2 ** (32 - prefixLength);
|
|
246
|
-
return Math.floor(address / blockSize) === Math.floor(prefixBase / blockSize);
|
|
247
|
-
}
|
|
248
|
-
function matchesIPv6Prefix(address, prefixWords, prefixLength) {
|
|
249
|
-
let remaining = prefixLength;
|
|
250
|
-
for (let i = 0; i < 8 && remaining > 0; i++) if (remaining >= 16) {
|
|
251
|
-
if (address[i] !== prefixWords[i]) return false;
|
|
252
|
-
remaining -= 16;
|
|
253
|
-
} else {
|
|
254
|
-
const mask = 65535 << 16 - remaining & 65535;
|
|
255
|
-
if ((address[i] & mask) !== (prefixWords[i] & mask)) return false;
|
|
256
|
-
remaining = 0;
|
|
257
|
-
}
|
|
258
|
-
return true;
|
|
259
|
-
}
|
|
260
|
-
//#endregion
|
|
261
|
-
export { isValidPublicIPv4Address as a, parseJsonLdId as c, haveSameIriOrigin as i, validatePublicUrl as l, expandIPv6Address as n, isValidPublicIPv6Address as o, formatIri as r, parseIri as s, UrlError as t };
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"@context": {
|
|
3
|
-
"FeaturedCollection": "https://w3id.org/fep/7aa9#FeaturedCollection",
|
|
4
|
-
"FeaturedItem": "https://w3id.org/fep/7aa9#FeaturedItem",
|
|
5
|
-
"FeatureRequest": "https://w3id.org/fep/7aa9#FeatureRequest",
|
|
6
|
-
"FeatureAuthorization": "https://w3id.org/fep/7aa9#FeatureAuthorization",
|
|
7
|
-
"topic": {
|
|
8
|
-
"@id": "https://w3id.org/fep/7aa9#topic",
|
|
9
|
-
"@type": "@id"
|
|
10
|
-
},
|
|
11
|
-
"featuredObject": {
|
|
12
|
-
"@id": "https://w3id.org/fep/7aa9#featuredObject",
|
|
13
|
-
"@type": "@id"
|
|
14
|
-
},
|
|
15
|
-
"canFeature": {
|
|
16
|
-
"@id": "https://w3id.org/fep/7aa9#canFeature",
|
|
17
|
-
"@type": "@id"
|
|
18
|
-
},
|
|
19
|
-
"featureAuthorization": {
|
|
20
|
-
"@id": "https://w3id.org/fep/7aa9#featureAuthorization",
|
|
21
|
-
"@type": "@id"
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|