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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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-JvzYDx0P.cjs} +3 -3
  4. package/dist/{http-CXX-QPzs.cjs → http-BG5cDmTS.cjs} +1 -1
  5. package/dist/{http-T7h9GBAN.js → http-MUf125Jc.js} +1 -1
  6. package/dist/{kv-cache-C6XX_fwz.cjs → kv-cache-A2po7h0_.cjs} +1 -1
  7. package/dist/{kv-cache-EFcb1kkU.js → kv-cache-CbWc1BZA.js} +1 -1
  8. package/dist/{middleware-Bj91r7r2.js → middleware-DzzdJ7Wz.js} +3 -3
  9. package/dist/mod.cjs +4 -4
  10. package/dist/mod.js +4 -4
  11. package/dist/{portable-collection-Do5QA2oy.js → portable-collection-CAer9RWN.js} +1 -1
  12. package/dist/{portable-collection-BOl9oKrt.cjs → portable-collection-d8w7TCn9.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 +10 -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,1019 +0,0 @@
1
- import "temporal-polyfill";
2
- import "urlpattern-polyfill";
3
- globalThis.addEventListener = () => {};
4
- import { a as getRawCanonicalPortableId, l as isPortableId, n as getCanonicalPortableId } from "./portable-key-id-Ca30-hOa.mjs";
5
- import { a as verificationObservation, t as observeAttempt } from "./verification-Dpfcw8ml.mjs";
6
- import { c as getNormalizationContextLoader } from "./ld-BpkhkDGj.mjs";
7
- import { n as preloadedOnlyDocumentLoader } from "./public-audience--klBIOk1.mjs";
8
- import { c as verifyMapLocalProof, f as verifyPortableObjectProofPolicy, m as verifyPreparedPortableObjectProofPolicy, n as classifyFep2277CoreType, o as preparePortableObjectProofPolicy } from "./proof-BlfZ95--.mjs";
9
- import { parseIri, preloadedContexts } from "@fedify/vocab-runtime";
10
- import jsonld from "@fedify/vocab-runtime/jsonld";
11
- //#region src/sig/compound-proof.ts
12
- const textEncoder = new TextEncoder();
13
- /**
14
- * Checks whether a literal `id` or `@id` value makes a map a portable object:
15
- * an `ap:` or `ap+ef61:` URI, or anything that looks like an FEP-ef61
16
- * compatible identifier, even a malformed one, which then fails the policy.
17
- */
18
- function isPortableIdValue(value) {
19
- return typeof value === "string" && isPortableId(value);
20
- }
21
- function isJsonMap(value) {
22
- return typeof value === "object" && value != null && !Array.isArray(value);
23
- }
24
- function isCompoundProofJsonObject(value) {
25
- return typeof value === "object" && value != null && !Array.isArray(value);
26
- }
27
- function hasOnlyDataProperties(value) {
28
- try {
29
- return Reflect.ownKeys(value).every((key) => {
30
- if (typeof key !== "string") return false;
31
- const descriptor = Object.getOwnPropertyDescriptor(value, key);
32
- return descriptor != null && "value" in descriptor && descriptor.enumerable;
33
- });
34
- } catch {
35
- return false;
36
- }
37
- }
38
- function hasOnlyJsonArrayProperties(value) {
39
- try {
40
- const keys = Reflect.ownKeys(value);
41
- if (keys.length !== value.length + 1) return false;
42
- for (const key of keys) {
43
- if (key === "length") continue;
44
- if (typeof key !== "string") return false;
45
- const index = Number(key);
46
- if (!Number.isInteger(index) || index < 0 || index >= value.length || String(index) !== key) return false;
47
- const descriptor = Object.getOwnPropertyDescriptor(value, key);
48
- if (descriptor == null || !descriptor.enumerable || !("value" in descriptor)) return false;
49
- }
50
- return true;
51
- } catch {
52
- return false;
53
- }
54
- }
55
- function escapeJsonPointerSegment(segment) {
56
- return segment.replaceAll("~", "~0").replaceAll("/", "~1");
57
- }
58
- function childPath(path, segment) {
59
- return `${path}/${escapeJsonPointerSegment(segment)}`;
60
- }
61
- function comparePaths(left, right) {
62
- return left < right ? -1 : left > right ? 1 : 0;
63
- }
64
- function encodedLength(value) {
65
- return textEncoder.encode(value).byteLength;
66
- }
67
- function validateLimits(limits) {
68
- for (const [name, value] of Object.entries(limits)) if (!Number.isSafeInteger(value) || value < 0) throw new RangeError(`${name} must be a non-negative safe integer.`);
69
- }
70
- function limitExceeded(limit, maximum, actual, path) {
71
- return {
72
- status: "unsupported",
73
- reason: {
74
- type: "limitExceeded",
75
- limit,
76
- maximum,
77
- actual,
78
- path
79
- }
80
- };
81
- }
82
- function addBytes(statistics, bytes, limits, path) {
83
- statistics.byteLength += bytes;
84
- if (statistics.byteLength > limits.maxBytes) return limitExceeded("bytes", limits.maxBytes, statistics.byteLength, path);
85
- }
86
- function inspectJsonTree(root, limits) {
87
- if (!isJsonMap(root)) return {
88
- status: "unsupported",
89
- reason: {
90
- type: "invalidJsonTree",
91
- path: ""
92
- }
93
- };
94
- const statistics = {
95
- byteLength: 0,
96
- mapCount: 0,
97
- proofCount: 0,
98
- maxDepth: 0
99
- };
100
- const seen = /* @__PURE__ */ new Set();
101
- const pending = [{
102
- value: root,
103
- path: "",
104
- depth: 0,
105
- insideProof: false,
106
- insideContext: false
107
- }];
108
- while (pending.length > 0) {
109
- const current = pending.pop();
110
- statistics.maxDepth = Math.max(statistics.maxDepth, current.depth);
111
- if (current.depth > limits.maxDepth) return limitExceeded("depth", limits.maxDepth, current.depth, current.path);
112
- const value = current.value;
113
- if (value === null) {
114
- const failure = addBytes(statistics, 4, limits, current.path);
115
- if (failure != null) return failure;
116
- } else if (typeof value === "boolean") {
117
- const failure = addBytes(statistics, value ? 4 : 5, limits, current.path);
118
- if (failure != null) return failure;
119
- } else if (typeof value === "string") {
120
- const failure = addBytes(statistics, encodedLength(JSON.stringify(value)), limits, current.path);
121
- if (failure != null) return failure;
122
- } else if (typeof value === "number") {
123
- if (!Number.isFinite(value)) return {
124
- status: "unsupported",
125
- reason: {
126
- type: "invalidJsonTree",
127
- path: current.path
128
- }
129
- };
130
- const failure = addBytes(statistics, encodedLength(JSON.stringify(value)), limits, current.path);
131
- if (failure != null) return failure;
132
- } else if (Array.isArray(value)) {
133
- if (seen.has(value) || !hasOnlyJsonArrayProperties(value)) return {
134
- status: "unsupported",
135
- reason: {
136
- type: "invalidJsonTree",
137
- path: current.path
138
- }
139
- };
140
- seen.add(value);
141
- const failure = addBytes(statistics, 2 + Math.max(0, value.length - 1), limits, current.path);
142
- if (failure != null) return failure;
143
- for (let index = value.length - 1; index >= 0; index--) pending.push({
144
- value: value[index],
145
- path: childPath(current.path, String(index)),
146
- depth: current.depth + 1,
147
- insideProof: current.insideProof,
148
- insideContext: current.insideContext
149
- });
150
- } else if (isJsonMap(value)) {
151
- const prototype = Object.getPrototypeOf(value);
152
- if (seen.has(value) || !hasOnlyDataProperties(value) || prototype !== Object.prototype && prototype !== null) return {
153
- status: "unsupported",
154
- reason: {
155
- type: "invalidJsonTree",
156
- path: current.path
157
- }
158
- };
159
- seen.add(value);
160
- statistics.mapCount++;
161
- if (statistics.mapCount > limits.maxMaps) return limitExceeded("maps", limits.maxMaps, statistics.mapCount, current.path);
162
- const entries = Object.entries(value).sort(([left], [right]) => comparePaths(left, right));
163
- let objectBytes = 2 + Math.max(0, entries.length - 1);
164
- for (const [key] of entries) objectBytes += encodedLength(JSON.stringify(key)) + 1;
165
- const failure = addBytes(statistics, objectBytes, limits, current.path);
166
- if (failure != null) return failure;
167
- const hasDirectProof = !current.insideProof && !current.insideContext && Object.hasOwn(value, "proof");
168
- if (hasDirectProof) {
169
- statistics.proofCount++;
170
- if (statistics.proofCount > limits.maxProofs) return limitExceeded("proofs", limits.maxProofs, statistics.proofCount, childPath(current.path, "proof"));
171
- if (!isJsonMap(value.proof)) return {
172
- status: "unsupported",
173
- reason: {
174
- type: "unsupportedProofShape",
175
- path: childPath(current.path, "proof")
176
- }
177
- };
178
- }
179
- for (let index = entries.length - 1; index >= 0; index--) {
180
- const [key, child] = entries[index];
181
- pending.push({
182
- value: child,
183
- path: childPath(current.path, key),
184
- depth: current.depth + 1,
185
- insideProof: current.insideProof || hasDirectProof && key === "proof",
186
- insideContext: current.insideContext || key === "@context"
187
- });
188
- }
189
- } else return {
190
- status: "unsupported",
191
- reason: {
192
- type: "invalidJsonTree",
193
- path: current.path
194
- }
195
- };
196
- }
197
- return statistics;
198
- }
199
- function freezeJsonTree(root) {
200
- const pending = [root];
201
- while (pending.length > 0) {
202
- const value = pending.pop();
203
- if (typeof value !== "object" || value == null) continue;
204
- if (Array.isArray(value)) for (const child of value) pending.push(child);
205
- else for (const child of Object.values(value)) pending.push(child);
206
- Object.freeze(value);
207
- }
208
- }
209
- function collectDocuments(snapshot) {
210
- const documents = [];
211
- const pending = [{
212
- value: snapshot,
213
- path: "",
214
- depth: 0
215
- }];
216
- while (pending.length > 0) {
217
- const current = pending.pop();
218
- if (Array.isArray(current.value)) {
219
- for (let index = current.value.length - 1; index >= 0; index--) pending.push({
220
- value: current.value[index],
221
- path: childPath(current.path, String(index)),
222
- depth: current.depth + 1
223
- });
224
- continue;
225
- }
226
- if (!isCompoundProofJsonObject(current.value)) continue;
227
- const document = current.value;
228
- if (Object.hasOwn(document, "proof")) {
229
- const proof = document.proof;
230
- const id = typeof document.id === "string" ? document.id : typeof document["@id"] === "string" ? document["@id"] : void 0;
231
- documents.push(Object.freeze({
232
- path: current.path,
233
- id,
234
- depth: current.depth,
235
- securedDocument: document,
236
- proof
237
- }));
238
- }
239
- const entries = Object.entries(document).sort(([left], [right]) => comparePaths(left, right));
240
- for (let index = entries.length - 1; index >= 0; index--) {
241
- const [key, value] = entries[index];
242
- if (key === "proof" || key === "@context") continue;
243
- pending.push({
244
- value,
245
- path: childPath(current.path, key),
246
- depth: current.depth + 1
247
- });
248
- }
249
- }
250
- documents.sort((left, right) => right.depth - left.depth || comparePaths(left.path, right.path));
251
- return documents;
252
- }
253
- function collectPortableObjects(snapshot) {
254
- const objects = [];
255
- const pending = [{
256
- value: snapshot,
257
- path: "",
258
- depth: 0
259
- }];
260
- while (pending.length > 0) {
261
- const current = pending.pop();
262
- if (Array.isArray(current.value)) {
263
- for (let index = current.value.length - 1; index >= 0; index--) pending.push({
264
- value: current.value[index],
265
- path: childPath(current.path, String(index)),
266
- depth: current.depth + 1,
267
- keyOf: current.keyOf
268
- });
269
- continue;
270
- }
271
- if (!isCompoundProofJsonObject(current.value)) continue;
272
- const document = current.value;
273
- const id = [document.id, document["@id"]].find(isPortableIdValue);
274
- if (typeof id === "string") objects.push(Object.freeze({
275
- path: current.path,
276
- id,
277
- depth: current.depth,
278
- document,
279
- ...current.keyOf == null ? {} : { keyOf: current.keyOf }
280
- }));
281
- const entries = Object.entries(document).sort(([left], [right]) => comparePaths(left, right));
282
- for (let index = entries.length - 1; index >= 0; index--) {
283
- const [key, value] = entries[index];
284
- if (key === "proof" || key === "@context") continue;
285
- pending.push({
286
- value,
287
- path: childPath(current.path, key),
288
- depth: current.depth + 1,
289
- ...typeof id === "string" && (key === "publicKey" || key === "assertionMethod") ? { keyOf: {
290
- path: current.path,
291
- member: key
292
- } } : {}
293
- });
294
- }
295
- }
296
- objects.sort((left, right) => right.depth - left.depth || comparePaths(left.path, right.path));
297
- return objects;
298
- }
299
- /**
300
- * Determines whether a received JSON tree contains a portable object ID, i.e.,
301
- * an `ap:` or `ap+ef61:` URI or an FEP-ef61 compatible identifier, outside
302
- * a proof or context definition, without exceeding discovery limits.
303
- *
304
- * This is only an applicability check. It does not authenticate the input;
305
- * callers must still use {@link verifyCompoundPortableObjectProofs} to obtain
306
- * an atomic result from an immutable snapshot. `indeterminate` means the
307
- * applicability scan itself reached a limit, so a caller must not treat the
308
- * document as outside the compound profile.
309
- *
310
- * @internal
311
- */
312
- function inspectCompoundPortableObjectApplicability(json, limits) {
313
- validateLimits(limits);
314
- let byteLength = 0;
315
- let mapCount = 0;
316
- const pending = [{
317
- iterator: [json].values(),
318
- depth: 0
319
- }];
320
- const addByteLength = (length) => {
321
- byteLength += length;
322
- return byteLength <= limits.maxBytes;
323
- };
324
- while (pending.length > 0) {
325
- const frame = pending[pending.length - 1];
326
- const next = frame.iterator.next();
327
- if (next.done) {
328
- pending.pop();
329
- if (byteLength > limits.maxBytes) return "indeterminate";
330
- continue;
331
- }
332
- const value = next.value;
333
- if (frame.depth > limits.maxDepth) return "indeterminate";
334
- if (value === null) {
335
- if (!addByteLength(4)) return "indeterminate";
336
- } else if (typeof value === "boolean") {
337
- if (!addByteLength(value ? 4 : 5)) return "indeterminate";
338
- } else if (typeof value === "string" || typeof value === "number") {
339
- if (!addByteLength(encodedLength(JSON.stringify(value)))) return "indeterminate";
340
- } else if (Array.isArray(value)) {
341
- if (!addByteLength(2 + Math.max(0, value.length - 1))) return "indeterminate";
342
- pending.push({
343
- iterator: value.values(),
344
- depth: frame.depth + 1
345
- });
346
- } else if (isJsonMap(value)) {
347
- mapCount++;
348
- if (mapCount > limits.maxMaps) return "indeterminate";
349
- if ([value.id, value["@id"]].some(isPortableIdValue)) return "present";
350
- if (!addByteLength(2)) return "indeterminate";
351
- const children = (function* () {
352
- let first = true;
353
- for (const key in value) {
354
- if (!Object.hasOwn(value, key)) continue;
355
- if (key === "proof" || key === "@context") continue;
356
- if (!first && !addByteLength(1)) return;
357
- first = false;
358
- if (!addByteLength(encodedLength(JSON.stringify(key)) + 1)) return;
359
- yield value[key];
360
- }
361
- })();
362
- pending.push({
363
- iterator: children,
364
- depth: frame.depth + 1
365
- });
366
- }
367
- if (byteLength > limits.maxBytes) return "indeterminate";
368
- }
369
- return "absent";
370
- }
371
- /**
372
- * Walks every JSON map in a finite, already-materialized JSON tree, depth
373
- * first in insertion order, skipping the values of `proof` and `@context`
374
- * members. A map reached a second time through a repeated reference is not
375
- * visited again, so a cyclic input terminates. The walk stops as soon as
376
- * the visitor returns a value other than `undefined`.
377
- */
378
- function findInJsonMaps(json, visit) {
379
- const seen = /* @__PURE__ */ new Set();
380
- const pending = [{
381
- value: json,
382
- path: ""
383
- }];
384
- while (pending.length > 0) {
385
- const { value, path } = pending.pop();
386
- if (typeof value !== "object" || value == null || seen.has(value)) continue;
387
- seen.add(value);
388
- if (Array.isArray(value)) {
389
- for (let index = value.length - 1; index >= 0; index--) pending.push({
390
- value: value[index],
391
- path: childPath(path, String(index))
392
- });
393
- continue;
394
- }
395
- const map = value;
396
- const result = visit(map, path);
397
- if (result !== void 0) return result;
398
- const keys = Object.keys(map);
399
- for (let index = keys.length - 1; index >= 0; index--) {
400
- const key = keys[index];
401
- if (key === "proof" || key === "@context") continue;
402
- pending.push({
403
- value: map[key],
404
- path: childPath(path, key)
405
- });
406
- }
407
- }
408
- }
409
- /**
410
- * Determines whether an outgoing JSON tree contains portable content that
411
- * requires producer-side proof-shape checks: a map, outside proof and context
412
- * values, whose direct `id` or `@id` is a portable ActivityPub URI or an
413
- * FEP-ef61 compatible identifier.
414
- *
415
- * This deliberately differs from
416
- * {@link inspectCompoundPortableObjectApplicability}: it applies no resource
417
- * limits, so a portable map or an unsupported proof beyond the inbox limits
418
- * is still found, and a large document without portable maps stays outside
419
- * the compound-proof profile. The input must be a finite JSON tree that has
420
- * already been materialized, such as a compact JSON-LD document about to be
421
- * sent.
422
- *
423
- * @internal
424
- */
425
- function containsCompoundPortableObject(json) {
426
- return findInJsonMaps(json, (map) => [map.id, map["@id"]].some(isPortableIdValue) ? true : void 0) ?? false;
427
- }
428
- /**
429
- * Finds the first direct `proof` member whose value the map-local
430
- * compound-proof profile does not accept, i.e., anything other than a single
431
- * JSON map: a proof set, `null`, or a scalar. Like the inbox discovery, it
432
- * does not look inside proof or context values.
433
- *
434
- * The input must be a finite, already-materialized JSON tree; see
435
- * {@link containsCompoundPortableObject}.
436
- *
437
- * @returns The RFC 6901 JSON Pointer to the unsupported `proof` member, or
438
- * `null` if every direct proof is a single JSON map.
439
- * @internal
440
- */
441
- function findUnsupportedCompoundProofShape(json) {
442
- return findInJsonMaps(json, (map, path) => Object.hasOwn(map, "proof") && !isJsonMap(map.proof) ? childPath(path, "proof") : void 0) ?? null;
443
- }
444
- /**
445
- * Finds the first embedded map that carries a direct `proof` but not its own
446
- * `@context`. The map-local compound-proof profile verifies each proof-bearing
447
- * map as a self-contained document, so the inbox rejects such a map with
448
- * `missingContext` whatever its proof. An object embedded with its captured
449
- * signed representation, e.g., by {@link signObject}, always keeps its own
450
- * `@context`; one rebuilt under its parent's context, e.g., an object parsed
451
- * from a received document, never does, and its proof no longer covers what
452
- * it has become anyway.
453
- *
454
- * The input must be a finite, already-materialized JSON tree; see
455
- * {@link containsCompoundPortableObject}.
456
- *
457
- * @returns The RFC 6901 JSON Pointer to the map, or `null` if every embedded
458
- * proof-bearing map has its own `@context`.
459
- * @internal
460
- */
461
- function findEmbeddedProofWithoutContext(json) {
462
- return findInJsonMaps(json, (map, path) => path !== "" && Object.hasOwn(map, "proof") && !Object.hasOwn(map, "@context") ? path : void 0) ?? null;
463
- }
464
- /**
465
- * Discovers direct literal proof-bearing maps in an immutable JSON snapshot.
466
- *
467
- * Discovery is bounded and atomic. Unsupported proof shapes, non-JSON input,
468
- * or a resource-limit failure return no partial candidate list. Proof
469
- * configuration subtrees are validated and counted as JSON, but are not
470
- * themselves searched for secured documents.
471
- */
472
- function discoverCompoundProofDocuments(json, limits) {
473
- validateLimits(limits);
474
- let inspected;
475
- try {
476
- inspected = inspectJsonTree(json, limits);
477
- } catch {
478
- return {
479
- status: "unsupported",
480
- reason: {
481
- type: "invalidJsonTree",
482
- path: ""
483
- }
484
- };
485
- }
486
- if ("status" in inspected) return inspected;
487
- let snapshot;
488
- try {
489
- snapshot = structuredClone(json);
490
- } catch {
491
- return {
492
- status: "unsupported",
493
- reason: {
494
- type: "invalidJsonTree",
495
- path: ""
496
- }
497
- };
498
- }
499
- freezeJsonTree(snapshot);
500
- return {
501
- status: "ok",
502
- snapshot,
503
- documents: Object.freeze(collectDocuments(snapshot)),
504
- statistics: Object.freeze({ ...inspected })
505
- };
506
- }
507
- /**
508
- * Verifies every direct literal proof found in one immutable JSON snapshot.
509
- *
510
- * Each current map is verified independently. Only its literal `proof`
511
- * member is removed from its JCS input, so descendant proofs and proof aliases
512
- * remain unchanged. This is the lower-level compound-document mechanism; it
513
- * does not apply portable-object policy or authenticate an inbox activity.
514
- *
515
- * @internal
516
- */
517
- async function verifyCompoundProofDocuments(json, limits, options = {}) {
518
- const discovered = discoverCompoundProofDocuments(json, limits);
519
- if (discovered.status !== "ok") return discovered;
520
- const documents = await verifyDiscoveredProofDocuments(discovered, options);
521
- return {
522
- status: "ok",
523
- verified: documents.length > 0 && documents.every((document) => document.verified),
524
- snapshot: discovered.snapshot,
525
- documents,
526
- statistics: discovered.statistics
527
- };
528
- }
529
- async function verifyDiscoveredProofDocuments(discovered, options, rejectedPolicies = /* @__PURE__ */ new Map()) {
530
- const documents = await Promise.all(discovered.documents.map(async (document) => {
531
- const metadata = {
532
- path: document.path,
533
- ...document.id == null ? {} : { id: document.id },
534
- depth: document.depth
535
- };
536
- if (document.depth > 0 && !Object.hasOwn(document.securedDocument, "@context")) return Object.freeze({
537
- ...metadata,
538
- verified: false,
539
- reason: { type: "missingContext" }
540
- });
541
- const inherited = options[verificationObservation];
542
- let subjectId = null;
543
- if (inherited != null && document.id != null) try {
544
- subjectId = parseIri(document.id);
545
- } catch {}
546
- const key = await observeAttempt({ [verificationObservation]: inherited == null ? void 0 : {
547
- ...inherited,
548
- attempt: void 0,
549
- check: void 0,
550
- subject: {
551
- id: subjectId,
552
- pointer: document.path
553
- }
554
- } }, "objectIntegrity", async (observation) => {
555
- const reason = rejectedPolicies.get(document.path);
556
- if (reason != null) {
557
- if (observation.attempt != null) observation.attempt.reason = {
558
- type: "proofPolicy",
559
- reason
560
- };
561
- return null;
562
- }
563
- return await verifyMapLocalProof(document.securedDocument, {
564
- ...options,
565
- [verificationObservation]: inherited == null ? void 0 : observation
566
- });
567
- }, (key) => key != null);
568
- return key == null ? Object.freeze({
569
- ...metadata,
570
- verified: false,
571
- reason: { type: "invalidProof" }
572
- }) : Object.freeze({
573
- ...metadata,
574
- verified: true,
575
- key
576
- });
577
- }));
578
- return Object.freeze(documents);
579
- }
580
- /**
581
- * Applies FEP-ef61 portable-object policy to every portable map in a compound
582
- * snapshot without changing the map-local cryptographic inputs.
583
- *
584
- * @internal
585
- */
586
- async function verifyCompoundPortableObjectProofs(json, limits, options = {}) {
587
- const discovered = discoverCompoundProofDocuments(json, limits);
588
- if (discovered.status !== "ok") return discovered;
589
- const collected = collectPortableObjects(discovered.snapshot);
590
- const objectByPath = new Map(collected.map((object) => [object.path, object]));
591
- const isKeyCandidate = (object) => object.keyOf != null && !Object.hasOwn(object.document, "proof");
592
- const prepareObject = async (object) => {
593
- if (object.depth > 0 && !Object.hasOwn(object.document, "@context")) return {
594
- prepared: false,
595
- reason: { type: "missingContext" }
596
- };
597
- try {
598
- const policy = await preparePortableObjectProofPolicy(object.document, options);
599
- return policy.prepared ? policy : {
600
- prepared: false,
601
- reason: policy.result.reason
602
- };
603
- } catch {
604
- return {
605
- prepared: false,
606
- reason: { type: "invalidPortableObject" }
607
- };
608
- }
609
- };
610
- const preparedByPath = new Map(await Promise.all(collected.filter((object) => !isKeyCandidate(object)).map(async (object) => [object.path, await prepareObject(object)])));
611
- const proofs = await verifyDiscoveredProofDocuments(discovered, options, new Map([...preparedByPath].flatMap(([path, prepared]) => prepared.prepared ? [] : [[path, prepared.reason]])));
612
- const proofByPath = new Map(proofs.map((proof) => [proof.path, proof]));
613
- const verifyPortableObject = async (object) => {
614
- const metadata = {
615
- path: object.path,
616
- id: object.id,
617
- depth: object.depth
618
- };
619
- try {
620
- const prepared = preparedByPath.get(object.path) ?? await prepareObject(object);
621
- const proof = proofByPath.get(object.path);
622
- const key = proof?.verified === true ? proof.key : null;
623
- const policy = prepared.prepared ? verifyPreparedPortableObjectProofPolicy(prepared, key) : {
624
- verified: false,
625
- reason: prepared.reason
626
- };
627
- return policy.verified ? Object.freeze({
628
- ...metadata,
629
- id: policy.objectId,
630
- verified: true,
631
- keys: policy.keys
632
- }) : Object.freeze({
633
- ...metadata,
634
- verified: false,
635
- reason: policy.reason
636
- });
637
- } catch {
638
- return Object.freeze({
639
- ...metadata,
640
- verified: false,
641
- reason: { type: "invalidPortableObject" }
642
- });
643
- }
644
- };
645
- const results = /* @__PURE__ */ new Map();
646
- for (const result of await Promise.all(collected.filter((object) => !isKeyCandidate(object)).map(verifyPortableObject))) results.set(result.path, result);
647
- let projections = 0;
648
- for (const object of collected) {
649
- if (!isKeyCandidate(object)) continue;
650
- const { path, member } = object.keyOf;
651
- const parent = results.get(path);
652
- const parentObject = objectByPath.get(path);
653
- if (parent?.verified === true && parentObject != null && projections++ < MAX_KEY_PROJECTIONS && await isEmbeddedPortableKey(parentObject.document, member, object.document, parent.id)) continue;
654
- results.set(object.path, await verifyPortableObject(object));
655
- }
656
- const portableObjects = Object.freeze(collected.flatMap((object) => {
657
- const result = results.get(object.path);
658
- return result == null ? [] : [result];
659
- }));
660
- return {
661
- status: "ok",
662
- verified: portableObjects.length > 0 && proofs.every((proof) => proof.verified) && portableObjects.every((object) => object.verified),
663
- snapshot: discovered.snapshot,
664
- proofs,
665
- portableObjects,
666
- statistics: discovered.statistics
667
- };
668
- }
669
- /**
670
- * The maximum number of embedded keys per compound document that are checked
671
- * by {@link isEmbeddedPortableKey}, each of which expands a projection of its
672
- * parent. Keys beyond it have to carry their own proofs.
673
- */
674
- const MAX_KEY_PROJECTIONS = 64;
675
- const SECURITY = "https://w3id.org/security#";
676
- const KEY_TYPES = /* @__PURE__ */ new Set([`${SECURITY}Key`, `${SECURITY}Multikey`]);
677
- const KEY_PROPERTIES = /* @__PURE__ */ new Set([
678
- "@id",
679
- "@type",
680
- `${SECURITY}owner`,
681
- `${SECURITY}controller`,
682
- `${SECURITY}publicKeyPem`,
683
- `${SECURITY}publicKeyMultibase`
684
- ]);
685
- /**
686
- * Tells whether an unsigned map embedded in a verified portable map is just
687
- * a key of the portable object the parent is, e.g., a gateway key of
688
- * a portable actor, which the parent's proof covers.
689
- *
690
- * The key's meaning depends on the contexts active where it sits, so it is
691
- * decided on a projection of the parent that keeps only what those contexts
692
- * depend on: the parent's `@context`, its types and ID, and the member that
693
- * holds the key. That holds only if nothing else in the parent can change
694
- * them, so both documents may only use contexts that define no keyword
695
- * aliases and no scoped contexts; Fedify's preloaded contexts, the only
696
- * remote ones the compound policy loads, alias just `id` and `type`.
697
- *
698
- * The projection must expand to the parent's verified ID with the key as the
699
- * single value of `publicKey` or `assertionMethod`, and the key must be
700
- * nothing but a `CryptographicKey` or `Multikey` whose ID is the parent's
701
- * `ap:` or `ap+ef61:` URI, or its compatible identifier, plus a fragment,
702
- * and whose owner or controller, if any, is the parent.
703
- */
704
- async function isEmbeddedPortableKey(parent, member, key, parentId) {
705
- const parentPortableId = getCanonicalPortableIdOf(parentId);
706
- if (parentPortableId == null) return false;
707
- if (!isSimpleContext(parent["@context"]) || Object.hasOwn(key, "@context") && !isSimpleContext(key["@context"])) return false;
708
- const literalIds = [key.id, key["@id"]].filter((id) => id !== void 0);
709
- if (literalIds.length < 1 || literalIds.some((id) => id !== literalIds[0]) || typeof literalIds[0] !== "string" || !isKeyIdOf(literalIds[0], parentPortableId)) return false;
710
- const projection = {};
711
- for (const property of [
712
- "@context",
713
- "type",
714
- "@type",
715
- "id",
716
- "@id"
717
- ]) if (Object.hasOwn(parent, property)) projection[property] = parent[property];
718
- projection[member] = key;
719
- let expanded;
720
- try {
721
- expanded = await jsonld.expand(structuredClone(projection), {
722
- documentLoader: getNormalizationContextLoader(preloadedOnlyDocumentLoader),
723
- keepFreeFloatingNodes: true
724
- });
725
- } catch {
726
- return false;
727
- }
728
- if (expanded.length !== 1 || hasGraphKeyword(expanded)) return false;
729
- const node = expanded[0];
730
- if (!isJsonMap(node) || typeof node["@id"] !== "string" || getCanonicalPortableIdOf(node["@id"]) !== parentPortableId) return false;
731
- const properties = Object.keys(node).filter((property) => property !== "@id" && property !== "@type");
732
- if (properties.length !== 1 || properties[0] !== `${SECURITY}${member}`) return false;
733
- const values = node[properties[0]];
734
- if (!Array.isArray(values) || values.length !== 1) return false;
735
- const expandedKey = values[0];
736
- if (!isJsonMap(expandedKey) || expandedKey["@id"] !== literalIds[0] || Object.keys(expandedKey).some((property) => !KEY_PROPERTIES.has(property))) return false;
737
- const types = expandedKey["@type"];
738
- if (types !== void 0 && (!Array.isArray(types) || types.some((type) => typeof type !== "string" || !KEY_TYPES.has(type)))) return false;
739
- const pem = expandedKey[`${SECURITY}publicKeyPem`];
740
- const multibase = expandedKey[`${SECURITY}publicKeyMultibase`];
741
- if (pem === void 0 && multibase === void 0) return false;
742
- if (pem !== void 0 && !isSingleStringValue(pem)) return false;
743
- if (multibase !== void 0 && !isSingleStringValue(multibase, `${SECURITY}multibase`)) return false;
744
- for (const property of [`${SECURITY}owner`, `${SECURITY}controller`]) {
745
- const owners = expandedKey[property];
746
- if (owners === void 0) continue;
747
- if (!Array.isArray(owners) || owners.length !== 1 || !isJsonMap(owners[0]) || Object.keys(owners[0]).length !== 1 || typeof owners[0]["@id"] !== "string" || getCanonicalPortableIdOf(owners[0]["@id"]) !== parentPortableId) return false;
748
- }
749
- return true;
750
- }
751
- function getCanonicalPortableIdOf(id) {
752
- try {
753
- return getCanonicalPortableId(parseIri(id));
754
- } catch (error) {
755
- if (error instanceof TypeError) return null;
756
- throw error;
757
- }
758
- }
759
- /**
760
- * Checks whether an ID is the given portable object's `ap:` or `ap+ef61:`
761
- * URI, or its compatible identifier, plus a non-empty fragment. The ID is
762
- * compared as it is written, so an ID that URL parsing would turn into
763
- * the portable object's, e.g., through dot segments, is not one of its keys.
764
- */
765
- function isKeyIdOf(id, portableId) {
766
- const canonical = getRawCanonicalPortableId(id);
767
- if (canonical == null) return false;
768
- const hash = canonical.indexOf("#");
769
- return hash >= 0 && hash < canonical.length - 1 && canonical.slice(0, hash) === portableId;
770
- }
771
- /**
772
- * Checks whether a `@context` value defines no keyword aliases and no scoped
773
- * contexts: only references to contexts and term definitions that map terms
774
- * to IRIs, optionally with an `@id` or `@vocab` type coercion or a datatype.
775
- * A `@set` container is accepted because it only forces expanded values into
776
- * arrays, which expansion does anyway; `@list` and other containers are not.
777
- */
778
- function isSimpleContext(context) {
779
- if (typeof context === "string") return true;
780
- if (Array.isArray(context)) return context.every(isSimpleContext);
781
- if (context === void 0 || !isCompoundProofJsonObject(context)) return false;
782
- return Object.entries(context).every(([term, definition]) => {
783
- if (term.startsWith("@")) return false;
784
- if (typeof definition === "string") return !definition.startsWith("@");
785
- if (!isCompoundProofJsonObject(definition)) return false;
786
- const { "@id": id, "@type": type, "@container": container, ...rest } = definition;
787
- return Object.keys(rest).length < 1 && typeof id === "string" && !id.startsWith("@") && (container === void 0 || container === "@set") && (type === void 0 || type === "@id" || type === "@vocab" || typeof type === "string" && !type.startsWith("@"));
788
- });
789
- }
790
- function hasGraphKeyword(value) {
791
- if (Array.isArray(value)) return value.some(hasGraphKeyword);
792
- if (!isJsonMap(value)) return false;
793
- return Object.entries(value).some(([key, child]) => key === "@graph" || key === "@included" || key === "@reverse" || hasGraphKeyword(child));
794
- }
795
- /**
796
- * Checks whether expanded values are a single plain string, optionally with
797
- * the given datatype.
798
- */
799
- function isSingleStringValue(values, datatype) {
800
- if (!Array.isArray(values) || values.length !== 1) return false;
801
- const value = values[0];
802
- if (!isJsonMap(value) || typeof value["@value"] !== "string") return false;
803
- const keys = Object.keys(value);
804
- return keys.length === 1 || datatype != null && keys.length === 2 && value["@type"] === datatype;
805
- }
806
- /**
807
- * FEP-2277 core types that FEP-ef61 does not require to carry proofs.
808
- */
809
- const UNSECURED_CORE_TYPES = /* @__PURE__ */ new Set([
810
- "collection",
811
- "link",
812
- "verificationMethod",
813
- "publicKey"
814
- ]);
815
- /**
816
- * Applies the FEP-ef61 proof policy, under the map-local compound-proof
817
- * profile, to every portable object embedded in a document that a gateway is
818
- * about to serve, e.g., the items of a portable collection page. The root
819
- * map itself is not checked, as the caller built it.
820
- *
821
- * Each map below the root whose direct `id` or `@id` is a portable ID or
822
- * a compatible identifier is checked independently of the others:
823
- *
824
- * - A map with a direct `proof` is a separately secured document: the proof
825
- * has to be a single map, the map has to carry its own `@context`, and
826
- * the proof has to verify over the map exactly as it is served, with only
827
- * its own proof removed, and be made with a key of the DID in the map's
828
- * ID, as the map-local compound-proof profile requires.
829
- * - A map without a proof has to be a collection, a link, or a key, which
830
- * FEP-ef61 does not require to be secured. It is classified under its
831
- * effective context, i.e., the `@context` values of its ancestors and
832
- * itself, which therefore may only use the simple, preloaded contexts
833
- * that the compound-proof policy accepts.
834
- *
835
- * Maps that merely refer to a portable object, i.e., have no member other
836
- * than `id` or `@id`, and maps with non-portable IDs, are neither checked nor
837
- * verified. Every `@context` in the document, however, has to be simple, and
838
- * every portable object that the expanded document describes has to be
839
- * a map found this way, so that no context can hide a portable object from
840
- * this check, e.g., by aliasing `@id` or redefining `proof`. The input must be a finite, already-materialized JSON tree, such
841
- * as the compact JSON-LD document that is about to be served.
842
- *
843
- * @param json The document to check.
844
- * @param options The options for verifying the proofs. Proofs whose
845
- * verification methods are not `did:key` DID URLs may make
846
- * the document loader fetch DID documents.
847
- * @returns The result, which names the first refused map, if any.
848
- * @internal
849
- */
850
- async function verifyServedPortableObjects(json, options = {}) {
851
- json = JSON.parse(JSON.stringify(json));
852
- const collected = collectServedPortableMaps(json);
853
- if (!("maps" in collected)) return collected;
854
- const maps = collected.maps;
855
- const discovered = /* @__PURE__ */ new Map();
856
- const ids = maps.map((candidate) => candidate.id);
857
- const rootId = isJsonMap(json) ? [json.id, json["@id"]].find(isPortableIdValue) : void 0;
858
- if (rootId != null) ids.push(rootId);
859
- for (const id of ids) discovered.set(id, (discovered.get(id) ?? 0) + 1);
860
- let hidden;
861
- try {
862
- hidden = findHiddenPortableObject(await jsonld.expand(json, { documentLoader: preloadedOnlyDocumentLoader }), discovered);
863
- } catch {
864
- return {
865
- verified: false,
866
- path: "",
867
- id: "",
868
- reason: "unsupportedContext"
869
- };
870
- }
871
- if (hidden != null) return {
872
- verified: false,
873
- path: "",
874
- id: hidden,
875
- reason: "hiddenPortableObject"
876
- };
877
- for (const candidate of maps) {
878
- const reason = await checkServedPortableMap(candidate, options);
879
- if (reason != null) return {
880
- verified: false,
881
- path: candidate.path,
882
- id: candidate.id,
883
- reason
884
- };
885
- }
886
- return { verified: true };
887
- }
888
- /**
889
- * Finds a portable object that an expanded JSON-LD document describes, i.e.,
890
- * a node with a portable `@id` and other members, more times than the given
891
- * numbers of maps found for its ID.
892
- */
893
- function findHiddenPortableObject(expanded, discovered) {
894
- const remaining = new Map(discovered);
895
- const pending = [expanded];
896
- while (pending.length > 0) {
897
- const value = pending.pop();
898
- if (Array.isArray(value)) {
899
- pending.push(...value);
900
- continue;
901
- }
902
- if (!isJsonMap(value)) continue;
903
- const id = value["@id"];
904
- if (isPortableIdValue(id) && Object.keys(value).some((key) => key !== "@id")) {
905
- const count = remaining.get(id) ?? 0;
906
- if (count < 1) return id;
907
- remaining.set(id, count - 1);
908
- }
909
- pending.push(...Object.values(value));
910
- }
911
- }
912
- /**
913
- * Checks whether a context uses only preloaded remote contexts and simple
914
- * term definitions, so that the context in effect for a map can be told from
915
- * the contexts of its ancestors.
916
- */
917
- function isCheckableContext(context) {
918
- return (Array.isArray(context) ? context : [context]).every((value) => typeof value === "string" ? Object.hasOwn(preloadedContexts, value) : !Array.isArray(value) && isSimpleContext(value));
919
- }
920
- /**
921
- * Checks whether every remote context that a context refers to is preloaded,
922
- * so that all of its definitions are known.
923
- */
924
- function hasOnlyPreloadedRemoteContexts(context) {
925
- return (Array.isArray(context) ? context : [context]).every((value) => typeof value === "string" ? Object.hasOwn(preloadedContexts, value) : !Array.isArray(value));
926
- }
927
- /**
928
- * Checks whether a map or any map below it, outside proof and context values,
929
- * has a portable `id` or `@id` and other members.
930
- */
931
- function hasLiteralPortableObject(json) {
932
- return findInJsonMaps(json, (map) => [map.id, map["@id"]].some(isPortableIdValue) && Object.keys(map).some((key) => key !== "id" && key !== "@id") ? true : void 0) ?? false;
933
- }
934
- function collectServedPortableMaps(json) {
935
- const maps = [];
936
- const seen = /* @__PURE__ */ new Set();
937
- const pending = [{
938
- value: json,
939
- path: "",
940
- depth: 0,
941
- contexts: []
942
- }];
943
- while (pending.length > 0) {
944
- const { value, path, depth, contexts } = pending.pop();
945
- if (typeof value !== "object" || value == null || seen.has(value)) continue;
946
- seen.add(value);
947
- if (Array.isArray(value)) {
948
- for (let index = value.length - 1; index >= 0; index--) pending.push({
949
- value: value[index],
950
- path: childPath(path, String(index)),
951
- depth: depth + 1,
952
- contexts
953
- });
954
- continue;
955
- }
956
- const map = value;
957
- const hasContext = Object.hasOwn(map, "@context");
958
- if (hasContext && !isCheckableContext(map["@context"])) {
959
- if (hasOnlyPreloadedRemoteContexts(map["@context"]) && !hasLiteralPortableObject(map)) continue;
960
- const id = [map.id, map["@id"]].find((v) => typeof v === "string");
961
- return {
962
- verified: false,
963
- path,
964
- id: typeof id === "string" ? id : "",
965
- reason: "unsupportedContext"
966
- };
967
- }
968
- const mapContexts = hasContext ? [...contexts, map["@context"]] : contexts;
969
- const id = [map.id, map["@id"]].find(isPortableIdValue);
970
- if (depth > 0 && id != null && Object.keys(map).some((key) => key !== "id" && key !== "@id")) maps.push({
971
- path,
972
- id,
973
- map,
974
- contexts: mapContexts
975
- });
976
- const keys = Object.keys(map);
977
- for (let index = keys.length - 1; index >= 0; index--) {
978
- const key = keys[index];
979
- if (key === "proof" || key === "@context") continue;
980
- pending.push({
981
- value: map[key],
982
- path: childPath(path, key),
983
- depth: depth + 1,
984
- contexts: mapContexts
985
- });
986
- }
987
- }
988
- maps.sort((left, right) => comparePaths(left.path, right.path));
989
- return { maps };
990
- }
991
- async function checkServedPortableMap({ map, contexts }, options) {
992
- if (Object.hasOwn(map, "proof")) {
993
- if (!isJsonMap(map.proof)) return "unsupportedProofShape";
994
- if (!Object.hasOwn(map, "@context")) return "missingContext";
995
- try {
996
- const policy = await verifyPortableObjectProofPolicy(map, await verifyMapLocalProof(map, options), options);
997
- return policy.verified ? null : policy.reason.type;
998
- } catch (error) {
999
- if (error instanceof TypeError) return "invalidPortableObject";
1000
- throw error;
1001
- }
1002
- }
1003
- const flattened = contexts.flatMap((context) => Array.isArray(context) ? context : [context]);
1004
- if (flattened.length < 1 || !isSimpleContext(flattened)) return "unsupportedContext";
1005
- let expanded;
1006
- try {
1007
- expanded = await jsonld.expand({
1008
- ...map,
1009
- "@context": flattened
1010
- }, { documentLoader: preloadedOnlyDocumentLoader });
1011
- } catch {
1012
- return "unsupportedContext";
1013
- }
1014
- if (expanded.length !== 1 || !isJsonMap(expanded[0])) return "invalidPortableObject";
1015
- const type = classifyFep2277CoreType(expanded[0]);
1016
- return UNSECURED_CORE_TYPES.has(type) ? null : "missingProof";
1017
- }
1018
- //#endregion
1019
- export { inspectCompoundPortableObjectApplicability as a, verifyServedPortableObjects as c, findUnsupportedCompoundProofShape as i, discoverCompoundProofDocuments as n, verifyCompoundPortableObjectProofs as o, findEmbeddedProofWithoutContext as r, verifyCompoundProofDocuments as s, containsCompoundPortableObject as t };