@fedify/fedify 2.3.6 → 2.3.8

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 (108) hide show
  1. package/dist/{assert_instance_of-DBC5X09g.mjs → assert-jSdZMYam.mjs} +19 -1
  2. package/dist/{builder-CW-9AJCO.mjs → builder-CDForRSL.mjs} +3 -3
  3. package/dist/{client-D1KuWxjH.mjs → client-DYDoDpMa.mjs} +78 -4
  4. package/dist/compat/mod.d.cts +1 -1
  5. package/dist/compat/mod.d.ts +1 -1
  6. package/dist/compat/outgoing-jsonld.test.mjs +1 -1
  7. package/dist/compat/public-audience.test.mjs +1 -1
  8. package/dist/compat/transformers.test.mjs +3 -4
  9. package/dist/{context-DVoTs_wM.mjs → context-BepjCFHN.mjs} +1 -0
  10. package/dist/{context-BxVVO5Ro.d.ts → context-CAKnQy1x.d.ts} +4 -3
  11. package/dist/{context-BpmLyiPx.d.cts → context-CSIggG9_.d.cts} +4 -3
  12. package/dist/{deno-KC-uf6Un.mjs → deno--DCBC7WG.mjs} +1 -1
  13. package/dist/{docloader-BHS1yt22.mjs → docloader-BAdp-ZeK.mjs} +2 -2
  14. package/dist/federation/builder.test.mjs +2 -2
  15. package/dist/federation/circuit-breaker.test.mjs +3 -3
  16. package/dist/federation/collection.test.mjs +1 -1
  17. package/dist/federation/handler.test.mjs +128 -11
  18. package/dist/federation/idempotency.test.mjs +4 -4
  19. package/dist/federation/inbox.test.mjs +1 -1
  20. package/dist/federation/keycache.test.mjs +44 -10
  21. package/dist/federation/kv.test.mjs +1 -1
  22. package/dist/federation/metrics.test.mjs +1 -1
  23. package/dist/federation/middleware.test.mjs +109 -19
  24. package/dist/federation/mod.cjs +1 -1
  25. package/dist/federation/mod.d.cts +2 -2
  26. package/dist/federation/mod.d.ts +2 -2
  27. package/dist/federation/mod.js +1 -1
  28. package/dist/federation/mq.test.mjs +1 -1
  29. package/dist/federation/negotiation.test.mjs +2 -2
  30. package/dist/federation/retry.test.mjs +1 -1
  31. package/dist/federation/router.test.mjs +1 -1
  32. package/dist/federation/send.test.mjs +141 -7
  33. package/dist/federation/temporal.test.mjs +2 -2
  34. package/dist/federation/webfinger.test.mjs +3 -3
  35. package/dist/{http-DzgaVy-x.js → http-BKJYj9oz.js} +155 -7
  36. package/dist/{http-DL5ZJBC4.cjs → http-ChVf88XZ.cjs} +166 -6
  37. package/dist/{http-BcbYkefs.mjs → http-ClEDLGpr.mjs} +4 -4
  38. package/dist/{http-lf8Hsd91.d.ts → http-DQmNpfym.d.ts} +1 -1
  39. package/dist/{http-VyDTd4G3.d.cts → http-DiWQB8Mr.d.cts} +1 -1
  40. package/dist/{key-Cbkqgt-G.mjs → key-C-7sqo8D.mjs} +155 -7
  41. package/dist/{keycache-BeU0LCII.mjs → keycache-B3qRQNLj.mjs} +12 -4
  42. package/dist/{kv-cache-CT228f7w.mjs → kv-cache-C5FK4PLG.mjs} +1 -1
  43. package/dist/{kv-cache-BWNRhap7.js → kv-cache-C_w70bFh.js} +2 -2
  44. package/dist/{kv-cache-Dw94l8rT.cjs → kv-cache-DFBOAyNj.cjs} +2 -2
  45. package/dist/{ld-c-9mCB36.mjs → ld-CtFHB6rU.mjs} +3 -3
  46. package/dist/{metrics-D8s1eKfJ.mjs → metrics-FDcYXRP5.mjs} +1 -1
  47. package/dist/{middleware-B29rR42K.cjs → middleware-BdDtzhXb.cjs} +101 -25
  48. package/dist/{middleware-3L9f9ZTw.mjs → middleware-COzI8bCL.mjs} +41 -30
  49. package/dist/{middleware-D_cNQ7Yq.js → middleware-DunPXFJL.js} +101 -25
  50. package/dist/{middleware-Be2z0t5f.mjs → middleware-K7mJeIgT.mjs} +1 -1
  51. package/dist/{mod-yvIXFAEi.d.cts → mod-1TuoXkGs.d.cts} +1 -1
  52. package/dist/{mod-B0hW12_O.d.cts → mod-Bid79LFU.d.cts} +1 -1
  53. package/dist/{mod-Rpy4Uwkk.d.ts → mod-CFuyfw3c.d.ts} +1 -1
  54. package/dist/{mod-DFvNJcNb.d.ts → mod-DHgc4ygG.d.ts} +1 -1
  55. package/dist/{mod-CjERT09F.d.cts → mod-PVR75MeU.d.cts} +1 -1
  56. package/dist/{mod-COIAjwRS.d.ts → mod-TLEvgT1i.d.ts} +1 -1
  57. package/dist/mod.cjs +5 -5
  58. package/dist/mod.d.cts +6 -6
  59. package/dist/mod.d.ts +6 -6
  60. package/dist/mod.js +5 -5
  61. package/dist/nodeinfo/client.test.mjs +28 -1
  62. package/dist/nodeinfo/handler.test.mjs +3 -3
  63. package/dist/nodeinfo/mod.cjs +1 -1
  64. package/dist/nodeinfo/mod.js +1 -1
  65. package/dist/nodeinfo/types.test.mjs +1 -1
  66. package/dist/otel/exporter.test.mjs +1 -1
  67. package/dist/{outgoing-jsonld-OFoubetZ.mjs → outgoing-jsonld-HOAywiid.mjs} +1 -1
  68. package/dist/{owner-CnngXDNJ.d.ts → owner-B3hNQCG8.d.cts} +12 -1
  69. package/dist/owner-CGuS9lE7.mjs +175 -0
  70. package/dist/{owner-DEvZuyOE.d.cts → owner-CiBPcC6F.d.ts} +12 -1
  71. package/dist/{proof-D_6qtBdQ.mjs → proof--56drUyt.mjs} +5 -5
  72. package/dist/{proof-DGlDXtcc.cjs → proof-Dgozne0w.cjs} +112 -58
  73. package/dist/{proof-CrtfGSi7.js → proof-tjbzynMa.js} +112 -58
  74. package/dist/{send-i63Det4Y.mjs → send-CC1A7ATC.mjs} +68 -11
  75. package/dist/sig/accept.test.mjs +1 -1
  76. package/dist/sig/http.test.mjs +4 -4
  77. package/dist/sig/key.test.mjs +170 -3
  78. package/dist/sig/ld.test.mjs +56 -4
  79. package/dist/sig/mod.cjs +2 -2
  80. package/dist/sig/mod.d.cts +3 -3
  81. package/dist/sig/mod.d.ts +3 -3
  82. package/dist/sig/mod.js +2 -2
  83. package/dist/sig/owner.test.mjs +105 -6
  84. package/dist/sig/proof.test.mjs +73 -5
  85. package/dist/{temporal-CcVjTX30.mjs → temporal-DvptXZ7V.mjs} +1 -1
  86. package/dist/testing/mod.mjs +1 -1
  87. package/dist/{types-m8u9L86X.cjs → types-DWs-qZaY.cjs} +94 -2
  88. package/dist/{types-DTONwNqL.js → types-sXITXcWa.js} +78 -4
  89. package/dist/utils/docloader.test.mjs +28 -4
  90. package/dist/utils/kv-cache.test.mjs +1 -1
  91. package/dist/utils/mod.cjs +1 -1
  92. package/dist/utils/mod.d.cts +1 -1
  93. package/dist/utils/mod.d.ts +1 -1
  94. package/dist/utils/mod.js +1 -1
  95. package/package.json +7 -7
  96. package/dist/assert-OguE97r2.mjs +0 -23
  97. package/dist/owner-CjkdUXE3.mjs +0 -120
  98. /package/dist/{accept-CPkZzmGN.mjs → accept-Dd__NiUL.mjs} +0 -0
  99. /package/dist/{activity-listener-tztVvlNb.mjs → activity-listener-ell7W1s9.mjs} +0 -0
  100. /package/dist/{circuit-breaker-D1fxthDI.mjs → circuit-breaker-CDABzqVf.mjs} +0 -0
  101. /package/dist/{circuit-breaker-test-utils-CAdwjgMi.mjs → circuit-breaker-test-utils-Dm5MM6l9.mjs} +0 -0
  102. /package/dist/{collection-Cc3DVAhE.mjs → collection-CA3V5zyK.mjs} +0 -0
  103. /package/dist/{keys-DGu1NFwu.mjs → keys-BAK-tUlf.mjs} +0 -0
  104. /package/dist/{kv-rV3vodCc.mjs → kv-CX8qfaad.mjs} +0 -0
  105. /package/dist/{negotiation-DDstyBvc.mjs → negotiation-CDW-_gUU.mjs} +0 -0
  106. /package/dist/{public-audience-DZJAUUi8.mjs → public-audience-jwoSIGUw.mjs} +0 -0
  107. /package/dist/{retry-CXg_MBI-.mjs → retry-_VvV0h9f.mjs} +0 -0
  108. /package/dist/{types-J53Kw7so.mjs → types-s4mCTGkT.mjs} +0 -0
@@ -4,7 +4,7 @@ globalThis.addEventListener = () => {};
4
4
  import { t as esm_default } from "../esm-vrlUxr60.mjs";
5
5
  import { t as assertEquals } from "../assert_equals-C-ZRDbaf.mjs";
6
6
  import "../std__assert-BBjXFNOb.mjs";
7
- import { a as parseProtocol, c as parseUsage, i as parseOutboundService, n as parseInboundService, o as parseServices, r as parseNodeInfo, s as parseSoftware, t as getNodeInfo } from "../client-D1KuWxjH.mjs";
7
+ import { a as parseProtocol, c as parseUsage, i as parseOutboundService, n as parseInboundService, o as parseServices, r as parseNodeInfo, s as parseSoftware, t as getNodeInfo } from "../client-DYDoDpMa.mjs";
8
8
  import { test } from "@fedify/fixture";
9
9
  //#region src/nodeinfo/client.test.ts
10
10
  test("getNodeInfo()", async (t) => {
@@ -967,5 +967,32 @@ test("parseUsage()", () => {
967
967
  localComments: 456
968
968
  });
969
969
  });
970
+ test("getNodeInfo() bounds both discovery and document responses", async () => {
971
+ esm_default.mockGlobal();
972
+ const base = "https://8.8.8.8";
973
+ const descriptor = { links: [{
974
+ rel: "http://nodeinfo.diaspora.software/ns/schema/2.1",
975
+ href: `${base}/nodeinfo`
976
+ }] };
977
+ let oversizedDescriptor = true;
978
+ let oversizedDocument = true;
979
+ const data = { padding: "a".repeat(256) };
980
+ try {
981
+ esm_default.get(`${base}/.well-known/nodeinfo`, () => new Response(JSON.stringify(descriptor), { headers: oversizedDescriptor ? { "Content-Length": String(16777217) } : {} }));
982
+ esm_default.get(`${base}/nodeinfo`, () => new Response(JSON.stringify(data), { headers: oversizedDocument ? { "Content-Length": String(16777217) } : {} }));
983
+ assertEquals(await getNodeInfo(base), void 0);
984
+ assertEquals(esm_default.callHistory.calls(`${base}/nodeinfo`).length, 0);
985
+ oversizedDescriptor = false;
986
+ assertEquals(await getNodeInfo(base), void 0);
987
+ assertEquals(await getNodeInfo(`${base}/nodeinfo`, {
988
+ direct: true,
989
+ parse: "none"
990
+ }), void 0);
991
+ oversizedDocument = false;
992
+ assertEquals(await getNodeInfo(base, { parse: "none" }), data);
993
+ } finally {
994
+ esm_default.hardReset();
995
+ }
996
+ });
970
997
  //#endregion
971
998
  export {};
@@ -1,11 +1,11 @@
1
1
  import "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
- import { r as createRequestContext } from "../context-DVoTs_wM.mjs";
4
+ import { r as createRequestContext } from "../context-BepjCFHN.mjs";
5
5
  import { t as assertEquals } from "../assert_equals-C-ZRDbaf.mjs";
6
6
  import "../std__assert-BBjXFNOb.mjs";
7
- import { t as MemoryKvStore } from "../kv-rV3vodCc.mjs";
8
- import { _ as handleNodeInfo, o as createFederation, v as handleNodeInfoJrd } from "../middleware-3L9f9ZTw.mjs";
7
+ import { t as MemoryKvStore } from "../kv-CX8qfaad.mjs";
8
+ import { _ as handleNodeInfo, o as createFederation, v as handleNodeInfoJrd } from "../middleware-COzI8bCL.mjs";
9
9
  import { test } from "@fedify/fixture";
10
10
  //#region src/nodeinfo/handler.test.ts
11
11
  test("handleNodeInfo()", async () => {
@@ -1,7 +1,7 @@
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_types = require("../types-m8u9L86X.cjs");
4
+ const require_types = require("../types-DWs-qZaY.cjs");
5
5
  exports.getNodeInfo = require_types.getNodeInfo;
6
6
  exports.nodeInfoToJson = require_types.nodeInfoToJson;
7
7
  exports.parseNodeInfo = require_types.parseNodeInfo;
@@ -1,4 +1,4 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
2
  import { URLPattern } from "urlpattern-polyfill";
3
- import { n as getNodeInfo, r as parseNodeInfo, t as nodeInfoToJson } from "../types-DTONwNqL.js";
3
+ import { n as getNodeInfo, r as parseNodeInfo, t as nodeInfoToJson } from "../types-sXITXcWa.js";
4
4
  export { getNodeInfo, nodeInfoToJson, parseNodeInfo };
@@ -4,7 +4,7 @@ globalThis.addEventListener = () => {};
4
4
  import { t as assertEquals } from "../assert_equals-C-ZRDbaf.mjs";
5
5
  import "../std__assert-BBjXFNOb.mjs";
6
6
  import { t as assertThrows } from "../assert_throws-BOkhLGYc.mjs";
7
- import { t as nodeInfoToJson } from "../types-J53Kw7so.mjs";
7
+ import { t as nodeInfoToJson } from "../types-s4mCTGkT.mjs";
8
8
  import { test } from "@fedify/fixture";
9
9
  //#region src/nodeinfo/types.test.ts
10
10
  test("nodeInfoToJson()", () => {
@@ -3,7 +3,7 @@ import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
4
  import { t as assertEquals } from "../assert_equals-C-ZRDbaf.mjs";
5
5
  import "../std__assert-BBjXFNOb.mjs";
6
- import { t as MemoryKvStore } from "../kv-rV3vodCc.mjs";
6
+ import { t as MemoryKvStore } from "../kv-CX8qfaad.mjs";
7
7
  import { getLogger } from "@logtape/logtape";
8
8
  import { ExportResultCode } from "@opentelemetry/core";
9
9
  import { SpanKind, SpanStatusCode, TraceFlags } from "@opentelemetry/api";
@@ -1,7 +1,7 @@
1
1
  import "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
- import { n as preloadedOnlyDocumentLoader, t as normalizePublicAudience } from "./public-audience-DZJAUUi8.mjs";
4
+ import { n as preloadedOnlyDocumentLoader, t as normalizePublicAudience } from "./public-audience-jwoSIGUw.mjs";
5
5
  import { getLogger } from "@logtape/logtape";
6
6
  import { preloadedContexts } from "@fedify/vocab-runtime";
7
7
  import jsonld from "@fedify/vocab-runtime/jsonld";
@@ -1,7 +1,7 @@
1
1
  /// <reference lib="esnext.temporal" />
2
2
  import { Activity, Actor, CryptographicKey } from "@fedify/vocab";
3
- import { TracerProvider } from "@opentelemetry/api";
4
3
  import { DocumentLoader } from "@fedify/vocab-runtime";
4
+ import { TracerProvider } from "@opentelemetry/api";
5
5
 
6
6
  //#region src/sig/owner.d.ts
7
7
  /**
@@ -26,6 +26,13 @@ interface DoesActorOwnKeyOptions {
26
26
  }
27
27
  /**
28
28
  * Checks if the actor of the given activity owns the specified key.
29
+ *
30
+ * Ownership is never taken from what the activity or the key say about
31
+ * themselves, as both are written by whoever sent the activity. It is
32
+ * established from a document that the owner itself serves: either the key's
33
+ * claimed owner links back to the key, or the actor the activity claims lists
34
+ * the key as its own.
35
+ *
29
36
  * @param activity The activity to check.
30
37
  * @param key The public key to check.
31
38
  * @param options Options for checking the key ownership.
@@ -56,6 +63,10 @@ interface GetKeyOwnerOptions {
56
63
  * Gets the actor that owns the specified key. Returns `null` if the key has no
57
64
  * known owner.
58
65
  *
66
+ * The owner is only returned when the key and the owner link to each other:
67
+ * a document that merely claims to own a key is not enough, as the claim and
68
+ * the key can come from the same host.
69
+ *
59
70
  * @param keyId The ID of the key to check, or the key itself.
60
71
  * @param options Options for getting the key owner.
61
72
  * @returns The actor that owns the key, or `null` if the key has no known
@@ -0,0 +1,175 @@
1
+ import "@js-temporal/polyfill";
2
+ import "urlpattern-polyfill";
3
+ globalThis.addEventListener = () => {};
4
+ import { n as version, t as name } from "./deno--DCBC7WG.mjs";
5
+ import { c as verifyKeyOwnership, n as fetchActorDocument, t as exportJwk } from "./key-C-7sqo8D.mjs";
6
+ import { getLogger } from "@logtape/logtape";
7
+ import { CryptographicKey, Object as Object$1, isActor } from "@fedify/vocab";
8
+ import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
9
+ import { getDocumentLoader } from "@fedify/vocab-runtime";
10
+ //#region src/sig/owner.ts
11
+ const logger = getLogger([
12
+ "fedify",
13
+ "sig",
14
+ "owner"
15
+ ]);
16
+ /**
17
+ * Checks if the actor of the given activity owns the specified key.
18
+ *
19
+ * Ownership is never taken from what the activity or the key say about
20
+ * themselves, as both are written by whoever sent the activity. It is
21
+ * established from a document that the owner itself serves: either the key's
22
+ * claimed owner links back to the key, or the actor the activity claims lists
23
+ * the key as its own.
24
+ *
25
+ * @param activity The activity to check.
26
+ * @param key The public key to check.
27
+ * @param options Options for checking the key ownership.
28
+ * @returns Whether the actor is the owner of the key.
29
+ */
30
+ async function doesActorOwnKey(activity, key, options) {
31
+ return await (options.tracerProvider ?? trace.getTracerProvider()).getTracer(name, version).startActiveSpan("activitypub.verify_key_ownership", {
32
+ kind: SpanKind.INTERNAL,
33
+ attributes: {
34
+ "activitypub.actor.id": activity.actorId?.href ?? "",
35
+ "activitypub.key.id": key.id?.href ?? ""
36
+ }
37
+ }, async (span) => {
38
+ try {
39
+ const actorId = activity.actorId;
40
+ if (actorId == null) {
41
+ span.setAttribute("activitypub.key_ownership.verified", false);
42
+ span.setAttribute("activitypub.key_ownership.method", "none");
43
+ return false;
44
+ }
45
+ if (key.ownerId != null) {
46
+ const owner = await verifyKeyOwnership(key, options);
47
+ if (owner?.id != null && owner.id.href === actorId.href) {
48
+ span.setAttribute("activitypub.key_ownership.verified", true);
49
+ span.setAttribute("activitypub.key_ownership.method", "key_owner");
50
+ return true;
51
+ }
52
+ }
53
+ const actor = await fetchActorDocument(actorId, options);
54
+ if (actor != null && await doesActorListKey(actor, key, options)) {
55
+ span.setAttribute("activitypub.key_ownership.verified", true);
56
+ span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
57
+ return true;
58
+ }
59
+ logger.debug("The actor {actorId} does not own key {keyId}: neither the owner the key claims nor the actor's own document links the two to each other.", {
60
+ actorId: actorId.href,
61
+ keyId: key.id?.href
62
+ });
63
+ span.setAttribute("activitypub.key_ownership.verified", false);
64
+ span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
65
+ return false;
66
+ } catch (error) {
67
+ span.recordException(error);
68
+ span.setStatus({
69
+ code: SpanStatusCode.ERROR,
70
+ message: String(error)
71
+ });
72
+ throw error;
73
+ } finally {
74
+ span.end();
75
+ }
76
+ });
77
+ }
78
+ /**
79
+ * Determines whether the given actor's own document lists the given key among
80
+ * its public keys.
81
+ *
82
+ * Keys are matched by id, which is what an actor document links to. A key
83
+ * document may leave its id implicit, though, and then there is no id for the
84
+ * actor to link to; the key material itself is the only thing the two can
85
+ * have in common, so that is what gets compared.
86
+ */
87
+ async function doesActorListKey(actor, key, options) {
88
+ if (key.id != null) {
89
+ for (const publicKeyId of actor.publicKeyIds) if (publicKeyId.href === key.id.href) return true;
90
+ return false;
91
+ }
92
+ if (key.publicKey == null) return false;
93
+ const publicKeys = actor.getPublicKeys({
94
+ ...options,
95
+ suppressError: true
96
+ });
97
+ for await (const publicKey of publicKeys) {
98
+ if (publicKey.publicKey == null) continue;
99
+ if (await isSamePublicKey(publicKey.publicKey, key.publicKey)) return true;
100
+ }
101
+ return false;
102
+ }
103
+ /**
104
+ * Compares two public keys by their key material, ignoring the metadata that
105
+ * surrounds it.
106
+ */
107
+ async function isSamePublicKey(a, b) {
108
+ const [x, y] = await Promise.all([exportJwk(a), exportJwk(b)]);
109
+ return x.kty === y.kty && x.crv === y.crv && x.n === y.n && x.e === y.e && x.x === y.x && x.y === y.y;
110
+ }
111
+ /**
112
+ * Gets the actor that owns the specified key. Returns `null` if the key has no
113
+ * known owner.
114
+ *
115
+ * The owner is only returned when the key and the owner link to each other:
116
+ * a document that merely claims to own a key is not enough, as the claim and
117
+ * the key can come from the same host.
118
+ *
119
+ * @param keyId The ID of the key to check, or the key itself.
120
+ * @param options Options for getting the key owner.
121
+ * @returns The actor that owns the key, or `null` if the key has no known
122
+ * owner.
123
+ * @since 0.7.0
124
+ */
125
+ async function getKeyOwner(keyId, options) {
126
+ const tracerProvider = options.tracerProvider ?? trace.getTracerProvider();
127
+ const documentLoader = options.documentLoader ?? getDocumentLoader();
128
+ const fetchOptions = {
129
+ documentLoader,
130
+ contextLoader: options.contextLoader ?? getDocumentLoader(),
131
+ tracerProvider
132
+ };
133
+ if (keyId instanceof CryptographicKey) return await verifyKeyOwnership(keyId, fetchOptions);
134
+ let keyDoc;
135
+ let documentUrl = keyId;
136
+ try {
137
+ const remoteDocument = await documentLoader(keyId.href);
138
+ keyDoc = remoteDocument.document;
139
+ documentUrl = new URL(remoteDocument.documentUrl ?? "", keyId);
140
+ } catch (_) {
141
+ return null;
142
+ }
143
+ let object;
144
+ try {
145
+ object = await Object$1.fromJsonLd(keyDoc, {
146
+ ...fetchOptions,
147
+ baseUrl: documentUrl
148
+ });
149
+ } catch (e) {
150
+ if (!(e instanceof TypeError)) throw e;
151
+ try {
152
+ object = await CryptographicKey.fromJsonLd(keyDoc, fetchOptions);
153
+ } catch (e) {
154
+ if (e instanceof TypeError) return null;
155
+ throw e;
156
+ }
157
+ }
158
+ if (object instanceof CryptographicKey) {
159
+ if (object.id != null && object.id.href !== keyId.href) return null;
160
+ return await verifyKeyOwnership(object.id == null ? object.clone({ id: keyId }) : object, fetchOptions);
161
+ }
162
+ if (!isActor(object)) return null;
163
+ if (object.id == null || object.id.origin !== documentUrl.origin) {
164
+ logger.debug("The document served at {documentUrl} claims to be the actor {actorId}, which belongs to another origin; refusing to treat it as the owner of key {keyId}.", {
165
+ documentUrl: documentUrl.href,
166
+ actorId: object.id?.href,
167
+ keyId: keyId.href
168
+ });
169
+ return null;
170
+ }
171
+ for (const kid of object.publicKeyIds) if (kid.href === keyId.href) return object;
172
+ return null;
173
+ }
174
+ //#endregion
175
+ export { getKeyOwner as n, doesActorOwnKey as t };
@@ -1,7 +1,7 @@
1
1
  /// <reference lib="esnext.temporal" />
2
2
  import { Activity, Actor, CryptographicKey } from "@fedify/vocab";
3
- import { DocumentLoader } from "@fedify/vocab-runtime";
4
3
  import { TracerProvider } from "@opentelemetry/api";
4
+ import { DocumentLoader } from "@fedify/vocab-runtime";
5
5
 
6
6
  //#region src/sig/owner.d.ts
7
7
  /**
@@ -26,6 +26,13 @@ interface DoesActorOwnKeyOptions {
26
26
  }
27
27
  /**
28
28
  * Checks if the actor of the given activity owns the specified key.
29
+ *
30
+ * Ownership is never taken from what the activity or the key say about
31
+ * themselves, as both are written by whoever sent the activity. It is
32
+ * established from a document that the owner itself serves: either the key's
33
+ * claimed owner links back to the key, or the actor the activity claims lists
34
+ * the key as its own.
35
+ *
29
36
  * @param activity The activity to check.
30
37
  * @param key The public key to check.
31
38
  * @param options Options for checking the key ownership.
@@ -56,6 +63,10 @@ interface GetKeyOwnerOptions {
56
63
  * Gets the actor that owns the specified key. Returns `null` if the key has no
57
64
  * known owner.
58
65
  *
66
+ * The owner is only returned when the key and the owner link to each other:
67
+ * a document that merely claims to own a key is not enough, as the claim and
68
+ * the key can come from the same host.
69
+ *
59
70
  * @param keyId The ID of the key to check, or the key itself.
60
71
  * @param options Options for getting the key owner.
61
72
  * @returns The actor that owns the key, or `null` if the key has no known
@@ -1,11 +1,11 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
2
  import "urlpattern-polyfill";
3
3
  globalThis.addEventListener = () => {};
4
- import { n as version, t as name } from "./deno-KC-uf6Un.mjs";
5
- import { n as getDurationMs, r as getFederationMetrics, s as measureSignatureKeyFetch } from "./metrics-D8s1eKfJ.mjs";
6
- import { n as fetchKey, o as validateCryptoKey } from "./key-Cbkqgt-G.mjs";
7
- import { n as preloadedOnlyDocumentLoader } from "./public-audience-DZJAUUi8.mjs";
8
- import { r as normalizeOutgoingActivityJsonLd } from "./outgoing-jsonld-OFoubetZ.mjs";
4
+ import { n as version, t as name } from "./deno--DCBC7WG.mjs";
5
+ import { n as getDurationMs, r as getFederationMetrics, s as measureSignatureKeyFetch } from "./metrics-FDcYXRP5.mjs";
6
+ import { r as fetchKey, s as validateCryptoKey } from "./key-C-7sqo8D.mjs";
7
+ import { n as preloadedOnlyDocumentLoader } from "./public-audience-jwoSIGUw.mjs";
8
+ import { r as normalizeOutgoingActivityJsonLd } from "./outgoing-jsonld-HOAywiid.mjs";
9
9
  import { getLogger } from "@logtape/logtape";
10
10
  import { Activity, DataIntegrityProof, Multikey, getTypeId } from "@fedify/vocab";
11
11
  import { SpanStatusCode, trace } from "@opentelemetry/api";
@@ -1,7 +1,7 @@
1
1
  const { Temporal } = require("@js-temporal/polyfill");
2
2
  const { URLPattern } = require("urlpattern-polyfill");
3
3
  const require_chunk = require("./chunk-DDcVe30Y.cjs");
4
- const require_http = require("./http-DL5ZJBC4.cjs");
4
+ const require_http = require("./http-ChVf88XZ.cjs");
5
5
  let _logtape_logtape = require("@logtape/logtape");
6
6
  let _fedify_vocab = require("@fedify/vocab");
7
7
  let _opentelemetry_api = require("@opentelemetry/api");
@@ -13,7 +13,7 @@ _fedify_vocab_runtime_jsonld = require_chunk.__toESM(_fedify_vocab_runtime_jsonl
13
13
  let json_canon = require("json-canon");
14
14
  json_canon = require_chunk.__toESM(json_canon, 1);
15
15
  //#region src/sig/ld.ts
16
- const logger$3 = (0, _logtape_logtape.getLogger)([
16
+ const logger$4 = (0, _logtape_logtape.getLogger)([
17
17
  "fedify",
18
18
  "sig",
19
19
  "ld"
@@ -456,7 +456,7 @@ async function verifySignature(jsonLd, options = {}) {
456
456
  try {
457
457
  signature = (0, byte_encodings_base64.decodeBase64)(sig.signatureValue);
458
458
  } catch (error) {
459
- logger$3.debug("Failed to verify; invalid base64 signatureValue: {signatureValue}", {
459
+ logger$4.debug("Failed to verify; invalid base64 signatureValue: {signatureValue}", {
460
460
  ...sig,
461
461
  error
462
462
  });
@@ -475,7 +475,7 @@ async function verifySignature(jsonLd, options = {}) {
475
475
  try {
476
476
  sigOptsHash = await hashJsonLd(sigOpts, options.contextLoader);
477
477
  } catch (error) {
478
- logger$3.warn("Failed to verify; failed to hash the signature options: {signatureOptions}\n{error}", {
478
+ logger$4.warn("Failed to verify; failed to hash the signature options: {signatureOptions}\n{error}", {
479
479
  signatureOptions: sigOpts,
480
480
  error
481
481
  });
@@ -487,7 +487,7 @@ async function verifySignature(jsonLd, options = {}) {
487
487
  try {
488
488
  docHash = await hashJsonLd(document, options.contextLoader);
489
489
  } catch (error) {
490
- logger$3.warn("Failed to verify; failed to hash the document: {document}\n{error}", {
490
+ logger$4.warn("Failed to verify; failed to hash the document: {document}\n{error}", {
491
491
  document,
492
492
  error
493
493
  });
@@ -498,7 +498,7 @@ async function verifySignature(jsonLd, options = {}) {
498
498
  const messageBytes = encoder.encode(message);
499
499
  if (await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key.publicKey, signature.slice(), messageBytes)) return key;
500
500
  if (cached) {
501
- logger$3.debug("Failed to verify with the cached key {keyId}; signature {signatureValue} is invalid. Retrying with the freshly fetched key...", {
501
+ logger$4.debug("Failed to verify with the cached key {keyId}; signature {signatureValue} is invalid. Retrying with the freshly fetched key...", {
502
502
  keyId: sig.creator,
503
503
  ...sig
504
504
  });
@@ -512,7 +512,7 @@ async function verifySignature(jsonLd, options = {}) {
512
512
  if (key == null) return null;
513
513
  return await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key.publicKey, signature.slice(), messageBytes) ? key : null;
514
514
  }
515
- logger$3.debug("Failed to verify with the fetched key {keyId}; signature {signatureValue} is invalid. Check if the key is correct or if the signed message is correct. The message to sign is:\n{message}", {
515
+ logger$4.debug("Failed to verify with the fetched key {keyId}; signature {signatureValue} is invalid. Check if the key is correct or if the signed message is correct. The message to sign is:\n{message}", {
516
516
  keyId: sig.creator,
517
517
  ...sig,
518
518
  message
@@ -591,12 +591,12 @@ async function verifyJsonLdInternal(jsonLd, options, compact) {
591
591
  const key = await verifySignature(compacted, verificationOptions);
592
592
  if (key == null) return false;
593
593
  if (key.ownerId == null) {
594
- logger$3.debug("Key {keyId} has no owner.", { keyId: key.id?.href });
594
+ logger$4.debug("Key {keyId} has no owner.", { keyId: key.id?.href });
595
595
  return false;
596
596
  }
597
597
  attributions.delete(key.ownerId.href);
598
598
  if (attributions.size > 0) {
599
- logger$3.debug("Some attributions are not authenticated by the Linked Data Signatures: {attributions}.", { attributions: [...attributions] });
599
+ logger$4.debug("Some attributions are not authenticated by the Linked Data Signatures: {attributions}.", { attributions: [...attributions] });
600
600
  return false;
601
601
  }
602
602
  verified = true;
@@ -625,8 +625,20 @@ async function hashJsonLd(jsonLd, contextLoader) {
625
625
  }
626
626
  //#endregion
627
627
  //#region src/sig/owner.ts
628
+ const logger$3 = (0, _logtape_logtape.getLogger)([
629
+ "fedify",
630
+ "sig",
631
+ "owner"
632
+ ]);
628
633
  /**
629
634
  * Checks if the actor of the given activity owns the specified key.
635
+ *
636
+ * Ownership is never taken from what the activity or the key say about
637
+ * themselves, as both are written by whoever sent the activity. It is
638
+ * established from a document that the owner itself serves: either the key's
639
+ * claimed owner links back to the key, or the actor the activity claims lists
640
+ * the key as its own.
641
+ *
630
642
  * @param activity The activity to check.
631
643
  * @param key The public key to check.
632
644
  * @param options Options for checking the key ownership.
@@ -641,23 +653,30 @@ async function doesActorOwnKey(activity, key, options) {
641
653
  }
642
654
  }, async (span) => {
643
655
  try {
644
- if (key.ownerId != null) {
645
- const owns = key.ownerId.href === activity.actorId?.href;
646
- span.setAttribute("activitypub.key_ownership.verified", owns);
647
- span.setAttribute("activitypub.key_ownership.method", "owner_id");
648
- return owns;
649
- }
650
- const actor = await activity.getActor(options);
651
- if (actor == null || !(0, _fedify_vocab.isActor)(actor)) {
656
+ const actorId = activity.actorId;
657
+ if (actorId == null) {
652
658
  span.setAttribute("activitypub.key_ownership.verified", false);
653
- span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
659
+ span.setAttribute("activitypub.key_ownership.method", "none");
654
660
  return false;
655
661
  }
656
- for (const publicKeyId of actor.publicKeyIds) if (key.id != null && publicKeyId.href === key.id.href) {
662
+ if (key.ownerId != null) {
663
+ const owner = await require_http.verifyKeyOwnership(key, options);
664
+ if (owner?.id != null && owner.id.href === actorId.href) {
665
+ span.setAttribute("activitypub.key_ownership.verified", true);
666
+ span.setAttribute("activitypub.key_ownership.method", "key_owner");
667
+ return true;
668
+ }
669
+ }
670
+ const actor = await require_http.fetchActorDocument(actorId, options);
671
+ if (actor != null && await doesActorListKey(actor, key, options)) {
657
672
  span.setAttribute("activitypub.key_ownership.verified", true);
658
673
  span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
659
674
  return true;
660
675
  }
676
+ logger$3.debug("The actor {actorId} does not own key {keyId}: neither the owner the key claims nor the actor's own document links the two to each other.", {
677
+ actorId: actorId.href,
678
+ keyId: key.id?.href
679
+ });
661
680
  span.setAttribute("activitypub.key_ownership.verified", false);
662
681
  span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
663
682
  return false;
@@ -674,9 +693,46 @@ async function doesActorOwnKey(activity, key, options) {
674
693
  });
675
694
  }
676
695
  /**
696
+ * Determines whether the given actor's own document lists the given key among
697
+ * its public keys.
698
+ *
699
+ * Keys are matched by id, which is what an actor document links to. A key
700
+ * document may leave its id implicit, though, and then there is no id for the
701
+ * actor to link to; the key material itself is the only thing the two can
702
+ * have in common, so that is what gets compared.
703
+ */
704
+ async function doesActorListKey(actor, key, options) {
705
+ if (key.id != null) {
706
+ for (const publicKeyId of actor.publicKeyIds) if (publicKeyId.href === key.id.href) return true;
707
+ return false;
708
+ }
709
+ if (key.publicKey == null) return false;
710
+ const publicKeys = actor.getPublicKeys({
711
+ ...options,
712
+ suppressError: true
713
+ });
714
+ for await (const publicKey of publicKeys) {
715
+ if (publicKey.publicKey == null) continue;
716
+ if (await isSamePublicKey(publicKey.publicKey, key.publicKey)) return true;
717
+ }
718
+ return false;
719
+ }
720
+ /**
721
+ * Compares two public keys by their key material, ignoring the metadata that
722
+ * surrounds it.
723
+ */
724
+ async function isSamePublicKey(a, b) {
725
+ const [x, y] = await Promise.all([require_http.exportJwk(a), require_http.exportJwk(b)]);
726
+ return x.kty === y.kty && x.crv === y.crv && x.n === y.n && x.e === y.e && x.x === y.x && x.y === y.y;
727
+ }
728
+ /**
677
729
  * Gets the actor that owns the specified key. Returns `null` if the key has no
678
730
  * known owner.
679
731
  *
732
+ * The owner is only returned when the key and the owner link to each other:
733
+ * a document that merely claims to own a key is not enough, as the claim and
734
+ * the key can come from the same host.
735
+ *
680
736
  * @param keyId The ID of the key to check, or the key itself.
681
737
  * @param options Options for getting the key owner.
682
738
  * @returns The actor that owns the key, or `null` if the key has no known
@@ -686,52 +742,50 @@ async function doesActorOwnKey(activity, key, options) {
686
742
  async function getKeyOwner(keyId, options) {
687
743
  const tracerProvider = options.tracerProvider ?? _opentelemetry_api.trace.getTracerProvider();
688
744
  const documentLoader = options.documentLoader ?? (0, _fedify_vocab_runtime.getDocumentLoader)();
689
- const contextLoader = options.contextLoader ?? (0, _fedify_vocab_runtime.getDocumentLoader)();
745
+ const fetchOptions = {
746
+ documentLoader,
747
+ contextLoader: options.contextLoader ?? (0, _fedify_vocab_runtime.getDocumentLoader)(),
748
+ tracerProvider
749
+ };
750
+ if (keyId instanceof _fedify_vocab.CryptographicKey) return await require_http.verifyKeyOwnership(keyId, fetchOptions);
751
+ let keyDoc;
752
+ let documentUrl = keyId;
753
+ try {
754
+ const remoteDocument = await documentLoader(keyId.href);
755
+ keyDoc = remoteDocument.document;
756
+ documentUrl = new URL(remoteDocument.documentUrl ?? "", keyId);
757
+ } catch (_) {
758
+ return null;
759
+ }
690
760
  let object;
691
- if (keyId instanceof _fedify_vocab.CryptographicKey) {
692
- object = keyId;
693
- if (object.id == null) return null;
694
- keyId = object.id;
695
- } else {
696
- let keyDoc;
697
- try {
698
- const { document } = await documentLoader(keyId.href);
699
- keyDoc = document;
700
- } catch (_) {
701
- return null;
702
- }
761
+ try {
762
+ object = await _fedify_vocab.Object.fromJsonLd(keyDoc, {
763
+ ...fetchOptions,
764
+ baseUrl: documentUrl
765
+ });
766
+ } catch (e) {
767
+ if (!(e instanceof TypeError)) throw e;
703
768
  try {
704
- object = await _fedify_vocab.Object.fromJsonLd(keyDoc, {
705
- documentLoader,
706
- contextLoader,
707
- tracerProvider
708
- });
769
+ object = await _fedify_vocab.CryptographicKey.fromJsonLd(keyDoc, fetchOptions);
709
770
  } catch (e) {
710
- if (!(e instanceof TypeError)) throw e;
711
- try {
712
- object = await _fedify_vocab.CryptographicKey.fromJsonLd(keyDoc, {
713
- documentLoader,
714
- contextLoader,
715
- tracerProvider
716
- });
717
- } catch (e) {
718
- if (e instanceof TypeError) return null;
719
- throw e;
720
- }
771
+ if (e instanceof TypeError) return null;
772
+ throw e;
721
773
  }
722
774
  }
723
- let owner = null;
724
775
  if (object instanceof _fedify_vocab.CryptographicKey) {
725
- if (object.ownerId == null) return null;
726
- owner = await object.getOwner({
727
- documentLoader,
728
- contextLoader,
729
- tracerProvider
776
+ if (object.id != null && object.id.href !== keyId.href) return null;
777
+ return await require_http.verifyKeyOwnership(object.id == null ? object.clone({ id: keyId }) : object, fetchOptions);
778
+ }
779
+ if (!(0, _fedify_vocab.isActor)(object)) return null;
780
+ if (object.id == null || object.id.origin !== documentUrl.origin) {
781
+ logger$3.debug("The document served at {documentUrl} claims to be the actor {actorId}, which belongs to another origin; refusing to treat it as the owner of key {keyId}.", {
782
+ documentUrl: documentUrl.href,
783
+ actorId: object.id?.href,
784
+ keyId: keyId.href
730
785
  });
731
- } else if ((0, _fedify_vocab.isActor)(object)) owner = object;
732
- else return null;
733
- if (owner == null) return null;
734
- for (const kid of owner.publicKeyIds) if (kid.href === keyId.href) return owner;
786
+ return null;
787
+ }
788
+ for (const kid of object.publicKeyIds) if (kid.href === keyId.href) return object;
735
789
  return null;
736
790
  }
737
791
  //#endregion