@fedify/fedify 2.5.0-dev.2687 → 2.5.0-dev.2726

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 (244) hide show
  1. package/dist/federation/mod.cjs +1 -1
  2. package/dist/federation/mod.js +1 -1
  3. package/dist/{federation-pFFwPyoZ.cjs → federation-C0zalE9W.cjs} +8 -3
  4. package/dist/{http-T7h9GBAN.js → http-BC5Bd3-E.js} +1 -1
  5. package/dist/{http-CXX-QPzs.cjs → http-Dqzgc4Rj.cjs} +1 -1
  6. package/dist/{kv-cache-EFcb1kkU.js → kv-cache-CQ0u0Ry0.js} +1 -1
  7. package/dist/{kv-cache-C6XX_fwz.cjs → kv-cache-ihEHdx1v.cjs} +1 -1
  8. package/dist/{middleware-Bj91r7r2.js → middleware-Bgm3A90j.js} +8 -3
  9. package/dist/mod.cjs +4 -4
  10. package/dist/mod.js +4 -4
  11. package/dist/{portable-collection-Do5QA2oy.js → portable-collection-DZ_XqNUM.js} +1 -1
  12. package/dist/{portable-collection-BOl9oKrt.cjs → portable-collection-nvog8lS6.cjs} +1 -1
  13. package/dist/sig/mod.cjs +2 -2
  14. package/dist/sig/mod.js +2 -2
  15. package/dist/utils/mod.cjs +1 -1
  16. package/dist/utils/mod.js +1 -1
  17. package/package.json +22 -9
  18. package/skills/fedify/SKILL.md +35 -0
  19. package/dist/accept-CPVqki78.mjs +0 -145
  20. package/dist/activity-listener-CELFAodc.mjs +0 -44
  21. package/dist/assert-D1Eafmd_.mjs +0 -23
  22. package/dist/assert_equals-Z6IcEbWU.mjs +0 -987
  23. package/dist/assert_exists-cffSpJ7X.mjs +0 -28
  24. package/dist/assert_false-CPdRnc3E.mjs +0 -23
  25. package/dist/assert_greater_or_equal-BlafOF4-.mjs +0 -31
  26. package/dist/assert_instance_of-mrXWWNY-.mjs +0 -37
  27. package/dist/assert_not_equals-CpPK2t89.mjs +0 -30
  28. package/dist/assert_rejects-CTptdYck.mjs +0 -39
  29. package/dist/assert_strict_equals-CYvBmkpC.mjs +0 -42
  30. package/dist/assert_throws-BYITOvPO.mjs +0 -74
  31. package/dist/assertion_error-B-cx0fFA.mjs +0 -31
  32. package/dist/audience-CHQ-FlLD.mjs +0 -95
  33. package/dist/builder-D1FP_5nQ.mjs +0 -911
  34. package/dist/circuit-breaker-XJ53md4S.mjs +0 -544
  35. package/dist/circuit-breaker-test-utils-DW6YC_Pe.mjs +0 -14
  36. package/dist/client-5SIFzW3q.mjs +0 -441
  37. package/dist/codec-C6rTrpIp.mjs +0 -147
  38. package/dist/collection-CnnP1UAv.mjs +0 -44
  39. package/dist/compat/outgoing-jsonld.test.d.mts +0 -2
  40. package/dist/compat/outgoing-jsonld.test.mjs +0 -257
  41. package/dist/compat/public-audience.test.d.mts +0 -2
  42. package/dist/compat/public-audience.test.mjs +0 -187
  43. package/dist/compat/secured-document.test.d.mts +0 -2
  44. package/dist/compat/secured-document.test.mjs +0 -58
  45. package/dist/compat/transformers.test.d.mts +0 -2
  46. package/dist/compat/transformers.test.mjs +0 -62
  47. package/dist/compound-proof-DSHhnOEW.mjs +0 -1019
  48. package/dist/context-LgGFFSz7.mjs +0 -146
  49. package/dist/deno-C50ikGBU.mjs +0 -8
  50. package/dist/dist-_geWsuaI.mjs +0 -189
  51. package/dist/docloader-BY-SEID0.mjs +0 -136
  52. package/dist/esm-BHXXx4Wx.mjs +0 -1233
  53. package/dist/exporter-DVG68ozH.mjs +0 -263
  54. package/dist/federation/audience.test.d.mts +0 -2
  55. package/dist/federation/audience.test.mjs +0 -130
  56. package/dist/federation/builder.test.d.mts +0 -2
  57. package/dist/federation/builder.test.mjs +0 -396
  58. package/dist/federation/circuit-breaker.test.d.mts +0 -2
  59. package/dist/federation/circuit-breaker.test.mjs +0 -1277
  60. package/dist/federation/collection.test.d.mts +0 -2
  61. package/dist/federation/collection.test.mjs +0 -21
  62. package/dist/federation/compound-fanout.test.d.mts +0 -2
  63. package/dist/federation/compound-fanout.test.mjs +0 -231
  64. package/dist/federation/handler-compound-proof.test.d.mts +0 -2
  65. package/dist/federation/handler-compound-proof.test.mjs +0 -384
  66. package/dist/federation/handler.test.d.mts +0 -2
  67. package/dist/federation/handler.test.mjs +0 -5445
  68. package/dist/federation/hashlink-media.test.d.mts +0 -2
  69. package/dist/federation/hashlink-media.test.mjs +0 -422
  70. package/dist/federation/idempotency.test.d.mts +0 -2
  71. package/dist/federation/idempotency.test.mjs +0 -207
  72. package/dist/federation/inbox-actorless.test.d.mts +0 -2
  73. package/dist/federation/inbox-actorless.test.mjs +0 -522
  74. package/dist/federation/inbox-authentication.test.d.mts +0 -2
  75. package/dist/federation/inbox-authentication.test.mjs +0 -220
  76. package/dist/federation/inbox-report.test.d.mts +0 -2
  77. package/dist/federation/inbox-report.test.mjs +0 -719
  78. package/dist/federation/inbox-view.test.d.mts +0 -2
  79. package/dist/federation/inbox-view.test.mjs +0 -369
  80. package/dist/federation/inbox.test.d.mts +0 -2
  81. package/dist/federation/inbox.test.mjs +0 -139
  82. package/dist/federation/keycache.test.d.mts +0 -2
  83. package/dist/federation/keycache.test.mjs +0 -337
  84. package/dist/federation/kv.test.d.mts +0 -2
  85. package/dist/federation/kv.test.mjs +0 -88
  86. package/dist/federation/metrics.test.d.mts +0 -2
  87. package/dist/federation/metrics.test.mjs +0 -634
  88. package/dist/federation/middleware.test.d.mts +0 -2
  89. package/dist/federation/middleware.test.mjs +0 -9314
  90. package/dist/federation/mq.test.d.mts +0 -2
  91. package/dist/federation/mq.test.mjs +0 -591
  92. package/dist/federation/negotiation.test.d.mts +0 -2
  93. package/dist/federation/negotiation.test.mjs +0 -18
  94. package/dist/federation/outbound-body.test.d.mts +0 -2
  95. package/dist/federation/outbound-body.test.mjs +0 -149
  96. package/dist/federation/outbound-capture.test.d.mts +0 -2
  97. package/dist/federation/outbound-capture.test.mjs +0 -394
  98. package/dist/federation/outbound-dispatch.test.d.mts +0 -2
  99. package/dist/federation/outbound-dispatch.test.mjs +0 -368
  100. package/dist/federation/outbound-document.test.d.mts +0 -2
  101. package/dist/federation/outbound-document.test.mjs +0 -171
  102. package/dist/federation/outbound-provenance.test.d.mts +0 -2
  103. package/dist/federation/outbound-provenance.test.mjs +0 -543
  104. package/dist/federation/outbound-report.test.d.mts +0 -2
  105. package/dist/federation/outbound-report.test.mjs +0 -5475
  106. package/dist/federation/outbound-workers.test.d.mts +0 -2
  107. package/dist/federation/outbound-workers.test.mjs +0 -64
  108. package/dist/federation/outbound-workflow.test.d.mts +0 -2
  109. package/dist/federation/outbound-workflow.test.mjs +0 -154
  110. package/dist/federation/outgoing-proof.test.d.mts +0 -2
  111. package/dist/federation/outgoing-proof.test.mjs +0 -933
  112. package/dist/federation/portable-actor.test.d.mts +0 -2
  113. package/dist/federation/portable-actor.test.mjs +0 -496
  114. package/dist/federation/portable-chain.test.d.mts +0 -2
  115. package/dist/federation/portable-chain.test.mjs +0 -370
  116. package/dist/federation/portable-collection.test.d.mts +0 -2
  117. package/dist/federation/portable-collection.test.mjs +0 -832
  118. package/dist/federation/portable-delivery.test.d.mts +0 -2
  119. package/dist/federation/portable-delivery.test.mjs +0 -845
  120. package/dist/federation/portable-gateway-keys.test.d.mts +0 -2
  121. package/dist/federation/portable-gateway-keys.test.mjs +0 -692
  122. package/dist/federation/portable-inbox.test.d.mts +0 -2
  123. package/dist/federation/portable-inbox.test.mjs +0 -1960
  124. package/dist/federation/portable-parse-uri.test.d.mts +0 -2
  125. package/dist/federation/portable-parse-uri.test.mjs +0 -348
  126. package/dist/federation/portable-route-activity.test.d.mts +0 -2
  127. package/dist/federation/portable-route-activity.test.mjs +0 -259
  128. package/dist/federation/portable.test.d.mts +0 -2
  129. package/dist/federation/portable.test.mjs +0 -1072
  130. package/dist/federation/retry.test.d.mts +0 -2
  131. package/dist/federation/retry.test.mjs +0 -42
  132. package/dist/federation/router.test.d.mts +0 -2
  133. package/dist/federation/router.test.mjs +0 -123
  134. package/dist/federation/send.test.d.mts +0 -2
  135. package/dist/federation/send.test.mjs +0 -1421
  136. package/dist/federation/tasks/codec.test.d.mts +0 -2
  137. package/dist/federation/tasks/codec.test.mjs +0 -302
  138. package/dist/federation/tasks/enqueue.test.d.mts +0 -2
  139. package/dist/federation/tasks/enqueue.test.mjs +0 -1095
  140. package/dist/federation/tasks/tasks.test.d.mts +0 -2
  141. package/dist/federation/tasks/tasks.test.mjs +0 -1229
  142. package/dist/federation/temporal.test.d.mts +0 -2
  143. package/dist/federation/temporal.test.mjs +0 -71
  144. package/dist/federation/webfinger.test.d.mts +0 -2
  145. package/dist/federation/webfinger.test.mjs +0 -944
  146. package/dist/http-BXbQQZDM.mjs +0 -1449
  147. package/dist/inbox-authentication-CgonVRkg.mjs +0 -198
  148. package/dist/inbox-cS97XzOD.mjs +0 -399
  149. package/dist/inbox-view-D-d0Ar6o.mjs +0 -385
  150. package/dist/key-owner-evidence-APB6NpSk.mjs +0 -108
  151. package/dist/keycache-N2opu4-3.mjs +0 -221
  152. package/dist/keys-DIJNHW-Y.mjs +0 -120
  153. package/dist/kv-DBRWVx3Q.mjs +0 -104
  154. package/dist/kv-DCHkaJ9-.mjs +0 -92
  155. package/dist/kv-cache-fgbLHGVL.mjs +0 -121
  156. package/dist/ld-BpkhkDGj.mjs +0 -1752
  157. package/dist/map-local-context-conflict-DOYEEJ3u.mjs +0 -204
  158. package/dist/map-local-create-note-B0SeXJms.mjs +0 -214
  159. package/dist/metrics-Uqf8GomB.mjs +0 -818
  160. package/dist/middleware-DcXzz-YK.mjs +0 -9971
  161. package/dist/middleware-DgwZkvJR.mjs +0 -5
  162. package/dist/mq-eF96QxUT.mjs +0 -288
  163. package/dist/negotiation-BAKO9nih.mjs +0 -96
  164. package/dist/nodeinfo/client.test.d.mts +0 -2
  165. package/dist/nodeinfo/client.test.mjs +0 -1364
  166. package/dist/nodeinfo/handler.test.d.mts +0 -2
  167. package/dist/nodeinfo/handler.test.mjs +0 -94
  168. package/dist/nodeinfo/types.test.d.mts +0 -2
  169. package/dist/nodeinfo/types.test.mjs +0 -192
  170. package/dist/otel/exporter.test.d.mts +0 -2
  171. package/dist/otel/exporter.test.mjs +0 -887
  172. package/dist/outbound-body-CckPA5gc.mjs +0 -83
  173. package/dist/outbound-capture-DVgnRzlf.mjs +0 -184
  174. package/dist/outbound-document-BMgwLXTY.mjs +0 -179
  175. package/dist/outgoing-jsonld-4IJqPYos.mjs +0 -205
  176. package/dist/owner-BVenIFg1.mjs +0 -267
  177. package/dist/portable-collection-DIk9gpjL.mjs +0 -418
  178. package/dist/portable-key-C7pVaHNR.mjs +0 -549
  179. package/dist/portable-key-id-Ca30-hOa.mjs +0 -248
  180. package/dist/proof-BlfZ95--.mjs +0 -1420
  181. package/dist/public-audience--klBIOk1.mjs +0 -197
  182. package/dist/retry-zX5xw5e0.mjs +0 -34
  183. package/dist/router-u5O5KCQr.mjs +0 -156
  184. package/dist/secured-document-CxzZZ9nL.mjs +0 -35
  185. package/dist/send-DEGHPGFk.mjs +0 -710
  186. package/dist/sig/accept.test.d.mts +0 -2
  187. package/dist/sig/accept.test.mjs +0 -416
  188. package/dist/sig/compound-proof-discovery.test.d.mts +0 -2
  189. package/dist/sig/compound-proof-discovery.test.mjs +0 -331
  190. package/dist/sig/compound-proof-producer.test.d.mts +0 -2
  191. package/dist/sig/compound-proof-producer.test.mjs +0 -107
  192. package/dist/sig/compound-proof-verification.test.d.mts +0 -2
  193. package/dist/sig/compound-proof-verification.test.mjs +0 -1017
  194. package/dist/sig/compound-proof.test.d.mts +0 -2
  195. package/dist/sig/compound-proof.test.mjs +0 -411
  196. package/dist/sig/http.test.d.mts +0 -2
  197. package/dist/sig/http.test.mjs +0 -3328
  198. package/dist/sig/key-owner-evidence.test.d.mts +0 -2
  199. package/dist/sig/key-owner-evidence.test.mjs +0 -100
  200. package/dist/sig/key.test.d.mts +0 -2
  201. package/dist/sig/key.test.mjs +0 -1250
  202. package/dist/sig/ld.test.d.mts +0 -2
  203. package/dist/sig/ld.test.mjs +0 -1044
  204. package/dist/sig/mitra-receive-interop.test.d.mts +0 -2
  205. package/dist/sig/mitra-receive-interop.test.mjs +0 -260
  206. package/dist/sig/mitra-send-interop.test.d.mts +0 -2
  207. package/dist/sig/mitra-send-interop.test.mjs +0 -461
  208. package/dist/sig/owner.test.d.mts +0 -2
  209. package/dist/sig/owner.test.mjs +0 -202
  210. package/dist/sig/portable-collection.test.d.mts +0 -2
  211. package/dist/sig/portable-collection.test.mjs +0 -697
  212. package/dist/sig/portable-interop.test.d.mts +0 -2
  213. package/dist/sig/portable-interop.test.mjs +0 -156
  214. package/dist/sig/portable-key-id.test.d.mts +0 -2
  215. package/dist/sig/portable-key-id.test.mjs +0 -67
  216. package/dist/sig/portable-key.test.d.mts +0 -2
  217. package/dist/sig/portable-key.test.mjs +0 -1438
  218. package/dist/sig/proof.test.d.mts +0 -2
  219. package/dist/sig/proof.test.mjs +0 -2638
  220. package/dist/sig/tootik-request-interop.test.d.mts +0 -2
  221. package/dist/sig/tootik-request-interop.test.mjs +0 -190
  222. package/dist/sig/tootik-send-interop.test.d.mts +0 -2
  223. package/dist/sig/tootik-send-interop.test.mjs +0 -331
  224. package/dist/sig/verification.test.d.mts +0 -2
  225. package/dist/sig/verification.test.mjs +0 -310
  226. package/dist/std__assert-DvMDRPes.mjs +0 -8
  227. package/dist/tasks-BCUmQbbz.mjs +0 -77
  228. package/dist/temporal-B4oQ8HpW.mjs +0 -95
  229. package/dist/testing/context.test.d.mts +0 -2
  230. package/dist/testing/context.test.mjs +0 -19
  231. package/dist/testing/kv.test.d.mts +0 -2
  232. package/dist/testing/kv.test.mjs +0 -101
  233. package/dist/testing/mod.d.mts +0 -6099
  234. package/dist/testing/mod.mjs +0 -9
  235. package/dist/testing-vrnD5Uaa.mjs +0 -8
  236. package/dist/types-DE_LV1JB.mjs +0 -46
  237. package/dist/utils/docloader.test.d.mts +0 -2
  238. package/dist/utils/docloader.test.mjs +0 -1076
  239. package/dist/utils/kv-cache.test.d.mts +0 -2
  240. package/dist/utils/kv-cache.test.mjs +0 -261
  241. package/dist/verification-C3954hQh.mjs +0 -5
  242. package/dist/verification-Dpfcw8ml.mjs +0 -105
  243. package/dist/vocab/cjs.test.d.mts +0 -2
  244. package/dist/vocab/cjs.test.mjs +0 -27
@@ -1,2638 +0,0 @@
1
- import { Temporal } from "temporal-polyfill";
2
- import "urlpattern-polyfill";
3
- globalThis.addEventListener = () => {};
4
- import { t as esm_default } from "../esm-BHXXx4Wx.mjs";
5
- import { t as assertEquals } from "../assert_equals-Z6IcEbWU.mjs";
6
- import { t as assertFalse } from "../assert_false-CPdRnc3E.mjs";
7
- import { t as assertGreaterOrEqual } from "../assert_greater_or_equal-BlafOF4-.mjs";
8
- import { t as assertInstanceOf } from "../assert_instance_of-mrXWWNY-.mjs";
9
- import "../std__assert-DvMDRPes.mjs";
10
- import { t as assertRejects } from "../assert_rejects-CTptdYck.mjs";
11
- import { t as assert } from "../assert-D1Eafmd_.mjs";
12
- import { a as verificationObservation } from "../verification-Dpfcw8ml.mjs";
13
- import { r as normalizeOutgoingActivityJsonLd } from "../outgoing-jsonld-4IJqPYos.mjs";
14
- import { a as hasProofLike, c as verifyMapLocalProof, d as verifyPortableObjectProof, h as verifyProof, l as verifyObject, r as createProof, s as signObject, u as verifyObjectDetailed } from "../proof-BlfZ95--.mjs";
15
- import { p as verifyRequest } from "../http-BXbQQZDM.mjs";
16
- import { i as rsaPrivateKey2, n as ed25519PrivateKey, r as ed25519PublicKey, s as rsaPublicKey2, t as ed25519Multikey } from "../keys-DIJNHW-Y.mjs";
17
- import { n as getAuthenticatedDocumentLoader } from "../docloader-BY-SEID0.mjs";
18
- import { Create, DataIntegrityProof, Document, Multikey, Note, PUBLIC_COLLECTION, Place, Translation } from "@fedify/vocab";
19
- import { FetchError, decodeMultibase, encodeMultibase, exportDidKey, exportMultibaseKey, formatIri, importMultibaseKey, parseIri } from "@fedify/vocab-runtime";
20
- import { createTestMeterProvider, mockDocumentLoader, test } from "@fedify/fixture";
21
- import { decodeHex } from "byte-encodings/hex";
22
- import jsonld from "@fedify/vocab-runtime/jsonld";
23
- import serialize from "json-canon";
24
- //#region src/sig/proof.test.ts
25
- const fep8b32TestVectorPrivateKey = await crypto.subtle.importKey("jwk", {
26
- "kty": "OKP",
27
- "crv": "Ed25519",
28
- "d": "yW756hDF5BTEcXI6_53nLDX6W3D66X6IMuysfS4rjtY",
29
- "x": "sA2Nk45_dz1RVlqtNqYj9TRPf10ZYPnPPo4SYg6igQ8",
30
- key_ops: ["sign"],
31
- ext: true
32
- }, "Ed25519", true, ["sign"]);
33
- const fep8b32TestVectorKeyId = new URL("https://server.example/users/alice#ed25519-key");
34
- const portableDid = await exportDidKey(ed25519PublicKey.publicKey);
35
- const portableDidMethod = portableDid.substring(8);
36
- const portableKeyId = new URL(`${portableDid}#${portableDidMethod}`);
37
- const portableContext = ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"];
38
- const portableActorContext = [...portableContext, "https://w3id.org/fep/ef61"];
39
- const portableProofCreated = "2023-02-24T23:36:38Z";
40
- async function signPortableJsonLd(document, { privateKey = ed25519PrivateKey, verificationMethod = portableKeyId, proofOptions = {} } = {}) {
41
- const proofConfig = {
42
- "@context": document["@context"],
43
- type: "DataIntegrityProof",
44
- cryptosuite: "eddsa-jcs-2022",
45
- verificationMethod: verificationMethod.href,
46
- proofPurpose: "assertionMethod",
47
- created: portableProofCreated,
48
- ...proofOptions
49
- };
50
- const encoder = new TextEncoder();
51
- const proofDigest = await crypto.subtle.digest("SHA-256", encoder.encode(serialize(proofConfig)));
52
- const messageDigest = await crypto.subtle.digest("SHA-256", encoder.encode(serialize(document)));
53
- const digest = new Uint8Array(proofDigest.byteLength + messageDigest.byteLength);
54
- digest.set(new Uint8Array(proofDigest), 0);
55
- digest.set(new Uint8Array(messageDigest), proofDigest.byteLength);
56
- const signature = await crypto.subtle.sign("Ed25519", privateKey, digest);
57
- return {
58
- ...document,
59
- proof: {
60
- ...proofConfig,
61
- proofValue: new TextDecoder().decode(encodeMultibase("base58btc", new Uint8Array(signature)))
62
- }
63
- };
64
- }
65
- async function parsePortableProof(document) {
66
- const proof = document.proof;
67
- assert(typeof proof === "object" && proof != null && !Array.isArray(proof));
68
- return await DataIntegrityProof.fromJsonLd({
69
- "@context": document["@context"],
70
- ...proof
71
- }, {
72
- documentLoader: mockDocumentLoader,
73
- contextLoader: mockDocumentLoader
74
- });
75
- }
76
- async function assertPortableProofVerified(document, options) {
77
- const key = await verifyProof(document, await parsePortableProof(document), options);
78
- assert(key != null);
79
- assertEquals(key.id, portableKeyId);
80
- }
81
- const fep8b32TestVectorActivity = new Create({
82
- id: new URL("https://server.example/activities/1"),
83
- actor: new URL("https://server.example/users/alice"),
84
- object: new Note({
85
- id: new URL("https://server.example/objects/1"),
86
- attribution: new URL("https://server.example/users/alice"),
87
- content: "Hello world",
88
- location: new Place({
89
- longitude: -71.184902,
90
- latitude: 25.273962
91
- })
92
- })
93
- });
94
- test("createProof()", async () => {
95
- const create = new Create({
96
- actor: new URL("https://example.com/person"),
97
- object: new Note({ content: "Hello, world!" })
98
- });
99
- const created = Temporal.Instant.from("2023-02-24T23:36:38Z");
100
- const proof = await createProof(create, ed25519PrivateKey, ed25519PublicKey.id, {
101
- created,
102
- contextLoader: mockDocumentLoader
103
- });
104
- assertEquals(proof.cryptosuite, "eddsa-jcs-2022");
105
- assertEquals(proof.verificationMethodId, ed25519PublicKey.id);
106
- assertEquals(proof.proofPurpose, "assertionMethod");
107
- assertEquals(proof.proofValue, decodeHex("0e63238fdb50a979a7fbd906b471d328a03504de7aa3a0409fad1500b85d6fecafa2223bfde21ba2eac9446d36f6583c45cb55a98017a0a6a275f50262a4ea06"));
108
- assertEquals(proof.created, created);
109
- assertEquals(await verifyProof(await create.toJsonLd({
110
- format: "compact",
111
- contextLoader: mockDocumentLoader
112
- }), proof, {
113
- documentLoader: mockDocumentLoader,
114
- contextLoader: mockDocumentLoader
115
- }), ed25519Multikey);
116
- const proof2 = await createProof(fep8b32TestVectorActivity, fep8b32TestVectorPrivateKey, fep8b32TestVectorKeyId, {
117
- created,
118
- contextLoader: mockDocumentLoader,
119
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
120
- });
121
- assertEquals(proof2.cryptosuite, "eddsa-jcs-2022");
122
- assertEquals(proof2.verificationMethodId, fep8b32TestVectorKeyId);
123
- assertEquals(proof2.proofPurpose, "assertionMethod");
124
- assertEquals(proof2.proofValue, decodeMultibase("zLaewdp4H9kqtwyrLatK4cjY5oRHwVcw4gibPSUDYDMhi4M49v8pcYk3ZB6D69dNpAPbUmY8ocuJ3m9KhKJEEg7z"));
125
- assertEquals(proof2.created, created);
126
- await assertRejects(() => createProof(create, rsaPrivateKey2, rsaPublicKey2.id, {
127
- created,
128
- contextLoader: mockDocumentLoader
129
- }), TypeError, "Unsupported algorithm");
130
- });
131
- test("signObject()", async () => {
132
- const options = {
133
- format: "compact",
134
- contextLoader: mockDocumentLoader,
135
- documentLoader: mockDocumentLoader,
136
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
137
- };
138
- const created = Temporal.Instant.from("2023-02-24T23:36:38Z");
139
- const signedObject = await signObject(fep8b32TestVectorActivity, fep8b32TestVectorPrivateKey, fep8b32TestVectorKeyId, {
140
- ...options,
141
- created
142
- });
143
- assertEquals(await signedObject.toJsonLd(options), {
144
- "@context": ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"],
145
- id: "https://server.example/activities/1",
146
- type: "Create",
147
- actor: "https://server.example/users/alice",
148
- object: {
149
- id: "https://server.example/objects/1",
150
- type: "Note",
151
- attributedTo: "https://server.example/users/alice",
152
- content: "Hello world",
153
- location: {
154
- type: "Place",
155
- longitude: -71.184902,
156
- latitude: 25.273962
157
- }
158
- },
159
- proof: {
160
- "@context": ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"],
161
- type: "DataIntegrityProof",
162
- cryptosuite: "eddsa-jcs-2022",
163
- verificationMethod: "https://server.example/users/alice#ed25519-key",
164
- proofPurpose: "assertionMethod",
165
- proofValue: "zLaewdp4H9kqtwyrLatK4cjY5oRHwVcw4gibPSUDYDMhi4M49v8pcYk3ZB6D69dNpAPbUmY8ocuJ3m9KhKJEEg7z",
166
- created: "2023-02-24T23:36:38Z"
167
- }
168
- });
169
- assertEquals(await (await signObject(signedObject, ed25519PrivateKey, ed25519Multikey.id, {
170
- ...options,
171
- created
172
- })).toJsonLd(options), {
173
- "@context": ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"],
174
- id: "https://server.example/activities/1",
175
- type: "Create",
176
- actor: "https://server.example/users/alice",
177
- object: {
178
- id: "https://server.example/objects/1",
179
- type: "Note",
180
- attributedTo: "https://server.example/users/alice",
181
- content: "Hello world",
182
- location: {
183
- type: "Place",
184
- longitude: -71.184902,
185
- latitude: 25.273962
186
- }
187
- },
188
- proof: [{
189
- "@context": ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"],
190
- type: "DataIntegrityProof",
191
- cryptosuite: "eddsa-jcs-2022",
192
- verificationMethod: "https://server.example/users/alice#ed25519-key",
193
- proofPurpose: "assertionMethod",
194
- proofValue: "zLaewdp4H9kqtwyrLatK4cjY5oRHwVcw4gibPSUDYDMhi4M49v8pcYk3ZB6D69dNpAPbUmY8ocuJ3m9KhKJEEg7z",
195
- created: "2023-02-24T23:36:38Z"
196
- }, {
197
- "@context": ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"],
198
- created: "2023-02-24T23:36:38Z",
199
- cryptosuite: "eddsa-jcs-2022",
200
- proofPurpose: "assertionMethod",
201
- proofValue: "zVrcY69MxozB9V9hmMmsjoB4YLCXvn6ienKr6jsP2rztSEr1WhMJymPqujKofkrV3C7A2C9iKYnRNSvtPgDQBCw2",
202
- type: "DataIntegrityProof",
203
- verificationMethod: "https://example.com/person2#key4"
204
- }]
205
- });
206
- await assertRejects(() => signObject(fep8b32TestVectorActivity, rsaPrivateKey2, rsaPublicKey2.id, {
207
- created,
208
- contextLoader: mockDocumentLoader
209
- }), TypeError, "Unsupported algorithm");
210
- const signed = await signObject(new Create({
211
- id: new URL("https://server.example/activities/2"),
212
- actor: new URL("https://server.example/users/alice"),
213
- object: new Note({
214
- id: new URL("https://server.example/objects/2"),
215
- attribution: new URL("https://server.example/users/alice"),
216
- content: "Hello public",
217
- attachments: [new Document({
218
- mediaType: "image/png",
219
- url: new URL("https://server.example/objects/2/image.png")
220
- })]
221
- }),
222
- tos: [PUBLIC_COLLECTION]
223
- }), fep8b32TestVectorPrivateKey, fep8b32TestVectorKeyId, {
224
- ...options,
225
- created
226
- });
227
- const [proof] = await Array.fromAsync(signed.getProofs(options));
228
- assertInstanceOf(proof, DataIntegrityProof);
229
- const signedJson = await normalizeOutgoingActivityJsonLd(await signed.toJsonLd(options), mockDocumentLoader);
230
- assertEquals(signedJson.to, PUBLIC_COLLECTION.href);
231
- const signedJsonObject = signedJson.object;
232
- assertEquals(Array.isArray(signedJsonObject.attachment), true);
233
- const verifyCache = {};
234
- const verifyOptions = {
235
- contextLoader: mockDocumentLoader,
236
- documentLoader: mockDocumentLoader,
237
- keyCache: {
238
- get: (keyId) => Promise.resolve(verifyCache[keyId.href]),
239
- set: (keyId, key) => {
240
- verifyCache[keyId.href] = key;
241
- return Promise.resolve();
242
- }
243
- }
244
- };
245
- assertInstanceOf(await verifyProof(signedJson, proof, verifyOptions), Multikey);
246
- const signedJsonWithCurie = await signed.toJsonLd(options);
247
- assertEquals(signedJsonWithCurie.to, "as:Public");
248
- const signedJsonWithCurieObject = signedJsonWithCurie.object;
249
- assertEquals(Array.isArray(signedJsonWithCurieObject.attachment), false);
250
- assertInstanceOf(await verifyProof(signedJsonWithCurie, proof, verifyOptions), Multikey);
251
- });
252
- test("hasProofLike()", () => {
253
- assert(hasProofLike({ proof: {
254
- type: "DataIntegrityProof",
255
- verificationMethod: "https://example.com/users/alice#main-key",
256
- proofPurpose: "assertionMethod",
257
- proofValue: "signature"
258
- } }));
259
- assert(hasProofLike({ proof: {
260
- type: "DataIntegrityProof",
261
- verificationMethod: { id: "https://example.com/users/alice#main-key" },
262
- proofPurpose: "assertionMethod",
263
- proofValue: "signature"
264
- } }));
265
- assert(hasProofLike({ proof: [{
266
- type: "DataIntegrityProof",
267
- verificationMethod: { id: "https://example.com/users/alice#main-key" },
268
- proofPurpose: "assertionMethod",
269
- proofValue: "signature"
270
- }] }));
271
- assert(hasProofLike({ proof: {
272
- type: ["https://w3id.org/security#DataIntegrityProof"],
273
- verificationMethod: [{ "@id": "https://example.com/users/alice#main-key" }],
274
- proofPurpose: { "@id": "https://w3id.org/security#assertionMethod" },
275
- proofValue: "signature"
276
- } }));
277
- assert(hasProofLike({ "https://w3id.org/security#proof": {
278
- type: "DataIntegrityProof",
279
- verificationMethod: { "@id": "https://example.com/users/alice#main-key" },
280
- proofPurpose: { "@id": "https://w3id.org/security#assertionMethod" },
281
- proofValue: "signature"
282
- } }));
283
- assert(hasProofLike({ "https://w3id.org/security#proof": [{
284
- "@type": ["https://w3id.org/security#DataIntegrityProof"],
285
- "https://w3id.org/security#verificationMethod": [{ "@id": "https://example.com/users/alice#main-key" }],
286
- "https://w3id.org/security#proofPurpose": [{ "@id": "https://w3id.org/security#assertionMethod" }],
287
- "https://w3id.org/security#proofValue": [{ "@value": "signature" }]
288
- }] }));
289
- assertFalse(hasProofLike({ proof: {
290
- type: "DataIntegrityProof",
291
- verificationMethod: { id: "https://example.com/users/alice#main-key" },
292
- proofPurpose: "assertionMethod"
293
- } }));
294
- });
295
- test("verifyProof()", async () => {
296
- const cache = {};
297
- const options = {
298
- documentLoader: mockDocumentLoader,
299
- contextLoader: mockDocumentLoader,
300
- keyCache: {
301
- get(keyId) {
302
- return Promise.resolve(cache[keyId.href]);
303
- },
304
- set(keyId, key) {
305
- cache[keyId.href] = key;
306
- return Promise.resolve();
307
- }
308
- }
309
- };
310
- const jsonLd = {
311
- "@context": ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"],
312
- id: "https://server.example/activities/1",
313
- type: "Create",
314
- actor: "https://server.example/users/alice",
315
- object: {
316
- id: "https://server.example/objects/1",
317
- type: "Note",
318
- attributedTo: "https://server.example/users/alice",
319
- content: "Hello world",
320
- location: {
321
- type: "Place",
322
- longitude: -71.184902,
323
- latitude: 25.273962
324
- }
325
- }
326
- };
327
- const proof = new DataIntegrityProof({
328
- cryptosuite: "eddsa-jcs-2022",
329
- verificationMethod: new URL("https://server.example/users/alice#ed25519-key"),
330
- proofPurpose: "assertionMethod",
331
- proofValue: decodeMultibase("zLaewdp4H9kqtwyrLatK4cjY5oRHwVcw4gibPSUDYDMhi4M49v8pcYk3ZB6D69dNpAPbUmY8ocuJ3m9KhKJEEg7z"),
332
- created: Temporal.Instant.from("2023-02-24T23:36:38Z")
333
- });
334
- const expectedKey = new Multikey({
335
- id: new URL("https://server.example/users/alice#ed25519-key"),
336
- controller: new URL("https://server.example/users/alice"),
337
- publicKey: await importMultibaseKey("z6MkrJVnaZkeFzdQyMZu1cgjg7k1pZZ6pvBQ7XJPt4swbTQ2")
338
- });
339
- assertEquals(await verifyProof(jsonLd, proof, options), expectedKey);
340
- assertEquals(cache["https://server.example/users/alice#ed25519-key"], expectedKey);
341
- cache["https://server.example/users/alice#ed25519-key"] = ed25519Multikey;
342
- assertEquals(await verifyProof(jsonLd, proof, options), expectedKey);
343
- assertEquals(cache["https://server.example/users/alice#ed25519-key"], expectedKey);
344
- assertEquals(await verifyProof({
345
- ...jsonLd,
346
- object: {
347
- ...jsonLd.object,
348
- content: "bye"
349
- }
350
- }, proof, options), null);
351
- assertEquals(await verifyProof(jsonLd, proof.clone({ created: Temporal.Now.instant() }), options), null);
352
- assertEquals(await verifyProof({
353
- ...jsonLd,
354
- "https://w3id.org/security#proof": {
355
- "@type": ["https://w3id.org/security#DataIntegrityProof"],
356
- "https://w3id.org/security#proofValue": [{ "@value": "stale" }]
357
- }
358
- }, proof, options), expectedKey);
359
- assertEquals(await verifyProof([jsonLd], proof, options), null);
360
- const attackerInput = {
361
- "@context": ["https://www.w3.org/ns/activitystreams", "https://attacker.example/ctx"],
362
- id: "https://server.example/activities/attacker",
363
- type: "Create",
364
- actor: "https://server.example/users/alice",
365
- object: {
366
- id: "https://server.example/objects/attacker",
367
- type: "Note",
368
- attributedTo: "https://server.example/users/alice",
369
- content: "n/a",
370
- to: "as:Public"
371
- }
372
- };
373
- const contextLoaderCalls = [];
374
- assertEquals(await verifyProof(attackerInput, proof, {
375
- contextLoader: async (url) => {
376
- contextLoaderCalls.push(url);
377
- return await mockDocumentLoader(url);
378
- },
379
- documentLoader: mockDocumentLoader,
380
- keyCache: options.keyCache
381
- }), null);
382
- assertFalse(contextLoaderCalls.includes("https://attacker.example/ctx"));
383
- });
384
- test("verifyProof() authenticates the complete proof configuration", async (t) => {
385
- const jsonLd = {
386
- "@context": portableContext,
387
- id: "https://example.com/activities/complete-proof-options",
388
- type: "Create",
389
- actor: "https://example.com/users/alice",
390
- object: {
391
- type: "Note",
392
- content: "Every proof option is authenticated."
393
- }
394
- };
395
- const options = {
396
- documentLoader: mockDocumentLoader,
397
- contextLoader: mockDocumentLoader
398
- };
399
- await t.step("rejects an expires option added after signing", async () => {
400
- const signed = await signPortableJsonLd(jsonLd);
401
- const tampered = {
402
- ...signed,
403
- proof: {
404
- ...signed.proof,
405
- expires: "3000-01-01T00:00:00Z"
406
- }
407
- };
408
- assertEquals(await verifyProof(tampered, await parsePortableProof(tampered), options), null);
409
- });
410
- await t.step("accepts an authenticated future expiration", async () => {
411
- await assertPortableProofVerified(await signPortableJsonLd(jsonLd, { proofOptions: { expires: "3000-01-01T00:00:00Z" } }), options);
412
- });
413
- await t.step("rejects an authenticated expired proof", async () => {
414
- const signed = await signPortableJsonLd(jsonLd, { proofOptions: { expires: "2000-01-01T00:00:00Z" } });
415
- assertEquals(await verifyProof(signed, await parsePortableProof(signed), options), null);
416
- });
417
- await t.step("authenticates domain, challenge, and nonce", async () => {
418
- const signed = await signPortableJsonLd(jsonLd, { proofOptions: {
419
- domain: ["social.example", "https://social.example"],
420
- challenge: "challenge-123",
421
- nonce: "nonce-456"
422
- } });
423
- const proof = await parsePortableProof(signed);
424
- const matchingKey = await verifyProof(signed, proof, {
425
- ...options,
426
- domain: ["https://social.example", "social.example"],
427
- challenge: "challenge-123"
428
- });
429
- assert(matchingKey != null);
430
- assertEquals(matchingKey.id, portableKeyId);
431
- assertEquals(await verifyProof(signed, proof, {
432
- ...options,
433
- domain: ["social.example"],
434
- challenge: "challenge-123"
435
- }), null);
436
- assertEquals(await verifyProof(signed, proof, {
437
- ...options,
438
- domain: ["https://social.example", "social.example"],
439
- challenge: "wrong-challenge"
440
- }), null);
441
- for (const [property, value] of [
442
- ["domain", ["other.example"]],
443
- ["challenge", "tampered-challenge"],
444
- ["nonce", "tampered-nonce"]
445
- ]) {
446
- const tampered = {
447
- ...signed,
448
- proof: {
449
- ...signed.proof,
450
- [property]: value
451
- }
452
- };
453
- assertEquals(await verifyProof(tampered, await parsePortableProof(tampered), options), null);
454
- }
455
- });
456
- await t.step("requires requested domain and challenge options", async () => {
457
- const signed = await signPortableJsonLd(jsonLd);
458
- const proof = await parsePortableProof(signed);
459
- assertEquals(await verifyProof(signed, proof, {
460
- ...options,
461
- domain: "social.example"
462
- }), null);
463
- assertEquals(await verifyProof(signed, proof, {
464
- ...options,
465
- challenge: "challenge-123"
466
- }), null);
467
- });
468
- await t.step("rejects malformed proof options", async () => {
469
- for (const proofOptions of [
470
- { expires: "not-a-date" },
471
- { domain: ["social.example", 42] },
472
- { challenge: 42 },
473
- { nonce: { value: "not-a-string" } },
474
- { nonce: [
475
- "not",
476
- "a",
477
- "string"
478
- ] },
479
- { previousProof: ["urn:uuid:proof-1", 42] }
480
- ]) {
481
- const signed = await signPortableJsonLd(jsonLd, { proofOptions });
482
- assertEquals(await verifyProof(signed, await parsePortableProof(signed), options), null);
483
- }
484
- });
485
- await t.step("authenticates aliased and extension options", async () => {
486
- const signed = await signPortableJsonLd({
487
- ...jsonLd,
488
- "@context": [...portableContext, {
489
- validUntil: {
490
- "@id": "https://w3id.org/security#expiration",
491
- "@type": "http://www.w3.org/2001/XMLSchema#dateTime"
492
- },
493
- extensionOption: "https://example.com/security#extensionOption"
494
- }]
495
- }, { proofOptions: {
496
- validUntil: "3000-01-01T00:00:00Z",
497
- extensionOption: { nested: ["one", "two"] }
498
- } });
499
- await assertPortableProofVerified(signed, options);
500
- for (const [property, value] of [["validUntil", "2999-01-01T00:00:00Z"], ["extensionOption", { nested: ["one", "changed"] }]]) {
501
- const tampered = {
502
- ...signed,
503
- proof: {
504
- ...signed.proof,
505
- [property]: value
506
- }
507
- };
508
- assertEquals(await verifyProof(tampered, await parsePortableProof(tampered), options), null);
509
- }
510
- });
511
- await t.step("distinguishes equivalent and ambiguous raw proof configurations", async () => {
512
- const signed = await signPortableJsonLd(jsonLd);
513
- const proof = signed.proof;
514
- const parsed = await parsePortableProof(signed);
515
- assert(await verifyProof({
516
- ...signed,
517
- proof: [proof, structuredClone(proof)]
518
- }, parsed, options) != null);
519
- const inheritedContextProof = structuredClone(proof);
520
- delete inheritedContextProof["@context"];
521
- assert(await verifyProof({
522
- ...signed,
523
- proof: [proof, inheritedContextProof]
524
- }, parsed, options) != null);
525
- assertEquals(await verifyProof({
526
- ...signed,
527
- proof: [proof, {
528
- ...structuredClone(proof),
529
- expires: "3000-01-01T00:00:00Z"
530
- }]
531
- }, parsed, options), null);
532
- });
533
- await t.step("accepts literal proof fields with an unresolved extra context", async () => {
534
- const signed = await signPortableJsonLd({
535
- ...jsonLd,
536
- "@context": [...portableContext, "https://context.example/unrelated"]
537
- });
538
- const rawProof = signed.proof;
539
- const key = await verifyProof(signed, new DataIntegrityProof({
540
- cryptosuite: "eddsa-jcs-2022",
541
- verificationMethod: portableKeyId,
542
- proofPurpose: "assertionMethod",
543
- proofValue: decodeMultibase(rawProof.proofValue),
544
- created: Temporal.Instant.from(portableProofCreated)
545
- }), options);
546
- assert(key != null);
547
- assertEquals(key.id, portableKeyId);
548
- });
549
- await t.step("rejects aliases whose proof context cannot be resolved safely", async () => {
550
- const contextUrl = "https://context.example/proof-options";
551
- const signed = await signPortableJsonLd({
552
- ...jsonLd,
553
- "@context": [...portableContext, contextUrl]
554
- }, { proofOptions: { validUntil: "2000-01-01T00:00:00Z" } });
555
- const rawProof = signed.proof;
556
- const proof = new DataIntegrityProof({
557
- cryptosuite: "eddsa-jcs-2022",
558
- verificationMethod: portableKeyId,
559
- proofPurpose: "assertionMethod",
560
- proofValue: decodeMultibase(rawProof.proofValue),
561
- created: Temporal.Instant.from(portableProofCreated)
562
- });
563
- const contextLoader = async (url) => {
564
- if (url !== contextUrl) return await mockDocumentLoader(url);
565
- return {
566
- contextUrl: null,
567
- documentUrl: url,
568
- document: { "@context": { validUntil: {
569
- "@id": "https://w3id.org/security#expiration",
570
- "@type": "http://www.w3.org/2001/XMLSchema#dateTime"
571
- } } }
572
- };
573
- };
574
- assertEquals(await verifyProof(signed, proof, {
575
- ...options,
576
- contextLoader
577
- }), null);
578
- });
579
- });
580
- test("verifyProof() resolves did:key verification methods locally", async () => {
581
- const multibaseKey = (await exportDidKey(ed25519PublicKey.publicKey)).slice(8);
582
- const did = `did:key:${multibaseKey}`;
583
- const keyId = new URL(`${did}#${multibaseKey}`);
584
- const created = Temporal.Instant.from("2023-02-24T23:36:38Z");
585
- const note = new Note({
586
- id: parseIri(`ap://did:key:${multibaseKey}/objects/1`),
587
- attribution: parseIri(`ap://did:key:${multibaseKey}/actor`),
588
- content: "Portable hello"
589
- });
590
- const proof = await createProof(note, ed25519PrivateKey, keyId, {
591
- created,
592
- contextLoader: mockDocumentLoader,
593
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
594
- });
595
- const jsonLd = await note.toJsonLd({
596
- format: "compact",
597
- contextLoader: mockDocumentLoader,
598
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
599
- });
600
- let documentLoaderCalls = 0;
601
- assertEquals(await verifyProof(jsonLd, proof, {
602
- contextLoader: mockDocumentLoader,
603
- documentLoader() {
604
- documentLoaderCalls++;
605
- throw new TypeError("did:key must not use the document loader");
606
- }
607
- }), new Multikey({
608
- id: keyId,
609
- controller: new URL(did),
610
- publicKey: ed25519PublicKey.publicKey
611
- }));
612
- assertEquals(documentLoaderCalls, 0);
613
- const tampered = { ...jsonLd };
614
- tampered.content = "Portable goodbye";
615
- assertEquals(await verifyProof(tampered, proof, {
616
- contextLoader: mockDocumentLoader,
617
- documentLoader() {
618
- throw new TypeError("did:key must not use the document loader");
619
- }
620
- }), null);
621
- assertEquals(await verifyProof(jsonLd, proof.clone({ verificationMethod: new URL(`${did}#z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK`) }), {
622
- contextLoader: mockDocumentLoader,
623
- documentLoader() {
624
- throw new TypeError("did:key must not use the document loader");
625
- }
626
- }), null);
627
- const unsupportedMultibaseKey = await exportMultibaseKey(rsaPublicKey2.publicKey);
628
- const unsupportedDid = `did:key:${unsupportedMultibaseKey}`;
629
- const unsupportedProof = proof.clone({ verificationMethod: new URL(`${unsupportedDid}#${unsupportedMultibaseKey}`) });
630
- let unsupportedDocumentLoaderCalls = 0;
631
- assertEquals(await verifyProof(jsonLd, unsupportedProof, {
632
- contextLoader: mockDocumentLoader,
633
- documentLoader() {
634
- unsupportedDocumentLoaderCalls++;
635
- throw new TypeError("did:key must not use the document loader");
636
- }
637
- }), null);
638
- assertEquals(unsupportedDocumentLoaderCalls, 0);
639
- });
640
- test("verifyProof() records verification duration metric", async (t) => {
641
- const jsonLd = {
642
- "@context": ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"],
643
- id: "https://server.example/activities/1",
644
- type: "Create",
645
- actor: "https://server.example/users/alice",
646
- object: {
647
- id: "https://server.example/objects/1",
648
- type: "Note",
649
- attributedTo: "https://server.example/users/alice",
650
- content: "Hello world",
651
- location: {
652
- type: "Place",
653
- longitude: -71.184902,
654
- latitude: 25.273962
655
- }
656
- }
657
- };
658
- const proof = new DataIntegrityProof({
659
- cryptosuite: "eddsa-jcs-2022",
660
- verificationMethod: new URL("https://server.example/users/alice#ed25519-key"),
661
- proofPurpose: "assertionMethod",
662
- proofValue: decodeMultibase("zLaewdp4H9kqtwyrLatK4cjY5oRHwVcw4gibPSUDYDMhi4M49v8pcYk3ZB6D69dNpAPbUmY8ocuJ3m9KhKJEEg7z"),
663
- created: Temporal.Instant.from("2023-02-24T23:36:38Z")
664
- });
665
- await t.step("verified path records result=verified with bounded cryptosuite", async () => {
666
- const [meterProvider, recorder] = createTestMeterProvider();
667
- assert(await verifyProof(jsonLd, proof, {
668
- documentLoader: mockDocumentLoader,
669
- contextLoader: mockDocumentLoader,
670
- meterProvider
671
- }) != null);
672
- const measurements = recorder.getMeasurements("activitypub.signature.verification.duration");
673
- assertEquals(measurements.length, 1);
674
- const m = measurements[0];
675
- assertEquals(m.type, "histogram");
676
- assertGreaterOrEqual(m.value, 0);
677
- assertEquals(m.attributes["activitypub.signature.kind"], "object_integrity");
678
- assertEquals(m.attributes["activitypub.signature.result"], "verified");
679
- assertEquals(m.attributes["object_integrity_proofs.cryptosuite"], "eddsa-jcs-2022");
680
- });
681
- await t.step("rejected path records result=rejected", async () => {
682
- const [meterProvider, recorder] = createTestMeterProvider();
683
- assertEquals(await verifyProof({
684
- ...jsonLd,
685
- object: {
686
- ...jsonLd.object,
687
- content: "bye"
688
- }
689
- }, proof, {
690
- documentLoader: mockDocumentLoader,
691
- contextLoader: mockDocumentLoader,
692
- meterProvider
693
- }), null);
694
- const measurements = recorder.getMeasurements("activitypub.signature.verification.duration");
695
- assertEquals(measurements.length, 1);
696
- assertEquals(measurements[0].attributes["activitypub.signature.result"], "rejected");
697
- assertEquals(measurements[0].attributes["object_integrity_proofs.cryptosuite"], "eddsa-jcs-2022");
698
- });
699
- await t.step("cached-key retry emits one measurement, not two", async () => {
700
- const [meterProvider, recorder] = createTestMeterProvider();
701
- const cache = { ["https://server.example/users/alice#ed25519-key"]: ed25519Multikey };
702
- assert(await verifyProof(jsonLd, proof, {
703
- documentLoader: mockDocumentLoader,
704
- contextLoader: mockDocumentLoader,
705
- meterProvider,
706
- keyCache: {
707
- get(id) {
708
- return Promise.resolve(cache[id.href]);
709
- },
710
- set(id, k) {
711
- cache[id.href] = k;
712
- return Promise.resolve();
713
- }
714
- }
715
- }) != null);
716
- assertEquals(recorder.getMeasurements("activitypub.signature.verification.duration").length, 1);
717
- const keyFetches = recorder.getMeasurements("activitypub.signature.key_fetch.duration");
718
- assertEquals(keyFetches.length, 2);
719
- assertEquals(keyFetches[0].attributes["activitypub.signature.key_fetch.result"], "hit");
720
- assertEquals(keyFetches[1].attributes["activitypub.signature.key_fetch.result"], "fetched");
721
- });
722
- await t.step("key fetch records result=fetched on a cold cache", async () => {
723
- const [meterProvider, recorder] = createTestMeterProvider();
724
- assert(await verifyProof(jsonLd, proof, {
725
- documentLoader: mockDocumentLoader,
726
- contextLoader: mockDocumentLoader,
727
- meterProvider
728
- }) != null);
729
- const measurements = recorder.getMeasurements("activitypub.signature.key_fetch.duration");
730
- assertEquals(measurements.length, 1);
731
- assertGreaterOrEqual(measurements[0].value, 0);
732
- assertEquals(measurements[0].attributes["activitypub.signature.kind"], "object_integrity");
733
- assertEquals(measurements[0].attributes["activitypub.signature.key_fetch.result"], "fetched");
734
- });
735
- await t.step("key fetch records result=hit when served from the key cache", async () => {
736
- const [meterProvider, recorder] = createTestMeterProvider();
737
- const cache = { "https://server.example/users/alice#ed25519-key": new Multikey({
738
- id: new URL("https://server.example/users/alice#ed25519-key"),
739
- controller: new URL("https://server.example/users/alice"),
740
- publicKey: await importMultibaseKey("z6MkrJVnaZkeFzdQyMZu1cgjg7k1pZZ6pvBQ7XJPt4swbTQ2")
741
- }) };
742
- assert(await verifyProof(jsonLd, proof, {
743
- documentLoader: mockDocumentLoader,
744
- contextLoader: mockDocumentLoader,
745
- meterProvider,
746
- keyCache: {
747
- get(id) {
748
- return Promise.resolve(cache[id.href]);
749
- },
750
- set(id, k) {
751
- cache[id.href] = k;
752
- return Promise.resolve();
753
- }
754
- }
755
- }) != null);
756
- const measurements = recorder.getMeasurements("activitypub.signature.key_fetch.duration");
757
- assertEquals(measurements.length, 1);
758
- assertEquals(measurements[0].attributes["activitypub.signature.key_fetch.result"], "hit");
759
- });
760
- await t.step("verifyObject() wrapper emits one measurement per inner verifyProof()", async () => {
761
- const [meterProvider, recorder] = createTestMeterProvider();
762
- const { "@context": _proofContext, ...embeddedProof } = await proof.toJsonLd({
763
- format: "compact",
764
- contextLoader: mockDocumentLoader
765
- });
766
- assert(await verifyObject(Create, {
767
- ...jsonLd,
768
- proof: embeddedProof
769
- }, {
770
- documentLoader: mockDocumentLoader,
771
- contextLoader: mockDocumentLoader,
772
- meterProvider
773
- }) != null);
774
- assertEquals(recorder.getMeasurements("activitypub.signature.verification.duration").length, 1);
775
- });
776
- await t.step("unknown cryptosuite omits the cryptosuite metric attribute", async () => {
777
- const [meterProvider, recorder] = createTestMeterProvider();
778
- const exoticProof = await DataIntegrityProof.fromJsonLd({
779
- "@context": "https://w3id.org/security/data-integrity/v1",
780
- type: "DataIntegrityProof",
781
- cryptosuite: "made-up-suite-9999",
782
- verificationMethod: "https://server.example/users/alice#ed25519-key",
783
- proofPurpose: "assertionMethod",
784
- proofValue: "zLaewdp4H9kqtwyrLatK4cjY5oRHwVcw4gibPSUDYDMhi4M49v8pcYk3ZB6D69dNpAPbUmY8ocuJ3m9KhKJEEg7z",
785
- created: "2023-02-24T23:36:38Z"
786
- }, {
787
- documentLoader: mockDocumentLoader,
788
- contextLoader: mockDocumentLoader
789
- });
790
- assertEquals(exoticProof.cryptosuite, "made-up-suite-9999");
791
- assertEquals(await verifyProof(jsonLd, exoticProof, {
792
- documentLoader: mockDocumentLoader,
793
- contextLoader: mockDocumentLoader,
794
- meterProvider
795
- }), null);
796
- const measurements = recorder.getMeasurements("activitypub.signature.verification.duration");
797
- assertEquals(measurements.length, 1);
798
- assertEquals(measurements[0].attributes["activitypub.signature.result"], "rejected");
799
- assertFalse("object_integrity_proofs.cryptosuite" in measurements[0].attributes);
800
- });
801
- });
802
- test("proof verification processes raw declaration contexts only for observers", async (t) => {
803
- const options = {
804
- documentLoader: mockDocumentLoader,
805
- contextLoader: mockDocumentLoader
806
- };
807
- const proofUrl = `ap://did:key:${portableDidMethod}/proofs/diagnostics`;
808
- const signed = await signPortableJsonLd({
809
- "@context": portableContext,
810
- id: `ap://did:key:${portableDidMethod}/objects/diagnostics`,
811
- type: "Note",
812
- attributedTo: `ap://did:key:${portableDidMethod}/actor`,
813
- content: "Observed proof"
814
- }, { proofOptions: { id: proofUrl } });
815
- const rawProof = signed.proof;
816
- const proof = await DataIntegrityProof.fromJsonLd(rawProof, options);
817
- const referenced = { ...signed };
818
- delete referenced.proof;
819
- referenced["https://w3id.org/security#proof"] = { "@graph": [{ "@id": proofUrl }] };
820
- const cases = [
821
- {
822
- name: "object",
823
- verify: async (options) => await verifyObject(Note, structuredClone(signed), options) != null
824
- },
825
- {
826
- name: "portable",
827
- verify: async (options) => (await verifyPortableObjectProof(structuredClone(signed), options)).verified
828
- },
829
- {
830
- name: "map-local",
831
- verify: async (options) => await verifyMapLocalProof(structuredClone(signed), options) != null
832
- },
833
- {
834
- name: "proof",
835
- verify: async (options) => await verifyProof(structuredClone(signed), proof, options) != null
836
- },
837
- {
838
- name: "remote proof",
839
- verify: async (options) => await verifyObject(Note, structuredClone(referenced), {
840
- ...options,
841
- documentLoader: (url) => Promise.resolve({
842
- contextUrl: null,
843
- documentUrl: url,
844
- document: structuredClone(rawProof)
845
- })
846
- }) != null
847
- }
848
- ];
849
- for (const { name, verify } of cases) await t.step(name, async () => {
850
- const original = jsonld.processContext;
851
- let calls = 0;
852
- jsonld.processContext = (...args) => {
853
- calls++;
854
- return original(...args);
855
- };
856
- try {
857
- assert(await verify(options));
858
- const unobservedCalls = calls;
859
- calls = 0;
860
- assert(await verify({
861
- ...options,
862
- [verificationObservation]: {
863
- attempts: [],
864
- attempt: { checks: [] }
865
- }
866
- }));
867
- assert(calls > unobservedCalls, "Raw declaration processing should be absent from unobserved verification");
868
- } finally {
869
- jsonld.processContext = original;
870
- }
871
- });
872
- });
873
- test("verifyPortableObjectProof()", async (t) => {
874
- const options = {
875
- documentLoader() {
876
- throw new TypeError("did:key must not use the document loader");
877
- },
878
- contextLoader: mockDocumentLoader
879
- };
880
- const objectId = `ap://did:key:${portableDidMethod}/objects/1`;
881
- const unsignedObject = {
882
- "@context": portableContext,
883
- id: objectId,
884
- type: "Note",
885
- attributedTo: `ap://did:key:${portableDidMethod}/actor`,
886
- content: "Portable note"
887
- };
888
- await t.step("verifies a portable object with a matching DID proof", async () => {
889
- const result = await verifyPortableObjectProof(await signPortableJsonLd(unsignedObject), options);
890
- assert(result.verified);
891
- assertEquals(result.keys.length, 1);
892
- assertEquals(result.keys[0].id, portableKeyId);
893
- });
894
- await t.step("rejects unauthenticated and expired proof options", async () => {
895
- const signed = await signPortableJsonLd(unsignedObject);
896
- const tamperedResult = await verifyPortableObjectProof({
897
- ...signed,
898
- proof: {
899
- ...signed.proof,
900
- expires: "3000-01-01T00:00:00Z"
901
- }
902
- }, options);
903
- assertFalse(tamperedResult.verified);
904
- assertEquals(tamperedResult.reason, {
905
- type: "invalidProof",
906
- proofIndex: 0
907
- });
908
- const expiredResult = await verifyPortableObjectProof(await signPortableJsonLd(unsignedObject, { proofOptions: { expires: "2000-01-01T00:00:00Z" } }), options);
909
- assertFalse(expiredResult.verified);
910
- assertEquals(expiredResult.reason, {
911
- type: "invalidProof",
912
- proofIndex: 0
913
- });
914
- });
915
- await t.step("verifies aliased proof properties", async () => {
916
- const proofAlias = "integrityProof";
917
- const proofIri = "https://w3id.org/security#proof";
918
- for (const { context, proofProperty, typeProperty, type } of [
919
- {
920
- context: { [proofAlias]: proofIri },
921
- proofProperty: proofAlias,
922
- typeProperty: "type",
923
- type: "Note"
924
- },
925
- {
926
- context: { PortableNote: {
927
- "@id": "https://www.w3.org/ns/activitystreams#Note",
928
- "@context": { [proofAlias]: proofIri }
929
- } },
930
- proofProperty: proofAlias,
931
- typeProperty: "type",
932
- type: "PortableNote"
933
- },
934
- {
935
- context: {
936
- kind: "@type",
937
- PortableNote: {
938
- "@id": "https://www.w3.org/ns/activitystreams#Note",
939
- "@context": { [proofAlias]: proofIri }
940
- }
941
- },
942
- proofProperty: proofAlias,
943
- typeProperty: "kind",
944
- type: "PortableNote"
945
- },
946
- {
947
- context: { sec: "https://w3id.org/security#" },
948
- proofProperty: "sec:proof",
949
- typeProperty: "type",
950
- type: "Note"
951
- },
952
- {
953
- context: { sec: {
954
- "@id": "https://w3id.org/security#",
955
- "@prefix": true
956
- } },
957
- proofProperty: "sec:proof",
958
- typeProperty: "type",
959
- type: "Note"
960
- }
961
- ]) {
962
- const document = {
963
- ...unsignedObject,
964
- "@context": [...portableContext, context]
965
- };
966
- delete document.type;
967
- document[typeProperty] = type;
968
- const { proof, ...signed } = await signPortableJsonLd(document);
969
- const result = await verifyPortableObjectProof({
970
- ...signed,
971
- [proofProperty]: proof
972
- }, options);
973
- assert(result.verified);
974
- assertEquals(result.keys.length, 1);
975
- assertEquals(result.keys[0].id, portableKeyId);
976
- }
977
- });
978
- await t.step("verifies proof aliases from caller-loaded contexts", async () => {
979
- const contextUrl = "https://context.example/security";
980
- const proofAlias = "integrityProof";
981
- const proofIri = "https://w3id.org/security#proof";
982
- let contextLoads = 0;
983
- const contextLoader = async (url) => {
984
- if (url !== contextUrl) return await mockDocumentLoader(url);
985
- contextLoads++;
986
- return {
987
- contextUrl: null,
988
- documentUrl: url,
989
- document: { "@context": { [proofAlias]: proofIri } }
990
- };
991
- };
992
- const { proof, ...signed } = await signPortableJsonLd({
993
- ...unsignedObject,
994
- "@context": [...portableContext, contextUrl]
995
- });
996
- const result = await verifyPortableObjectProof({
997
- ...signed,
998
- [proofAlias]: proof
999
- }, {
1000
- ...options,
1001
- contextLoader
1002
- });
1003
- assert(result.verified);
1004
- assertEquals(result.keys.length, 1);
1005
- assertEquals(result.keys[0].id, portableKeyId);
1006
- assertEquals(contextLoads, 1);
1007
- });
1008
- await t.step("verifies portable actors and activities by shape", async () => {
1009
- for (const document of [{
1010
- "@context": portableActorContext,
1011
- id: `ap+ef61://did:key:${portableDidMethod}/actor`,
1012
- type: "UnknownActorType",
1013
- inbox: "https://gateway.example/users/alice/inbox",
1014
- outbox: "https://gateway.example/users/alice/outbox",
1015
- gateways: ["https://gateway.example"]
1016
- }, {
1017
- "@context": portableContext,
1018
- id: `ap://did:key:${portableDidMethod}/activities/1`,
1019
- type: "UnknownActivityType",
1020
- actor: `ap://did:key:${portableDidMethod}/actor`,
1021
- object: objectId
1022
- }]) {
1023
- const result = await verifyPortableObjectProof(await signPortableJsonLd(document), options);
1024
- assert(result.verified);
1025
- assertEquals(result.keys.length, 1);
1026
- }
1027
- });
1028
- await t.step("requires portable actors to have valid gateways", async () => {
1029
- const actorIds = [
1030
- `ap://did:key:${portableDidMethod}/actor`,
1031
- `ap+ef61://did%3Akey%3A${portableDidMethod}/actor`,
1032
- `https://gateway.example/.well-known/apgateway/did:key:${portableDidMethod}/actor`
1033
- ];
1034
- const actor = (id, extra) => ({
1035
- "@context": portableActorContext,
1036
- id,
1037
- type: "Person",
1038
- inbox: `${id}/inbox`,
1039
- outbox: `${id}/outbox`,
1040
- ...extra
1041
- });
1042
- const validGateways = [
1043
- ["https://gateway.example"],
1044
- ["https://gateway.example/"],
1045
- ["https://gateway.example", "http://other.example:8080"]
1046
- ];
1047
- const invalidGateways = [
1048
- {},
1049
- { gateways: [] },
1050
- { gateways: ["https://gateway.example/path"] },
1051
- { gateways: ["https://gateway.example/?query"] },
1052
- { gateways: ["https://gateway.example/?"] },
1053
- { gateways: ["https://gateway.example/#fragment"] },
1054
- { gateways: ["https://gateway.example/#"] },
1055
- { gateways: ["ftp://gateway.example"] },
1056
- { gateways: ["https://user:password@gateway.example"] },
1057
- { gateways: ["gateway.example"] },
1058
- { gateways: ["https://gateway.example", "https://other.example/path"] }
1059
- ];
1060
- for (const id of actorIds) {
1061
- for (const gateways of validGateways) assert((await verifyPortableObjectProof(await signPortableJsonLd(actor(id, { gateways })), options)).verified, `${id} with ${gateways}`);
1062
- for (const extra of invalidGateways) assertEquals(await verifyPortableObjectProof(await signPortableJsonLd(actor(id, extra)), options), {
1063
- verified: false,
1064
- reason: { type: "invalidGateways" }
1065
- }, `${id} with ${JSON.stringify(extra)}`);
1066
- }
1067
- });
1068
- await t.step("reads gateways in any JSON-LD form", async () => {
1069
- const id = `ap://did:key:${portableDidMethod}/actor`;
1070
- const property = "https://w3id.org/fep/ef61/gateways";
1071
- const documents = [
1072
- {
1073
- "@context": portableContext,
1074
- [property]: { "@list": [{ "@id": "https://gateway.example" }] }
1075
- },
1076
- {
1077
- "@context": portableContext,
1078
- [property]: "https://gateway.example"
1079
- },
1080
- {
1081
- "@context": [...portableContext, { relays: {
1082
- "@id": property,
1083
- "@type": "@id",
1084
- "@container": "@list"
1085
- } }],
1086
- relays: ["https://gateway.example"]
1087
- }
1088
- ];
1089
- for (const document of documents) assert((await verifyPortableObjectProof(await signPortableJsonLd({
1090
- ...document,
1091
- id,
1092
- type: "Person",
1093
- inbox: `${id}/inbox`,
1094
- outbox: `${id}/outbox`
1095
- }), options)).verified, JSON.stringify(document));
1096
- assertEquals(await verifyPortableObjectProof(await signPortableJsonLd({
1097
- "@context": portableContext,
1098
- id,
1099
- type: "Person",
1100
- inbox: `${id}/inbox`,
1101
- outbox: `${id}/outbox`,
1102
- [property]: [{ "@list": [{ "@id": "https://gateway.example" }] }, { "@list": [{ "@id": "https://other.example" }] }]
1103
- }), options), {
1104
- verified: false,
1105
- reason: { type: "invalidGateways" }
1106
- });
1107
- });
1108
- await t.step("verifies JCS-signed actors with unmapped gateways", async () => {
1109
- for (const id of [`ap+ef61://${portableDid}/actor`, `https://gateway.example/.well-known/apgateway/${portableDid}/actor`]) {
1110
- const signed = await signPortableJsonLd({
1111
- "@context": portableContext,
1112
- id,
1113
- type: "Person",
1114
- inbox: `${id}/inbox`,
1115
- outbox: `${id}/outbox`,
1116
- gateways: ["https://gateway.example", "https://other.example"]
1117
- });
1118
- const original = structuredClone(signed);
1119
- assert((await verifyPortableObjectProof(signed, options)).verified);
1120
- assertEquals(signed, original);
1121
- assertEquals(await verifyPortableObjectProof({
1122
- ...signed,
1123
- gateways: ["https://attacker.example"]
1124
- }, options), {
1125
- verified: false,
1126
- reason: {
1127
- type: "invalidProof",
1128
- proofIndex: 0
1129
- }
1130
- });
1131
- }
1132
- });
1133
- await t.step("rejects malformed unmapped actor gateways", async () => {
1134
- const id = `ap+ef61://${portableDid}/actor`;
1135
- for (const gateways of [
1136
- [],
1137
- ["https://gateway.example", 42],
1138
- [{ bad: "https://gateway.example" }],
1139
- ["ftp://gateway.example"],
1140
- ["https://gateway.example/path"],
1141
- ["https://gateway.example/?query"],
1142
- ["https://gateway.example/#fragment"],
1143
- ["https://user:password@gateway.example"]
1144
- ]) assertEquals(await verifyPortableObjectProof(await signPortableJsonLd({
1145
- "@context": portableContext,
1146
- id,
1147
- type: "Person",
1148
- inbox: `${id}/inbox`,
1149
- outbox: `${id}/outbox`,
1150
- gateways
1151
- }), options), {
1152
- verified: false,
1153
- reason: { type: "invalidGateways" }
1154
- }, JSON.stringify(gateways));
1155
- });
1156
- await t.step("does not replace context-defined gateway semantics", async () => {
1157
- const id = `ap+ef61://${portableDid}/actor`;
1158
- const actor = {
1159
- "@context": portableContext,
1160
- id,
1161
- type: "Person",
1162
- inbox: `${id}/inbox`,
1163
- outbox: `${id}/outbox`,
1164
- gateways: ["https://gateway.example"]
1165
- };
1166
- const property = "https://w3id.org/fep/ef61/gateways";
1167
- for (const extra of [
1168
- { [property]: { "@list": [] } },
1169
- { [property]: { "@list": [{ "@id": "https://bad.example/path" }] } },
1170
- { "@context": [...portableContext, { gateways: null }] },
1171
- { "@context": [...portableContext, { gateways: "https://other.example/gateways" }] },
1172
- { "@context": [...portableContext, { Person: {
1173
- "@id": "https://www.w3.org/ns/activitystreams#Person",
1174
- "@context": { gateways: null }
1175
- } }] }
1176
- ]) assertEquals(await verifyPortableObjectProof(await signPortableJsonLd({
1177
- ...actor,
1178
- ...extra
1179
- }), options), {
1180
- verified: false,
1181
- reason: { type: "invalidGateways" }
1182
- }, JSON.stringify(extra));
1183
- assert((await verifyPortableObjectProof(await signPortableJsonLd({
1184
- ...actor,
1185
- gateways: ["https://bad.example/path"],
1186
- [property]: { "@list": [{ "@id": "https://canonical.example" }] }
1187
- }), options)).verified);
1188
- assertEquals(await verifyPortableObjectProof(await signPortableJsonLd(actor, { proofOptions: { cryptosuite: "eddsa-rdfc-2022" } }), options), {
1189
- verified: false,
1190
- reason: { type: "invalidGateways" }
1191
- });
1192
- });
1193
- await t.step("checks gateways only for FEP-2277 actors", async () => {
1194
- assert((await verifyPortableObjectProof(await signPortableJsonLd({
1195
- "@context": portableContext,
1196
- id: `ap://did:key:${portableDidMethod}/actor`,
1197
- type: "Person",
1198
- inbox: `ap://did:key:${portableDidMethod}/actor/inbox`
1199
- }), options)).verified);
1200
- assert((await verifyPortableObjectProof(await signPortableJsonLd(unsignedObject), options)).verified);
1201
- });
1202
- await t.step("checks gateways after the proof shape and before the keys", async () => {
1203
- const id = `ap://did:key:${portableDidMethod}/actor`;
1204
- const document = {
1205
- "@context": portableActorContext,
1206
- id,
1207
- type: "Person",
1208
- inbox: `${id}/inbox`,
1209
- outbox: `${id}/outbox`
1210
- };
1211
- assertEquals(await verifyPortableObjectProof(document, options), {
1212
- verified: false,
1213
- reason: { type: "missingProof" }
1214
- });
1215
- const keyIds = [];
1216
- const keyCache = {
1217
- get(keyId) {
1218
- keyIds.push(keyId.href);
1219
- return Promise.resolve(void 0);
1220
- },
1221
- set(keyId) {
1222
- keyIds.push(keyId.href);
1223
- return Promise.resolve();
1224
- }
1225
- };
1226
- assertEquals(await verifyPortableObjectProof({
1227
- ...await signPortableJsonLd(document),
1228
- name: "Tampered"
1229
- }, {
1230
- ...options,
1231
- keyCache
1232
- }), {
1233
- verified: false,
1234
- reason: { type: "invalidGateways" }
1235
- });
1236
- assertEquals(keyIds, []);
1237
- assert((await verifyPortableObjectProof(await signPortableJsonLd({
1238
- ...document,
1239
- gateways: ["https://gateway.example"]
1240
- }), {
1241
- ...options,
1242
- keyCache
1243
- })).verified);
1244
- assert(keyIds.length > 0);
1245
- });
1246
- await t.step("uses the normative FEP-2277 precedence order", async () => {
1247
- for (const document of [{
1248
- "@context": portableContext,
1249
- id: `ap://did:key:${portableDidMethod}/actor`,
1250
- type: "Collection",
1251
- inbox: "https://gateway.example/users/alice/inbox",
1252
- outbox: "https://gateway.example/users/alice/outbox",
1253
- totalItems: 0
1254
- }, {
1255
- "@context": portableContext,
1256
- id: `ap://did:key:${portableDidMethod}/activities/1`,
1257
- type: "Collection",
1258
- actor: `ap://did:key:${portableDidMethod}/actor`,
1259
- totalItems: 0
1260
- }]) assertEquals(await verifyPortableObjectProof(document, options), {
1261
- verified: false,
1262
- reason: { type: "missingProof" }
1263
- });
1264
- });
1265
- await t.step("accepts portable URI spelling and location-hint variants", async () => {
1266
- const variants = [
1267
- `ap+ef61://did%3Akey%3A${portableDidMethod}/objects/1`,
1268
- `AP://did:key:${portableDidMethod}/objects/1`,
1269
- `ap://did:key:${portableDidMethod}/objects/1?%40gateway=${encodeURIComponent("https://gateway.example")}`,
1270
- `ap://did:key:${portableDidMethod}/objects/1#content`
1271
- ];
1272
- for (const id of variants) assert((await verifyPortableObjectProof(await signPortableJsonLd({
1273
- ...unsignedObject,
1274
- id
1275
- }), options)).verified, id);
1276
- });
1277
- await t.step("supports matching DID methods resolved by a caller", async () => {
1278
- const verificationMethod = new URL("did:web:example.com#key");
1279
- const document = {
1280
- ...unsignedObject,
1281
- id: "ap://did:web:example.com/objects/1"
1282
- };
1283
- let fetches = 0;
1284
- assert((await verifyPortableObjectProof(await signPortableJsonLd(document, { verificationMethod }), {
1285
- contextLoader: mockDocumentLoader,
1286
- documentLoader: async (url) => {
1287
- fetches++;
1288
- assertEquals(url, verificationMethod.href);
1289
- return {
1290
- contextUrl: null,
1291
- documentUrl: url,
1292
- document: {
1293
- "@context": "https://w3id.org/security/multikey/v1",
1294
- id: verificationMethod.href,
1295
- type: "Multikey",
1296
- controller: "did:web:example.com",
1297
- publicKeyMultibase: await exportMultibaseKey(ed25519PublicKey.publicKey)
1298
- }
1299
- };
1300
- }
1301
- })).verified);
1302
- assertEquals(fetches, 1);
1303
- });
1304
- await t.step("caches keys owned by their own DID", async () => {
1305
- const verificationMethod = new URL("did:web:example.com#key");
1306
- const document = {
1307
- ...unsignedObject,
1308
- id: "ap://did:web:example.com/objects/1"
1309
- };
1310
- const cached = [];
1311
- assert((await verifyPortableObjectProof(await signPortableJsonLd(document, { verificationMethod }), {
1312
- contextLoader: mockDocumentLoader,
1313
- keyCache: {
1314
- get: () => Promise.resolve(void 0),
1315
- set: (keyId) => {
1316
- cached.push(keyId.href);
1317
- return Promise.resolve();
1318
- }
1319
- },
1320
- documentLoader: async (url) => ({
1321
- contextUrl: null,
1322
- documentUrl: url,
1323
- document: {
1324
- "@context": "https://w3id.org/security/multikey/v1",
1325
- id: verificationMethod.href,
1326
- type: "Multikey",
1327
- controller: "did:web:example.com",
1328
- publicKeyMultibase: await exportMultibaseKey(ed25519PublicKey.publicKey)
1329
- }
1330
- })
1331
- })).verified);
1332
- assertEquals(cached, [verificationMethod.href]);
1333
- });
1334
- await t.step("rejects a portable key that names another controller", async () => {
1335
- const verificationMethod = new URL("did:web:example.com#key");
1336
- assertEquals((await verifyPortableObjectProof(await signPortableJsonLd({
1337
- ...unsignedObject,
1338
- id: "ap://did:web:example.com/objects/1"
1339
- }, { verificationMethod }), {
1340
- contextLoader: mockDocumentLoader,
1341
- documentLoader: async (url) => ({
1342
- contextUrl: null,
1343
- documentUrl: url,
1344
- document: {
1345
- "@context": "https://w3id.org/security/multikey/v1",
1346
- id: verificationMethod.href,
1347
- type: "Multikey",
1348
- controller: "did:web:attacker.example",
1349
- publicKeyMultibase: await exportMultibaseKey(ed25519PublicKey.publicKey)
1350
- }
1351
- })
1352
- })).verified, false);
1353
- });
1354
- await t.step("reports a missing proof on portable objects", async () => {
1355
- assertEquals(await verifyPortableObjectProof(unsignedObject, options), {
1356
- verified: false,
1357
- reason: { type: "missingProof" }
1358
- });
1359
- });
1360
- await t.step("treats compatible identifiers as portable IDs", async () => {
1361
- const compatibleActorId = `https://gateway.example/.well-known/apgateway/did:key:${portableDidMethod}/actor`;
1362
- const actor = {
1363
- "@context": portableActorContext,
1364
- id: compatibleActorId,
1365
- type: "Person",
1366
- inbox: `${compatibleActorId}/inbox`,
1367
- outbox: `${compatibleActorId}/outbox`,
1368
- gateways: ["https://gateway.example"]
1369
- };
1370
- const signed = await signPortableJsonLd(actor);
1371
- const result = await verifyPortableObjectProof(signed, options);
1372
- assert(result.verified);
1373
- assertEquals(result.keys[0].id, portableKeyId);
1374
- assertEquals(signed.id, compatibleActorId);
1375
- assertEquals(await verifyPortableObjectProof(actor, options), {
1376
- verified: false,
1377
- reason: { type: "missingProof" }
1378
- });
1379
- const otherId = `https://gateway.example/.well-known/apgateway/did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK/activities/1`;
1380
- assertEquals(await verifyPortableObjectProof(await signPortableJsonLd({
1381
- "@context": portableContext,
1382
- id: otherId,
1383
- type: "Create",
1384
- actor: compatibleActorId,
1385
- object: "https://social.example/objects/1"
1386
- }), options), {
1387
- verified: false,
1388
- reason: {
1389
- type: "verificationMethodMismatch",
1390
- proofIndex: 0,
1391
- objectId: new URL(otherId),
1392
- verificationMethod: portableKeyId
1393
- }
1394
- });
1395
- await assertRejects(() => verifyPortableObjectProof({
1396
- ...actor,
1397
- id: `https://user@gateway.example/.well-known/apgateway/did:key:${portableDidMethod}/actor`
1398
- }, options), TypeError);
1399
- });
1400
- await t.step("keeps non-portable documents outside the policy", async () => {
1401
- for (const document of [
1402
- {
1403
- ...unsignedObject,
1404
- id: "https://social.example/objects/1"
1405
- },
1406
- {
1407
- ...unsignedObject,
1408
- id: "https://gateway.example/.well-known/apgateway/hl:zQmdfTbBqBPQ"
1409
- },
1410
- {
1411
- "@context": portableContext,
1412
- type: "Note",
1413
- content: "Object without an ID"
1414
- }
1415
- ]) assertEquals(await verifyPortableObjectProof(document, options), {
1416
- verified: false,
1417
- reason: { type: "notPortableObject" }
1418
- });
1419
- });
1420
- await t.step("short-circuits clearly non-portable raw IDs", async () => {
1421
- let contextLoads = 0;
1422
- assertEquals(await verifyPortableObjectProof({
1423
- "@context": "https://attacker.example/context",
1424
- "@id": "https://social.example/objects/1"
1425
- }, {
1426
- ...options,
1427
- contextLoader() {
1428
- contextLoads++;
1429
- throw new TypeError("unexpected context load");
1430
- }
1431
- }), {
1432
- verified: false,
1433
- reason: { type: "notPortableObject" }
1434
- });
1435
- assertEquals(contextLoads, 0);
1436
- });
1437
- await t.step("distinguishes unsecured and signed collections", async () => {
1438
- const collection = {
1439
- "@context": portableContext,
1440
- id: `ap://did:key:${portableDidMethod}/collections/1`,
1441
- type: "Note",
1442
- totalItems: 0
1443
- };
1444
- assertEquals(await verifyPortableObjectProof(collection, options), {
1445
- verified: false,
1446
- reason: { type: "unsecuredCollection" }
1447
- });
1448
- assert((await verifyPortableObjectProof(await signPortableJsonLd(collection), options)).verified);
1449
- });
1450
- await t.step("classifies unsupported core types without using type", async () => {
1451
- const cases = [
1452
- {
1453
- document: {
1454
- ...unsignedObject,
1455
- href: "https://example.com/"
1456
- },
1457
- objectType: "link"
1458
- },
1459
- {
1460
- document: {
1461
- ...unsignedObject,
1462
- "https://w3id.org/security#publicKeyMultibase": "z6MkrJVnaZkeFzdQyMZu1cgjg7k1pZZ6pvBQ7XJPt4swbTQ2"
1463
- },
1464
- objectType: "verificationMethod"
1465
- },
1466
- {
1467
- document: {
1468
- ...unsignedObject,
1469
- "https://w3id.org/security#publicKeyPem": "not-a-real-key"
1470
- },
1471
- objectType: "publicKey"
1472
- }
1473
- ];
1474
- for (const { document, objectType } of cases) assertEquals(await verifyPortableObjectProof(document, options), {
1475
- verified: false,
1476
- reason: {
1477
- type: "unsupportedObjectType",
1478
- objectType
1479
- }
1480
- });
1481
- });
1482
- await t.step("rejects non-DID verification methods before fetching", async () => {
1483
- let fetched = false;
1484
- const verificationMethod = new URL("https://attacker.example/keys/ed25519");
1485
- const result = await verifyPortableObjectProof(await signPortableJsonLd(unsignedObject, { verificationMethod }), {
1486
- contextLoader: mockDocumentLoader,
1487
- documentLoader() {
1488
- fetched = true;
1489
- throw new TypeError("unexpected fetch");
1490
- }
1491
- });
1492
- assertFalse(result.verified);
1493
- assertEquals(result.reason, {
1494
- type: "unsupportedVerificationMethod",
1495
- proofIndex: 0,
1496
- verificationMethod
1497
- });
1498
- assertFalse(fetched);
1499
- });
1500
- await t.step("rejects malformed DID verification methods", async () => {
1501
- const verificationMethod = new URL("did:invalid");
1502
- const result = await verifyPortableObjectProof(await signPortableJsonLd(unsignedObject, { verificationMethod }), options);
1503
- assertFalse(result.verified);
1504
- assertEquals(result.reason, {
1505
- type: "unsupportedVerificationMethod",
1506
- proofIndex: 0,
1507
- verificationMethod
1508
- });
1509
- });
1510
- await t.step("rejects a proof from another DID before fetching", async () => {
1511
- let fetched = false;
1512
- const verificationMethod = new URL("did:key:z6MkMallory#z6MkMallory");
1513
- const result = await verifyPortableObjectProof(await signPortableJsonLd(unsignedObject, { verificationMethod }), {
1514
- contextLoader: mockDocumentLoader,
1515
- documentLoader() {
1516
- fetched = true;
1517
- throw new TypeError("unexpected fetch");
1518
- }
1519
- });
1520
- assertFalse(result.verified);
1521
- assertEquals(result.reason, {
1522
- type: "verificationMethodMismatch",
1523
- proofIndex: 0,
1524
- objectId: parseIri(objectId),
1525
- verificationMethod
1526
- });
1527
- assertFalse(fetched);
1528
- });
1529
- await t.step("rejects proofs with multiple verification methods", async () => {
1530
- const signed = await signPortableJsonLd(unsignedObject);
1531
- const proof = signed.proof;
1532
- for (const additionalVerificationMethod of ["did:key:z6MkMallory#z6MkMallory", "https://attacker.example/key"]) assertEquals(await verifyPortableObjectProof({
1533
- ...signed,
1534
- proof: {
1535
- ...proof,
1536
- verificationMethod: [portableKeyId.href, additionalVerificationMethod]
1537
- }
1538
- }, options), {
1539
- verified: false,
1540
- reason: {
1541
- type: "invalidProof",
1542
- proofIndex: 0
1543
- }
1544
- });
1545
- });
1546
- await t.step("rejects multiple values in functional proof fields", async () => {
1547
- const signed = await signPortableJsonLd(unsignedObject);
1548
- const proof = signed.proof;
1549
- const cases = [
1550
- ["cryptosuite", "eddsa-jcs-2022"],
1551
- ["proofPurpose", "authentication"],
1552
- ["proofValue", proof.proofValue],
1553
- ["created", "2024-01-01T00:00:00Z"]
1554
- ];
1555
- for (const [field, additionalValue] of cases) {
1556
- const originalValue = proof[field];
1557
- assert(originalValue != null);
1558
- assertEquals(await verifyPortableObjectProof({
1559
- ...signed,
1560
- proof: {
1561
- ...proof,
1562
- [field]: [originalValue, additionalValue]
1563
- }
1564
- }, options), {
1565
- verified: false,
1566
- reason: {
1567
- type: "invalidProof",
1568
- proofIndex: 0
1569
- }
1570
- });
1571
- }
1572
- });
1573
- await t.step("requires exactly the DataIntegrityProof type", async () => {
1574
- const signed = await signPortableJsonLd(unsignedObject);
1575
- const proof = signed.proof;
1576
- const missingType = { ...proof };
1577
- delete missingType.type;
1578
- for (const invalidProof of [
1579
- missingType,
1580
- {
1581
- ...proof,
1582
- type: "Object"
1583
- },
1584
- {
1585
- ...proof,
1586
- type: ["DataIntegrityProof", "Object"]
1587
- }
1588
- ]) assertEquals(await verifyPortableObjectProof({
1589
- ...signed,
1590
- proof: invalidProof
1591
- }, options), {
1592
- verified: false,
1593
- reason: {
1594
- type: "invalidProof",
1595
- proofIndex: 0
1596
- }
1597
- });
1598
- });
1599
- await t.step("reports cryptographic failures separately", async () => {
1600
- assertEquals(await verifyPortableObjectProof({
1601
- ...await signPortableJsonLd(unsignedObject),
1602
- content: "Tampered"
1603
- }, options), {
1604
- verified: false,
1605
- reason: {
1606
- type: "invalidProof",
1607
- proofIndex: 0
1608
- }
1609
- });
1610
- });
1611
- await t.step("reports malformed proof structures as invalid", async () => {
1612
- for (const proof of [{ type: "DataIntegrityProof" }, {
1613
- type: "DataIntegrityProof",
1614
- cryptosuite: "eddsa-jcs-2022",
1615
- verificationMethod: portableKeyId.href,
1616
- proofPurpose: "assertionMethod",
1617
- proofValue: "zInvalid",
1618
- created: "not-an-instant"
1619
- }]) assertEquals(await verifyPortableObjectProof({
1620
- ...unsignedObject,
1621
- proof
1622
- }, options), {
1623
- verified: false,
1624
- reason: {
1625
- type: "invalidProof",
1626
- proofIndex: 0
1627
- }
1628
- });
1629
- });
1630
- await t.step("accepts multiple valid proofs in input order", async () => {
1631
- const signed = await signPortableJsonLd(unsignedObject);
1632
- const proof = signed.proof;
1633
- const result = await verifyPortableObjectProof({
1634
- ...signed,
1635
- proof: [proof, structuredClone(proof)]
1636
- }, options);
1637
- assert(result.verified);
1638
- assertEquals(result.keys.length, 2);
1639
- assertEquals(result.keys.map((key) => key.id), [portableKeyId, portableKeyId]);
1640
- });
1641
- await t.step("rejects a tampered duplicate proof", async () => {
1642
- const signed = await signPortableJsonLd(unsignedObject);
1643
- const proof = signed.proof;
1644
- assertFalse((await verifyPortableObjectProof({
1645
- ...signed,
1646
- proof: [proof, {
1647
- ...structuredClone(proof),
1648
- expires: "3000-01-01T00:00:00Z"
1649
- }]
1650
- }, options)).verified);
1651
- });
1652
- await t.step("matches proofs across aliased properties", async () => {
1653
- const document = {
1654
- ...unsignedObject,
1655
- "@context": [...portableContext, {
1656
- firstProof: "https://w3id.org/security#proof",
1657
- secondProof: "https://w3id.org/security#proof"
1658
- }]
1659
- };
1660
- const firstSigned = await signPortableJsonLd(document, { proofOptions: { domain: "first.example" } });
1661
- const secondSigned = await signPortableJsonLd(document, { proofOptions: { domain: "second.example" } });
1662
- const result = await verifyPortableObjectProof({
1663
- ...document,
1664
- secondProof: secondSigned.proof,
1665
- firstProof: firstSigned.proof
1666
- }, options);
1667
- assert(result.verified);
1668
- assertEquals(result.keys.length, 2);
1669
- });
1670
- await t.step("canonicalizes the document only once for multiple proofs", async () => {
1671
- async function countContentReads(proofCount) {
1672
- const signed = await signPortableJsonLd(unsignedObject);
1673
- const proof = signed.proof;
1674
- const document = {
1675
- ...signed,
1676
- proof: Array.from({ length: proofCount }, () => structuredClone(proof))
1677
- };
1678
- let contentReads = 0;
1679
- Object.defineProperty(document, "content", {
1680
- configurable: true,
1681
- enumerable: true,
1682
- get() {
1683
- contentReads++;
1684
- return unsignedObject.content;
1685
- }
1686
- });
1687
- const result = await verifyPortableObjectProof(document, options);
1688
- assert(result.verified);
1689
- assertEquals(result.keys.length, proofCount);
1690
- return contentReads;
1691
- }
1692
- assertEquals(await countContentReads(3), await countContentReads(1));
1693
- });
1694
- await t.step("requires every proof to verify", async () => {
1695
- const signed = await signPortableJsonLd(unsignedObject);
1696
- const proof = signed.proof;
1697
- assertEquals(await verifyPortableObjectProof({
1698
- ...signed,
1699
- proof: [proof, {
1700
- ...proof,
1701
- proofValue: "zInvalid"
1702
- }]
1703
- }, options), {
1704
- verified: false,
1705
- reason: {
1706
- type: "invalidProof",
1707
- proofIndex: 1
1708
- }
1709
- });
1710
- });
1711
- await t.step("checks every proof policy before resolving keys", async () => {
1712
- const didWebId = "ap://did:web:example.com/objects/1";
1713
- const didWebMethod = new URL("did:web:example.com#key");
1714
- const didWebSigned = await signPortableJsonLd({
1715
- ...unsignedObject,
1716
- id: didWebId
1717
- }, { verificationMethod: didWebMethod });
1718
- const httpMethod = new URL("https://attacker.example/key");
1719
- const httpSigned = await signPortableJsonLd({
1720
- ...unsignedObject,
1721
- id: didWebId
1722
- }, { verificationMethod: httpMethod });
1723
- let fetched = false;
1724
- const result = await verifyPortableObjectProof({
1725
- ...didWebSigned,
1726
- proof: [didWebSigned.proof, httpSigned.proof]
1727
- }, {
1728
- contextLoader: mockDocumentLoader,
1729
- documentLoader() {
1730
- fetched = true;
1731
- throw new TypeError("unexpected fetch");
1732
- }
1733
- });
1734
- assertFalse(result.verified);
1735
- assertEquals(result.reason, {
1736
- type: "unsupportedVerificationMethod",
1737
- proofIndex: 1,
1738
- verificationMethod: httpMethod
1739
- });
1740
- assertFalse(fetched);
1741
- });
1742
- await t.step("accepts the expanded proof property", async () => {
1743
- const { proof, ...document } = await signPortableJsonLd(unsignedObject);
1744
- const expandedProof = await (await DataIntegrityProof.fromJsonLd(proof, {
1745
- contextLoader: mockDocumentLoader,
1746
- documentLoader: mockDocumentLoader
1747
- })).toJsonLd({
1748
- format: "expand",
1749
- contextLoader: mockDocumentLoader
1750
- });
1751
- assert((await verifyPortableObjectProof({
1752
- ...document,
1753
- "https://w3id.org/security#proof": expandedProof
1754
- }, options)).verified);
1755
- });
1756
- await t.step("classifies JSON-LD aliases by their expanded properties", async () => {
1757
- assert((await verifyPortableObjectProof(await signPortableJsonLd({
1758
- "@context": [...portableContext, { performedBy: "https://www.w3.org/ns/activitystreams#actor" }],
1759
- id: `ap://did:key:${portableDidMethod}/activities/aliased`,
1760
- type: "UnknownActivity",
1761
- performedBy: `ap://did:key:${portableDidMethod}/actor`
1762
- }), options)).verified);
1763
- });
1764
- await t.step("does not claim to verify embedded portable objects", async () => {
1765
- assert((await verifyPortableObjectProof(await signPortableJsonLd({
1766
- "@context": portableContext,
1767
- id: `ap://did:key:${portableDidMethod}/activities/outer`,
1768
- type: "Create",
1769
- actor: `ap://did:key:${portableDidMethod}/actor`,
1770
- object: {
1771
- id: `ap://did:key:${portableDidMethod}/objects/embedded`,
1772
- type: "Note",
1773
- content: "Unsigned embedded object"
1774
- }
1775
- }), options)).verified);
1776
- });
1777
- await t.step("rejects malformed roots and portable IDs", async () => {
1778
- await assertRejects(() => verifyPortableObjectProof([], options), TypeError, "single JSON-LD object");
1779
- await assertRejects(() => verifyPortableObjectProof({
1780
- ...unsignedObject,
1781
- id: `ap://did:key:${portableDidMethod}`
1782
- }, options), TypeError);
1783
- await assertRejects(() => verifyPortableObjectProof({
1784
- ...unsignedObject,
1785
- id: "ap://did%ZZkey/objects/1"
1786
- }, options), TypeError);
1787
- });
1788
- await t.step("emits metrics only for attempted proof verification", async () => {
1789
- const [meterProvider, recorder] = createTestMeterProvider();
1790
- const signed = await signPortableJsonLd(unsignedObject);
1791
- const proof = signed.proof;
1792
- assert((await verifyPortableObjectProof({
1793
- ...signed,
1794
- proof: [proof, structuredClone(proof)]
1795
- }, {
1796
- ...options,
1797
- meterProvider
1798
- })).verified);
1799
- assertEquals(recorder.getMeasurements("activitypub.signature.verification.duration").length, 2);
1800
- const [rejectedMeterProvider, rejectedRecorder] = createTestMeterProvider();
1801
- await verifyPortableObjectProof(unsignedObject, {
1802
- ...options,
1803
- meterProvider: rejectedMeterProvider
1804
- });
1805
- assertEquals(rejectedRecorder.getMeasurements("activitypub.signature.verification.duration").length, 0);
1806
- });
1807
- });
1808
- test("verifyObject()", async () => {
1809
- const options = {
1810
- documentLoader: mockDocumentLoader,
1811
- contextLoader: mockDocumentLoader
1812
- };
1813
- const create = await verifyObject(Create, {
1814
- "@context": ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"],
1815
- id: "https://server.example/activities/1",
1816
- type: "Create",
1817
- actor: "https://server.example/users/alice",
1818
- object: {
1819
- id: "https://server.example/objects/1",
1820
- type: "Note",
1821
- attributedTo: "https://server.example/users/alice",
1822
- content: "Hello world",
1823
- location: {
1824
- type: "Place",
1825
- longitude: -71.184902,
1826
- latitude: 25.273962
1827
- }
1828
- },
1829
- proof: [{
1830
- type: "DataIntegrityProof",
1831
- cryptosuite: "eddsa-jcs-2022",
1832
- verificationMethod: "https://server.example/users/alice#ed25519-key",
1833
- proofPurpose: "assertionMethod",
1834
- proofValue: "zLaewdp4H9kqtwyrLatK4cjY5oRHwVcw4gibPSUDYDMhi4M49v8pcYk3ZB6D69dNpAPbUmY8ocuJ3m9KhKJEEg7z",
1835
- created: "2023-02-24T23:36:38Z"
1836
- }, {
1837
- created: "2023-02-24T23:36:38Z",
1838
- cryptosuite: "eddsa-jcs-2022",
1839
- proofPurpose: "assertionMethod",
1840
- proofValue: "zVrcY69MxozB9V9hmMmsjoB4YLCXvn6ienKr6jsP2rztSEr1WhMJymPqujKofkrV3C7A2C9iKYnRNSvtPgDQBCw2",
1841
- type: "DataIntegrityProof",
1842
- verificationMethod: "https://example.com/person2#key4"
1843
- }]
1844
- }, options);
1845
- assertInstanceOf(create, Create);
1846
- assertEquals(create.actorId, new URL("https://server.example/users/alice"));
1847
- const note = await create.getObject(options);
1848
- assertInstanceOf(note, Note);
1849
- assertEquals(note.content, "Hello world");
1850
- });
1851
- test("verifyObject() accepts did:key proofs for matching portable attribution origin", async () => {
1852
- const did = await exportDidKey(ed25519PublicKey.publicKey);
1853
- const keyId = new URL(`${did}#${did.substring(8)}`);
1854
- const jsonLd = await (await signObject(new Note({
1855
- id: parseIri(`ap://did:key:${did.substring(8)}/objects/1`),
1856
- attribution: parseIri(`ap://did:key:${did.substring(8)}/actor`),
1857
- content: "Portable note"
1858
- }), ed25519PrivateKey, keyId, {
1859
- contextLoader: mockDocumentLoader,
1860
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
1861
- })).toJsonLd({
1862
- format: "compact",
1863
- contextLoader: mockDocumentLoader,
1864
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
1865
- });
1866
- const verified = await verifyObject(Note, jsonLd, {
1867
- documentLoader() {
1868
- throw new TypeError("did:key must not use the document loader");
1869
- },
1870
- contextLoader: mockDocumentLoader
1871
- });
1872
- assertInstanceOf(verified, Note);
1873
- assertEquals(verified.content, "Portable note");
1874
- assert(typeof jsonLd === "object" && jsonLd != null && !Array.isArray(jsonLd));
1875
- const rawProof = jsonLd.proof;
1876
- assert(typeof rawProof === "object" && rawProof != null && !Array.isArray(rawProof));
1877
- assertEquals(await verifyObject(Note, {
1878
- ...jsonLd,
1879
- proof: [rawProof, {
1880
- ...structuredClone(rawProof),
1881
- expires: "3000-01-01T00:00:00Z"
1882
- }]
1883
- }, {
1884
- documentLoader() {
1885
- throw new TypeError("did:key must not use the document loader");
1886
- },
1887
- contextLoader: mockDocumentLoader
1888
- }), null);
1889
- async function verifyRemotelyReferencedProof(proofOptions = {}, proofUrl = "https://proof.example/proofs/1") {
1890
- const remotelyReferencedJsonLd = { ...jsonLd };
1891
- delete remotelyReferencedJsonLd.proof;
1892
- remotelyReferencedJsonLd["https://w3id.org/security#proof"] = [{ "@graph": [rawProof] }, { "@graph": [{ "@id": proofUrl }] }];
1893
- let proofFetches = 0;
1894
- const result = await verifyObject(Note, remotelyReferencedJsonLd, {
1895
- documentLoader(url) {
1896
- if (url !== formatIri(parseIri(proofUrl))) throw new TypeError(`unexpected document URL: ${url}`);
1897
- proofFetches++;
1898
- return Promise.resolve({
1899
- contextUrl: null,
1900
- document: {
1901
- "@context": jsonLd["@context"],
1902
- ...structuredClone(rawProof),
1903
- ...proofOptions
1904
- },
1905
- documentUrl: url
1906
- });
1907
- },
1908
- contextLoader: mockDocumentLoader
1909
- });
1910
- assertEquals(proofFetches, 1);
1911
- return result;
1912
- }
1913
- assertInstanceOf(await verifyRemotelyReferencedProof(), Note);
1914
- assertInstanceOf(await verifyRemotelyReferencedProof({}, `ap://did:key:${did.substring(8)}/proofs/1`), Note);
1915
- assertEquals(await verifyRemotelyReferencedProof({ expires: "3000-01-01T00:00:00Z" }), null);
1916
- });
1917
- test("verifyObject() hydrates pending proof references", async () => {
1918
- const did = await exportDidKey(ed25519PublicKey.publicKey);
1919
- const method = did.substring(8);
1920
- const keyId = new URL(`${did}#${method}`);
1921
- const proofUrl = `ap://did:key:${method}/proofs/1`;
1922
- const signed = await signPortableJsonLd({
1923
- "@context": portableContext,
1924
- id: `ap://did:key:${method}/objects/1`,
1925
- type: "Note",
1926
- attributedTo: `ap://did:key:${method}/actor`,
1927
- content: "Portable note with a referenced proof"
1928
- }, {
1929
- verificationMethod: keyId,
1930
- proofOptions: { id: proofUrl }
1931
- });
1932
- const rawProof = signed.proof;
1933
- const remotelyReferencedJsonLd = { ...signed };
1934
- delete remotelyReferencedJsonLd.proof;
1935
- remotelyReferencedJsonLd["https://w3id.org/security#proof"] = [{ "@graph": [rawProof] }, { "@graph": [{ "@id": proofUrl }] }];
1936
- let proofFetches = 0;
1937
- const verified = await verifyObject(Note, remotelyReferencedJsonLd, {
1938
- documentLoader(url) {
1939
- assertEquals(url, formatIri(parseIri(proofUrl)));
1940
- proofFetches++;
1941
- return Promise.resolve({
1942
- contextUrl: null,
1943
- document: structuredClone(rawProof),
1944
- documentUrl: url
1945
- });
1946
- },
1947
- contextLoader: mockDocumentLoader
1948
- });
1949
- assertEquals(proofFetches, 1);
1950
- assertInstanceOf(verified, Note);
1951
- });
1952
- test("verifyObject() does not pass its proof verifier on to cached proofs", async () => {
1953
- const did = await exportDidKey(ed25519PublicKey.publicKey);
1954
- const method = did.substring(8);
1955
- const keyId = new URL(`${did}#${method}`);
1956
- const proofUrl = `ap://did:key:${method}/proofs/1`;
1957
- const signed = await signPortableJsonLd({
1958
- "@context": portableContext,
1959
- id: `ap://did:key:${method}/objects/1`,
1960
- type: "Note",
1961
- attributedTo: `ap://did:key:${method}/actor`,
1962
- content: "Portable note with a referenced proof"
1963
- }, {
1964
- verificationMethod: keyId,
1965
- proofOptions: { id: proofUrl }
1966
- });
1967
- const rawProof = signed.proof;
1968
- const referencedJsonLd = { ...signed };
1969
- delete referencedJsonLd.proof;
1970
- referencedJsonLd["https://w3id.org/security#proof"] = [{ "@graph": [rawProof] }, { "@graph": [{ "@id": proofUrl }] }];
1971
- const rootVerifier = async () => ({ verified: false });
1972
- for (const verifyPortableObject of [void 0, rootVerifier]) {
1973
- let proofFetches = 0;
1974
- const verified = await verifyObject(Note, referencedJsonLd, {
1975
- documentLoader(url) {
1976
- proofFetches++;
1977
- return Promise.resolve({
1978
- contextUrl: null,
1979
- document: structuredClone(rawProof),
1980
- documentUrl: url
1981
- });
1982
- },
1983
- contextLoader: mockDocumentLoader,
1984
- ...verifyPortableObject == null ? {} : { verifyPortableObject }
1985
- });
1986
- assertInstanceOf(verified, Note);
1987
- assertEquals(proofFetches, 1);
1988
- const proofs = await Array.fromAsync(verified.getProofs());
1989
- assertEquals(proofs.length, 2);
1990
- assertEquals(proofFetches, 1);
1991
- for (const proof of proofs) assert(proof._verifyPortableObject === verifyPortableObject);
1992
- }
1993
- });
1994
- test("verifyObject() hydrates portable proof references with gateway hints", async () => {
1995
- const did = await exportDidKey(ed25519PublicKey.publicKey);
1996
- const method = did.substring(8);
1997
- const keyId = new URL(`${did}#${method}`);
1998
- const proofId = `ap://did:key:${method}/proofs/1`;
1999
- const proofReference = `${proofId}?@gateway=https%3A%2F%2Fgateway.example`;
2000
- const signed = await signPortableJsonLd({
2001
- "@context": portableContext,
2002
- id: `ap://did:key:${method}/objects/1`,
2003
- type: "Note",
2004
- attributedTo: `ap://did:key:${method}/actor`,
2005
- content: "Portable note with a hinted proof reference"
2006
- }, {
2007
- verificationMethod: keyId,
2008
- proofOptions: { id: proofId }
2009
- });
2010
- const rawProof = signed.proof;
2011
- const referencedJsonLd = { ...signed };
2012
- delete referencedJsonLd.proof;
2013
- referencedJsonLd["https://w3id.org/security#proof"] = [{ "@graph": [rawProof] }, { "@graph": [{ "@id": proofReference }] }];
2014
- const fetched = [];
2015
- const verified = await verifyObject(Note, referencedJsonLd, {
2016
- documentLoader(url) {
2017
- fetched.push(url);
2018
- return Promise.resolve({
2019
- contextUrl: null,
2020
- document: structuredClone(rawProof),
2021
- documentUrl: url
2022
- });
2023
- },
2024
- contextLoader: mockDocumentLoader
2025
- });
2026
- assertEquals(fetched, [formatIri(parseIri(proofReference))]);
2027
- assertInstanceOf(verified, Note);
2028
- });
2029
- test("verifyObject() rejects portable proof references it cannot dereference", async () => {
2030
- const did = await exportDidKey(ed25519PublicKey.publicKey);
2031
- const method = did.substring(8);
2032
- const keyId = new URL(`${did}#${method}`);
2033
- const proofUrl = `ap://did:key:${method}/proofs/2`;
2034
- const signed = await signPortableJsonLd({
2035
- "@context": portableContext,
2036
- id: `ap://did:key:${method}/objects/1`,
2037
- type: "Note",
2038
- attributedTo: `ap://did:key:${method}/actor`,
2039
- content: "Portable note with an extra referenced proof"
2040
- }, { verificationMethod: keyId });
2041
- const rawProof = signed.proof;
2042
- const withProofs = (proofs) => {
2043
- const referencedJsonLd = { ...signed };
2044
- delete referencedJsonLd.proof;
2045
- referencedJsonLd["https://w3id.org/security#proof"] = proofs;
2046
- return referencedJsonLd;
2047
- };
2048
- const embedded = { "@graph": [rawProof] };
2049
- const referenced = { "@graph": [{ "@id": proofUrl }] };
2050
- for (const [id, referencedJsonLd] of [
2051
- [`ap://did:key:${method}/proofs/3`, withProofs([embedded, referenced])],
2052
- [`ap://did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK/proofs/2`, withProofs([embedded, referenced])],
2053
- [`ap://did:key:${method}/proofs/3`, withProofs([referenced, embedded])]
2054
- ]) assertEquals(await verifyObject(Note, referencedJsonLd, {
2055
- documentLoader(url) {
2056
- return Promise.resolve({
2057
- contextUrl: null,
2058
- document: {
2059
- ...rawProof,
2060
- id
2061
- },
2062
- documentUrl: url
2063
- });
2064
- },
2065
- contextLoader: mockDocumentLoader
2066
- }), null);
2067
- });
2068
- test("verifyObject() accepts multiple portable attributions from the same did:key origin", async () => {
2069
- const did = await exportDidKey(ed25519PublicKey.publicKey);
2070
- const method = did.substring(8);
2071
- const keyId = new URL(`${did}#${method}`);
2072
- const verified = await verifyObject(Create, await (await signObject(new Create({
2073
- id: parseIri(`ap://did:key:${method}/activities/1`),
2074
- actor: parseIri(`ap://did:key:${method}/actor`),
2075
- attribution: parseIri(`ap://did:key:${method}/profile`),
2076
- object: new Note({
2077
- id: parseIri(`ap://did:key:${method}/objects/1`),
2078
- content: "Portable note"
2079
- })
2080
- }), ed25519PrivateKey, keyId, {
2081
- contextLoader: mockDocumentLoader,
2082
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
2083
- })).toJsonLd({
2084
- format: "compact",
2085
- contextLoader: mockDocumentLoader,
2086
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
2087
- }), {
2088
- documentLoader() {
2089
- throw new TypeError("did:key must not use the document loader");
2090
- },
2091
- contextLoader: mockDocumentLoader
2092
- });
2093
- assertInstanceOf(verified, Create);
2094
- assertEquals(verified.actorId, parseIri(`ap://did:key:${method}/actor`));
2095
- assertEquals(verified.attributionId, parseIri(`ap://did:key:${method}/profile`));
2096
- });
2097
- test("verifyObject() rejects HTTPS keys claiming a portable DID controller", async () => {
2098
- const victimDid = "did:key:z6MkVictim";
2099
- const keyId = new URL("https://attacker.example/keys/ed25519");
2100
- assertEquals(await verifyObject(Note, await (await signObject(new Note({
2101
- id: parseIri("ap://did:key:z6MkVictim/objects/1"),
2102
- attribution: parseIri("ap://did:key:z6MkVictim/actor"),
2103
- content: "Spoofed portable note"
2104
- }), ed25519PrivateKey, keyId, {
2105
- contextLoader: mockDocumentLoader,
2106
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
2107
- })).toJsonLd({
2108
- format: "compact",
2109
- contextLoader: mockDocumentLoader,
2110
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
2111
- }), {
2112
- documentLoader: async (url) => {
2113
- if (url === keyId.href) return {
2114
- documentUrl: url,
2115
- contextUrl: null,
2116
- document: {
2117
- "@context": "https://w3id.org/security/multikey/v1",
2118
- id: keyId.href,
2119
- type: "Multikey",
2120
- controller: victimDid,
2121
- publicKeyMultibase: await exportMultibaseKey(ed25519PublicKey.publicKey)
2122
- }
2123
- };
2124
- throw new TypeError(`Unexpected fetch: ${url}`);
2125
- },
2126
- contextLoader: mockDocumentLoader
2127
- }), null);
2128
- });
2129
- test("verifyObject() rejects HTTPS keys claiming an exact DID attribution", async () => {
2130
- const victimDid = "did:key:z6MkVictim";
2131
- const keyId = new URL("https://attacker.example/keys/ed25519");
2132
- assertEquals(await verifyObject(Note, await (await signObject(new Note({
2133
- id: parseIri("ap://did:key:z6MkVictim/objects/1"),
2134
- attribution: new URL(victimDid),
2135
- content: "Spoofed DID-attributed note"
2136
- }), ed25519PrivateKey, keyId, {
2137
- contextLoader: mockDocumentLoader,
2138
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
2139
- })).toJsonLd({
2140
- format: "compact",
2141
- contextLoader: mockDocumentLoader,
2142
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
2143
- }), {
2144
- documentLoader: async (url) => {
2145
- if (url === keyId.href) return {
2146
- documentUrl: url,
2147
- contextUrl: null,
2148
- document: {
2149
- "@context": "https://w3id.org/security/multikey/v1",
2150
- id: keyId.href,
2151
- type: "Multikey",
2152
- controller: victimDid,
2153
- publicKeyMultibase: await exportMultibaseKey(ed25519PublicKey.publicKey)
2154
- }
2155
- };
2156
- throw new TypeError(`Unexpected fetch: ${url}`);
2157
- },
2158
- contextLoader: mockDocumentLoader
2159
- }), null);
2160
- });
2161
- test("verifyObject() rejects did:key proofs from another portable attribution origin", async () => {
2162
- const did = await exportDidKey(ed25519PublicKey.publicKey);
2163
- const keyId = new URL(`${did}#${did.substring(8)}`);
2164
- assertEquals(await verifyObject(Note, await (await signObject(new Note({
2165
- id: parseIri(`ap://did:key:${did.substring(8)}/objects/1`),
2166
- attribution: parseIri("ap://did:key:z6MkOther/actor"),
2167
- content: "Portable note"
2168
- }), ed25519PrivateKey, keyId, {
2169
- contextLoader: mockDocumentLoader,
2170
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
2171
- })).toJsonLd({
2172
- format: "compact",
2173
- contextLoader: mockDocumentLoader,
2174
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
2175
- }), {
2176
- documentLoader() {
2177
- throw new TypeError("did:key must not use the document loader");
2178
- },
2179
- contextLoader: mockDocumentLoader
2180
- }), null);
2181
- });
2182
- async function signCompatibleNote(attribution, keyId) {
2183
- const context = ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"];
2184
- return await (await signObject(new Note({
2185
- id: new URL(`${attribution}/notes/1`),
2186
- attribution: new URL(attribution),
2187
- content: "Note of a compatible-ID actor"
2188
- }), ed25519PrivateKey, keyId, {
2189
- contextLoader: mockDocumentLoader,
2190
- context
2191
- })).toJsonLd({
2192
- format: "compact",
2193
- contextLoader: mockDocumentLoader,
2194
- context
2195
- });
2196
- }
2197
- test("verifyObject() authenticates compatible-ID attributions by DID proofs", async () => {
2198
- const did = await exportDidKey(ed25519PublicKey.publicKey);
2199
- const keyId = new URL(`${did}#${did.substring(8)}`);
2200
- const options = {
2201
- documentLoader() {
2202
- throw new TypeError("did:key must not use the document loader");
2203
- },
2204
- contextLoader: mockDocumentLoader
2205
- };
2206
- for (const gateway of ["https://gw1.example", "https://gw2.example"]) assertInstanceOf(await verifyObject(Note, await signCompatibleNote(`${gateway}/.well-known/apgateway/${did}/actor`, keyId), options), Note);
2207
- assertEquals(await verifyObject(Note, await signCompatibleNote("https://gw1.example/.well-known/apgateway/did:key:z6MkOther/actor", keyId), options), null);
2208
- });
2209
- test("verifyObject() does not trust compatible-ID actors by web origin", async () => {
2210
- const did = await exportDidKey(ed25519PublicKey.publicKey);
2211
- const attribution = `https://evil.example/.well-known/apgateway/${did}/actor`;
2212
- const keyId = new URL("https://evil.example/keys/1");
2213
- const documents = {
2214
- [keyId.href]: {
2215
- "@context": "https://w3id.org/security/multikey/v1",
2216
- id: keyId.href,
2217
- type: "Multikey",
2218
- controller: attribution,
2219
- publicKeyMultibase: await exportMultibaseKey(ed25519PublicKey.publicKey)
2220
- },
2221
- [attribution]: {
2222
- "@context": ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/multikey/v1"],
2223
- id: attribution,
2224
- type: "Person",
2225
- inbox: `${attribution}/inbox`,
2226
- assertionMethod: keyId.href
2227
- }
2228
- };
2229
- const fetched = [];
2230
- assertEquals(await verifyObject(Note, await signCompatibleNote(attribution, keyId), {
2231
- documentLoader: (url) => {
2232
- fetched.push(url);
2233
- const document = documents[url];
2234
- if (document == null) throw new TypeError(`Unexpected: ${url}`);
2235
- return Promise.resolve({
2236
- contextUrl: null,
2237
- documentUrl: url,
2238
- document
2239
- });
2240
- },
2241
- contextLoader: mockDocumentLoader
2242
- }), null);
2243
- assertEquals(fetched, [keyId.href]);
2244
- assertEquals(await verifyObject(Note, await signCompatibleNote(`https://user@gw.example/.well-known/apgateway/${did}/actor`, new URL(`${did}#${did.substring(8)}`)), { contextLoader: mockDocumentLoader }), null);
2245
- });
2246
- test("signObject() preserves FEP-22cd contexts through proof verification", async () => {
2247
- const did = await exportDidKey(ed25519PublicKey.publicKey);
2248
- const keyId = new URL(`${did}#${did.substring(8)}`);
2249
- const id = parseIri(`ap://${did}/objects/translation`);
2250
- const json = await (await signObject(new Note({
2251
- id,
2252
- attribution: parseIri(`ap://${did}/actor`),
2253
- content: "A translated note",
2254
- translations: [new Translation({
2255
- language: new Intl.Locale("ko"),
2256
- translator: new URL("https://example.com/translator"),
2257
- original: id
2258
- })]
2259
- }), ed25519PrivateKey, keyId)).toJsonLd({ format: "compact" });
2260
- assert(typeof json === "object" && json != null && "@context" in json && "proof" in json);
2261
- assertEquals(json.proof["@context"], json["@context"]);
2262
- const verified = await verifyObject(Note, json, { documentLoader() {
2263
- throw new Error("No document fetch expected");
2264
- } });
2265
- assertInstanceOf(verified, Note);
2266
- assertEquals(verified.translations[0].language?.baseName, "ko");
2267
- });
2268
- async function signLicensedNote(license, context = []) {
2269
- return await signPortableJsonLd({
2270
- "@context": [...portableContext, ...context],
2271
- id: `ap+ef61://${portableDid}/objects/licensed`,
2272
- type: "Note",
2273
- attributedTo: `ap+ef61://${portableDid}/actor`,
2274
- content: "A licensed note",
2275
- ...license
2276
- });
2277
- }
2278
- const licenseVerifyOptions = {
2279
- documentLoader() {
2280
- throw new Error("No document fetch expected");
2281
- },
2282
- contextLoader: mockDocumentLoader
2283
- };
2284
- test("verifyObject() keeps FEP-6757 license synonyms of a parsed object", async () => {
2285
- const license = "https://creativecommons.org/licenses/by/4.0/";
2286
- const signed = await signLicensedNote({ "schema:license": license }, [{ schema: "https://schema.org/" }]);
2287
- const verified = await verifyObject(Note, signed, licenseVerifyOptions);
2288
- assertInstanceOf(verified, Note);
2289
- assertEquals(verified.licenses, [new URL(license)]);
2290
- const json = await (await Note.fromJsonLd(signed, licenseVerifyOptions)).toJsonLd();
2291
- assertEquals(json, signed);
2292
- assertFalse("license" in json);
2293
- assertInstanceOf(await verifyObject(Note, json, licenseVerifyOptions), Note);
2294
- });
2295
- test("signObject() signs FEP-6757 licenses", async () => {
2296
- const license = new URL("https://creativecommons.org/licenses/by/4.0/");
2297
- const signed = await signObject(new Note({
2298
- id: parseIri(`ap://${portableDid}/objects/licensed`),
2299
- attribution: parseIri(`ap://${portableDid}/actor`),
2300
- content: "A licensed note",
2301
- licenses: [license]
2302
- }), ed25519PrivateKey, portableKeyId, { contextLoader: mockDocumentLoader });
2303
- const json = await signed.toJsonLd({
2304
- format: "compact",
2305
- contextLoader: mockDocumentLoader
2306
- });
2307
- assertEquals(json.license, license.href);
2308
- assert(json["@context"].includes("https://w3id.org/fep/6757"));
2309
- const verified = await verifyObject(Note, json, licenseVerifyOptions);
2310
- assertInstanceOf(verified, Note);
2311
- assertEquals(verified.licenses, [license]);
2312
- const create = await new Create({
2313
- actor: parseIri(`ap://${portableDid}/actor`),
2314
- object: signed
2315
- }).toJsonLd({ contextLoader: mockDocumentLoader });
2316
- assertEquals(create.object, json);
2317
- assertInstanceOf(await verifyObject(Note, create.object, licenseVerifyOptions), Note);
2318
- const relicensed = signed.clone({ licenses: [new URL("https://creativecommons.org/publicdomain/zero/1.0/")] });
2319
- assertEquals(await verifyObject(Note, await relicensed.toJsonLd({ contextLoader: mockDocumentLoader }), licenseVerifyOptions), null);
2320
- assertInstanceOf(await verifyObject(Note, await (await signObject(relicensed.clone({ proofs: [] }), ed25519PrivateKey, portableKeyId, { contextLoader: mockDocumentLoader })).toJsonLd({ contextLoader: mockDocumentLoader }), licenseVerifyOptions), Note);
2321
- });
2322
- test("verifyObject() and FEP-6757 licenses that cannot be preserved", async () => {
2323
- const license = "https://creativecommons.org/licenses/by/4.0/";
2324
- const contexts = ["https://w3id.org/fep/6757"];
2325
- const aliased = await signLicensedNote({ "schema:license": license }, [{ schema: "https://schema.org/" }]);
2326
- const create = await new Create({
2327
- actor: parseIri(`ap://${portableDid}/actor`),
2328
- object: await Note.fromJsonLd(aliased, licenseVerifyOptions)
2329
- }).toJsonLd({
2330
- format: "compact",
2331
- contextLoader: mockDocumentLoader
2332
- });
2333
- const object = create.object;
2334
- assertEquals(object.license, license);
2335
- assertEquals(await verifyObject(Note, {
2336
- ...object,
2337
- "@context": create["@context"]
2338
- }, licenseVerifyOptions), null);
2339
- const invalid = await signLicensedNote({ license: ["CC-BY-4.0", license] }, contexts);
2340
- const verified = await verifyObject(Note, invalid, licenseVerifyOptions);
2341
- assertInstanceOf(verified, Note);
2342
- assertEquals(verified.licenses, [new URL(license)]);
2343
- const json = await (await Note.fromJsonLd(invalid, licenseVerifyOptions)).toJsonLd();
2344
- assertEquals(json.license, license);
2345
- assertEquals(await verifyObject(Note, json, licenseVerifyOptions), null);
2346
- });
2347
- test("verifyObject() rejects a key that claims a forged controller", async () => {
2348
- const keyId = new URL("https://attacker.example/multikey");
2349
- const impersonated = "https://example.com/person2";
2350
- const attackerActor = "https://attacker.example/actor";
2351
- const multikeyDocument = await ed25519Multikey.toJsonLd({ contextLoader: mockDocumentLoader });
2352
- const options = {
2353
- format: "compact",
2354
- contextLoader: mockDocumentLoader,
2355
- documentLoader: mockDocumentLoader,
2356
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
2357
- };
2358
- const signed = await signObject(new Create({
2359
- id: new URL("https://attacker.example/activities/1"),
2360
- actor: new URL(impersonated),
2361
- object: new Note({
2362
- id: new URL("https://attacker.example/notes/1"),
2363
- attribution: new URL(impersonated),
2364
- content: "Hello world"
2365
- })
2366
- }), ed25519PrivateKey, keyId, options);
2367
- const jsonLd = await signed.toJsonLd(options);
2368
- const serveKeyControlledBy = (controller) => (resource) => {
2369
- if (resource === keyId.href) return Promise.resolve({
2370
- contextUrl: null,
2371
- documentUrl: resource,
2372
- document: {
2373
- ...multikeyDocument,
2374
- id: keyId.href,
2375
- controller
2376
- }
2377
- });
2378
- if (resource === attackerActor) return Promise.resolve({
2379
- contextUrl: null,
2380
- documentUrl: resource,
2381
- document: {
2382
- "@context": [
2383
- "https://www.w3.org/ns/activitystreams",
2384
- "https://w3id.org/security/v1",
2385
- "https://w3id.org/security/multikey/v1",
2386
- "https://w3id.org/security/data-integrity/v1",
2387
- "https://www.w3.org/ns/did/v1"
2388
- ],
2389
- id: resource,
2390
- type: "Person",
2391
- assertionMethod: [keyId.href]
2392
- }
2393
- });
2394
- return mockDocumentLoader(resource);
2395
- };
2396
- assertEquals(await verifyObject(Create, jsonLd, {
2397
- documentLoader: serveKeyControlledBy(impersonated),
2398
- contextLoader: mockDocumentLoader
2399
- }), null);
2400
- const honestLoader = serveKeyControlledBy(attackerActor);
2401
- let proof = null;
2402
- for await (const p of signed.getProofs(options)) {
2403
- proof = p;
2404
- break;
2405
- }
2406
- assertInstanceOf(proof, DataIntegrityProof);
2407
- assertInstanceOf(await verifyProof(jsonLd, proof, {
2408
- documentLoader: honestLoader,
2409
- contextLoader: mockDocumentLoader
2410
- }), Multikey);
2411
- assertEquals(await verifyObject(Create, jsonLd, {
2412
- documentLoader: honestLoader,
2413
- contextLoader: mockDocumentLoader
2414
- }), null);
2415
- });
2416
- test("verifyPortableObjectProof() verifies objects fetched through gateways", async (t) => {
2417
- const objectId = `ap://${portableDid}/objects/gateway`;
2418
- const gatewayUrl = `https://gateway.example/.well-known/apgateway/${portableDid}/objects/gateway`;
2419
- const signedNote = await signPortableJsonLd({
2420
- "@context": portableContext,
2421
- id: objectId,
2422
- type: "Note",
2423
- attributedTo: `ap://${portableDid}/actor`,
2424
- content: "Hello from a gateway"
2425
- });
2426
- const getObject = async (document, object = objectId, url = gatewayUrl) => {
2427
- return await (await Create.fromJsonLd({
2428
- "@context": "https://www.w3.org/ns/activitystreams",
2429
- type: "Create",
2430
- id: `ap://${portableDid}/activities/1`,
2431
- object
2432
- }, { contextLoader: mockDocumentLoader })).getObject({
2433
- documentLoader: async (requestUrl) => {
2434
- if (requestUrl !== url) throw new Error(`Unexpected URL: ${requestUrl}`);
2435
- return {
2436
- contextUrl: null,
2437
- documentUrl: requestUrl,
2438
- document
2439
- };
2440
- },
2441
- contextLoader: mockDocumentLoader,
2442
- gateways: ["https://gateway.example"],
2443
- verifyPortableObject: verifyPortableObjectProof
2444
- });
2445
- };
2446
- await t.step("a properly signed object", async () => {
2447
- const object = await getObject(signedNote);
2448
- assertInstanceOf(object, Note);
2449
- assertEquals(object.content, "Hello from a gateway");
2450
- });
2451
- await t.step("a tampered object", async () => {
2452
- assertEquals(await getObject({
2453
- ...signedNote,
2454
- content: "Tampered"
2455
- }), null);
2456
- });
2457
- await t.step("an object without a proof", async () => {
2458
- const { proof: _, ...unsigned } = signedNote;
2459
- assertEquals(await getObject(unsigned), null);
2460
- });
2461
- await t.step("an object without an ID", async () => {
2462
- const withoutId = await signPortableJsonLd({
2463
- "@context": portableContext,
2464
- type: "Note",
2465
- content: "Hello from a gateway"
2466
- });
2467
- assertEquals(await getObject(withoutId), null);
2468
- });
2469
- await t.step("another object of the same DID", async () => {
2470
- assertEquals(await getObject(signedNote, `ap://${portableDid}/objects/other`, `https://gateway.example/.well-known/apgateway/${portableDid}/objects/other`), null);
2471
- });
2472
- await t.step("an object signed by another DID", async () => {
2473
- const otherDid = await exportDidKey(await crypto.subtle.importKey("jwk", {
2474
- kty: "OKP",
2475
- crv: "Ed25519",
2476
- x: "sA2Nk45_dz1RVlqtNqYj9TRPf10ZYPnPPo4SYg6igQ8",
2477
- key_ops: ["verify"],
2478
- ext: true
2479
- }, "Ed25519", true, ["verify"]));
2480
- const otherObjectId = `ap://${otherDid}/objects/gateway`;
2481
- const otherGatewayUrl = `https://gateway.example/.well-known/apgateway/${otherDid}/objects/gateway`;
2482
- const spoofed = await signPortableJsonLd({
2483
- "@context": portableContext,
2484
- id: otherObjectId,
2485
- type: "Note",
2486
- content: "Spoofed"
2487
- });
2488
- assertEquals(await getObject(spoofed, otherObjectId, otherGatewayUrl), null);
2489
- const forged = await signPortableJsonLd({
2490
- "@context": portableContext,
2491
- id: objectId,
2492
- type: "Note",
2493
- content: "Forged"
2494
- }, { privateKey: fep8b32TestVectorPrivateKey });
2495
- assertEquals(await getObject(forged), null);
2496
- });
2497
- });
2498
- test("getAuthenticatedDocumentLoader() signs portable object requests to gateways", {
2499
- sanitizeResources: false,
2500
- sanitizeOps: false
2501
- }, async () => {
2502
- const objectId = `ap://${portableDid}/objects/private`;
2503
- const signedNote = await signPortableJsonLd({
2504
- "@context": portableContext,
2505
- id: objectId,
2506
- type: "Note",
2507
- attributedTo: `ap://${portableDid}/actor`,
2508
- content: "Followers only"
2509
- });
2510
- esm_default.spyGlobal();
2511
- let signed = false;
2512
- let accept = null;
2513
- esm_default.get(`https://example.com/.well-known/apgateway/${portableDid}/objects/private`, async (cl) => {
2514
- accept = cl.request.headers.get("Accept");
2515
- signed = await verifyRequest(cl.request, {
2516
- documentLoader: mockDocumentLoader,
2517
- contextLoader: mockDocumentLoader,
2518
- currentTime: Temporal.Now.instant()
2519
- }) != null;
2520
- if (!signed) return new Response(null, { status: 401 });
2521
- return new Response(JSON.stringify(signedNote), { headers: { "Content-Type": "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" } });
2522
- });
2523
- try {
2524
- const object = await (await Create.fromJsonLd({
2525
- "@context": "https://www.w3.org/ns/activitystreams",
2526
- type: "Create",
2527
- id: `ap://${portableDid}/activities/1`,
2528
- object: objectId
2529
- }, { contextLoader: mockDocumentLoader })).getObject({
2530
- documentLoader: getAuthenticatedDocumentLoader({
2531
- keyId: new URL("https://example.com/key2"),
2532
- privateKey: rsaPrivateKey2
2533
- }, { allowPrivateAddress: true }),
2534
- contextLoader: mockDocumentLoader,
2535
- gateways: ["https://example.com"],
2536
- verifyPortableObject: verifyPortableObjectProof
2537
- });
2538
- assertInstanceOf(object, Note);
2539
- assertEquals(object.content, "Followers only");
2540
- assert(signed);
2541
- assertEquals(accept, "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"");
2542
- } finally {
2543
- esm_default.hardReset();
2544
- }
2545
- });
2546
- test("verifyObject() rejects a key whose controller redirects to another actor", async () => {
2547
- const keyId = new URL("https://evil.example/keys/ed25519");
2548
- const victim = "https://victim.example/users/alice";
2549
- const mallory = "https://evil.example/users/mallory";
2550
- const multikeyDocument = await ed25519Multikey.toJsonLd({ contextLoader: mockDocumentLoader });
2551
- const signOptions = {
2552
- format: "compact",
2553
- contextLoader: mockDocumentLoader,
2554
- documentLoader: mockDocumentLoader,
2555
- context: ["https://www.w3.org/ns/activitystreams", "https://w3id.org/security/data-integrity/v1"]
2556
- };
2557
- assertEquals(await verifyObject(Create, await (await signObject(new Create({
2558
- id: new URL("https://victim.example/activities/forged"),
2559
- actor: new URL(victim),
2560
- object: new Note({
2561
- id: new URL("https://victim.example/notes/forged"),
2562
- attribution: new URL(victim),
2563
- content: "forged"
2564
- })
2565
- }), ed25519PrivateKey, keyId, signOptions)).toJsonLd(signOptions), {
2566
- documentLoader(resource) {
2567
- if (resource === keyId.href) return Promise.resolve({
2568
- contextUrl: null,
2569
- documentUrl: resource,
2570
- document: {
2571
- ...multikeyDocument,
2572
- id: keyId.href,
2573
- controller: victim
2574
- }
2575
- });
2576
- if (resource === victim || resource === mallory) return Promise.resolve({
2577
- contextUrl: null,
2578
- documentUrl: mallory,
2579
- document: {
2580
- "@context": [
2581
- "https://www.w3.org/ns/activitystreams",
2582
- "https://w3id.org/security/v1",
2583
- "https://w3id.org/security/multikey/v1",
2584
- "https://w3id.org/security/data-integrity/v1",
2585
- "https://www.w3.org/ns/did/v1"
2586
- ],
2587
- id: mallory,
2588
- type: "Person",
2589
- assertionMethod: [keyId.href]
2590
- }
2591
- });
2592
- return mockDocumentLoader(resource);
2593
- },
2594
- contextLoader: mockDocumentLoader
2595
- }), null);
2596
- });
2597
- test("verifyObjectDetailed() tells who signed the proofs", async () => {
2598
- const keyId = new URL("https://example.com/person2#key4");
2599
- const actor = new URL("https://example.com/person2");
2600
- const json = await (await signObject(new Create({
2601
- id: new URL("https://example.com/activities/detailed"),
2602
- actor,
2603
- object: new Note({ content: "Hello" })
2604
- }), ed25519PrivateKey, keyId, { contextLoader: mockDocumentLoader })).toJsonLd({ contextLoader: mockDocumentLoader });
2605
- const options = {
2606
- documentLoader: mockDocumentLoader,
2607
- contextLoader: mockDocumentLoader
2608
- };
2609
- const result = await verifyObjectDetailed(Create, json, options);
2610
- assertEquals(result.status, "verified");
2611
- assertEquals(result.status === "verified" ? result.signers.map((s) => [
2612
- s.kind,
2613
- s.id.href,
2614
- s.keyId.href
2615
- ]) : [], [[
2616
- "actor",
2617
- actor.href,
2618
- keyId.href
2619
- ]]);
2620
- const unsigned = await verifyObjectDetailed(Note, {
2621
- "@context": "https://www.w3.org/ns/activitystreams",
2622
- id: "https://example.com/notes/unsigned",
2623
- type: "Note"
2624
- }, options);
2625
- assertEquals(unsigned.status, "verified");
2626
- assertEquals(unsigned.status === "verified" ? unsigned.signers : null, []);
2627
- assertEquals((await verifyObjectDetailed(Create, json, {
2628
- ...options,
2629
- documentLoader: (url) => url.startsWith("https://example.com/person2") ? Promise.reject(new FetchError(url, "Unavailable", new Response(null, { status: 503 }))) : mockDocumentLoader(url)
2630
- })).status, "unavailable");
2631
- assertEquals((await verifyObjectDetailed(Create, json, {
2632
- ...options,
2633
- documentLoader: (url) => url.startsWith("https://example.com/person2") ? Promise.reject(new FetchError(url, "Gone", new Response(null, { status: 410 }))) : mockDocumentLoader(url)
2634
- })).status, "invalid");
2635
- assertEquals(await verifyObject(Create, json, options) != null, true);
2636
- });
2637
- //#endregion
2638
- export {};