@fedify/fedify 2.5.0-dev.2664 → 2.5.0-dev.2710

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 (266) hide show
  1. package/dist/compat/mod.d.cts +1 -1
  2. package/dist/compat/mod.d.ts +1 -1
  3. package/dist/{context-xrqq4Hrq.d.ts → context-CuLvR1Os.d.ts} +2 -2
  4. package/dist/{context-C1NqcIct.d.cts → context-apq0IBg2.d.cts} +2 -2
  5. package/dist/federation/mod.cjs +1 -1
  6. package/dist/federation/mod.d.cts +3 -3
  7. package/dist/federation/mod.d.ts +3 -3
  8. package/dist/federation/mod.js +1 -1
  9. package/dist/{federation-FbwcnRHg.cjs → federation-JvzYDx0P.cjs} +5 -5
  10. package/dist/{http-DbvOPsFZ.cjs → http-BG5cDmTS.cjs} +1 -1
  11. package/dist/{http-CWn0K5jD.js → http-MUf125Jc.js} +1 -1
  12. package/dist/{key-dwwAg5bA.d.ts → key-BZw-YiK5.d.ts} +2 -2
  13. package/dist/{key-DlHT4R6k.d.cts → key-D5fbQkKM.d.cts} +2 -2
  14. package/dist/{kv-cache-B-fOrhb-.cjs → kv-cache-A2po7h0_.cjs} +1 -1
  15. package/dist/{kv-cache-BC837EqC.js → kv-cache-CbWc1BZA.js} +1 -1
  16. package/dist/{ld-CWVpo_NM.d.cts → ld-DEi9ERHf.d.cts} +1 -1
  17. package/dist/{ld-o4LnhCYY.d.ts → ld-DlC78ioJ.d.ts} +1 -1
  18. package/dist/{middleware-n2UrG62B.js → middleware-DzzdJ7Wz.js} +5 -5
  19. package/dist/{mod-D22ZsiKi.d.ts → mod-BJRtsw7l.d.ts} +1 -1
  20. package/dist/{mod-C1nT74IG.d.cts → mod-BamwZjJx.d.cts} +1 -1
  21. package/dist/{mod-DO0epJGH.d.ts → mod-BkTi19dI.d.ts} +1 -1
  22. package/dist/{mod-CQC9xlLZ.d.ts → mod-CMtF0VVy.d.ts} +1 -1
  23. package/dist/{mod-YvA4Rk8J.d.cts → mod-D2Z9MiCm.d.cts} +1 -1
  24. package/dist/{mod-TwiQgEf3.d.cts → mod-DB2NOzQF.d.cts} +1 -1
  25. package/dist/mod.cjs +4 -4
  26. package/dist/mod.d.cts +6 -6
  27. package/dist/mod.d.ts +6 -6
  28. package/dist/mod.js +4 -4
  29. package/dist/{portable-collection-DQhIkX5a.js → portable-collection-CAer9RWN.js} +1 -1
  30. package/dist/{portable-collection-DseoiPPj.cjs → portable-collection-d8w7TCn9.cjs} +1 -1
  31. package/dist/sig/mod.cjs +2 -2
  32. package/dist/sig/mod.d.cts +3 -3
  33. package/dist/sig/mod.d.ts +3 -3
  34. package/dist/sig/mod.js +2 -2
  35. package/dist/utils/mod.cjs +1 -1
  36. package/dist/utils/mod.d.cts +1 -1
  37. package/dist/utils/mod.d.ts +1 -1
  38. package/dist/utils/mod.js +1 -1
  39. package/package.json +22 -9
  40. package/skills/fedify/SKILL.md +10 -0
  41. package/dist/accept-CPVqki78.mjs +0 -145
  42. package/dist/activity-listener-CELFAodc.mjs +0 -44
  43. package/dist/assert-D1Eafmd_.mjs +0 -23
  44. package/dist/assert_equals-Z6IcEbWU.mjs +0 -987
  45. package/dist/assert_exists-cffSpJ7X.mjs +0 -28
  46. package/dist/assert_false-CPdRnc3E.mjs +0 -23
  47. package/dist/assert_greater_or_equal-BlafOF4-.mjs +0 -31
  48. package/dist/assert_instance_of-mrXWWNY-.mjs +0 -37
  49. package/dist/assert_not_equals-CpPK2t89.mjs +0 -30
  50. package/dist/assert_rejects-CTptdYck.mjs +0 -39
  51. package/dist/assert_strict_equals-CYvBmkpC.mjs +0 -42
  52. package/dist/assert_throws-BYITOvPO.mjs +0 -74
  53. package/dist/assertion_error-B-cx0fFA.mjs +0 -31
  54. package/dist/audience-CHQ-FlLD.mjs +0 -95
  55. package/dist/builder-CGJ-NXcZ.mjs +0 -911
  56. package/dist/circuit-breaker-XJ53md4S.mjs +0 -544
  57. package/dist/circuit-breaker-test-utils-DW6YC_Pe.mjs +0 -14
  58. package/dist/client-5SIFzW3q.mjs +0 -441
  59. package/dist/codec-C6rTrpIp.mjs +0 -147
  60. package/dist/collection-CnnP1UAv.mjs +0 -44
  61. package/dist/compat/outgoing-jsonld.test.d.mts +0 -2
  62. package/dist/compat/outgoing-jsonld.test.mjs +0 -257
  63. package/dist/compat/public-audience.test.d.mts +0 -2
  64. package/dist/compat/public-audience.test.mjs +0 -187
  65. package/dist/compat/secured-document.test.d.mts +0 -2
  66. package/dist/compat/secured-document.test.mjs +0 -58
  67. package/dist/compat/transformers.test.d.mts +0 -2
  68. package/dist/compat/transformers.test.mjs +0 -62
  69. package/dist/compound-proof-CnzwOddk.mjs +0 -1019
  70. package/dist/context-LgGFFSz7.mjs +0 -146
  71. package/dist/deno-eoFpPk7W.mjs +0 -8
  72. package/dist/dist-_geWsuaI.mjs +0 -189
  73. package/dist/docloader-DwkipyL1.mjs +0 -136
  74. package/dist/esm-BHXXx4Wx.mjs +0 -1233
  75. package/dist/exporter-DVG68ozH.mjs +0 -263
  76. package/dist/federation/audience.test.d.mts +0 -2
  77. package/dist/federation/audience.test.mjs +0 -130
  78. package/dist/federation/builder.test.d.mts +0 -2
  79. package/dist/federation/builder.test.mjs +0 -396
  80. package/dist/federation/circuit-breaker.test.d.mts +0 -2
  81. package/dist/federation/circuit-breaker.test.mjs +0 -1277
  82. package/dist/federation/collection.test.d.mts +0 -2
  83. package/dist/federation/collection.test.mjs +0 -21
  84. package/dist/federation/compound-fanout.test.d.mts +0 -2
  85. package/dist/federation/compound-fanout.test.mjs +0 -231
  86. package/dist/federation/handler-compound-proof.test.d.mts +0 -2
  87. package/dist/federation/handler-compound-proof.test.mjs +0 -384
  88. package/dist/federation/handler.test.d.mts +0 -2
  89. package/dist/federation/handler.test.mjs +0 -5445
  90. package/dist/federation/hashlink-media.test.d.mts +0 -2
  91. package/dist/federation/hashlink-media.test.mjs +0 -422
  92. package/dist/federation/idempotency.test.d.mts +0 -2
  93. package/dist/federation/idempotency.test.mjs +0 -207
  94. package/dist/federation/inbox-actorless.test.d.mts +0 -2
  95. package/dist/federation/inbox-actorless.test.mjs +0 -522
  96. package/dist/federation/inbox-authentication.test.d.mts +0 -2
  97. package/dist/federation/inbox-authentication.test.mjs +0 -220
  98. package/dist/federation/inbox-report.test.d.mts +0 -2
  99. package/dist/federation/inbox-report.test.mjs +0 -719
  100. package/dist/federation/inbox-view.test.d.mts +0 -2
  101. package/dist/federation/inbox-view.test.mjs +0 -369
  102. package/dist/federation/inbox.test.d.mts +0 -2
  103. package/dist/federation/inbox.test.mjs +0 -139
  104. package/dist/federation/keycache.test.d.mts +0 -2
  105. package/dist/federation/keycache.test.mjs +0 -337
  106. package/dist/federation/kv.test.d.mts +0 -2
  107. package/dist/federation/kv.test.mjs +0 -88
  108. package/dist/federation/metrics.test.d.mts +0 -2
  109. package/dist/federation/metrics.test.mjs +0 -634
  110. package/dist/federation/middleware.test.d.mts +0 -2
  111. package/dist/federation/middleware.test.mjs +0 -9314
  112. package/dist/federation/mq.test.d.mts +0 -2
  113. package/dist/federation/mq.test.mjs +0 -591
  114. package/dist/federation/negotiation.test.d.mts +0 -2
  115. package/dist/federation/negotiation.test.mjs +0 -18
  116. package/dist/federation/outbound-body.test.d.mts +0 -2
  117. package/dist/federation/outbound-body.test.mjs +0 -149
  118. package/dist/federation/outbound-capture.test.d.mts +0 -2
  119. package/dist/federation/outbound-capture.test.mjs +0 -394
  120. package/dist/federation/outbound-dispatch.test.d.mts +0 -2
  121. package/dist/federation/outbound-dispatch.test.mjs +0 -368
  122. package/dist/federation/outbound-document.test.d.mts +0 -2
  123. package/dist/federation/outbound-document.test.mjs +0 -171
  124. package/dist/federation/outbound-provenance.test.d.mts +0 -2
  125. package/dist/federation/outbound-provenance.test.mjs +0 -543
  126. package/dist/federation/outbound-report.test.d.mts +0 -2
  127. package/dist/federation/outbound-report.test.mjs +0 -5475
  128. package/dist/federation/outbound-workers.test.d.mts +0 -2
  129. package/dist/federation/outbound-workers.test.mjs +0 -64
  130. package/dist/federation/outbound-workflow.test.d.mts +0 -2
  131. package/dist/federation/outbound-workflow.test.mjs +0 -154
  132. package/dist/federation/outgoing-proof.test.d.mts +0 -2
  133. package/dist/federation/outgoing-proof.test.mjs +0 -933
  134. package/dist/federation/portable-actor.test.d.mts +0 -2
  135. package/dist/federation/portable-actor.test.mjs +0 -496
  136. package/dist/federation/portable-chain.test.d.mts +0 -2
  137. package/dist/federation/portable-chain.test.mjs +0 -370
  138. package/dist/federation/portable-collection.test.d.mts +0 -2
  139. package/dist/federation/portable-collection.test.mjs +0 -832
  140. package/dist/federation/portable-delivery.test.d.mts +0 -2
  141. package/dist/federation/portable-delivery.test.mjs +0 -845
  142. package/dist/federation/portable-gateway-keys.test.d.mts +0 -2
  143. package/dist/federation/portable-gateway-keys.test.mjs +0 -692
  144. package/dist/federation/portable-inbox.test.d.mts +0 -2
  145. package/dist/federation/portable-inbox.test.mjs +0 -1960
  146. package/dist/federation/portable-parse-uri.test.d.mts +0 -2
  147. package/dist/federation/portable-parse-uri.test.mjs +0 -348
  148. package/dist/federation/portable-route-activity.test.d.mts +0 -2
  149. package/dist/federation/portable-route-activity.test.mjs +0 -259
  150. package/dist/federation/portable.test.d.mts +0 -2
  151. package/dist/federation/portable.test.mjs +0 -1072
  152. package/dist/federation/retry.test.d.mts +0 -2
  153. package/dist/federation/retry.test.mjs +0 -42
  154. package/dist/federation/router.test.d.mts +0 -2
  155. package/dist/federation/router.test.mjs +0 -123
  156. package/dist/federation/send.test.d.mts +0 -2
  157. package/dist/federation/send.test.mjs +0 -1421
  158. package/dist/federation/tasks/codec.test.d.mts +0 -2
  159. package/dist/federation/tasks/codec.test.mjs +0 -302
  160. package/dist/federation/tasks/enqueue.test.d.mts +0 -2
  161. package/dist/federation/tasks/enqueue.test.mjs +0 -1095
  162. package/dist/federation/tasks/tasks.test.d.mts +0 -2
  163. package/dist/federation/tasks/tasks.test.mjs +0 -1229
  164. package/dist/federation/temporal.test.d.mts +0 -2
  165. package/dist/federation/temporal.test.mjs +0 -71
  166. package/dist/federation/webfinger.test.d.mts +0 -2
  167. package/dist/federation/webfinger.test.mjs +0 -944
  168. package/dist/http-l1zKGCll.mjs +0 -1449
  169. package/dist/inbox-CWX3EBTO.mjs +0 -399
  170. package/dist/inbox-authentication-CgonVRkg.mjs +0 -198
  171. package/dist/inbox-view-DkPCF71K.mjs +0 -385
  172. package/dist/key-owner-evidence-APB6NpSk.mjs +0 -108
  173. package/dist/keycache-N2opu4-3.mjs +0 -221
  174. package/dist/keys-DIJNHW-Y.mjs +0 -120
  175. package/dist/kv-DBRWVx3Q.mjs +0 -104
  176. package/dist/kv-DCHkaJ9-.mjs +0 -92
  177. package/dist/kv-cache-8eJh4Xum.mjs +0 -121
  178. package/dist/ld-Cdt8qD-r.mjs +0 -1752
  179. package/dist/map-local-context-conflict-DOYEEJ3u.mjs +0 -204
  180. package/dist/map-local-create-note-B0SeXJms.mjs +0 -214
  181. package/dist/metrics-BnfvSPj9.mjs +0 -818
  182. package/dist/middleware-CwnXJze2.mjs +0 -5
  183. package/dist/middleware-D_W_LdJN.mjs +0 -9971
  184. package/dist/mq-eF96QxUT.mjs +0 -288
  185. package/dist/negotiation-BAKO9nih.mjs +0 -96
  186. package/dist/nodeinfo/client.test.d.mts +0 -2
  187. package/dist/nodeinfo/client.test.mjs +0 -1364
  188. package/dist/nodeinfo/handler.test.d.mts +0 -2
  189. package/dist/nodeinfo/handler.test.mjs +0 -94
  190. package/dist/nodeinfo/types.test.d.mts +0 -2
  191. package/dist/nodeinfo/types.test.mjs +0 -192
  192. package/dist/otel/exporter.test.d.mts +0 -2
  193. package/dist/otel/exporter.test.mjs +0 -887
  194. package/dist/outbound-body-CckPA5gc.mjs +0 -83
  195. package/dist/outbound-capture-DVgnRzlf.mjs +0 -184
  196. package/dist/outbound-document-BMgwLXTY.mjs +0 -179
  197. package/dist/outgoing-jsonld-4IJqPYos.mjs +0 -205
  198. package/dist/owner-DukEQnv3.mjs +0 -267
  199. package/dist/portable-collection-OHaqa11n.mjs +0 -418
  200. package/dist/portable-key-BkOLJxVz.mjs +0 -549
  201. package/dist/portable-key-id-Ca30-hOa.mjs +0 -248
  202. package/dist/proof-Y7omSVvc.mjs +0 -1420
  203. package/dist/public-audience--klBIOk1.mjs +0 -197
  204. package/dist/retry-zX5xw5e0.mjs +0 -34
  205. package/dist/router-u5O5KCQr.mjs +0 -156
  206. package/dist/secured-document-CxzZZ9nL.mjs +0 -35
  207. package/dist/send-ccQynabr.mjs +0 -710
  208. package/dist/sig/accept.test.d.mts +0 -2
  209. package/dist/sig/accept.test.mjs +0 -416
  210. package/dist/sig/compound-proof-discovery.test.d.mts +0 -2
  211. package/dist/sig/compound-proof-discovery.test.mjs +0 -331
  212. package/dist/sig/compound-proof-producer.test.d.mts +0 -2
  213. package/dist/sig/compound-proof-producer.test.mjs +0 -107
  214. package/dist/sig/compound-proof-verification.test.d.mts +0 -2
  215. package/dist/sig/compound-proof-verification.test.mjs +0 -1017
  216. package/dist/sig/compound-proof.test.d.mts +0 -2
  217. package/dist/sig/compound-proof.test.mjs +0 -411
  218. package/dist/sig/http.test.d.mts +0 -2
  219. package/dist/sig/http.test.mjs +0 -3328
  220. package/dist/sig/key-owner-evidence.test.d.mts +0 -2
  221. package/dist/sig/key-owner-evidence.test.mjs +0 -100
  222. package/dist/sig/key.test.d.mts +0 -2
  223. package/dist/sig/key.test.mjs +0 -1250
  224. package/dist/sig/ld.test.d.mts +0 -2
  225. package/dist/sig/ld.test.mjs +0 -1044
  226. package/dist/sig/mitra-receive-interop.test.d.mts +0 -2
  227. package/dist/sig/mitra-receive-interop.test.mjs +0 -260
  228. package/dist/sig/mitra-send-interop.test.d.mts +0 -2
  229. package/dist/sig/mitra-send-interop.test.mjs +0 -461
  230. package/dist/sig/owner.test.d.mts +0 -2
  231. package/dist/sig/owner.test.mjs +0 -202
  232. package/dist/sig/portable-collection.test.d.mts +0 -2
  233. package/dist/sig/portable-collection.test.mjs +0 -697
  234. package/dist/sig/portable-interop.test.d.mts +0 -2
  235. package/dist/sig/portable-interop.test.mjs +0 -156
  236. package/dist/sig/portable-key-id.test.d.mts +0 -2
  237. package/dist/sig/portable-key-id.test.mjs +0 -67
  238. package/dist/sig/portable-key.test.d.mts +0 -2
  239. package/dist/sig/portable-key.test.mjs +0 -1438
  240. package/dist/sig/proof.test.d.mts +0 -2
  241. package/dist/sig/proof.test.mjs +0 -2638
  242. package/dist/sig/tootik-request-interop.test.d.mts +0 -2
  243. package/dist/sig/tootik-request-interop.test.mjs +0 -190
  244. package/dist/sig/tootik-send-interop.test.d.mts +0 -2
  245. package/dist/sig/tootik-send-interop.test.mjs +0 -331
  246. package/dist/sig/verification.test.d.mts +0 -2
  247. package/dist/sig/verification.test.mjs +0 -310
  248. package/dist/std__assert-DvMDRPes.mjs +0 -8
  249. package/dist/tasks-BCUmQbbz.mjs +0 -77
  250. package/dist/temporal-f_K4lAVR.mjs +0 -95
  251. package/dist/testing/context.test.d.mts +0 -2
  252. package/dist/testing/context.test.mjs +0 -19
  253. package/dist/testing/kv.test.d.mts +0 -2
  254. package/dist/testing/kv.test.mjs +0 -101
  255. package/dist/testing/mod.d.mts +0 -6099
  256. package/dist/testing/mod.mjs +0 -9
  257. package/dist/testing-vrnD5Uaa.mjs +0 -8
  258. package/dist/types-DE_LV1JB.mjs +0 -46
  259. package/dist/utils/docloader.test.d.mts +0 -2
  260. package/dist/utils/docloader.test.mjs +0 -1076
  261. package/dist/utils/kv-cache.test.d.mts +0 -2
  262. package/dist/utils/kv-cache.test.mjs +0 -261
  263. package/dist/verification-C3954hQh.mjs +0 -5
  264. package/dist/verification-Dpfcw8ml.mjs +0 -105
  265. package/dist/vocab/cjs.test.d.mts +0 -2
  266. package/dist/vocab/cjs.test.mjs +0 -27
@@ -1,1438 +0,0 @@
1
- import { Temporal } from "temporal-polyfill";
2
- import "urlpattern-polyfill";
3
- globalThis.addEventListener = () => {};
4
- import { t as MemoryKvStore } from "../kv-DCHkaJ9-.mjs";
5
- import { C as fetchKey, _ as verifyJsonLd, m as signJsonLd, w as fetchKeyDetailed } from "../ld-Cdt8qD-r.mjs";
6
- import { h as verifyProof, r as createProof } from "../proof-Y7omSVvc.mjs";
7
- import { n as resolvePortableActorKey } from "../portable-key-BkOLJxVz.mjs";
8
- import { m as verifyRequestDetailed, p as verifyRequest, u as signRequest } from "../http-l1zKGCll.mjs";
9
- import { a as rsaPrivateKey3, c as rsaPublicKey3, i as rsaPrivateKey2, n as ed25519PrivateKey, r as ed25519PublicKey, s as rsaPublicKey2 } from "../keys-DIJNHW-Y.mjs";
10
- import { n as getKeyOwner, t as doesActorOwnKey } from "../owner-DukEQnv3.mjs";
11
- import { t as KvKeyCache } from "../keycache-N2opu4-3.mjs";
12
- import { Create, CryptographicKey, Multikey, Note, Person } from "@fedify/vocab";
13
- import { FetchError, encodeMultibase, exportDidKey, exportSpki, parseIri, preloadedContexts } from "@fedify/vocab-runtime";
14
- import { test } from "@fedify/fixture";
15
- import { deepStrictEqual, ok, strictEqual } from "node:assert/strict";
16
- import serialize from "json-canon";
17
- //#region src/sig/portable-key.test.ts
18
- const did = await exportDidKey(ed25519PublicKey.publicKey);
19
- const didKeyId = `${did}#${did.slice(8)}`;
20
- const otherKeyPair = await crypto.subtle.generateKey("Ed25519", true, ["sign", "verify"]);
21
- const otherDid = await exportDidKey(otherKeyPair.publicKey);
22
- const actorId = `ap+ef61://${did}/actor`;
23
- const gw1 = "https://gw1.example";
24
- const gw2 = "https://gw2.example";
25
- function compatibleId(gateway, id = actorId) {
26
- return `${gateway}/.well-known/apgateway/${id.replace(/^ap\+ef61:\/\//, "")}`;
27
- }
28
- const keyId = new URL(`${compatibleId(gw1)}#main-key`);
29
- const gatewayPublicKey = rsaPublicKey2.publicKey;
30
- const contextLoader = (url) => {
31
- const document = preloadedContexts[url];
32
- if (document == null) return Promise.reject(/* @__PURE__ */ new Error(`No context: ${url}`));
33
- return Promise.resolve({
34
- contextUrl: null,
35
- documentUrl: url,
36
- document
37
- });
38
- };
39
- function createLoader(responses) {
40
- const fetched = [];
41
- const loader = (url) => {
42
- fetched.push(url);
43
- const document = responses[url.replace(/#.*$/, "")];
44
- if (document == null) return Promise.reject(new FetchError(url, "HTTP 404", new Response(null, { status: 404 })));
45
- return Promise.resolve({
46
- contextUrl: null,
47
- documentUrl: url,
48
- document: structuredClone(document)
49
- });
50
- };
51
- return Object.assign(loader, { fetched });
52
- }
53
- async function sign(document, privateKey = ed25519PrivateKey, verificationMethod = didKeyId, proofOptions = {}) {
54
- const proofConfig = {
55
- "@context": document["@context"],
56
- type: "DataIntegrityProof",
57
- cryptosuite: "eddsa-jcs-2022",
58
- verificationMethod,
59
- proofPurpose: "assertionMethod",
60
- created: "2023-02-24T23:36:38Z",
61
- ...proofOptions
62
- };
63
- const encoder = new TextEncoder();
64
- const proofDigest = await crypto.subtle.digest("SHA-256", encoder.encode(serialize(proofConfig)));
65
- const messageDigest = await crypto.subtle.digest("SHA-256", encoder.encode(serialize(document)));
66
- const digest = /* @__PURE__ */ new Uint8Array(64);
67
- digest.set(new Uint8Array(proofDigest), 0);
68
- digest.set(new Uint8Array(messageDigest), 32);
69
- const signature = await crypto.subtle.sign("Ed25519", privateKey, digest);
70
- return {
71
- ...document,
72
- proof: {
73
- ...proofConfig,
74
- proofValue: new TextDecoder().decode(encodeMultibase("base58btc", new Uint8Array(signature)))
75
- }
76
- };
77
- }
78
- async function actorJson(options = {}) {
79
- const id = parseIri(options.id ?? actorId);
80
- const key = options.key ?? keyId;
81
- const json = await new Person({
82
- id,
83
- preferredUsername: "alice",
84
- inbox: parseIri(`${options.id ?? actorId}/inbox`),
85
- gateways: (options.gateways ?? [gw1, gw2]).map((g) => new URL(g)),
86
- assertionMethods: options.assertionMethod === false ? [] : [new Multikey({
87
- id: key,
88
- controller: id,
89
- publicKey: gatewayPublicKey
90
- })],
91
- publicKeys: options.publicKey === null ? [] : [new CryptographicKey({
92
- id: key,
93
- owner: id,
94
- publicKey: options.publicKey ?? gatewayPublicKey
95
- })]
96
- }).toJsonLd({
97
- format: "compact",
98
- contextLoader
99
- });
100
- const context = json["@context"];
101
- if (!context.includes("https://w3id.org/security/data-integrity/v1")) json["@context"] = [...context, "https://w3id.org/security/data-integrity/v1"];
102
- return json;
103
- }
104
- async function signedRequest(spec, key = keyId) {
105
- return await signRequest(new Request("https://recipient.example/inbox", {
106
- method: "POST",
107
- body: "{}",
108
- headers: { "Content-Type": "application/activity+json" }
109
- }), rsaPrivateKey2, key, { spec });
110
- }
111
- test("verifyRequest() accepts gateway keys vouched for by portable actors", async () => {
112
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(await actorJson()) });
113
- for (const spec of ["draft-cavage-http-signatures-12", "rfc9421"]) {
114
- const key = await verifyRequest(await signedRequest(spec), {
115
- documentLoader,
116
- contextLoader,
117
- spec
118
- });
119
- ok(key != null, spec);
120
- strictEqual(key.id?.href, keyId.href);
121
- strictEqual(key.ownerId?.href, parseIri(actorId).href);
122
- }
123
- });
124
- test("verifyRequest() accepts gateway keys only in assertionMethod", async () => {
125
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(await actorJson({ publicKey: null })) });
126
- strictEqual((await verifyRequest(await signedRequest(), {
127
- documentLoader,
128
- contextLoader
129
- }))?.id?.href, keyId.href);
130
- });
131
- test("verifyRequest() rejects gateway keys that portable actors do not vouch for", async () => {
132
- const unsigned = await actorJson();
133
- const cases = {
134
- "an unsigned actor document": unsigned,
135
- "an actor document signed by another DID": await sign(unsigned, otherKeyPair.privateKey, `${otherDid}#${otherDid.slice(8)}`),
136
- "an unlisted gateway": await sign(await actorJson({ gateways: [gw2] })),
137
- "a key missing from both assertionMethod and publicKey": await sign(await actorJson({
138
- assertionMethod: false,
139
- publicKey: null
140
- })),
141
- "a key referred to by URL in assertionMethod, embedded in publicKey": await sign({
142
- ...await actorJson({ assertionMethod: false }),
143
- assertionMethod: [keyId.href]
144
- }),
145
- "a key only in publicKey without an owner": await sign(withoutPublicKeyOwner(await actorJson({ assertionMethod: false }))),
146
- "two keys with the same ID in publicKey": await sign(await duplicatePublicKey(await actorJson({ assertionMethod: false }))),
147
- "a key referred to by URL": await sign({
148
- ...await actorJson({
149
- publicKey: null,
150
- assertionMethod: false
151
- }),
152
- assertionMethod: [keyId.href]
153
- }),
154
- "a publicKey entry with other key material": await sign(await actorJson({ publicKey: rsaPublicKey3.publicKey })),
155
- "a key ID for another actor": await sign(await actorJson({ key: new URL(`${compatibleId(gw1, `${actorId}/other`)}#main-key`) }))
156
- };
157
- for (const [name, document] of Object.entries(cases)) {
158
- const documentLoader = createLoader({ [compatibleId(gw1)]: document });
159
- strictEqual(await verifyRequest(await signedRequest(), {
160
- documentLoader,
161
- contextLoader
162
- }), null, name);
163
- }
164
- const otherActorKey = new URL(`${compatibleId(gw1, `${actorId}/other`)}#main-key`);
165
- const documentLoader = createLoader({ [compatibleId(gw1, `${actorId}/other`)]: await sign(await actorJson()) });
166
- strictEqual(await verifyRequest(await signedRequest(void 0, otherActorKey), {
167
- documentLoader,
168
- contextLoader
169
- }), null);
170
- });
171
- function withoutPublicKeyOwner(json) {
172
- const { owner: _, ...publicKey } = json.publicKey;
173
- return {
174
- ...json,
175
- publicKey
176
- };
177
- }
178
- async function duplicatePublicKey(json) {
179
- const publicKey = json.publicKey;
180
- return {
181
- ...json,
182
- publicKey: [publicKey, {
183
- ...publicKey,
184
- publicKeyPem: await exportSpki(rsaPublicKey3.publicKey)
185
- }]
186
- };
187
- }
188
- test("verifyRequest() accepts gateway keys referred to by URL in publicKey", async () => {
189
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign({
190
- ...await actorJson({ publicKey: null }),
191
- publicKey: keyId.href
192
- }) });
193
- strictEqual((await verifyRequest(await signedRequest(), {
194
- documentLoader,
195
- contextLoader
196
- }))?.id?.href, keyId.href);
197
- });
198
- test("verifyRequest() accepts gateway keys only in publicKey", async () => {
199
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(await actorJson({ assertionMethod: false })) });
200
- const key = await verifyRequest(await signedRequest(), {
201
- documentLoader,
202
- contextLoader
203
- });
204
- strictEqual(key?.id?.href, keyId.href);
205
- strictEqual(key?.ownerId?.href, parseIri(actorId).href);
206
- });
207
- test("verifyRequest() resolves gateway keys of compatible-ID actors", async () => {
208
- const id = compatibleId(gw1);
209
- const signed = await sign(await actorJson({
210
- id,
211
- gateways: [gw1]
212
- }));
213
- let key = await verifyRequest(await signedRequest(), {
214
- documentLoader: createLoader({ [id]: signed }),
215
- contextLoader
216
- });
217
- strictEqual(key?.id?.href, keyId.href);
218
- strictEqual(key?.ownerId?.href, id);
219
- key = await verifyRequest(await signedRequest(), {
220
- documentLoader: createLoader({ [id]: await sign(await actorJson({
221
- id,
222
- gateways: [gw1],
223
- assertionMethod: false
224
- })) }),
225
- contextLoader
226
- });
227
- strictEqual(key?.id?.href, keyId.href);
228
- const evil = "https://evil.example";
229
- const evilId = compatibleId(evil);
230
- const evilKeyId = new URL(`${evilId}#main-key`);
231
- const unsigned = await actorJson({
232
- id: evilId,
233
- gateways: [evil],
234
- key: evilKeyId
235
- });
236
- const documentLoader = createLoader({ [evilId]: unsigned });
237
- strictEqual(await verifyRequest(await signedRequest(void 0, evilKeyId), {
238
- documentLoader,
239
- contextLoader
240
- }), null);
241
- strictEqual(await getKeyOwner(evilKeyId, {
242
- documentLoader,
243
- contextLoader
244
- }), null);
245
- strictEqual(await verifyRequest(await signedRequest(), {
246
- documentLoader: createLoader({ [id]: await sign(await actorJson({
247
- id,
248
- gateways: [gw1]
249
- }), otherKeyPair.privateKey, `${otherDid}#${otherDid.slice(8)}`) }),
250
- contextLoader
251
- }), null);
252
- });
253
- test("keys at ordinary URLs cannot belong to portable actors", async () => {
254
- const evil = "https://evil.example";
255
- const evilActorId = compatibleId(evil);
256
- const evilKeyId = new URL(`${evil}/keys/1`);
257
- const options = {
258
- documentLoader: createLoader({
259
- [evilKeyId.href]: {
260
- "@context": "https://w3id.org/security/v1",
261
- id: evilKeyId.href,
262
- type: "Key",
263
- owner: evilActorId,
264
- publicKeyPem: await exportSpki(gatewayPublicKey)
265
- },
266
- [evilActorId]: await actorJson({
267
- id: evilActorId,
268
- gateways: [evil],
269
- key: evilKeyId,
270
- assertionMethod: false
271
- })
272
- }),
273
- contextLoader
274
- };
275
- strictEqual(await verifyRequest(await signedRequest(void 0, evilKeyId), options), null);
276
- strictEqual((await fetchKey(evilKeyId, CryptographicKey, options)).key, null);
277
- strictEqual(await getKeyOwner(evilKeyId, options), null);
278
- const key = new CryptographicKey({
279
- id: evilKeyId,
280
- owner: new URL(evilActorId),
281
- publicKey: gatewayPublicKey
282
- });
283
- strictEqual(await getKeyOwner(key, options), null);
284
- ok(!await doesActorOwnKey(new Create({
285
- id: new URL(`${evil}/activities/1`),
286
- actor: new URL(evilActorId)
287
- }), key, options));
288
- const actorKeyId = new URL(`${evil}/users/alice#main-key`);
289
- for (const document of [await actorJson({
290
- id: evilActorId,
291
- gateways: [evil],
292
- key: actorKeyId
293
- }), withoutPublicKeyOwner(await actorJson({
294
- id: evilActorId,
295
- gateways: [evil],
296
- key: actorKeyId,
297
- assertionMethod: false
298
- }))]) {
299
- const actorOptions = {
300
- documentLoader: createLoader({ [`${evil}/users/alice`]: document }),
301
- contextLoader
302
- };
303
- strictEqual(await verifyRequest(await signedRequest(void 0, actorKeyId), actorOptions), null);
304
- strictEqual((await fetchKey(actorKeyId, CryptographicKey, actorOptions)).key, null);
305
- strictEqual(await getKeyOwner(actorKeyId, actorOptions), null);
306
- }
307
- const ownerKeyId = new URL(`${evil}/keys/2`);
308
- const ownerOptions = {
309
- documentLoader: createLoader({
310
- [ownerKeyId.href]: {
311
- "@context": "https://w3id.org/security/v1",
312
- id: ownerKeyId.href,
313
- type: "Key",
314
- owner: `${evil}/users/bob`,
315
- publicKeyPem: await exportSpki(gatewayPublicKey)
316
- },
317
- [`${evil}/users/bob`]: await actorJson({
318
- id: evilActorId,
319
- gateways: [evil],
320
- key: ownerKeyId,
321
- assertionMethod: false
322
- })
323
- }),
324
- contextLoader
325
- };
326
- strictEqual(await getKeyOwner(ownerKeyId, ownerOptions), null);
327
- strictEqual(await getKeyOwner(new CryptographicKey({
328
- id: ownerKeyId,
329
- owner: new URL(`${evil}/users/bob`),
330
- publicKey: gatewayPublicKey
331
- }), ownerOptions), null);
332
- const keyCache = {
333
- get: () => Promise.resolve(key),
334
- set: () => Promise.resolve()
335
- };
336
- strictEqual((await fetchKey(evilKeyId, CryptographicKey, {
337
- ...options,
338
- keyCache
339
- })).key, null);
340
- });
341
- const specs = ["draft-cavage-http-signatures-12", "rfc9421"];
342
- function fetchKeyOf(cls, options) {
343
- return cls === CryptographicKey ? fetchKey(keyId, CryptographicKey, options) : fetchKey(keyId, Multikey, options);
344
- }
345
- function fetchCount(loader, url = keyId) {
346
- const document = url.href.replace(/#.*$/, "");
347
- return loader.fetched.filter((u) => u.replace(/#.*$/, "") === document).length;
348
- }
349
- test("verifyRequest() caches failures to fetch compatible key IDs", async () => {
350
- const unreachable = {
351
- "404 Not Found": createLoader({}),
352
- "a network error": (url) => Promise.reject(/* @__PURE__ */ new TypeError(`${url}`))
353
- };
354
- for (const spec of specs) for (const [name, inner] of Object.entries(unreachable)) {
355
- const fetched = [];
356
- const documentLoader = Object.assign((url) => {
357
- fetched.push(url);
358
- return inner(url);
359
- }, { fetched });
360
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"]);
361
- const options = {
362
- documentLoader,
363
- contextLoader,
364
- keyCache,
365
- spec
366
- };
367
- for (let i = 0; i < 3; i++) {
368
- const result = await verifyRequestDetailed(await signedRequest(spec), options);
369
- ok(!result.verified, `${spec}, ${name}`);
370
- strictEqual(result.reason.type, "keyFetchError", `${spec}, ${name}`);
371
- }
372
- strictEqual(fetchCount(documentLoader), 1, `${spec}, ${name}`);
373
- for (const cls of [CryptographicKey, Multikey]) {
374
- const result = await fetchKeyOf(cls, options);
375
- strictEqual(result.key, null);
376
- ok(result.cached, `${spec}, ${name}, ${cls.name}`);
377
- }
378
- strictEqual(fetchCount(documentLoader), 1, `${spec}, ${name}`);
379
- }
380
- });
381
- test("verifyRequest() caches gateway keys only for HTTP Signatures", async () => {
382
- for (const spec of specs) {
383
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(await actorJson()) });
384
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"]);
385
- const options = {
386
- documentLoader,
387
- contextLoader,
388
- keyCache,
389
- spec
390
- };
391
- for (let i = 0; i < 3; i++) strictEqual((await verifyRequest(await signedRequest(spec), options))?.ownerId?.href, parseIri(actorId).href, spec);
392
- strictEqual(fetchCount(documentLoader), 1, spec);
393
- const document = {
394
- "@context": "https://www.w3.org/ns/activitystreams",
395
- type: "Create",
396
- id: "https://gw1.example/activities/1",
397
- actor: compatibleId(gw1)
398
- };
399
- const { signature } = await signJsonLd(document, rsaPrivateKey2, keyId, { contextLoader });
400
- ok(!await verifyJsonLd({
401
- ...document,
402
- signature
403
- }, options), spec);
404
- const note = new Note({
405
- id: new URL("https://gw1.example/notes/1"),
406
- attribution: new URL(compatibleId(gw1)),
407
- content: "Hello"
408
- });
409
- const proof = await createProof(note, ed25519PrivateKey, keyId, { contextLoader });
410
- strictEqual(await verifyProof(await note.toJsonLd({
411
- format: "compact",
412
- contextLoader
413
- }), proof, options), null, spec);
414
- for (const cls of [CryptographicKey, Multikey]) strictEqual((await fetchKeyOf(cls, options)).key, null, spec);
415
- const fetched = fetchCount(documentLoader);
416
- ok(await verifyRequest(await signedRequest(spec), options) != null, spec);
417
- strictEqual(fetchCount(documentLoader), fetched, spec);
418
- }
419
- });
420
- test("verifyRequest() is not rejected by other purposes' negative entries", async () => {
421
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(await actorJson()) });
422
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"]);
423
- const options = {
424
- documentLoader,
425
- contextLoader,
426
- keyCache
427
- };
428
- for (const cls of [CryptographicKey, Multikey]) strictEqual((await fetchKeyOf(cls, options)).key, null);
429
- ok(await verifyRequest(await signedRequest(), options) != null);
430
- const fetched = fetchCount(documentLoader);
431
- for (const cls of [CryptographicKey, Multikey]) {
432
- const result = await fetchKeyOf(cls, options);
433
- strictEqual(result.key, null);
434
- ok(result.cached);
435
- }
436
- strictEqual(fetchCount(documentLoader), fetched);
437
- });
438
- test("fetchKey() does not share negative entries of compatible key IDs across key classes", async () => {
439
- const ownerId = new URL("https://gw1.example/users/bob");
440
- const multikey = new Multikey({
441
- id: keyId,
442
- controller: ownerId,
443
- publicKey: ed25519PublicKey.publicKey
444
- });
445
- const documentLoader = createLoader({
446
- [compatibleId(gw1)]: await multikey.toJsonLd({ contextLoader }),
447
- [ownerId.href]: await new Person({
448
- id: ownerId,
449
- assertionMethods: [multikey]
450
- }).toJsonLd({ contextLoader })
451
- });
452
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"]);
453
- const options = {
454
- documentLoader,
455
- contextLoader,
456
- keyCache
457
- };
458
- strictEqual((await fetchKey(keyId, CryptographicKey, options)).key, null);
459
- strictEqual(await verifyRequest(await signedRequest(), options), null);
460
- const result = await fetchKey(keyId, Multikey, options);
461
- strictEqual(result.key?.id?.href, keyId.href);
462
- ok(!result.cached);
463
- const cached = await fetchKey(keyId, Multikey, options);
464
- strictEqual(cached.key?.id?.href, keyId.href);
465
- ok(cached.cached);
466
- const other = await fetchKey(keyId, CryptographicKey, options);
467
- strictEqual(other.key, null);
468
- ok(other.cached);
469
- });
470
- test("verifyRequest() caches rejected portable actor documents", async () => {
471
- for (const spec of specs) {
472
- const documentLoader = createLoader({ [compatibleId(gw1)]: await actorJson() });
473
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"]);
474
- const options = {
475
- documentLoader,
476
- contextLoader,
477
- keyCache,
478
- spec
479
- };
480
- for (let i = 0; i < 3; i++) {
481
- const result = await verifyRequestDetailed(await signedRequest(spec), options);
482
- ok(!result.verified, spec);
483
- strictEqual(result.reason.type, "invalidSignature", spec);
484
- }
485
- strictEqual(fetchCount(documentLoader), 1, spec);
486
- }
487
- });
488
- test("verifyRequest() refetches a cached gateway key that fails to verify", async () => {
489
- for (const spec of specs) {
490
- const responses = { [compatibleId(gw1)]: await sign(await actorJson()) };
491
- const documentLoader = createLoader(responses);
492
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"]);
493
- const options = {
494
- documentLoader,
495
- contextLoader,
496
- keyCache,
497
- spec
498
- };
499
- ok(await verifyRequest(await signedRequest(spec), options) != null);
500
- responses[compatibleId(gw1)] = await sign(await actorJson({
501
- publicKey: rsaPublicKey3.publicKey,
502
- assertionMethod: false
503
- }));
504
- const rotated = async () => await signRequest(new Request("https://recipient.example/inbox", {
505
- method: "POST",
506
- body: "{}",
507
- headers: { "Content-Type": "application/activity+json" }
508
- }), rsaPrivateKey3, keyId, { spec });
509
- ok(await verifyRequest(await rotated(), options) != null, spec);
510
- strictEqual(fetchCount(documentLoader), 2, spec);
511
- ok(await verifyRequest(await rotated(), options) != null, spec);
512
- strictEqual(fetchCount(documentLoader), 2, spec);
513
- }
514
- });
515
- test("verifyRequest() drops a cached gateway key when its refetch fails", async () => {
516
- for (const spec of specs) {
517
- const responses = { [compatibleId(gw1)]: await sign(await actorJson()) };
518
- const documentLoader = createLoader(responses);
519
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"]);
520
- const options = {
521
- documentLoader,
522
- contextLoader,
523
- keyCache,
524
- spec
525
- };
526
- ok(await verifyRequest(await signedRequest(spec), options) != null);
527
- delete responses[compatibleId(gw1)];
528
- const result = await verifyRequestDetailed(await signRequest(new Request("https://recipient.example/inbox", {
529
- method: "POST",
530
- body: "{}",
531
- headers: { "Content-Type": "application/activity+json" }
532
- }), rsaPrivateKey3, keyId, { spec }), options);
533
- ok(!result.verified, spec);
534
- strictEqual(result.reason.type, "keyFetchError", spec);
535
- strictEqual(fetchCount(documentLoader), 2, spec);
536
- const next = await verifyRequestDetailed(await signedRequest(spec), options);
537
- ok(!next.verified, spec);
538
- strictEqual(next.reason.type, "keyFetchError", spec);
539
- strictEqual(fetchCount(documentLoader), 2, spec);
540
- }
541
- });
542
- test("verifyRequest() does not trust a cached gateway key for longer than its proof", async () => {
543
- let now = Temporal.Now.instant();
544
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"], { now: () => now });
545
- const expires = now.add({ minutes: 30 });
546
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(await actorJson(), ed25519PrivateKey, didKeyId, { expires: expires.toString() }) });
547
- const options = {
548
- documentLoader,
549
- contextLoader,
550
- keyCache
551
- };
552
- ok(await verifyRequest(await signedRequest(), options) != null);
553
- now = expires.subtract({ seconds: 1 });
554
- ok(await verifyRequest(await signedRequest(), options) != null);
555
- strictEqual(fetchCount(documentLoader), 1);
556
- now = expires;
557
- ok(await verifyRequest(await signedRequest(), options) != null);
558
- strictEqual(fetchCount(documentLoader), 2);
559
- });
560
- test("verifyRequest() looks up a cached gateway key again after an hour", async () => {
561
- let now = Temporal.Now.instant();
562
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"], { now: () => now });
563
- const responses = { [compatibleId(gw1)]: await sign(await actorJson()) };
564
- const documentLoader = createLoader(responses);
565
- const options = {
566
- documentLoader,
567
- contextLoader,
568
- keyCache
569
- };
570
- const start = now;
571
- ok(await verifyRequest(await signedRequest(), options) != null);
572
- now = start.add({ minutes: 59 });
573
- ok(await verifyRequest(await signedRequest(), options) != null);
574
- strictEqual(fetchCount(documentLoader), 1);
575
- responses[compatibleId(gw1)] = await sign(await actorJson({ gateways: [gw2] }));
576
- now = start.add({ hours: 1 });
577
- strictEqual(await verifyRequest(await signedRequest(), options), null);
578
- strictEqual(fetchCount(documentLoader), 2);
579
- });
580
- test("verifyRequest() rejects and caches gateway keys of expired documents", async () => {
581
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(await actorJson(), ed25519PrivateKey, didKeyId, { expires: "2000-01-01T00:00:00Z" }) });
582
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"]);
583
- const options = {
584
- documentLoader,
585
- contextLoader,
586
- keyCache
587
- };
588
- strictEqual(await verifyRequest(await signedRequest(), options), null);
589
- strictEqual(await verifyRequest(await signedRequest(), options), null);
590
- strictEqual(fetchCount(documentLoader), 1);
591
- });
592
- test("verifyRequest() takes the expiration of a proof whatever its context", async () => {
593
- let now = Temporal.Now.instant();
594
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"], { now: () => now });
595
- const expires = now.add({ minutes: 30 });
596
- const actor = await actorJson();
597
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign({
598
- ...actor,
599
- "@context": [...actor["@context"], { expires: "https://example.com/ns#notExpiration" }]
600
- }, ed25519PrivateKey, didKeyId, { expires: expires.toString() }) });
601
- const options = {
602
- documentLoader,
603
- contextLoader,
604
- keyCache
605
- };
606
- ok(await verifyRequest(await signedRequest(), options) != null);
607
- now = expires.subtract({ seconds: 1 });
608
- ok(await verifyRequest(await signedRequest(), options) != null);
609
- strictEqual(fetchCount(documentLoader), 1);
610
- now = expires;
611
- ok(await verifyRequest(await signedRequest(), options) != null);
612
- strictEqual(fetchCount(documentLoader), 2);
613
- });
614
- test("a key cache without namespaces caches only fetch failures of compatible key IDs", async () => {
615
- const calls = [];
616
- const entries = /* @__PURE__ */ new Map();
617
- const keyCache = {
618
- get(id) {
619
- calls.push(`get ${id.href}`);
620
- return Promise.resolve(entries.get(id.href));
621
- },
622
- set(id, key) {
623
- calls.push(`set ${id.href} ${key == null ? "null" : "key"}`);
624
- entries.set(id.href, key);
625
- return Promise.resolve();
626
- }
627
- };
628
- const options = {
629
- documentLoader: createLoader({ [compatibleId(gw1)]: await sign(await actorJson()) }),
630
- contextLoader,
631
- keyCache
632
- };
633
- ok(await verifyRequest(await signedRequest(), options) != null);
634
- for (const cls of [CryptographicKey, Multikey]) strictEqual((await fetchKeyOf(cls, options)).key, null);
635
- ok(await verifyRequest(await signedRequest(), options) != null);
636
- deepStrictEqual(calls.filter((c) => c.startsWith("set ") && c.includes(keyId.origin)), []);
637
- const unreachable = new URL(`${compatibleId(gw2)}#main-key`);
638
- strictEqual((await fetchKey(unreachable, CryptographicKey, options)).key, null);
639
- deepStrictEqual(calls.filter((c) => c.startsWith("set ") && c.includes(gw2)), [`set ${unreachable.href} null`]);
640
- });
641
- test("doesActorOwnKey() and getKeyOwner() resolve gateway keys to portable actors", async () => {
642
- const options = {
643
- documentLoader: createLoader({ [compatibleId(gw1)]: await sign(await actorJson()) }),
644
- contextLoader
645
- };
646
- const key = await verifyRequest(await signedRequest(), options);
647
- ok(key != null);
648
- const activity = (actor) => new Create({
649
- id: parseIri(`ap+ef61://${did}/activities/1`),
650
- actor: parseIri(actor)
651
- });
652
- ok(await doesActorOwnKey(activity(actorId), key, options));
653
- ok(await doesActorOwnKey(activity(compatibleId(gw2)), key, options));
654
- ok(!await doesActorOwnKey(activity(`${actorId}/other`), key, options));
655
- strictEqual((await getKeyOwner(keyId, options))?.id?.href, parseIri(actorId).href);
656
- strictEqual((await getKeyOwner(key, options))?.id?.href, key.ownerId?.href);
657
- const forged = key.clone({ publicKey: rsaPublicKey3.publicKey });
658
- strictEqual(await getKeyOwner(forged, options), null);
659
- ok(!await doesActorOwnKey(activity(actorId), forged, options));
660
- });
661
- test("doesActorOwnKey() and getKeyOwner() do not fall back for rejected gateway keys", async () => {
662
- const options = {
663
- documentLoader: createLoader({ [compatibleId(gw1)]: await actorJson() }),
664
- contextLoader
665
- };
666
- const key = new CryptographicKey({
667
- id: keyId,
668
- owner: parseIri(actorId),
669
- publicKey: gatewayPublicKey
670
- });
671
- ok(!await doesActorOwnKey(new Create({
672
- id: parseIri(`ap+ef61://${did}/activities/1`),
673
- actor: parseIri(actorId)
674
- }), key, options));
675
- strictEqual(await getKeyOwner(keyId, options), null);
676
- strictEqual(await getKeyOwner(key, options), null);
677
- });
678
- test("verifyRequest() binds gateway keys to the actor whose document is signed", async () => {
679
- const attackerDid = otherDid;
680
- const attackerActorId = `ap+ef61://${attackerDid}/actor`;
681
- const shiftingContext = "https://attacker.example/context";
682
- let loads = 0;
683
- const shiftingContextLoader = (url) => {
684
- if (url !== shiftingContext) return contextLoader(url);
685
- loads++;
686
- const document = { "@context": loads <= 2 ? {
687
- victimId: "@id",
688
- attackerId: "https://attacker.example/ns#a"
689
- } : {
690
- victimId: "https://attacker.example/ns#v",
691
- attackerId: "@id"
692
- } };
693
- return Promise.resolve({
694
- contextUrl: null,
695
- documentUrl: url,
696
- document
697
- });
698
- };
699
- const { id: _, ...victim } = await actorJson({ gateways: [gw1] });
700
- const document = await sign({
701
- ...victim,
702
- "@context": [...victim["@context"], shiftingContext],
703
- victimId: actorId,
704
- attackerId: attackerActorId
705
- }, otherKeyPair.privateKey, `${attackerDid}#${attackerDid.slice(8)}`);
706
- const documentLoader = createLoader({ [compatibleId(gw1)]: document });
707
- strictEqual(await verifyRequest(await signedRequest(), {
708
- documentLoader,
709
- contextLoader: shiftingContextLoader
710
- }), null);
711
- ok(loads > 2, "the shifting context was loaded for the proof");
712
- loads = 0;
713
- strictEqual(await getKeyOwner(keyId, {
714
- documentLoader,
715
- contextLoader: shiftingContextLoader
716
- }), null);
717
- });
718
- test("verifyRequest() matches gateway keys listed under ap: URIs", async () => {
719
- const portableKeyId = parseIri(`${actorId}#main-key`);
720
- const cases = {
721
- "both properties": await actorJson({ key: portableKeyId }),
722
- "assertionMethod only": await actorJson({
723
- key: portableKeyId,
724
- publicKey: null
725
- }),
726
- "publicKey only": await actorJson({
727
- key: portableKeyId,
728
- assertionMethod: false
729
- })
730
- };
731
- for (const [name, document] of Object.entries(cases)) {
732
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(document) });
733
- for (const spec of specs) {
734
- const key = await verifyRequest(await signedRequest(spec), {
735
- documentLoader,
736
- contextLoader,
737
- spec
738
- });
739
- strictEqual(key?.id?.href, keyId.href, `${name}, ${spec}`);
740
- strictEqual(key?.ownerId?.href, parseIri(actorId).href, name);
741
- }
742
- }
743
- });
744
- test("verifyRequest() rejects ambiguous or foreign aliases of gateway keys", async () => {
745
- const portableKeyId = `${actorId}#main-key`;
746
- const base = await actorJson();
747
- const assertionMethod = base.assertionMethod;
748
- const publicKey = base.publicKey;
749
- const cases = {
750
- "an exact entry and an ap: alias in assertionMethod": {
751
- ...base,
752
- assertionMethod: [assertionMethod, {
753
- ...assertionMethod,
754
- id: portableKeyId
755
- }]
756
- },
757
- "a reference and an embedded ap: alias in assertionMethod": {
758
- ...base,
759
- assertionMethod: [keyId.href, {
760
- ...assertionMethod,
761
- id: portableKeyId
762
- }]
763
- },
764
- "an exact entry and an ap: alias in publicKey": {
765
- ...base,
766
- publicKey: [publicKey, {
767
- ...publicKey,
768
- id: portableKeyId
769
- }]
770
- },
771
- "a key of another gateway": await actorJson({ key: new URL(`${compatibleId(gw2)}#main-key`) }),
772
- "an ap: key ID with dot segments": {
773
- ...base,
774
- assertionMethod: {
775
- ...assertionMethod,
776
- id: portableKeyId.replace("/actor#", "/x/../actor#")
777
- },
778
- publicKey: {
779
- ...publicKey,
780
- id: portableKeyId.replace("/actor#", "/x/../actor#")
781
- }
782
- },
783
- "a compatible key ID with dot segments": {
784
- ...base,
785
- assertionMethod: {
786
- ...assertionMethod,
787
- id: keyId.href.replace("/actor#", "/x/../actor#")
788
- },
789
- publicKey: {
790
- ...publicKey,
791
- id: keyId.href.replace("/actor#", "/x/../actor#")
792
- }
793
- }
794
- };
795
- for (const [name, document] of Object.entries(cases)) {
796
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(document) });
797
- strictEqual(await verifyRequest(await signedRequest(), {
798
- documentLoader,
799
- contextLoader
800
- }), null, name);
801
- }
802
- });
803
- test("verifyRequest() refuses key IDs whose identity URL parsing changes", async () => {
804
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(await actorJson()) });
805
- const request = await signedRequest("draft-cavage-http-signatures-12");
806
- const dotted = keyId.href.replace("/actor#", "/x/../actor#");
807
- const headers = new Headers(request.headers);
808
- headers.set("Signature", headers.get("Signature").replace(keyId.href, dotted));
809
- strictEqual(await verifyRequest(new Request(request, { headers }), {
810
- documentLoader,
811
- contextLoader
812
- }), null);
813
- strictEqual(documentLoader.fetched.length, 0);
814
- const dottedPortable = `${actorId.replace("/actor", "/x/../actor")}#main-key`;
815
- for (const id of [
816
- dotted,
817
- dottedPortable,
818
- ` ${dotted}`,
819
- ` ${dottedPortable}`,
820
- ` ${parseIri(actorId).href}#main-key`,
821
- dottedPortable.replace("ap+ef61:", "ap+ ef61:"),
822
- dotted.replace("did:key:", "did:ke y:"),
823
- `${gw1}/.well-known/apgateway/${did}/../../../users/alice#main-key`,
824
- ` ap://not-a-did/actor#main-key`
825
- ]) {
826
- let error;
827
- try {
828
- await fetchKey(id, CryptographicKey, {
829
- documentLoader,
830
- contextLoader
831
- });
832
- } catch (e) {
833
- error = e;
834
- }
835
- ok(error instanceof TypeError, id);
836
- }
837
- });
838
- const apKeyId = parseIri(`${actorId}#main-key`);
839
- function hinted(...gateways) {
840
- const query = gateways.map((g) => `@gateway=${encodeURIComponent(g)}`).join("&");
841
- return parseIri(`${actorId}${query === "" ? "" : `?${query}`}#main-key`);
842
- }
843
- async function apActorJson(options = {}) {
844
- return await actorJson({
845
- key: apKeyId,
846
- ...options
847
- });
848
- }
849
- test("verifyRequest() accepts keys of portable actors at ap: key IDs", async () => {
850
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(await apActorJson()) });
851
- const hintedKeyId = hinted(gw1);
852
- for (const spec of specs) {
853
- const key = await verifyRequest(await signedRequest(spec, hintedKeyId), {
854
- documentLoader,
855
- contextLoader,
856
- spec
857
- });
858
- ok(key != null, spec);
859
- strictEqual(key.id?.href, hintedKeyId.href, spec);
860
- strictEqual(key.ownerId?.href, parseIri(actorId).href, spec);
861
- }
862
- const raw = `ap://${did}/actor?@gateway=${encodeURIComponent(gw1)}#main-key`;
863
- for (const written of [raw, raw.replace(/^ap:/, "ap+ef61:")]) {
864
- const request = await signedRequest("draft-cavage-http-signatures-12", hintedKeyId);
865
- const headers = new Headers(request.headers);
866
- headers.set("Signature", headers.get("Signature").replace(hintedKeyId.href, written));
867
- strictEqual((await verifyRequest(new Request(request, { headers }), {
868
- documentLoader,
869
- contextLoader
870
- }))?.ownerId?.href, parseIri(actorId).href, written);
871
- }
872
- deepStrictEqual([...new Set(documentLoader.fetched)], [compatibleId(gw1)]);
873
- });
874
- test("verifyRequest() asks the document loader for ap: key IDs without hints", async () => {
875
- const documentLoader = createLoader({ [actorId]: await sign(await apActorJson()) });
876
- strictEqual((await verifyRequest(await signedRequest(void 0, apKeyId), {
877
- documentLoader,
878
- contextLoader
879
- }))?.ownerId?.href, parseIri(actorId).href);
880
- deepStrictEqual(documentLoader.fetched, [actorId]);
881
- const result = await verifyRequestDetailed(await signedRequest(void 0, apKeyId), {
882
- documentLoader: createLoader({}),
883
- contextLoader
884
- });
885
- ok(!result.verified);
886
- strictEqual(result.reason.type, "keyFetchError");
887
- });
888
- test("verifyRequest() rejects keys at ap: key IDs that portable actors do not vouch for", async () => {
889
- const unsigned = await apActorJson();
890
- const ordinaryActorId = "https://gw1.example/users/alice";
891
- const cases = {
892
- "an unsigned actor document": unsigned,
893
- "an actor document signed by another DID": await sign(unsigned, otherKeyPair.privateKey, `${otherDid}#${otherDid.slice(8)}`),
894
- "a key missing from both assertionMethod and publicKey": await sign(await apActorJson({
895
- assertionMethod: false,
896
- publicKey: null
897
- })),
898
- "a key referred to by URL": await sign({
899
- ...await apActorJson({
900
- publicKey: null,
901
- assertionMethod: false
902
- }),
903
- assertionMethod: [apKeyId.href]
904
- }),
905
- "a key only in publicKey without an owner": await sign(withoutPublicKeyOwner(await apActorJson({ assertionMethod: false }))),
906
- "a publicKey entry with other key material": await sign(await apActorJson({ publicKey: rsaPublicKey3.publicKey })),
907
- "a key only at a compatible identifier": await sign(await actorJson()),
908
- "an actor without gateways": await sign(await apActorJson({ gateways: [] })),
909
- "the document of another actor": await sign(await apActorJson({ id: `${actorId}/other` })),
910
- "an ordinary actor": await actorJson({
911
- id: ordinaryActorId,
912
- key: apKeyId
913
- }),
914
- "a standalone key": {
915
- "@context": "https://w3id.org/security/v1",
916
- id: apKeyId.href,
917
- type: "Key",
918
- owner: actorId,
919
- publicKeyPem: await exportSpki(gatewayPublicKey)
920
- }
921
- };
922
- for (const [name, document] of Object.entries(cases)) {
923
- const documentLoader = createLoader({ [compatibleId(gw1)]: document });
924
- strictEqual(await verifyRequest(await signedRequest(void 0, hinted(gw1)), {
925
- documentLoader,
926
- contextLoader
927
- }), null, name);
928
- }
929
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(await apActorJson()) });
930
- strictEqual(await verifyRequest(await signedRequest(void 0, parseIri(`${actorId}?@gateway=${encodeURIComponent(gw1)}`)), {
931
- documentLoader,
932
- contextLoader
933
- }), null);
934
- strictEqual(documentLoader.fetched.length, 0);
935
- });
936
- test("verifyRequest() tries the gateways of ap: key IDs in order", async () => {
937
- const signed = await sign(await apActorJson());
938
- const cases = {
939
- "an invalid document at the first gateway": {
940
- [compatibleId(gw1)]: await apActorJson(),
941
- [compatibleId(gw2)]: signed
942
- },
943
- "an unreachable first gateway": { [compatibleId(gw2)]: signed }
944
- };
945
- for (const [name, responses] of Object.entries(cases)) {
946
- const documentLoader = createLoader(responses);
947
- strictEqual((await verifyRequest(await signedRequest(void 0, hinted(gw1, gw2)), {
948
- documentLoader,
949
- contextLoader
950
- }))?.ownerId?.href, parseIri(actorId).href, name);
951
- deepStrictEqual(documentLoader.fetched, [compatibleId(gw1), compatibleId(gw2)], name);
952
- }
953
- const documentLoader = createLoader({
954
- [compatibleId(gw1)]: signed,
955
- [compatibleId(gw2)]: signed
956
- });
957
- ok(await verifyRequest(await signedRequest(void 0, hinted(gw1, gw2)), {
958
- documentLoader,
959
- contextLoader
960
- }) != null);
961
- deepStrictEqual(documentLoader.fetched, [compatibleId(gw1)]);
962
- const gateways = Array.from({ length: 7 }, (_, i) => `https://gw${i + 10}.example`);
963
- const many = createLoader({});
964
- ok(!(await verifyRequestDetailed(await signedRequest(void 0, hinted(...gateways)), {
965
- documentLoader: many,
966
- contextLoader
967
- })).verified);
968
- deepStrictEqual(many.fetched, gateways.slice(0, 3).map((g) => compatibleId(g)));
969
- for (const [index, expected] of [[2, true], [3, false]]) {
970
- const documentLoader = createLoader({ [compatibleId(gateways[index])]: signed });
971
- strictEqual(await verifyRequest(await signedRequest(void 0, hinted(...gateways)), {
972
- documentLoader,
973
- contextLoader
974
- }) != null, expected, `gateway ${index + 1}`);
975
- deepStrictEqual(documentLoader.fetched, gateways.slice(0, 3).map((g) => compatibleId(g)), `gateway ${index + 1}`);
976
- }
977
- const sparse = createLoader({ [compatibleId(gateways[2])]: signed });
978
- ok(await verifyRequest(await signedRequest(void 0, hinted("not a gateway", gateways[0], gateways[0].toUpperCase(), "https://gw.example/path", gateways[1], gateways[2])), {
979
- documentLoader: sparse,
980
- contextLoader
981
- }) != null);
982
- deepStrictEqual(sparse.fetched, gateways.slice(0, 3).map((g) => compatibleId(g)));
983
- });
984
- function failingLoader(responses) {
985
- const fetched = [];
986
- const loader = (url) => {
987
- fetched.push(url);
988
- const response = responses[url.replace(/#.*$/, "")] ?? 404;
989
- if (typeof response === "number") return Promise.reject(new FetchError(url, `HTTP ${response}`, new Response(null, { status: response })));
990
- return Promise.resolve({
991
- contextUrl: null,
992
- documentUrl: url,
993
- document: structuredClone(response)
994
- });
995
- };
996
- return Object.assign(loader, { fetched });
997
- }
998
- test("verifyRequest() reports why keys at ap: key IDs could not be looked up", async () => {
999
- const hintedKeyId = hinted(gw1, gw2);
1000
- const invalid = await apActorJson();
1001
- const cases = [
1002
- {
1003
- name: "every gateway serves an invalid document",
1004
- responses: {
1005
- [compatibleId(gw1)]: invalid,
1006
- [compatibleId(gw2)]: invalid
1007
- },
1008
- reason: "invalidSignature",
1009
- cached: true
1010
- },
1011
- {
1012
- name: "every gateway responds with 410 Gone",
1013
- responses: {
1014
- [compatibleId(gw1)]: 410,
1015
- [compatibleId(gw2)]: 410
1016
- },
1017
- reason: "keyFetchError",
1018
- status: 410,
1019
- cached: true
1020
- },
1021
- {
1022
- name: "the gateways respond with different statuses",
1023
- responses: {
1024
- [compatibleId(gw1)]: 404,
1025
- [compatibleId(gw2)]: 503
1026
- },
1027
- reason: "keyFetchError",
1028
- cached: true
1029
- },
1030
- {
1031
- name: "one gateway serves an invalid document, the other is gone",
1032
- responses: {
1033
- [compatibleId(gw1)]: invalid,
1034
- [compatibleId(gw2)]: 410
1035
- },
1036
- reason: "keyFetchError",
1037
- cached: false
1038
- }
1039
- ];
1040
- for (const { name, responses, reason, status, cached } of cases) for (const spec of specs) {
1041
- const documentLoader = failingLoader(responses);
1042
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"]);
1043
- const options = {
1044
- documentLoader,
1045
- contextLoader,
1046
- keyCache,
1047
- spec
1048
- };
1049
- for (let i = 0; i < 2; i++) {
1050
- const result = await verifyRequestDetailed(await signedRequest(spec, hintedKeyId), options);
1051
- ok(!result.verified, `${name}, ${spec}`);
1052
- strictEqual(result.reason.type, reason, `${name}, ${spec}`);
1053
- if (result.reason.type === "keyFetchError") strictEqual("status" in result.reason.result ? result.reason.result.status : void 0, status, `${name}, ${spec}`);
1054
- }
1055
- strictEqual(documentLoader.fetched.length, cached ? 2 : 4, `${name}, ${spec}`);
1056
- }
1057
- });
1058
- test("verifyRequest() accepts keys at ap: key IDs only for HTTP Signatures", async () => {
1059
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(await apActorJson()) });
1060
- const hintedKeyId = hinted(gw1);
1061
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"]);
1062
- const options = {
1063
- documentLoader,
1064
- contextLoader,
1065
- keyCache
1066
- };
1067
- for (const cls of [CryptographicKey, Multikey]) strictEqual((cls === CryptographicKey ? await fetchKey(hintedKeyId, CryptographicKey, options) : await fetchKey(hintedKeyId, Multikey, options)).key, null);
1068
- strictEqual(documentLoader.fetched.length, 0);
1069
- for (let i = 0; i < 3; i++) {
1070
- const key = await verifyRequest(await signedRequest(void 0, hintedKeyId), options);
1071
- strictEqual(key?.id?.href, hintedKeyId.href);
1072
- strictEqual(key?.ownerId?.href, parseIri(actorId).href);
1073
- }
1074
- strictEqual(documentLoader.fetched.length, 1);
1075
- for (const cls of [CryptographicKey, Multikey]) strictEqual((cls === CryptographicKey ? await fetchKey(hintedKeyId, CryptographicKey, options) : await fetchKey(hintedKeyId, Multikey, options)).key, null);
1076
- const document = {
1077
- "@context": "https://www.w3.org/ns/activitystreams",
1078
- type: "Create",
1079
- id: "https://gw1.example/activities/1",
1080
- actor: compatibleId(gw1)
1081
- };
1082
- const { signature } = await signJsonLd(document, rsaPrivateKey2, hintedKeyId, { contextLoader });
1083
- ok(!await verifyJsonLd({
1084
- ...document,
1085
- signature
1086
- }, options));
1087
- strictEqual(documentLoader.fetched.length, 1);
1088
- const legacyCache = {
1089
- get: () => Promise.resolve(new CryptographicKey({
1090
- id: hintedKeyId,
1091
- owner: parseIri(actorId),
1092
- publicKey: gatewayPublicKey
1093
- })),
1094
- set: () => Promise.resolve()
1095
- };
1096
- const empty = createLoader({});
1097
- strictEqual(await verifyRequest(await signedRequest(void 0, hintedKeyId), {
1098
- documentLoader: empty,
1099
- contextLoader,
1100
- keyCache: legacyCache
1101
- }), null);
1102
- strictEqual(empty.fetched.length, 1);
1103
- });
1104
- test("verifyRequest() refreshes a cached key at an ap: key ID that fails to verify", async () => {
1105
- for (const spec of specs) {
1106
- const responses = { [compatibleId(gw1)]: await sign(await apActorJson()) };
1107
- const documentLoader = failingLoader(responses);
1108
- const hintedKeyId = hinted(gw1, gw2);
1109
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"]);
1110
- const options = {
1111
- documentLoader,
1112
- contextLoader,
1113
- keyCache,
1114
- spec
1115
- };
1116
- ok(await verifyRequest(await signedRequest(spec, hintedKeyId), options) != null);
1117
- strictEqual(documentLoader.fetched.length, 1, spec);
1118
- responses[compatibleId(gw1)] = await apActorJson();
1119
- responses[compatibleId(gw2)] = 503;
1120
- ok(!(await verifyRequestDetailed(await signRequest(new Request("https://recipient.example/inbox", {
1121
- method: "POST",
1122
- body: "{}",
1123
- headers: { "Content-Type": "application/activity+json" }
1124
- }), rsaPrivateKey3, hintedKeyId, { spec }), options)).verified, spec);
1125
- strictEqual(documentLoader.fetched.length, 3, spec);
1126
- const next = await verifyRequestDetailed(await signedRequest(spec, hintedKeyId), options);
1127
- ok(!next.verified, spec);
1128
- strictEqual(next.reason.type, "keyFetchError", spec);
1129
- strictEqual(documentLoader.fetched.length, 5, spec);
1130
- }
1131
- });
1132
- test("doesActorOwnKey() and getKeyOwner() resolve keys at ap: key IDs to portable actors", async () => {
1133
- const options = {
1134
- documentLoader: createLoader({ [compatibleId(gw1)]: await sign(await apActorJson()) }),
1135
- contextLoader
1136
- };
1137
- const hintedKeyId = hinted(gw1);
1138
- const key = new CryptographicKey({
1139
- id: hintedKeyId,
1140
- owner: parseIri(actorId),
1141
- publicKey: gatewayPublicKey
1142
- });
1143
- const activity = new Create({
1144
- id: parseIri(`${actorId.replace("/actor", "")}/activities/1`),
1145
- actor: parseIri(`ap://${did}/actor`)
1146
- });
1147
- ok(await doesActorOwnKey(activity, key, options));
1148
- strictEqual((await getKeyOwner(hintedKeyId, options))?.id?.href, parseIri(actorId).href);
1149
- strictEqual((await getKeyOwner(key, options))?.id?.href, parseIri(actorId).href);
1150
- ok(!await doesActorOwnKey(new Create({
1151
- id: parseIri(`ap+ef61://${otherDid}/activities/1`),
1152
- actor: parseIri(`ap+ef61://${otherDid}/actor`)
1153
- }), key, options));
1154
- const other = key.clone({ publicKey: rsaPublicKey3.publicKey });
1155
- ok(!await doesActorOwnKey(activity, other, options));
1156
- strictEqual(await getKeyOwner(other, options), null);
1157
- const unsigned = {
1158
- documentLoader: createLoader({ [compatibleId(gw1)]: await apActorJson() }),
1159
- contextLoader
1160
- };
1161
- ok(!await doesActorOwnKey(activity, key, unsigned));
1162
- strictEqual(await getKeyOwner(hintedKeyId, unsigned), null);
1163
- });
1164
- test("verifyRequest() does not trust a cached key at an ap: key ID for longer than its proof", async () => {
1165
- let now = Temporal.Now.instant();
1166
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"], { now: () => now });
1167
- const expires = now.add({ minutes: 30 });
1168
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(await apActorJson(), ed25519PrivateKey, didKeyId, { expires: expires.toString() }) });
1169
- const options = {
1170
- documentLoader,
1171
- contextLoader,
1172
- keyCache
1173
- };
1174
- const hintedKeyId = hinted(gw1);
1175
- ok(await verifyRequest(await signedRequest(void 0, hintedKeyId), options) != null);
1176
- now = expires.subtract({ seconds: 1 });
1177
- ok(await verifyRequest(await signedRequest(void 0, hintedKeyId), options) != null);
1178
- strictEqual(documentLoader.fetched.length, 1);
1179
- now = expires;
1180
- ok(await verifyRequest(await signedRequest(void 0, hintedKeyId), options) != null);
1181
- strictEqual(documentLoader.fetched.length, 2);
1182
- });
1183
- test("verifyRequest() does not cache keys at ap: key IDs it could not process", async () => {
1184
- let failures = 1;
1185
- const flakyContextLoader = (url, options) => {
1186
- if (failures > 0) {
1187
- failures--;
1188
- return Promise.reject(/* @__PURE__ */ new Error(`Temporarily failed to load ${url}`));
1189
- }
1190
- return contextLoader(url, options);
1191
- };
1192
- const documentLoader = createLoader({ [compatibleId(gw1)]: await sign(await apActorJson()) });
1193
- const options = {
1194
- documentLoader,
1195
- contextLoader: flakyContextLoader,
1196
- keyCache: new KvKeyCache(new MemoryKvStore(), ["pk"])
1197
- };
1198
- const hintedKeyId = hinted(gw1);
1199
- const result = await verifyRequestDetailed(await signedRequest(void 0, hintedKeyId), options);
1200
- ok(!result.verified);
1201
- strictEqual(result.reason.type, "keyFetchError");
1202
- if (result.reason.type === "keyFetchError") ok(!("status" in result.reason.result));
1203
- strictEqual((await verifyRequest(await signedRequest(void 0, hintedKeyId), options))?.ownerId?.href, parseIri(actorId).href);
1204
- strictEqual(documentLoader.fetched.length, 2);
1205
- });
1206
- /**
1207
- * A loader that serves the given responses, and never responds for the URLs
1208
- * in `hanging`. A hanging response is given up when its signal aborts,
1209
- * unless `ignoreSignal` is set.
1210
- */
1211
- function controlledLoader(responses, hanging, { ignoreSignal = false, delays = {} } = {}) {
1212
- const fetched = [];
1213
- const signals = [];
1214
- const respond = failingLoader(responses);
1215
- const loader = async (url, options) => {
1216
- fetched.push(url);
1217
- signals.push(options?.signal);
1218
- const signal = ignoreSignal ? void 0 : options?.signal;
1219
- const delay = delays[url];
1220
- if (delay != null) await new Promise((resolve) => setTimeout(resolve, delay));
1221
- if (hanging.includes(url)) return await new Promise((_, reject) => {
1222
- signal?.addEventListener("abort", () => reject(signal.reason));
1223
- });
1224
- return await respond(url);
1225
- };
1226
- return Object.assign(loader, {
1227
- fetched,
1228
- signals
1229
- });
1230
- }
1231
- function isTimeoutError(error) {
1232
- return error instanceof DOMException && error.name === "TimeoutError";
1233
- }
1234
- test("resolvePortableActorKey() gives up gateways that do not respond in time", async () => {
1235
- const signed = await sign(await apActorJson());
1236
- for (const ignoreSignal of [false, true]) {
1237
- const documentLoader = controlledLoader({ [compatibleId(gw2)]: signed }, [compatibleId(gw1)], { ignoreSignal });
1238
- const resolution = await resolvePortableActorKey(hinted(gw1, gw2), {
1239
- documentLoader,
1240
- contextLoader,
1241
- lookupTimeout: 50
1242
- });
1243
- strictEqual(resolution.type, "unavailable", `ignoreSignal: ${ignoreSignal}`);
1244
- if (resolution.type === "unavailable") strictEqual(resolution.cacheable, false);
1245
- deepStrictEqual(documentLoader.fetched, [compatibleId(gw1)]);
1246
- const [signal] = documentLoader.signals;
1247
- ok(signal?.aborted);
1248
- ok(isTimeoutError(signal.reason));
1249
- }
1250
- });
1251
- test("resolvePortableActorKey() caches timeouts only as failures to fetch", async () => {
1252
- const invalid = await apActorJson();
1253
- const cases = [
1254
- {
1255
- name: "the only gateway times out",
1256
- responses: {},
1257
- hanging: [compatibleId(gw1)],
1258
- hints: [gw1],
1259
- cacheable: true
1260
- },
1261
- {
1262
- name: "a gateway responds with 404, and the last one times out",
1263
- responses: { [compatibleId(gw1)]: 404 },
1264
- hanging: [compatibleId(gw2)],
1265
- hints: [gw1, gw2],
1266
- cacheable: true
1267
- },
1268
- {
1269
- name: "a gateway serves an invalid document, and the last one times out",
1270
- responses: { [compatibleId(gw1)]: invalid },
1271
- hanging: [compatibleId(gw2)],
1272
- hints: [gw1, gw2],
1273
- cacheable: false
1274
- },
1275
- {
1276
- name: "the document loader cannot resolve the ap: URI in time",
1277
- responses: {},
1278
- hanging: [actorId],
1279
- hints: [],
1280
- cacheable: true
1281
- }
1282
- ];
1283
- for (const { name, responses, hanging, hints, cacheable } of cases) {
1284
- const documentLoader = controlledLoader(responses, hanging);
1285
- const resolution = await resolvePortableActorKey(hinted(...hints), {
1286
- documentLoader,
1287
- contextLoader,
1288
- lookupTimeout: 50
1289
- });
1290
- strictEqual(resolution.type, "unavailable", name);
1291
- if (resolution.type !== "unavailable") continue;
1292
- strictEqual(resolution.cacheable, cacheable, name);
1293
- ok(!(resolution.error instanceof FetchError), name);
1294
- if (hanging.length === 1 && hints.length < 2) ok(isTimeoutError(resolution.error), name);
1295
- }
1296
- });
1297
- test("resolvePortableActorKey() shares one timeout among all gateways", async () => {
1298
- const documentLoader = controlledLoader({ [compatibleId(gw1)]: 404 }, [compatibleId(gw2)], { delays: { [compatibleId(gw1)]: 250 } });
1299
- const started = Date.now();
1300
- const resolution = await resolvePortableActorKey(hinted(gw1, gw2), {
1301
- documentLoader,
1302
- contextLoader,
1303
- lookupTimeout: 300
1304
- });
1305
- const elapsed = Date.now() - started;
1306
- ok(elapsed < 500, `took ${elapsed} ms`);
1307
- strictEqual(resolution.type, "unavailable");
1308
- deepStrictEqual(documentLoader.fetched, [compatibleId(gw1), compatibleId(gw2)]);
1309
- ok(documentLoader.signals[1]?.aborted);
1310
- });
1311
- test("resolvePortableActorKey() bounds loading the contexts of actor documents", async () => {
1312
- const documentLoader = controlledLoader({ [compatibleId(gw1)]: await sign(await apActorJson()) }, []);
1313
- const hangingContextLoader = (_url, options) => new Promise((_, reject) => {
1314
- options?.signal?.addEventListener("abort", () => reject(options.signal.reason));
1315
- });
1316
- const resolution = await resolvePortableActorKey(hinted(gw1, gw2), {
1317
- documentLoader,
1318
- contextLoader: hangingContextLoader,
1319
- lookupTimeout: 50
1320
- });
1321
- strictEqual(resolution.type, "unavailable");
1322
- if (resolution.type === "unavailable") strictEqual(resolution.cacheable, false);
1323
- deepStrictEqual(documentLoader.fetched, [compatibleId(gw1)]);
1324
- });
1325
- test("resolvePortableActorKey() ignores what is resolved after the timeout", async () => {
1326
- let verifiedLate = () => {};
1327
- const late = new Promise((resolve) => verifiedLate = resolve);
1328
- let lookupDone = () => {};
1329
- const done = new Promise((resolve) => lookupDone = resolve);
1330
- const slowKeyCache = {
1331
- async get() {
1332
- await done;
1333
- },
1334
- set() {
1335
- verifiedLate();
1336
- return Promise.resolve();
1337
- }
1338
- };
1339
- const documentLoader = controlledLoader({ [compatibleId(gw1)]: await sign(await apActorJson()) }, []);
1340
- const resolution = await resolvePortableActorKey(hinted(gw1), {
1341
- documentLoader,
1342
- contextLoader,
1343
- keyCache: slowKeyCache,
1344
- lookupTimeout: 300
1345
- });
1346
- lookupDone();
1347
- strictEqual(resolution.type, "unavailable");
1348
- if (resolution.type === "unavailable") strictEqual(resolution.cacheable, false);
1349
- await late;
1350
- const lateRejection = controlledLoader({}, [], { delays: { [compatibleId(gw1)]: 60 } });
1351
- strictEqual((await resolvePortableActorKey(hinted(gw1), {
1352
- documentLoader: lateRejection,
1353
- contextLoader,
1354
- lookupTimeout: 20
1355
- })).type, "unavailable");
1356
- await new Promise((resolve) => setTimeout(resolve, 80));
1357
- });
1358
- test("resolvePortableActorKey() validates the lookup timeout", async () => {
1359
- const documentLoader = controlledLoader({}, []);
1360
- for (const lookupTimeout of [
1361
- -1,
1362
- NaN,
1363
- Infinity,
1364
- 2 ** 31
1365
- ]) {
1366
- let error;
1367
- try {
1368
- await resolvePortableActorKey(hinted(gw1), {
1369
- documentLoader,
1370
- contextLoader,
1371
- lookupTimeout
1372
- });
1373
- } catch (e) {
1374
- error = e;
1375
- }
1376
- ok(error instanceof RangeError, String(lookupTimeout));
1377
- }
1378
- const resolution = await resolvePortableActorKey(hinted(gw1), {
1379
- documentLoader,
1380
- contextLoader,
1381
- lookupTimeout: 0
1382
- });
1383
- strictEqual(resolution.type, "unavailable");
1384
- if (resolution.type === "unavailable") strictEqual(resolution.cacheable, false);
1385
- deepStrictEqual(documentLoader.fetched, []);
1386
- });
1387
- test("fetchKeyDetailed() caches keys at ap: key IDs that time out as fetch failures", async () => {
1388
- const cases = [{
1389
- responses: { [compatibleId(gw1)]: 404 },
1390
- hanging: [compatibleId(gw2)],
1391
- cached: true
1392
- }, {
1393
- responses: {},
1394
- hanging: [compatibleId(gw1)],
1395
- cached: false
1396
- }];
1397
- for (const { responses, hanging, cached } of cases) {
1398
- const documentLoader = controlledLoader(responses, hanging);
1399
- const keyCache = new KvKeyCache(new MemoryKvStore(), ["pk"]);
1400
- const options = {
1401
- documentLoader,
1402
- contextLoader,
1403
- keyCache,
1404
- portableKeyResolvers: {
1405
- gatewayKey: () => Promise.resolve({ type: "legacy" }),
1406
- actorKey: (keyId) => resolvePortableActorKey(keyId, {
1407
- documentLoader,
1408
- contextLoader,
1409
- lookupTimeout: 50
1410
- })
1411
- }
1412
- };
1413
- const hintedKeyId = hinted(gw1, gw2);
1414
- for (let i = 0; i < 2; i++) {
1415
- const result = await fetchKeyDetailed(hintedKeyId, CryptographicKey, options);
1416
- strictEqual(result.key, null);
1417
- strictEqual(result.cached, cached && i > 0, `${cached}, ${i}`);
1418
- ok(result.fetchError == null || !("status" in result.fetchError), "a timeout has no HTTP status");
1419
- }
1420
- strictEqual(documentLoader.fetched.filter((url) => url === compatibleId(gw1)).length, cached ? 1 : 2);
1421
- }
1422
- });
1423
- test("verifyRequestDetailed() reports keys at ap: key IDs that time out as fetch errors", async () => {
1424
- const timeout = new DOMException("Timed out", "TimeoutError");
1425
- const documentLoader = () => Promise.reject(timeout);
1426
- for (const spec of specs) {
1427
- const result = await verifyRequestDetailed(await signedRequest(spec, hinted(gw1)), {
1428
- documentLoader,
1429
- contextLoader,
1430
- spec
1431
- });
1432
- ok(!result.verified, spec);
1433
- strictEqual(result.reason.type, "keyFetchError", spec);
1434
- if (result.reason.type === "keyFetchError") ok(!("status" in result.reason.result), spec);
1435
- }
1436
- });
1437
- //#endregion
1438
- export {};