@fedify/vocab-runtime 2.1.24 → 2.1.26
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 -1
- package/dist/mod.cjs +55 -7
- package/dist/mod.d.cts +10 -0
- package/dist/mod.d.ts +10 -0
- package/dist/mod.js +55 -8
- package/dist/tests/{body-D_GduSEt.mjs → body-BnzEiyNX.mjs} +1 -1
- package/dist/tests/{body-BWNGF5gy.cjs → body-DZVjD47E.cjs} +1 -1
- package/dist/tests/body.test.cjs +1 -1
- package/dist/tests/body.test.mjs +1 -1
- package/dist/tests/decimal.test.cjs +2 -2
- package/dist/tests/decimal.test.mjs +2 -2
- package/dist/tests/{docloader-BBEZYcQU.mjs → docloader-B6t23peg.mjs} +43 -6
- package/dist/tests/{docloader-CrsmzPTc.cjs → docloader-CXABd-sp.cjs} +43 -6
- package/dist/tests/docloader.test.cjs +241 -3
- package/dist/tests/docloader.test.mjs +240 -3
- package/dist/tests/{request-xQEQeUO1.cjs → request-BY5xuo0B.cjs} +1 -1
- package/dist/tests/{request-CbVzw-Ce.mjs → request-DNWAyq5z.mjs} +1 -1
- package/dist/tests/request.test.cjs +1 -1
- package/dist/tests/request.test.mjs +1 -1
- package/dist/tests/{url-BBfduh6o.mjs → url-3fyJuqC0.mjs} +14 -4
- package/dist/tests/{url-CnyF489g.cjs → url-vZTgkIZg.cjs} +15 -4
- package/dist/tests/url.test.cjs +36 -3
- package/dist/tests/url.test.mjs +37 -4
- package/package.json +1 -1
- package/src/contexts/fep-7aa9.json +24 -0
- package/src/contexts.ts +8 -0
- package/src/docloader.test.ts +307 -0
- package/src/docloader.ts +35 -6
- package/src/url.test.ts +29 -3
- package/src/url.ts +21 -5
package/dist/tests/url.test.cjs
CHANGED
|
@@ -1,8 +1,31 @@
|
|
|
1
1
|
require("./chunk-Do9eywBl.cjs");
|
|
2
|
-
const require_url = require("./url-
|
|
2
|
+
const require_url = require("./url-vZTgkIZg.cjs");
|
|
3
3
|
let node_test = require("node:test");
|
|
4
4
|
let node_assert = require("node:assert");
|
|
5
5
|
//#region src/url.test.ts
|
|
6
|
+
(0, node_test.test)("UrlError reason is independent of its cause", () => {
|
|
7
|
+
const cause = /* @__PURE__ */ new Error("Underlying failure");
|
|
8
|
+
(0, node_assert.strictEqual)(new require_url.UrlError("Rejected URL").reason, "disallowed");
|
|
9
|
+
const disallowedError = new require_url.UrlError("Rejected URL", { cause });
|
|
10
|
+
(0, node_assert.strictEqual)(disallowedError.reason, "disallowed");
|
|
11
|
+
(0, node_assert.strictEqual)(disallowedError.cause, cause);
|
|
12
|
+
const dnsError = new require_url.UrlError("Lookup failed", {
|
|
13
|
+
reason: "dns",
|
|
14
|
+
cause
|
|
15
|
+
});
|
|
16
|
+
(0, node_assert.strictEqual)(dnsError.reason, "dns");
|
|
17
|
+
(0, node_assert.strictEqual)(dnsError.cause, cause);
|
|
18
|
+
});
|
|
19
|
+
(0, node_test.test)("validatePublicUrl() classifies disallowed URLs", async () => {
|
|
20
|
+
for (const url of [
|
|
21
|
+
"ftp://example.com",
|
|
22
|
+
"https://localhost",
|
|
23
|
+
"https://127.0.0.1"
|
|
24
|
+
]) await (0, node_assert.rejects)(() => require_url.validatePublicUrl(url), {
|
|
25
|
+
name: "UrlError",
|
|
26
|
+
reason: "disallowed"
|
|
27
|
+
});
|
|
28
|
+
});
|
|
6
29
|
(0, node_test.test)("validatePublicUrl()", async () => {
|
|
7
30
|
await (0, node_assert.rejects)(() => require_url.validatePublicUrl("ftp://localhost"), require_url.UrlError);
|
|
8
31
|
await (0, node_assert.rejects)(() => require_url.validatePublicUrl("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="), require_url.UrlError);
|
|
@@ -52,11 +75,21 @@ let node_assert = require("node:assert");
|
|
|
52
75
|
}, {
|
|
53
76
|
address: "127.0.0.1",
|
|
54
77
|
family: 4
|
|
55
|
-
}]),
|
|
78
|
+
}]), {
|
|
79
|
+
name: "UrlError",
|
|
80
|
+
reason: "disallowed"
|
|
81
|
+
});
|
|
56
82
|
(0, node_assert.throws)(() => require_url.validateLookupAddresses([{
|
|
57
83
|
address: "app-host.example.net.",
|
|
58
84
|
family: 4
|
|
59
|
-
}]),
|
|
85
|
+
}]), {
|
|
86
|
+
name: "UrlError",
|
|
87
|
+
reason: "dns"
|
|
88
|
+
});
|
|
89
|
+
(0, node_assert.throws)(() => require_url.validateLookupAddresses([]), {
|
|
90
|
+
name: "UrlError",
|
|
91
|
+
reason: "dns"
|
|
92
|
+
});
|
|
60
93
|
});
|
|
61
94
|
(0, node_test.test)("isValidPublicIPv4Address()", () => {
|
|
62
95
|
(0, node_assert.ok)(require_url.isValidPublicIPv4Address("8.8.8.8"));
|
package/dist/tests/url.test.mjs
CHANGED
|
@@ -1,7 +1,30 @@
|
|
|
1
|
-
import { a as validateLookupAddresses, i as isValidPublicIPv6Address, n as expandIPv6Address, o as validatePublicUrl, r as isValidPublicIPv4Address, t as UrlError } from "./url-
|
|
1
|
+
import { a as validateLookupAddresses, i as isValidPublicIPv6Address, n as expandIPv6Address, o as validatePublicUrl, r as isValidPublicIPv4Address, t as UrlError } from "./url-3fyJuqC0.mjs";
|
|
2
2
|
import { test } from "node:test";
|
|
3
|
-
import { deepStrictEqual, ok, rejects, throws } from "node:assert";
|
|
3
|
+
import { deepStrictEqual, ok, rejects, strictEqual, throws } from "node:assert";
|
|
4
4
|
//#region src/url.test.ts
|
|
5
|
+
test("UrlError reason is independent of its cause", () => {
|
|
6
|
+
const cause = /* @__PURE__ */ new Error("Underlying failure");
|
|
7
|
+
strictEqual(new UrlError("Rejected URL").reason, "disallowed");
|
|
8
|
+
const disallowedError = new UrlError("Rejected URL", { cause });
|
|
9
|
+
strictEqual(disallowedError.reason, "disallowed");
|
|
10
|
+
strictEqual(disallowedError.cause, cause);
|
|
11
|
+
const dnsError = new UrlError("Lookup failed", {
|
|
12
|
+
reason: "dns",
|
|
13
|
+
cause
|
|
14
|
+
});
|
|
15
|
+
strictEqual(dnsError.reason, "dns");
|
|
16
|
+
strictEqual(dnsError.cause, cause);
|
|
17
|
+
});
|
|
18
|
+
test("validatePublicUrl() classifies disallowed URLs", async () => {
|
|
19
|
+
for (const url of [
|
|
20
|
+
"ftp://example.com",
|
|
21
|
+
"https://localhost",
|
|
22
|
+
"https://127.0.0.1"
|
|
23
|
+
]) await rejects(() => validatePublicUrl(url), {
|
|
24
|
+
name: "UrlError",
|
|
25
|
+
reason: "disallowed"
|
|
26
|
+
});
|
|
27
|
+
});
|
|
5
28
|
test("validatePublicUrl()", async () => {
|
|
6
29
|
await rejects(() => validatePublicUrl("ftp://localhost"), UrlError);
|
|
7
30
|
await rejects(() => validatePublicUrl("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=="), UrlError);
|
|
@@ -51,11 +74,21 @@ test("validateLookupAddresses() rejects unsafe or CNAME-only Cloudflare Workers
|
|
|
51
74
|
}, {
|
|
52
75
|
address: "127.0.0.1",
|
|
53
76
|
family: 4
|
|
54
|
-
}]),
|
|
77
|
+
}]), {
|
|
78
|
+
name: "UrlError",
|
|
79
|
+
reason: "disallowed"
|
|
80
|
+
});
|
|
55
81
|
throws(() => validateLookupAddresses([{
|
|
56
82
|
address: "app-host.example.net.",
|
|
57
83
|
family: 4
|
|
58
|
-
}]),
|
|
84
|
+
}]), {
|
|
85
|
+
name: "UrlError",
|
|
86
|
+
reason: "dns"
|
|
87
|
+
});
|
|
88
|
+
throws(() => validateLookupAddresses([]), {
|
|
89
|
+
name: "UrlError",
|
|
90
|
+
reason: "dns"
|
|
91
|
+
});
|
|
59
92
|
});
|
|
60
93
|
test("isValidPublicIPv4Address()", () => {
|
|
61
94
|
ok(isValidPublicIPv4Address("8.8.8.8"));
|
package/package.json
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
}
|
package/src/contexts.ts
CHANGED
|
@@ -8,6 +8,7 @@ import activitystreams from "./contexts/activitystreams.json" with {
|
|
|
8
8
|
import cidV1Context from "./contexts/cid-v1.json" with { type: "json" };
|
|
9
9
|
import didV1 from "./contexts/did-v1.json" with { type: "json" };
|
|
10
10
|
import fep5711 from "./contexts/fep-5711.json" with { type: "json" };
|
|
11
|
+
import fep7aa9 from "./contexts/fep-7aa9.json" with { type: "json" };
|
|
11
12
|
import fepEf61 from "./contexts/fep-ef61.json" with { type: "json" };
|
|
12
13
|
import gotosocial from "./contexts/gotosocial.json" with { type: "json" };
|
|
13
14
|
import identityV1 from "./contexts/identity-v1.json" with { type: "json" };
|
|
@@ -56,6 +57,13 @@ const preloadedContexts: Record<string, unknown> = {
|
|
|
56
57
|
// https://github.com/fedify-dev/fedify/issues/982
|
|
57
58
|
"https://w3id.org/fep/ef61": fepEf61,
|
|
58
59
|
|
|
60
|
+
// The FEP-7aa9 context is hosted on Codeberg Pages, whose outages can
|
|
61
|
+
// prevent actor documents from being parsed. Resolve it locally so that
|
|
62
|
+
// parsing does not depend on the remote context server's availability.
|
|
63
|
+
// See: https://w3id.org/fep/7aa9
|
|
64
|
+
// https://github.com/fedify-dev/fedify/issues/1078
|
|
65
|
+
"https://w3id.org/fep/7aa9": fep7aa9,
|
|
66
|
+
|
|
59
67
|
// Lemmy's context document is served as application/json without the JSON-LD
|
|
60
68
|
// context Link header. The default document loader treats that as a regular
|
|
61
69
|
// JSON response instead of a JSON-LD context, so every Lemmy activity that
|
package/src/docloader.test.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { configure, type LogRecord, reset } from "@logtape/logtape";
|
|
1
2
|
import fetchMock from "fetch-mock";
|
|
2
3
|
import { deepStrictEqual, ok, rejects } from "node:assert";
|
|
4
|
+
import dns from "node:dns/promises";
|
|
3
5
|
import { test } from "node:test";
|
|
4
6
|
import { createServer } from "node:http";
|
|
5
7
|
import { gzipSync } from "node:zlib";
|
|
@@ -336,6 +338,43 @@ test("getDocumentLoader()", async (t) => {
|
|
|
336
338
|
});
|
|
337
339
|
});
|
|
338
340
|
|
|
341
|
+
// A Codeberg Pages outage must not prevent loading the FEP-7aa9 context.
|
|
342
|
+
// See: https://github.com/fedify-dev/fedify/issues/1078
|
|
343
|
+
fetchMock.get("https://w3id.org/fep/7aa9", { status: 502 });
|
|
344
|
+
await t.test("preloaded FEP-7aa9 context", async () => {
|
|
345
|
+
const url = "https://w3id.org/fep/7aa9";
|
|
346
|
+
deepStrictEqual(await fetchDocumentLoader(url), {
|
|
347
|
+
contextUrl: null,
|
|
348
|
+
documentUrl: url,
|
|
349
|
+
document: {
|
|
350
|
+
"@context": {
|
|
351
|
+
"FeaturedCollection": "https://w3id.org/fep/7aa9#FeaturedCollection",
|
|
352
|
+
"FeaturedItem": "https://w3id.org/fep/7aa9#FeaturedItem",
|
|
353
|
+
"FeatureRequest": "https://w3id.org/fep/7aa9#FeatureRequest",
|
|
354
|
+
"FeatureAuthorization":
|
|
355
|
+
"https://w3id.org/fep/7aa9#FeatureAuthorization",
|
|
356
|
+
"topic": {
|
|
357
|
+
"@id": "https://w3id.org/fep/7aa9#topic",
|
|
358
|
+
"@type": "@id",
|
|
359
|
+
},
|
|
360
|
+
"featuredObject": {
|
|
361
|
+
"@id": "https://w3id.org/fep/7aa9#featuredObject",
|
|
362
|
+
"@type": "@id",
|
|
363
|
+
},
|
|
364
|
+
"canFeature": {
|
|
365
|
+
"@id": "https://w3id.org/fep/7aa9#canFeature",
|
|
366
|
+
"@type": "@id",
|
|
367
|
+
},
|
|
368
|
+
"featureAuthorization": {
|
|
369
|
+
"@id": "https://w3id.org/fep/7aa9#featureAuthorization",
|
|
370
|
+
"@type": "@id",
|
|
371
|
+
},
|
|
372
|
+
},
|
|
373
|
+
},
|
|
374
|
+
});
|
|
375
|
+
deepStrictEqual(fetchMock.callHistory.calls(url).length, 0);
|
|
376
|
+
});
|
|
377
|
+
|
|
339
378
|
await t.test("deny non-HTTP/HTTPS", async () => {
|
|
340
379
|
await rejects(
|
|
341
380
|
() => fetchDocumentLoader("ftp://localhost"),
|
|
@@ -630,3 +669,271 @@ test("getRemoteDocument() bounds JSON by default", async () => {
|
|
|
630
669
|
deepStrictEqual(canceled, true);
|
|
631
670
|
deepStrictEqual(pulls, 257);
|
|
632
671
|
});
|
|
672
|
+
|
|
673
|
+
test("getDocumentLoader() logs DNS failures as such", async (t) => {
|
|
674
|
+
// The validator skips DNS when Deno has no network permission. Checked
|
|
675
|
+
// here rather than with top-level await, which the CommonJS build rejects.
|
|
676
|
+
if (
|
|
677
|
+
"Deno" in globalThis &&
|
|
678
|
+
(await Deno.permissions.query({ name: "net" })).state !== "granted"
|
|
679
|
+
) {
|
|
680
|
+
t.skip("requires the net permission");
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
const loader = getDocumentLoader();
|
|
684
|
+
for (const result of ["throws", "empty", "private"] as const) {
|
|
685
|
+
await t.test(result, async () => {
|
|
686
|
+
// Stubbing works only because url.ts uses the default node:dns/promises
|
|
687
|
+
// import; see the FIXME there.
|
|
688
|
+
const originalLookup = dns.lookup;
|
|
689
|
+
dns.lookup = (() =>
|
|
690
|
+
result === "throws"
|
|
691
|
+
? Promise.reject(new Error("Resolver unavailable"))
|
|
692
|
+
: Promise.resolve(
|
|
693
|
+
result === "empty" ? [] : [{ address: "127.0.0.1", family: 4 }],
|
|
694
|
+
)) as typeof dns.lookup;
|
|
695
|
+
const records: LogRecord[] = [];
|
|
696
|
+
await configure({
|
|
697
|
+
sinks: {
|
|
698
|
+
buffer: (record) =>
|
|
699
|
+
records.push(record),
|
|
700
|
+
},
|
|
701
|
+
loggers: [
|
|
702
|
+
{ category: "fedify", sinks: ["buffer"], lowestLevel: "debug" },
|
|
703
|
+
{ category: ["logtape", "meta"], sinks: [] },
|
|
704
|
+
],
|
|
705
|
+
reset: true,
|
|
706
|
+
});
|
|
707
|
+
try {
|
|
708
|
+
const url = "https://dns-failure.invalid/object";
|
|
709
|
+
let error: unknown;
|
|
710
|
+
await rejects(() => loader(url), (e) => {
|
|
711
|
+
error = e;
|
|
712
|
+
return true;
|
|
713
|
+
});
|
|
714
|
+
ok(error instanceof UrlError);
|
|
715
|
+
deepStrictEqual(
|
|
716
|
+
error.reason,
|
|
717
|
+
result === "private" ? "disallowed" : "dns",
|
|
718
|
+
);
|
|
719
|
+
deepStrictEqual(
|
|
720
|
+
records.map((r) => [r.level, r.rawMessage, r.properties.url]),
|
|
721
|
+
[
|
|
722
|
+
result === "private"
|
|
723
|
+
? ["error", "Disallowed private URL: {url}", url]
|
|
724
|
+
: ["debug", "DNS lookup failed for {url}", url],
|
|
725
|
+
],
|
|
726
|
+
);
|
|
727
|
+
ok(records[0].properties.error === error);
|
|
728
|
+
} finally {
|
|
729
|
+
await reset();
|
|
730
|
+
dns.lookup = originalLookup;
|
|
731
|
+
}
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
|
|
736
|
+
test("getDocumentLoader() bounds alternate document chains", async (t) => {
|
|
737
|
+
const base = "https://example.com/alternate-chain/";
|
|
738
|
+
const loader = getDocumentLoader({ allowPrivateAddress: true });
|
|
739
|
+
for (const mode of ["header", "html", "mixed"] as const) {
|
|
740
|
+
for (const hops of [20, 21]) {
|
|
741
|
+
await t.test(`${mode}: ${hops} hops`, async () => {
|
|
742
|
+
fetchMock.mockGlobal();
|
|
743
|
+
let requests = 0;
|
|
744
|
+
fetchMock.get(`begin:${base}`, ({ url }) => {
|
|
745
|
+
requests++;
|
|
746
|
+
const index = Number(new URL(url).pathname.split("/").at(-1));
|
|
747
|
+
// Keep the unpatched regression bounded too.
|
|
748
|
+
if (index === hops || requests > 30) {
|
|
749
|
+
return Response.json({ done: true });
|
|
750
|
+
}
|
|
751
|
+
const next = `${base}${index + 1}`;
|
|
752
|
+
if (mode === "mixed" && index % 2 === 1) {
|
|
753
|
+
return Response.redirect(next, 302);
|
|
754
|
+
}
|
|
755
|
+
return alternate(next, mode === "html");
|
|
756
|
+
});
|
|
757
|
+
try {
|
|
758
|
+
if (hops === 20) {
|
|
759
|
+
deepStrictEqual((await loader(`${base}0`)).document, {
|
|
760
|
+
done: true,
|
|
761
|
+
});
|
|
762
|
+
} else {
|
|
763
|
+
await rejects(loader(`${base}0`), (error: unknown) => {
|
|
764
|
+
ok(error instanceof FetchError);
|
|
765
|
+
ok(error.message.includes("Too many redirections (21)"));
|
|
766
|
+
return true;
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
deepStrictEqual(requests, 21);
|
|
770
|
+
} finally {
|
|
771
|
+
fetchMock.hardReset();
|
|
772
|
+
}
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
for (
|
|
778
|
+
const mode of [
|
|
779
|
+
"header",
|
|
780
|
+
"html",
|
|
781
|
+
"alternate-redirect",
|
|
782
|
+
"redirect-alternate",
|
|
783
|
+
"redirect-intermediate",
|
|
784
|
+
]
|
|
785
|
+
) {
|
|
786
|
+
await t.test(`${mode}: cycle`, async () => {
|
|
787
|
+
fetchMock.mockGlobal();
|
|
788
|
+
let requests = 0;
|
|
789
|
+
fetchMock.get(`begin:${base}`, ({ url }) => {
|
|
790
|
+
requests++;
|
|
791
|
+
if (requests > 30) return Response.json({ stopped: true });
|
|
792
|
+
const index = Number(new URL(url).pathname.split("/").at(-1));
|
|
793
|
+
const next = `${base}${
|
|
794
|
+
mode === "redirect-intermediate"
|
|
795
|
+
? (index === 0 ? 1 : index === 1 ? 2 : 1)
|
|
796
|
+
: 1 - index
|
|
797
|
+
}`;
|
|
798
|
+
if (
|
|
799
|
+
(mode === "alternate-redirect" && index === 1) ||
|
|
800
|
+
((mode === "redirect-alternate" ||
|
|
801
|
+
mode === "redirect-intermediate") && index === 0)
|
|
802
|
+
) {
|
|
803
|
+
return Response.redirect(next, 302);
|
|
804
|
+
}
|
|
805
|
+
return alternate(next, mode === "html");
|
|
806
|
+
});
|
|
807
|
+
try {
|
|
808
|
+
await rejects(loader(`${base}0`), (error: unknown) => {
|
|
809
|
+
ok(error instanceof FetchError);
|
|
810
|
+
ok(error.message.includes("Redirect loop detected:"));
|
|
811
|
+
return true;
|
|
812
|
+
});
|
|
813
|
+
deepStrictEqual(requests, mode === "redirect-intermediate" ? 3 : 2);
|
|
814
|
+
} finally {
|
|
815
|
+
fetchMock.hardReset();
|
|
816
|
+
}
|
|
817
|
+
});
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
await t.test("alternate followed by 20 redirects shares the limit", async () => {
|
|
821
|
+
fetchMock.mockGlobal();
|
|
822
|
+
let requests = 0;
|
|
823
|
+
fetchMock.get(`begin:${base}`, ({ url }) => {
|
|
824
|
+
requests++;
|
|
825
|
+
const index = Number(new URL(url).pathname.split("/").at(-1));
|
|
826
|
+
if (index === 21) return Response.json({ done: true });
|
|
827
|
+
return index === 0
|
|
828
|
+
? alternate(`${base}1`, false)
|
|
829
|
+
: Response.redirect(`${base}${index + 1}`, 302);
|
|
830
|
+
});
|
|
831
|
+
try {
|
|
832
|
+
await rejects(loader(`${base}0`), (error: unknown) => {
|
|
833
|
+
ok(error instanceof FetchError);
|
|
834
|
+
ok(error.message.includes("Too many redirections (21)"));
|
|
835
|
+
return true;
|
|
836
|
+
});
|
|
837
|
+
deepStrictEqual(requests, 21);
|
|
838
|
+
} finally {
|
|
839
|
+
fetchMock.hardReset();
|
|
840
|
+
}
|
|
841
|
+
});
|
|
842
|
+
|
|
843
|
+
await t.test("relative alternate after redirect and isolated calls", async () => {
|
|
844
|
+
fetchMock.mockGlobal();
|
|
845
|
+
fetchMock.get(`${base}start`, Response.redirect(`${base}html`, 302));
|
|
846
|
+
fetchMock.get(`${base}html`, alternate("./document", true));
|
|
847
|
+
fetchMock.get(`${base}document`, Response.json({ done: true }));
|
|
848
|
+
try {
|
|
849
|
+
for (let i = 0; i < 2; i++) {
|
|
850
|
+
const results = await Promise.all([
|
|
851
|
+
loader(`${base}start`),
|
|
852
|
+
loader(`${base}start`),
|
|
853
|
+
]);
|
|
854
|
+
for (const result of results) {
|
|
855
|
+
deepStrictEqual(result.document, { done: true });
|
|
856
|
+
deepStrictEqual(result.documentUrl, `${base}document`);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
} finally {
|
|
860
|
+
fetchMock.hardReset();
|
|
861
|
+
}
|
|
862
|
+
});
|
|
863
|
+
|
|
864
|
+
function alternate(next: string, html: boolean): Response {
|
|
865
|
+
return html
|
|
866
|
+
? new Response(
|
|
867
|
+
`<link rel="alternate" type="application/activity+json" href="${next}">`,
|
|
868
|
+
{
|
|
869
|
+
headers: { "Content-Type": "text/html" },
|
|
870
|
+
},
|
|
871
|
+
)
|
|
872
|
+
: new Response("not JSON", {
|
|
873
|
+
headers: {
|
|
874
|
+
"Content-Type": "text/plain",
|
|
875
|
+
Link: `<${next}>; rel="alternate"; type="application/activity+json"`,
|
|
876
|
+
},
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
test("getDocumentLoader() preserves cancellation across alternates", async (t) => {
|
|
882
|
+
const base = "https://example.com/alternate-abort/";
|
|
883
|
+
const loader = getDocumentLoader({ allowPrivateAddress: true });
|
|
884
|
+
for (const html of [false, true]) {
|
|
885
|
+
await t.test(html ? "HTML" : "Link header", async () => {
|
|
886
|
+
fetchMock.mockGlobal();
|
|
887
|
+
const controller = new AbortController();
|
|
888
|
+
let requests = 0;
|
|
889
|
+
fetchMock.get(`begin:${base}`, ({ url }) => {
|
|
890
|
+
requests++;
|
|
891
|
+
const index = Number(new URL(url).pathname.split("/").at(-1));
|
|
892
|
+
if (index === 2) return Response.json({ done: true });
|
|
893
|
+
if (index === 1) controller.abort();
|
|
894
|
+
const next = `${base}${index + 1}`;
|
|
895
|
+
return new Response(
|
|
896
|
+
html
|
|
897
|
+
? `<link rel="alternate" type="application/activity+json" href="${next}">`
|
|
898
|
+
: "not JSON",
|
|
899
|
+
{
|
|
900
|
+
headers: html ? { "Content-Type": "text/html" } : {
|
|
901
|
+
"Content-Type": "text/plain",
|
|
902
|
+
Link:
|
|
903
|
+
`<${next}>; rel="alternate"; type="application/activity+json"`,
|
|
904
|
+
},
|
|
905
|
+
},
|
|
906
|
+
);
|
|
907
|
+
});
|
|
908
|
+
try {
|
|
909
|
+
await rejects(loader(`${base}0`, { signal: controller.signal }), {
|
|
910
|
+
name: "AbortError",
|
|
911
|
+
});
|
|
912
|
+
deepStrictEqual(requests, 2);
|
|
913
|
+
} finally {
|
|
914
|
+
fetchMock.hardReset();
|
|
915
|
+
}
|
|
916
|
+
});
|
|
917
|
+
}
|
|
918
|
+
});
|
|
919
|
+
|
|
920
|
+
test("getDocumentLoader() rejects cancellation before fetching", async () => {
|
|
921
|
+
fetchMock.mockGlobal();
|
|
922
|
+
let requests = 0;
|
|
923
|
+
const url = "https://example.com/pre-aborted-alternate";
|
|
924
|
+
fetchMock.get(url, () => {
|
|
925
|
+
requests++;
|
|
926
|
+
return Response.json({ done: true });
|
|
927
|
+
});
|
|
928
|
+
try {
|
|
929
|
+
const loader = getDocumentLoader({ allowPrivateAddress: true });
|
|
930
|
+
const controller = new AbortController();
|
|
931
|
+
controller.abort();
|
|
932
|
+
await rejects(loader(url, { signal: controller.signal }), {
|
|
933
|
+
name: "AbortError",
|
|
934
|
+
});
|
|
935
|
+
deepStrictEqual(requests, 0);
|
|
936
|
+
} finally {
|
|
937
|
+
fetchMock.hardReset();
|
|
938
|
+
}
|
|
939
|
+
});
|
package/src/docloader.ts
CHANGED
|
@@ -267,6 +267,8 @@ export interface GetDocumentLoaderOptions extends DocumentLoaderFactoryOptions {
|
|
|
267
267
|
|
|
268
268
|
/**
|
|
269
269
|
* Creates a JSON-LD document loader that utilizes the browser's `fetch` API.
|
|
270
|
+
* At most 20 HTTP redirects and alternate document links are followed in total
|
|
271
|
+
* per call. Revisiting a URL within that chain throws a {@link FetchError}.
|
|
270
272
|
*
|
|
271
273
|
* The created loader preloads the below frequently used contexts by default
|
|
272
274
|
* (unless `options.skipPreloadedContexts` is set to `true`):
|
|
@@ -278,6 +280,7 @@ export interface GetDocumentLoaderOptions extends DocumentLoaderFactoryOptions {
|
|
|
278
280
|
* - <https://www.w3.org/ns/cid/v1>
|
|
279
281
|
* - <https://w3id.org/security/multikey/v1>
|
|
280
282
|
* - <https://w3id.org/fep/ef61>
|
|
283
|
+
* - <https://w3id.org/fep/7aa9>
|
|
281
284
|
* - <https://purl.archive.org/socialweb/webfinger>
|
|
282
285
|
* - <http://schema.org/>
|
|
283
286
|
* @param options Options for the document loader.
|
|
@@ -312,10 +315,17 @@ export function getDocumentLoader(
|
|
|
312
315
|
await validatePublicUrl(currentUrl);
|
|
313
316
|
} catch (error) {
|
|
314
317
|
if (error instanceof UrlError) {
|
|
315
|
-
|
|
316
|
-
url
|
|
317
|
-
|
|
318
|
-
|
|
318
|
+
if (error.reason === "dns") {
|
|
319
|
+
logger.debug("DNS lookup failed for {url}", {
|
|
320
|
+
url: currentUrl,
|
|
321
|
+
error,
|
|
322
|
+
});
|
|
323
|
+
} else {
|
|
324
|
+
logger.error("Disallowed private URL: {url}", {
|
|
325
|
+
url: currentUrl,
|
|
326
|
+
error,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
319
329
|
}
|
|
320
330
|
throw error;
|
|
321
331
|
}
|
|
@@ -377,7 +387,26 @@ export function getDocumentLoader(
|
|
|
377
387
|
return await load(redirectUrl, options, redirected + 1, visited);
|
|
378
388
|
}
|
|
379
389
|
|
|
380
|
-
const result = await getRemoteDocument(
|
|
390
|
+
const result = await getRemoteDocument(
|
|
391
|
+
currentUrl,
|
|
392
|
+
response,
|
|
393
|
+
async (alternateUrl) => {
|
|
394
|
+
options?.signal?.throwIfAborted();
|
|
395
|
+
if (redirected >= DEFAULT_MAX_REDIRECTION) {
|
|
396
|
+
throw new FetchError(
|
|
397
|
+
currentUrl,
|
|
398
|
+
`Too many redirections (${redirected + 1})`,
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
if (visited.has(alternateUrl)) {
|
|
402
|
+
throw new FetchError(
|
|
403
|
+
currentUrl,
|
|
404
|
+
`Redirect loop detected: ${alternateUrl}`,
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
return await load(alternateUrl, options, redirected + 1, visited);
|
|
408
|
+
},
|
|
409
|
+
);
|
|
381
410
|
span.setAttribute("docloader.document_url", result.documentUrl);
|
|
382
411
|
if (result.contextUrl != null) {
|
|
383
412
|
span.setAttribute("docloader.context_url", result.contextUrl);
|
|
@@ -396,5 +425,5 @@ export function getDocumentLoader(
|
|
|
396
425
|
},
|
|
397
426
|
);
|
|
398
427
|
}
|
|
399
|
-
return load;
|
|
428
|
+
return (url, options) => load(url, options);
|
|
400
429
|
}
|
package/src/url.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { deepStrictEqual, ok, rejects, throws } from "node:assert";
|
|
1
|
+
import { deepStrictEqual, ok, rejects, strictEqual, throws } from "node:assert";
|
|
2
2
|
import { test } from "node:test";
|
|
3
3
|
import {
|
|
4
4
|
expandIPv6Address,
|
|
@@ -9,6 +9,28 @@ import {
|
|
|
9
9
|
validatePublicUrl,
|
|
10
10
|
} from "./url.ts";
|
|
11
11
|
|
|
12
|
+
test("UrlError reason is independent of its cause", () => {
|
|
13
|
+
const cause = new Error("Underlying failure");
|
|
14
|
+
strictEqual(new UrlError("Rejected URL").reason, "disallowed");
|
|
15
|
+
const disallowedError = new UrlError("Rejected URL", { cause });
|
|
16
|
+
strictEqual(disallowedError.reason, "disallowed");
|
|
17
|
+
strictEqual(disallowedError.cause, cause);
|
|
18
|
+
const dnsError = new UrlError("Lookup failed", { reason: "dns", cause });
|
|
19
|
+
strictEqual(dnsError.reason, "dns");
|
|
20
|
+
strictEqual(dnsError.cause, cause);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("validatePublicUrl() classifies disallowed URLs", async () => {
|
|
24
|
+
for (
|
|
25
|
+
const url of ["ftp://example.com", "https://localhost", "https://127.0.0.1"]
|
|
26
|
+
) {
|
|
27
|
+
await rejects(() => validatePublicUrl(url), {
|
|
28
|
+
name: "UrlError",
|
|
29
|
+
reason: "disallowed",
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
12
34
|
test("validatePublicUrl()", async () => {
|
|
13
35
|
await rejects(() => validatePublicUrl("ftp://localhost"), UrlError);
|
|
14
36
|
await rejects(
|
|
@@ -81,15 +103,19 @@ test("validateLookupAddresses() rejects unsafe or CNAME-only Cloudflare Workers
|
|
|
81
103
|
{ address: "private-host.example.net.", family: 4 },
|
|
82
104
|
{ address: "127.0.0.1", family: 4 },
|
|
83
105
|
]),
|
|
84
|
-
UrlError,
|
|
106
|
+
{ name: "UrlError", reason: "disallowed" },
|
|
85
107
|
);
|
|
86
108
|
throws(
|
|
87
109
|
() =>
|
|
88
110
|
validateLookupAddresses([
|
|
89
111
|
{ address: "app-host.example.net.", family: 4 },
|
|
90
112
|
]),
|
|
91
|
-
UrlError,
|
|
113
|
+
{ name: "UrlError", reason: "dns" },
|
|
92
114
|
);
|
|
115
|
+
throws(() => validateLookupAddresses([]), {
|
|
116
|
+
name: "UrlError",
|
|
117
|
+
reason: "dns",
|
|
118
|
+
});
|
|
93
119
|
});
|
|
94
120
|
|
|
95
121
|
test("isValidPublicIPv4Address()", () => {
|
package/src/url.ts
CHANGED
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
import type { LookupAddress } from "node:dns";
|
|
2
|
-
import
|
|
2
|
+
// FIXME: the default dns import exists since tests can stub `dns.lookup()`.
|
|
3
|
+
// This should be replaced by injecting the lookup function such as an internal
|
|
4
|
+
// option on `validatePublicUrl()`
|
|
5
|
+
import dns from "node:dns/promises";
|
|
3
6
|
import { isIP } from "node:net";
|
|
4
7
|
|
|
5
8
|
export class UrlError extends Error {
|
|
6
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The failure category. Defaults to `"disallowed"`.
|
|
11
|
+
* `"dns"` includes lookup errors and results with no usable IP addresses.
|
|
12
|
+
* @since 2.0.29
|
|
13
|
+
*/
|
|
14
|
+
readonly reason: "dns" | "disallowed";
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
message: string,
|
|
18
|
+
options?: { cause?: unknown; reason?: "dns" | "disallowed" },
|
|
19
|
+
) {
|
|
7
20
|
super(message, options);
|
|
8
21
|
this.name = "UrlError";
|
|
22
|
+
this.reason = options?.reason ?? "disallowed";
|
|
9
23
|
}
|
|
10
24
|
}
|
|
11
25
|
|
|
@@ -50,9 +64,9 @@ export async function validatePublicUrl(url: string): Promise<void> {
|
|
|
50
64
|
// and ensure that they are all public:
|
|
51
65
|
let addresses: LookupAddress[];
|
|
52
66
|
try {
|
|
53
|
-
addresses = await lookup(hostname, { all: true });
|
|
67
|
+
addresses = await dns.lookup(hostname, { all: true });
|
|
54
68
|
} catch (error) {
|
|
55
|
-
throw new UrlError("DNS lookup failed", { cause: error });
|
|
69
|
+
throw new UrlError("DNS lookup failed", { cause: error, reason: "dns" });
|
|
56
70
|
}
|
|
57
71
|
validateLookupAddresses(addresses);
|
|
58
72
|
}
|
|
@@ -85,7 +99,9 @@ export function validateLookupAddresses(
|
|
|
85
99
|
validatePublicIpAddress(address, family);
|
|
86
100
|
}
|
|
87
101
|
if (ipAddressCount === 0) {
|
|
88
|
-
throw new UrlError("DNS lookup did not return any IP address"
|
|
102
|
+
throw new UrlError("DNS lookup did not return any IP address", {
|
|
103
|
+
reason: "dns",
|
|
104
|
+
});
|
|
89
105
|
}
|
|
90
106
|
}
|
|
91
107
|
|