@fedify/fedify 2.2.12 → 2.2.14-dev.2082

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 (102) hide show
  1. package/dist/{assert_instance_of-C4Ri6VuN.mjs → assert-BhQd8QHP.mjs} +19 -1
  2. package/dist/{builder-TJzcpmDm.mjs → builder-Cwgo1nK5.mjs} +3 -3
  3. package/dist/{client-dx126nMS.mjs → client-CQ7zd8tW.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-BU-1O90h.d.ts → context-Bt2jnbC7.d.ts} +4 -3
  10. package/dist/{context-DVA8wHZ0.d.cts → context-Cls_M1HU.d.cts} +4 -3
  11. package/dist/{deno-DeYUdFal.mjs → deno-CV4wsQzy.mjs} +1 -1
  12. package/dist/{docloader-CXH2XqYP.mjs → docloader-DV9F08Dn.mjs} +2 -2
  13. package/dist/federation/builder.test.mjs +2 -2
  14. package/dist/federation/collection.test.mjs +1 -1
  15. package/dist/federation/handler.test.mjs +127 -10
  16. package/dist/federation/idempotency.test.mjs +4 -4
  17. package/dist/federation/inbox.test.mjs +1 -1
  18. package/dist/federation/keycache.test.mjs +44 -10
  19. package/dist/federation/kv.test.mjs +1 -1
  20. package/dist/federation/middleware.test.mjs +103 -15
  21. package/dist/federation/mod.cjs +1 -1
  22. package/dist/federation/mod.d.cts +2 -2
  23. package/dist/federation/mod.d.ts +2 -2
  24. package/dist/federation/mod.js +1 -1
  25. package/dist/federation/mq.test.mjs +1 -1
  26. package/dist/federation/negotiation.test.mjs +2 -2
  27. package/dist/federation/retry.test.mjs +1 -1
  28. package/dist/federation/router.test.mjs +1 -1
  29. package/dist/federation/send.test.mjs +138 -6
  30. package/dist/federation/temporal.test.mjs +2 -2
  31. package/dist/federation/webfinger.test.mjs +2 -2
  32. package/dist/{http-D6cIKrI1.cjs → http-BWNuy1sb.cjs} +166 -6
  33. package/dist/{http-CNsxTbVr.js → http-C5T-lWqB.js} +155 -7
  34. package/dist/{http-79lZeYEo.mjs → http-C9MzFrOj.mjs} +3 -3
  35. package/dist/{http-BDZeS5om.d.ts → http-Dl00HXr4.d.ts} +1 -1
  36. package/dist/{http-C87EWkO0.d.cts → http-DqZhyEso.d.cts} +1 -1
  37. package/dist/{key-B4kUuy3o.mjs → key-DiziIx9e.mjs} +154 -6
  38. package/dist/{keycache-EGATflN-.mjs → keycache-C1aVprwy.mjs} +12 -4
  39. package/dist/{kv-cache-CIZ1m-94.js → kv-cache-CM-zJoep.js} +1 -1
  40. package/dist/{kv-cache-BtXEUvHT.cjs → kv-cache-DGPEHc1T.cjs} +1 -1
  41. package/dist/{ld-2R9vZ_ym.mjs → ld-BAeuDKjx.mjs} +2 -2
  42. package/dist/{middleware-CjrIRAsQ.cjs → middleware-BbbKc-Tr.cjs} +90 -14
  43. package/dist/{middleware-D5NE6PkU.mjs → middleware-CkJOjZjS.mjs} +31 -20
  44. package/dist/{middleware-_qt3kHUU.mjs → middleware-DA2CvTQL.mjs} +1 -1
  45. package/dist/{middleware-DsEAPFhi.js → middleware-J32whqNP.js} +90 -14
  46. package/dist/{middleware-2bSnAJOQ.cjs → middleware-XNvq3NUZ.cjs} +1 -1
  47. package/dist/{mod-BhU_H1I_.d.ts → mod-0MWo2Nu5.d.ts} +1 -1
  48. package/dist/{mod-DXY9JF28.d.cts → mod-D2JyWtW3.d.cts} +1 -1
  49. package/dist/{mod-q-NFLW6B.d.cts → mod-DNvnDTS7.d.cts} +1 -1
  50. package/dist/{mod-BVt6iTmH.d.ts → mod-DaiJDJdQ.d.ts} +1 -1
  51. package/dist/{mod-B0rWmfW5.d.cts → mod-DzPMtIV3.d.cts} +1 -1
  52. package/dist/{mod-DHO9lk3D.d.ts → mod-ej_rLx8h.d.ts} +1 -1
  53. package/dist/mod.cjs +5 -5
  54. package/dist/mod.d.cts +6 -6
  55. package/dist/mod.d.ts +6 -6
  56. package/dist/mod.js +5 -5
  57. package/dist/nodeinfo/client.test.mjs +28 -1
  58. package/dist/nodeinfo/handler.test.mjs +2 -2
  59. package/dist/nodeinfo/mod.cjs +1 -1
  60. package/dist/nodeinfo/mod.js +1 -1
  61. package/dist/nodeinfo/types.test.mjs +1 -1
  62. package/dist/otel/exporter.test.mjs +1 -1
  63. package/dist/{outgoing-jsonld-CfUItkGW.mjs → outgoing-jsonld-Dc2OXyB-.mjs} +1 -1
  64. package/dist/{owner-CnngXDNJ.d.ts → owner-B3hNQCG8.d.cts} +12 -1
  65. package/dist/{owner-DEvZuyOE.d.cts → owner-CiBPcC6F.d.ts} +12 -1
  66. package/dist/owner-D08_JUSb.mjs +175 -0
  67. package/dist/{proof-CmtXtaul.cjs → proof-7T47tIu5.cjs} +112 -58
  68. package/dist/{proof-BhW_iARR.js → proof-BdRnLnh7.js} +112 -58
  69. package/dist/{proof-BzTt6nlk.mjs → proof-_S-wFUFM.mjs} +4 -4
  70. package/dist/{send-CZwOs7XD.mjs → send-DmcKKipr.mjs} +64 -6
  71. package/dist/sig/accept.test.mjs +1 -1
  72. package/dist/sig/http.test.mjs +4 -4
  73. package/dist/sig/key.test.mjs +170 -3
  74. package/dist/sig/ld.test.mjs +56 -4
  75. package/dist/sig/mod.cjs +2 -2
  76. package/dist/sig/mod.d.cts +3 -3
  77. package/dist/sig/mod.d.ts +3 -3
  78. package/dist/sig/mod.js +2 -2
  79. package/dist/sig/owner.test.mjs +105 -6
  80. package/dist/sig/proof.test.mjs +73 -5
  81. package/dist/{temporal-BYCnXIGk.mjs → temporal-CX_L28b-.mjs} +1 -1
  82. package/dist/{types-m8u9L86X.cjs → types-DWs-qZaY.cjs} +94 -2
  83. package/dist/{types-DTONwNqL.js → types-sXITXcWa.js} +78 -4
  84. package/dist/utils/docloader.test.mjs +28 -4
  85. package/dist/utils/kv-cache.test.mjs +1 -1
  86. package/dist/utils/mod.cjs +1 -1
  87. package/dist/utils/mod.d.cts +1 -1
  88. package/dist/utils/mod.d.ts +1 -1
  89. package/dist/utils/mod.js +1 -1
  90. package/package.json +5 -5
  91. package/dist/assert-DikXweDx.mjs +0 -23
  92. package/dist/owner-CqYsVXcd.mjs +0 -120
  93. /package/dist/{accept-CPkZzmGN.mjs → accept-Dd__NiUL.mjs} +0 -0
  94. /package/dist/{activity-listener-ell7W1s9.mjs → activity-listener-IojJ9SVl.mjs} +0 -0
  95. /package/dist/{collection-D-HqUuA2.mjs → collection-DnoYNwBC.mjs} +0 -0
  96. /package/dist/{keys-DGu1NFwu.mjs → keys-BAK-tUlf.mjs} +0 -0
  97. /package/dist/{kv-rV3vodCc.mjs → kv-CX8qfaad.mjs} +0 -0
  98. /package/dist/{kv-cache-DYsF2MhP.mjs → kv-cache-FsT1ljhs.mjs} +0 -0
  99. /package/dist/{negotiation-SQvQgUqe.mjs → negotiation-BHtZN6Y1.mjs} +0 -0
  100. /package/dist/{public-audience-Ci-r-4Q-.mjs → public-audience-CS1udGkN.mjs} +0 -0
  101. /package/dist/{retry-v_sGLH1d.mjs → retry-bMXBL97A.mjs} +0 -0
  102. /package/dist/{types-J53Kw7so.mjs → types-s4mCTGkT.mjs} +0 -0
@@ -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-CV4wsQzy.mjs";
5
+ import { c as verifyKeyOwnership, n as fetchActorDocument, t as exportJwk } from "./key-DiziIx9e.mjs";
6
+ import { CryptographicKey, Object as Object$1, isActor } from "@fedify/vocab";
7
+ import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
8
+ import { getDocumentLoader } from "@fedify/vocab-runtime";
9
+ import { getLogger } from "@logtape/logtape";
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
  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-D6cIKrI1.cjs");
4
+ const require_http = require("./http-BWNuy1sb.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);
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"
@@ -443,7 +443,7 @@ async function verifySignature(jsonLd, options = {}) {
443
443
  try {
444
444
  signature = (0, byte_encodings_base64.decodeBase64)(sig.signatureValue);
445
445
  } catch (error) {
446
- logger$3.debug("Failed to verify; invalid base64 signatureValue: {signatureValue}", {
446
+ logger$4.debug("Failed to verify; invalid base64 signatureValue: {signatureValue}", {
447
447
  ...sig,
448
448
  error
449
449
  });
@@ -462,7 +462,7 @@ async function verifySignature(jsonLd, options = {}) {
462
462
  try {
463
463
  sigOptsHash = await hashJsonLd(sigOpts, options.contextLoader);
464
464
  } catch (error) {
465
- logger$3.warn("Failed to verify; failed to hash the signature options: {signatureOptions}\n{error}", {
465
+ logger$4.warn("Failed to verify; failed to hash the signature options: {signatureOptions}\n{error}", {
466
466
  signatureOptions: sigOpts,
467
467
  error
468
468
  });
@@ -474,7 +474,7 @@ async function verifySignature(jsonLd, options = {}) {
474
474
  try {
475
475
  docHash = await hashJsonLd(document, options.contextLoader);
476
476
  } catch (error) {
477
- logger$3.warn("Failed to verify; failed to hash the document: {document}\n{error}", {
477
+ logger$4.warn("Failed to verify; failed to hash the document: {document}\n{error}", {
478
478
  document,
479
479
  error
480
480
  });
@@ -485,7 +485,7 @@ async function verifySignature(jsonLd, options = {}) {
485
485
  const messageBytes = encoder.encode(message);
486
486
  if (await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key.publicKey, signature.slice(), messageBytes)) return key;
487
487
  if (cached) {
488
- logger$3.debug("Failed to verify with the cached key {keyId}; signature {signatureValue} is invalid. Retrying with the freshly fetched key...", {
488
+ logger$4.debug("Failed to verify with the cached key {keyId}; signature {signatureValue} is invalid. Retrying with the freshly fetched key...", {
489
489
  keyId: sig.creator,
490
490
  ...sig
491
491
  });
@@ -499,7 +499,7 @@ async function verifySignature(jsonLd, options = {}) {
499
499
  if (key == null) return null;
500
500
  return await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key.publicKey, signature.slice(), messageBytes) ? key : null;
501
501
  }
502
- 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}", {
502
+ 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}", {
503
503
  keyId: sig.creator,
504
504
  ...sig,
505
505
  message
@@ -543,12 +543,12 @@ async function verifyJsonLdInternal(jsonLd, options, compact) {
543
543
  const key = await verifySignature(compacted, verificationOptions);
544
544
  if (key == null) return false;
545
545
  if (key.ownerId == null) {
546
- logger$3.debug("Key {keyId} has no owner.", { keyId: key.id?.href });
546
+ logger$4.debug("Key {keyId} has no owner.", { keyId: key.id?.href });
547
547
  return false;
548
548
  }
549
549
  attributions.delete(key.ownerId.href);
550
550
  if (attributions.size > 0) {
551
- logger$3.debug("Some attributions are not authenticated by the Linked Data Signatures: {attributions}.", { attributions: [...attributions] });
551
+ logger$4.debug("Some attributions are not authenticated by the Linked Data Signatures: {attributions}.", { attributions: [...attributions] });
552
552
  return false;
553
553
  }
554
554
  return true;
@@ -573,8 +573,20 @@ async function hashJsonLd(jsonLd, contextLoader) {
573
573
  }
574
574
  //#endregion
575
575
  //#region src/sig/owner.ts
576
+ const logger$3 = (0, _logtape_logtape.getLogger)([
577
+ "fedify",
578
+ "sig",
579
+ "owner"
580
+ ]);
576
581
  /**
577
582
  * Checks if the actor of the given activity owns the specified key.
583
+ *
584
+ * Ownership is never taken from what the activity or the key say about
585
+ * themselves, as both are written by whoever sent the activity. It is
586
+ * established from a document that the owner itself serves: either the key's
587
+ * claimed owner links back to the key, or the actor the activity claims lists
588
+ * the key as its own.
589
+ *
578
590
  * @param activity The activity to check.
579
591
  * @param key The public key to check.
580
592
  * @param options Options for checking the key ownership.
@@ -589,23 +601,30 @@ async function doesActorOwnKey(activity, key, options) {
589
601
  }
590
602
  }, async (span) => {
591
603
  try {
592
- if (key.ownerId != null) {
593
- const owns = key.ownerId.href === activity.actorId?.href;
594
- span.setAttribute("activitypub.key_ownership.verified", owns);
595
- span.setAttribute("activitypub.key_ownership.method", "owner_id");
596
- return owns;
597
- }
598
- const actor = await activity.getActor(options);
599
- if (actor == null || !(0, _fedify_vocab.isActor)(actor)) {
604
+ const actorId = activity.actorId;
605
+ if (actorId == null) {
600
606
  span.setAttribute("activitypub.key_ownership.verified", false);
601
- span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
607
+ span.setAttribute("activitypub.key_ownership.method", "none");
602
608
  return false;
603
609
  }
604
- for (const publicKeyId of actor.publicKeyIds) if (key.id != null && publicKeyId.href === key.id.href) {
610
+ if (key.ownerId != null) {
611
+ const owner = await require_http.verifyKeyOwnership(key, options);
612
+ if (owner?.id != null && owner.id.href === actorId.href) {
613
+ span.setAttribute("activitypub.key_ownership.verified", true);
614
+ span.setAttribute("activitypub.key_ownership.method", "key_owner");
615
+ return true;
616
+ }
617
+ }
618
+ const actor = await require_http.fetchActorDocument(actorId, options);
619
+ if (actor != null && await doesActorListKey(actor, key, options)) {
605
620
  span.setAttribute("activitypub.key_ownership.verified", true);
606
621
  span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
607
622
  return true;
608
623
  }
624
+ 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.", {
625
+ actorId: actorId.href,
626
+ keyId: key.id?.href
627
+ });
609
628
  span.setAttribute("activitypub.key_ownership.verified", false);
610
629
  span.setAttribute("activitypub.key_ownership.method", "actor_fetch");
611
630
  return false;
@@ -622,9 +641,46 @@ async function doesActorOwnKey(activity, key, options) {
622
641
  });
623
642
  }
624
643
  /**
644
+ * Determines whether the given actor's own document lists the given key among
645
+ * its public keys.
646
+ *
647
+ * Keys are matched by id, which is what an actor document links to. A key
648
+ * document may leave its id implicit, though, and then there is no id for the
649
+ * actor to link to; the key material itself is the only thing the two can
650
+ * have in common, so that is what gets compared.
651
+ */
652
+ async function doesActorListKey(actor, key, options) {
653
+ if (key.id != null) {
654
+ for (const publicKeyId of actor.publicKeyIds) if (publicKeyId.href === key.id.href) return true;
655
+ return false;
656
+ }
657
+ if (key.publicKey == null) return false;
658
+ const publicKeys = actor.getPublicKeys({
659
+ ...options,
660
+ suppressError: true
661
+ });
662
+ for await (const publicKey of publicKeys) {
663
+ if (publicKey.publicKey == null) continue;
664
+ if (await isSamePublicKey(publicKey.publicKey, key.publicKey)) return true;
665
+ }
666
+ return false;
667
+ }
668
+ /**
669
+ * Compares two public keys by their key material, ignoring the metadata that
670
+ * surrounds it.
671
+ */
672
+ async function isSamePublicKey(a, b) {
673
+ const [x, y] = await Promise.all([require_http.exportJwk(a), require_http.exportJwk(b)]);
674
+ 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;
675
+ }
676
+ /**
625
677
  * Gets the actor that owns the specified key. Returns `null` if the key has no
626
678
  * known owner.
627
679
  *
680
+ * The owner is only returned when the key and the owner link to each other:
681
+ * a document that merely claims to own a key is not enough, as the claim and
682
+ * the key can come from the same host.
683
+ *
628
684
  * @param keyId The ID of the key to check, or the key itself.
629
685
  * @param options Options for getting the key owner.
630
686
  * @returns The actor that owns the key, or `null` if the key has no known
@@ -634,52 +690,50 @@ async function doesActorOwnKey(activity, key, options) {
634
690
  async function getKeyOwner(keyId, options) {
635
691
  const tracerProvider = options.tracerProvider ?? _opentelemetry_api.trace.getTracerProvider();
636
692
  const documentLoader = options.documentLoader ?? (0, _fedify_vocab_runtime.getDocumentLoader)();
637
- const contextLoader = options.contextLoader ?? (0, _fedify_vocab_runtime.getDocumentLoader)();
693
+ const fetchOptions = {
694
+ documentLoader,
695
+ contextLoader: options.contextLoader ?? (0, _fedify_vocab_runtime.getDocumentLoader)(),
696
+ tracerProvider
697
+ };
698
+ if (keyId instanceof _fedify_vocab.CryptographicKey) return await require_http.verifyKeyOwnership(keyId, fetchOptions);
699
+ let keyDoc;
700
+ let documentUrl = keyId;
701
+ try {
702
+ const remoteDocument = await documentLoader(keyId.href);
703
+ keyDoc = remoteDocument.document;
704
+ documentUrl = new URL(remoteDocument.documentUrl ?? "", keyId);
705
+ } catch (_) {
706
+ return null;
707
+ }
638
708
  let object;
639
- if (keyId instanceof _fedify_vocab.CryptographicKey) {
640
- object = keyId;
641
- if (object.id == null) return null;
642
- keyId = object.id;
643
- } else {
644
- let keyDoc;
645
- try {
646
- const { document } = await documentLoader(keyId.href);
647
- keyDoc = document;
648
- } catch (_) {
649
- return null;
650
- }
709
+ try {
710
+ object = await _fedify_vocab.Object.fromJsonLd(keyDoc, {
711
+ ...fetchOptions,
712
+ baseUrl: documentUrl
713
+ });
714
+ } catch (e) {
715
+ if (!(e instanceof TypeError)) throw e;
651
716
  try {
652
- object = await _fedify_vocab.Object.fromJsonLd(keyDoc, {
653
- documentLoader,
654
- contextLoader,
655
- tracerProvider
656
- });
717
+ object = await _fedify_vocab.CryptographicKey.fromJsonLd(keyDoc, fetchOptions);
657
718
  } catch (e) {
658
- if (!(e instanceof TypeError)) throw e;
659
- try {
660
- object = await _fedify_vocab.CryptographicKey.fromJsonLd(keyDoc, {
661
- documentLoader,
662
- contextLoader,
663
- tracerProvider
664
- });
665
- } catch (e) {
666
- if (e instanceof TypeError) return null;
667
- throw e;
668
- }
719
+ if (e instanceof TypeError) return null;
720
+ throw e;
669
721
  }
670
722
  }
671
- let owner = null;
672
723
  if (object instanceof _fedify_vocab.CryptographicKey) {
673
- if (object.ownerId == null) return null;
674
- owner = await object.getOwner({
675
- documentLoader,
676
- contextLoader,
677
- tracerProvider
724
+ if (object.id != null && object.id.href !== keyId.href) return null;
725
+ return await require_http.verifyKeyOwnership(object.id == null ? object.clone({ id: keyId }) : object, fetchOptions);
726
+ }
727
+ if (!(0, _fedify_vocab.isActor)(object)) return null;
728
+ if (object.id == null || object.id.origin !== documentUrl.origin) {
729
+ 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}.", {
730
+ documentUrl: documentUrl.href,
731
+ actorId: object.id?.href,
732
+ keyId: keyId.href
678
733
  });
679
- } else if ((0, _fedify_vocab.isActor)(object)) owner = object;
680
- else return null;
681
- if (owner == null) return null;
682
- for (const kid of owner.publicKeyIds) if (kid.href === keyId.href) return owner;
734
+ return null;
735
+ }
736
+ for (const kid of object.publicKeyIds) if (kid.href === keyId.href) return object;
683
737
  return null;
684
738
  }
685
739
  //#endregion