@kya-os/checkpoint-shared 1.4.0 → 1.5.1

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 (161) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/dist/beacon-types/index.d.mts +448 -432
  3. package/dist/beacon-types/index.d.mts.map +1 -0
  4. package/dist/beacon-types/index.d.ts +448 -432
  5. package/dist/beacon-types/index.d.ts.map +1 -0
  6. package/dist/beacon-types/index.js +286 -182
  7. package/dist/beacon-types/index.mjs +284 -180
  8. package/dist/beacon-types/index.mjs.map +1 -0
  9. package/dist/browser-posture-cookie-Dmq9Brf_.d.mts +5 -0
  10. package/dist/browser-posture-cookie-Dmq9Brf_.d.mts.map +1 -0
  11. package/dist/browser-posture-cookie-Dmq9Brf_.d.ts +5 -0
  12. package/dist/browser-posture-cookie-Dmq9Brf_.d.ts.map +1 -0
  13. package/dist/browser-posture-cookie.d.mts +2 -0
  14. package/dist/browser-posture-cookie.d.ts +2 -0
  15. package/dist/browser-posture-cookie.js +1 -0
  16. package/dist/browser-posture-cookie.mjs +2 -0
  17. package/dist/browser-posture-cookie.mjs.map +1 -0
  18. package/dist/browser-posture-report.d.mts +48 -44
  19. package/dist/browser-posture-report.d.mts.map +1 -0
  20. package/dist/browser-posture-report.d.ts +48 -44
  21. package/dist/browser-posture-report.d.ts.map +1 -0
  22. package/dist/browser-posture-report.js +1 -1
  23. package/dist/browser-posture-report.mjs +2 -1
  24. package/dist/browser-posture-report.mjs.map +1 -0
  25. package/dist/browser-posture.d.mts +82 -75
  26. package/dist/browser-posture.d.mts.map +1 -0
  27. package/dist/browser-posture.d.ts +82 -75
  28. package/dist/browser-posture.d.ts.map +1 -0
  29. package/dist/browser-posture.js +167 -18993
  30. package/dist/browser-posture.mjs +166 -18990
  31. package/dist/browser-posture.mjs.map +1 -0
  32. package/dist/browser.d.mts +323 -316
  33. package/dist/browser.d.mts.map +1 -0
  34. package/dist/browser.d.ts +323 -316
  35. package/dist/browser.d.ts.map +1 -0
  36. package/dist/browser.js +1 -1
  37. package/dist/browser.mjs +2 -1
  38. package/dist/browser.mjs.map +1 -0
  39. package/dist/canonical-signer/index.d.mts +38 -33
  40. package/dist/canonical-signer/index.d.mts.map +1 -0
  41. package/dist/canonical-signer/index.d.ts +38 -33
  42. package/dist/canonical-signer/index.d.ts.map +1 -0
  43. package/dist/canonical-signer/index.js +411 -181
  44. package/dist/canonical-signer/index.mjs +383 -175
  45. package/dist/canonical-signer/index.mjs.map +1 -0
  46. package/dist/clearance.d.mts +30 -0
  47. package/dist/clearance.d.mts.map +1 -0
  48. package/dist/clearance.d.ts +30 -0
  49. package/dist/clearance.d.ts.map +1 -0
  50. package/dist/clearance.js +120 -0
  51. package/dist/clearance.mjs +112 -0
  52. package/dist/clearance.mjs.map +1 -0
  53. package/dist/client-ip-1y2iXAYW.js +237 -0
  54. package/dist/client-ip-HOfe6ucV.mjs +215 -0
  55. package/dist/client-ip-HOfe6ucV.mjs.map +1 -0
  56. package/dist/deploy/index.d.mts +339 -339
  57. package/dist/deploy/index.d.mts.map +1 -0
  58. package/dist/deploy/index.d.ts +339 -339
  59. package/dist/deploy/index.d.ts.map +1 -0
  60. package/dist/deploy/index.js +505 -413
  61. package/dist/deploy/index.mjs +504 -410
  62. package/dist/deploy/index.mjs.map +1 -0
  63. package/dist/errors/index.d.mts +101 -101
  64. package/dist/errors/index.d.mts.map +1 -0
  65. package/dist/errors/index.d.ts +101 -101
  66. package/dist/errors/index.d.ts.map +1 -0
  67. package/dist/errors/index.js +341 -314
  68. package/dist/errors/index.mjs +340 -311
  69. package/dist/errors/index.mjs.map +1 -0
  70. package/dist/external-jr_S4iSS.d.mts +4187 -0
  71. package/dist/external-jr_S4iSS.d.mts.map +1 -0
  72. package/dist/external-jr_S4iSS.d.ts +4187 -0
  73. package/dist/external-jr_S4iSS.d.ts.map +1 -0
  74. package/dist/gateway/index.d.mts +138 -132
  75. package/dist/gateway/index.d.mts.map +1 -0
  76. package/dist/gateway/index.d.ts +138 -132
  77. package/dist/gateway/index.d.ts.map +1 -0
  78. package/dist/gateway/index.js +117 -81
  79. package/dist/gateway/index.mjs +116 -78
  80. package/dist/gateway/index.mjs.map +1 -0
  81. package/dist/gateway-client-ip.d.mts +59 -0
  82. package/dist/gateway-client-ip.d.mts.map +1 -0
  83. package/dist/gateway-client-ip.d.ts +59 -0
  84. package/dist/gateway-client-ip.d.ts.map +1 -0
  85. package/dist/gateway-client-ip.js +149 -0
  86. package/dist/gateway-client-ip.mjs +139 -0
  87. package/dist/gateway-client-ip.mjs.map +1 -0
  88. package/dist/index.d.mts +14901 -14246
  89. package/dist/index.d.mts.map +1 -0
  90. package/dist/index.d.ts +14901 -14246
  91. package/dist/index.d.ts.map +1 -0
  92. package/dist/index.js +15180 -31025
  93. package/dist/index.mjs +15065 -30942
  94. package/dist/index.mjs.map +1 -0
  95. package/dist/mcpi/agentshield-api/index.d.mts +45 -44
  96. package/dist/mcpi/agentshield-api/index.d.mts.map +1 -0
  97. package/dist/mcpi/agentshield-api/index.d.ts +45 -44
  98. package/dist/mcpi/agentshield-api/index.d.ts.map +1 -0
  99. package/dist/mcpi/agentshield-api/index.js +0 -2
  100. package/dist/mcpi/agentshield-api/index.mjs +1 -1
  101. package/dist/mcpi/approval-grant/index.d.mts +63 -61
  102. package/dist/mcpi/approval-grant/index.d.mts.map +1 -0
  103. package/dist/mcpi/approval-grant/index.d.ts +63 -61
  104. package/dist/mcpi/approval-grant/index.d.ts.map +1 -0
  105. package/dist/mcpi/approval-grant/index.js +220 -140
  106. package/dist/mcpi/approval-grant/index.mjs +219 -137
  107. package/dist/mcpi/approval-grant/index.mjs.map +1 -0
  108. package/dist/mcpi/dashboard-config/index.d.mts +2323 -2318
  109. package/dist/mcpi/dashboard-config/index.d.mts.map +1 -0
  110. package/dist/mcpi/dashboard-config/index.d.ts +2323 -2318
  111. package/dist/mcpi/dashboard-config/index.d.ts.map +1 -0
  112. package/dist/mcpi/dashboard-config/index.js +605 -458
  113. package/dist/mcpi/dashboard-config/index.mjs +604 -455
  114. package/dist/mcpi/dashboard-config/index.mjs.map +1 -0
  115. package/dist/mcpi/multibase-ed25519.d.mts +10 -8
  116. package/dist/mcpi/multibase-ed25519.d.mts.map +1 -0
  117. package/dist/mcpi/multibase-ed25519.d.ts +10 -8
  118. package/dist/mcpi/multibase-ed25519.d.ts.map +1 -0
  119. package/dist/mcpi/multibase-ed25519.js +182 -106
  120. package/dist/mcpi/multibase-ed25519.mjs +181 -103
  121. package/dist/mcpi/multibase-ed25519.mjs.map +1 -0
  122. package/dist/patterns-CkRVOdtV.js +243 -0
  123. package/dist/patterns-DzkGy_qk.mjs +173 -0
  124. package/dist/patterns-DzkGy_qk.mjs.map +1 -0
  125. package/dist/project-verification-key-C3L5rd8a.js +41 -0
  126. package/dist/project-verification-key-CgSVX9_m.mjs +37 -0
  127. package/dist/project-verification-key-CgSVX9_m.mjs.map +1 -0
  128. package/dist/rolldown-runtime-iY2fYEqi.mjs +19 -0
  129. package/dist/schemas-BmjNLAE7.js +5677 -0
  130. package/dist/schemas-DxY-dk9y.mjs +5511 -0
  131. package/dist/schemas-DxY-dk9y.mjs.map +1 -0
  132. package/dist/signals/index.d.mts +125 -115
  133. package/dist/signals/index.d.mts.map +1 -0
  134. package/dist/signals/index.d.ts +125 -115
  135. package/dist/signals/index.d.ts.map +1 -0
  136. package/dist/signals/index.js +1693 -1536
  137. package/dist/signals/index.mjs +1692 -1533
  138. package/dist/signals/index.mjs.map +1 -0
  139. package/dist/test-fixtures/index.d.mts +124 -119
  140. package/dist/test-fixtures/index.d.mts.map +1 -0
  141. package/dist/test-fixtures/index.d.ts +124 -119
  142. package/dist/test-fixtures/index.d.ts.map +1 -0
  143. package/dist/test-fixtures/index.js +460 -228
  144. package/dist/test-fixtures/index.mjs +459 -225
  145. package/dist/test-fixtures/index.mjs.map +1 -0
  146. package/dist/tool-protection/index.d.mts +343 -343
  147. package/dist/tool-protection/index.d.mts.map +1 -0
  148. package/dist/tool-protection/index.d.ts +343 -343
  149. package/dist/tool-protection/index.d.ts.map +1 -0
  150. package/dist/tool-protection/index.js +366 -285
  151. package/dist/tool-protection/index.mjs +365 -282
  152. package/dist/tool-protection/index.mjs.map +1 -0
  153. package/fixtures/bot-signal-resolution.json +142 -0
  154. package/fixtures/browser-posture-claims.json +421 -0
  155. package/fixtures/browser-posture-headers.json +46 -0
  156. package/fixtures/detection/reason-codes.json +10 -1
  157. package/fixtures/kya-http/challenge.delegation.fetcher-200.json +9 -3
  158. package/fixtures/kya-http/challenge.delegation.full.json +9 -3
  159. package/fixtures/kya-http/challenge.delegation.minimal.json +9 -3
  160. package/fixtures/kya-http/error-codes.json +7 -2
  161. package/package.json +20 -6
@@ -1,157 +1,237 @@
1
- 'use strict';
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let jose = require("jose");
2
3
 
3
- var jose = require('jose');
4
-
5
- // src/mcpi/approval-grant/grant.ts
6
- var APPROVAL_GRANT_TYP = "approval-grant+jwt";
7
- var APPROVAL_GRANT_MAX_LIFETIME_SEC = 300;
4
+ //#region src/mcpi/approval-grant/grant.ts
5
+ /**
6
+ * Step-up approval grants — portable, edge-safe reference implementation.
7
+ *
8
+ * The signed approval-grant scheme the step-up gate verifies. Lives in
9
+ * `@kya-os/checkpoint-shared` (the runtime-neutral home) so every runtime that
10
+ * enforces N-of-M step-up at the edge — the central gateway AND the SDK
11
+ * middleware adapters (Express / Next.js / Cloudflare-worker / .NET-port) —
12
+ * injects the SAME verifier into the step-up core
13
+ * (`@kya-os/checkpoint-wasm-runtime/step-up`) instead of re-implementing it.
14
+ *
15
+ * A grant is a compact EdDSA JWS (one crypto path with the delegation proof)
16
+ * binding five independent dimensions so a captured grant can't be replayed or
17
+ * repurposed past any one of them:
18
+ * - `requestHash` — the exact suspended action (TOCTOU)
19
+ * - `aud` — the resource/gateway it's valid for
20
+ * - `sub` — the AGENT being approved (`agentDid`; an approval for X must
21
+ * not satisfy Y's byte-identical call)
22
+ * - `iat`/`exp` — freshness (lifetime <= 300s; jose enforces exp)
23
+ * - `nonce` — server-issued + single-use; consumed on quorum satisfaction
24
+ * so a resubmission of the same grants is rejected
25
+ *
26
+ * The approver holds their own key and signs client-side; verification resolves
27
+ * the approver DID (via the host-injected {@link ApproverKeyResolver}) and is
28
+ * fail-closed.
29
+ *
30
+ * Edge-safe: `jose` is isomorphic (Web Crypto `SubtleCrypto`); nonce generation
31
+ * uses `globalThis.crypto.getRandomValues` (see `./nonce`). No `node:*` import.
32
+ *
33
+ * @module @kya-os/checkpoint-shared/mcpi/approval-grant
34
+ */
35
+ const APPROVAL_GRANT_TYP = "approval-grant+jwt";
36
+ const APPROVAL_GRANT_MAX_LIFETIME_SEC = 300;
37
+ /** Produce a signed grant (runs in the approver's client, with their key). */
8
38
  async function signApprovalGrant(meta, privateKeyJwk) {
9
- if (!Number.isInteger(meta.created) || !Number.isInteger(meta.expires)) {
10
- throw new Error("approval grant: created/expires must be integer unix seconds");
11
- }
12
- if (meta.expires <= meta.created) {
13
- throw new Error("approval grant: expires must be after created");
14
- }
15
- if (meta.expires - meta.created > APPROVAL_GRANT_MAX_LIFETIME_SEC) {
16
- throw new Error(`approval grant: lifetime exceeds ${APPROVAL_GRANT_MAX_LIFETIME_SEC}s ceiling`);
17
- }
18
- const key = await jose.importJWK(privateKeyJwk, "EdDSA");
19
- const claims = {
20
- requestHash: meta.requestHash,
21
- decision: meta.decision,
22
- nonce: meta.nonce
23
- };
24
- if (meta.amr) claims.amr = meta.amr;
25
- if (meta.delegationRef) claims.delegationRef = meta.delegationRef;
26
- const jws = await new jose.SignJWT(claims).setProtectedHeader({
27
- alg: "EdDSA",
28
- kid: `${meta.approverDid}#key-1`,
29
- typ: APPROVAL_GRANT_TYP
30
- }).setIssuer(meta.approverDid).setSubject(meta.agentDid).setAudience(meta.audience).setJti(meta.approvalRequestId).setIssuedAt(new Date(meta.created * 1e3)).setExpirationTime(new Date(meta.expires * 1e3)).sign(key);
31
- return { jws, meta };
39
+ if (!Number.isInteger(meta.created) || !Number.isInteger(meta.expires)) throw new Error("approval grant: created/expires must be integer unix seconds");
40
+ if (meta.expires <= meta.created) throw new Error("approval grant: expires must be after created");
41
+ if (meta.expires - meta.created > 300) throw new Error(`approval grant: lifetime exceeds ${300}s ceiling`);
42
+ const key = await (0, jose.importJWK)(privateKeyJwk, "EdDSA");
43
+ const claims = {
44
+ requestHash: meta.requestHash,
45
+ decision: meta.decision,
46
+ nonce: meta.nonce
47
+ };
48
+ if (meta.amr) claims.amr = meta.amr;
49
+ if (meta.delegationRef) claims.delegationRef = meta.delegationRef;
50
+ return {
51
+ jws: await new jose.SignJWT(claims).setProtectedHeader({
52
+ alg: "EdDSA",
53
+ kid: `${meta.approverDid}#key-1`,
54
+ typ: APPROVAL_GRANT_TYP
55
+ }).setIssuer(meta.approverDid).setSubject(meta.agentDid).setAudience(meta.audience).setJti(meta.approvalRequestId).setIssuedAt(/* @__PURE__ */ new Date(meta.created * 1e3)).setExpirationTime(/* @__PURE__ */ new Date(meta.expires * 1e3)).sign(key),
56
+ meta
57
+ };
32
58
  }
59
+ /** Verify a single grant against the gateway's expectation. Fail-closed. */
33
60
  async function verifyApprovalGrant(opts) {
34
- const { grant, expected, resolveKey } = opts;
35
- const approverDid = grant?.meta?.approverDid;
36
- if (typeof approverDid !== "string" || approverDid.length === 0) {
37
- return { valid: false, reason: "missing_approver_did" };
38
- }
39
- let publicKeyJwk = null;
40
- try {
41
- publicKeyJwk = await resolveKey(approverDid);
42
- } catch {
43
- return { valid: false, reason: "approver_key_unresolved" };
44
- }
45
- if (!publicKeyJwk) {
46
- return { valid: false, reason: "approver_key_unresolved" };
47
- }
48
- try {
49
- const key = await jose.importJWK(publicKeyJwk, "EdDSA");
50
- const { payload, protectedHeader } = await jose.jwtVerify(grant.jws, key, {
51
- algorithms: ["EdDSA"],
52
- issuer: approverDid,
53
- audience: expected.audience,
54
- ...expected.now !== void 0 ? { currentDate: new Date(expected.now * 1e3) } : {}
55
- });
56
- if (protectedHeader.typ !== APPROVAL_GRANT_TYP) {
57
- return { valid: false, reason: "bad_typ" };
58
- }
59
- if (payload.requestHash !== expected.requestHash) {
60
- return { valid: false, reason: "request_hash_mismatch" };
61
- }
62
- if (payload.nonce !== expected.nonce) {
63
- return { valid: false, reason: "nonce_mismatch" };
64
- }
65
- if (payload.sub !== expected.agentDid) {
66
- return { valid: false, reason: "agent_mismatch" };
67
- }
68
- if (payload.decision !== "approve") {
69
- return { valid: false, reason: "not_approved" };
70
- }
71
- const iat = payload.iat;
72
- const exp = payload.exp;
73
- if (typeof iat !== "number" || typeof exp !== "number") {
74
- return { valid: false, reason: "missing_times" };
75
- }
76
- if (exp - iat > (expected.maxLifetimeSec ?? APPROVAL_GRANT_MAX_LIFETIME_SEC)) {
77
- return { valid: false, reason: "lifetime_exceeded" };
78
- }
79
- if (grant.meta.requestHash !== payload.requestHash || grant.meta.nonce !== payload.nonce || grant.meta.agentDid !== payload.sub || grant.meta.audience !== payload.aud || grant.meta.decision !== payload.decision || grant.meta.approverDid !== payload.iss) {
80
- return { valid: false, reason: "meta_mismatch" };
81
- }
82
- return { valid: true, approverDid };
83
- } catch {
84
- return { valid: false, reason: "verify_failed" };
85
- }
61
+ const { grant, expected, resolveKey } = opts;
62
+ const approverDid = grant?.meta?.approverDid;
63
+ if (typeof approverDid !== "string" || approverDid.length === 0) return {
64
+ valid: false,
65
+ reason: "missing_approver_did"
66
+ };
67
+ let publicKeyJwk = null;
68
+ try {
69
+ publicKeyJwk = await resolveKey(approverDid);
70
+ } catch {
71
+ return {
72
+ valid: false,
73
+ reason: "approver_key_unresolved"
74
+ };
75
+ }
76
+ if (!publicKeyJwk) return {
77
+ valid: false,
78
+ reason: "approver_key_unresolved"
79
+ };
80
+ try {
81
+ const key = await (0, jose.importJWK)(publicKeyJwk, "EdDSA");
82
+ const { payload, protectedHeader } = await (0, jose.jwtVerify)(grant.jws, key, {
83
+ algorithms: ["EdDSA"],
84
+ issuer: approverDid,
85
+ audience: expected.audience,
86
+ ...expected.now !== void 0 ? { currentDate: /* @__PURE__ */ new Date(expected.now * 1e3) } : {}
87
+ });
88
+ if (protectedHeader.typ !== "approval-grant+jwt") return {
89
+ valid: false,
90
+ reason: "bad_typ"
91
+ };
92
+ if (payload.requestHash !== expected.requestHash) return {
93
+ valid: false,
94
+ reason: "request_hash_mismatch"
95
+ };
96
+ if (payload.nonce !== expected.nonce) return {
97
+ valid: false,
98
+ reason: "nonce_mismatch"
99
+ };
100
+ if (payload.sub !== expected.agentDid) return {
101
+ valid: false,
102
+ reason: "agent_mismatch"
103
+ };
104
+ if (payload.decision !== "approve") return {
105
+ valid: false,
106
+ reason: "not_approved"
107
+ };
108
+ const iat = payload.iat;
109
+ const exp = payload.exp;
110
+ if (typeof iat !== "number" || typeof exp !== "number") return {
111
+ valid: false,
112
+ reason: "missing_times"
113
+ };
114
+ if (exp - iat > (expected.maxLifetimeSec ?? 300)) return {
115
+ valid: false,
116
+ reason: "lifetime_exceeded"
117
+ };
118
+ if (grant.meta.requestHash !== payload.requestHash || grant.meta.nonce !== payload.nonce || grant.meta.agentDid !== payload.sub || grant.meta.audience !== payload.aud || grant.meta.decision !== payload.decision || grant.meta.approverDid !== payload.iss) return {
119
+ valid: false,
120
+ reason: "meta_mismatch"
121
+ };
122
+ return {
123
+ valid: true,
124
+ approverDid
125
+ };
126
+ } catch {
127
+ return {
128
+ valid: false,
129
+ reason: "verify_failed"
130
+ };
131
+ }
86
132
  }
133
+ /**
134
+ * Quorum is satisfied iff >= `quorum.n` DISTINCT allow-listed approvers each
135
+ * signed a valid grant bound to the suspended action's outstanding nonce. On
136
+ * satisfaction the nonce is consumed — a replay of the same grants then finds no
137
+ * outstanding nonce and is rejected. Reuses {@link verifyApprovalGrant} per grant.
138
+ */
87
139
  async function verifySignedApprovalQuorum(opts) {
88
- const { grants, expected, quorum, resolveKey, nonceStore } = opts;
89
- if (!Number.isInteger(quorum.n) || quorum.n < 1) {
90
- return { satisfied: false, reason: "invalid_quorum" };
91
- }
92
- if (!Array.isArray(quorum.approvers) || quorum.approvers.length === 0) {
93
- return { satisfied: false, reason: "no_approvers" };
94
- }
95
- const nonce = nonceStore.current(expected.requestHash);
96
- if (!nonce) {
97
- return { satisfied: false, reason: "no_outstanding_challenge" };
98
- }
99
- const approvers = /* @__PURE__ */ new Set();
100
- for (const grant of grants) {
101
- const v = await verifyApprovalGrant({
102
- grant,
103
- expected: {
104
- requestHash: expected.requestHash,
105
- audience: expected.audience,
106
- agentDid: expected.agentDid,
107
- nonce,
108
- ...expected.now !== void 0 ? { now: expected.now } : {}
109
- },
110
- resolveKey
111
- });
112
- if (!v.valid || !v.approverDid) continue;
113
- if (!quorum.approvers.includes(v.approverDid)) continue;
114
- approvers.add(v.approverDid);
115
- }
116
- if (approvers.size >= quorum.n) {
117
- if (!nonceStore.tryConsume(expected.requestHash)) {
118
- return { satisfied: false, reason: "nonce_already_consumed" };
119
- }
120
- return { satisfied: true };
121
- }
122
- return { satisfied: false, reason: `quorum_not_met:${approvers.size}/${quorum.n}` };
140
+ const { grants, expected, quorum, resolveKey, nonceStore } = opts;
141
+ if (!Number.isInteger(quorum.n) || quorum.n < 1) return {
142
+ satisfied: false,
143
+ reason: "invalid_quorum"
144
+ };
145
+ if (!Array.isArray(quorum.approvers) || quorum.approvers.length === 0) return {
146
+ satisfied: false,
147
+ reason: "no_approvers"
148
+ };
149
+ const nonce = nonceStore.current(expected.requestHash);
150
+ if (!nonce) return {
151
+ satisfied: false,
152
+ reason: "no_outstanding_challenge"
153
+ };
154
+ const approvers = /* @__PURE__ */ new Set();
155
+ for (const grant of grants) {
156
+ const v = await verifyApprovalGrant({
157
+ grant,
158
+ expected: {
159
+ requestHash: expected.requestHash,
160
+ audience: expected.audience,
161
+ agentDid: expected.agentDid,
162
+ nonce,
163
+ ...expected.now !== void 0 ? { now: expected.now } : {}
164
+ },
165
+ resolveKey
166
+ });
167
+ if (!v.valid || !v.approverDid) continue;
168
+ if (!quorum.approvers.includes(v.approverDid)) continue;
169
+ approvers.add(v.approverDid);
170
+ }
171
+ if (approvers.size >= quorum.n) {
172
+ if (!nonceStore.tryConsume(expected.requestHash)) return {
173
+ satisfied: false,
174
+ reason: "nonce_already_consumed"
175
+ };
176
+ return { satisfied: true };
177
+ }
178
+ return {
179
+ satisfied: false,
180
+ reason: `quorum_not_met:${approvers.size}/${quorum.n}`
181
+ };
123
182
  }
183
+
184
+ //#endregion
185
+ //#region src/mcpi/approval-grant/nonce.ts
186
+ /**
187
+ * Server-issued, single-use nonce store for step-up approval grants.
188
+ *
189
+ * Issues one nonce per suspended action (`requestHash`) and consumes it when the
190
+ * quorum is satisfied. A resubmission of the same grants finds the nonce gone →
191
+ * rejected. This is what makes the grant single-use (closes replay across
192
+ * connections). In production this is shared state (Redis/DO); in-memory here.
193
+ *
194
+ * Edge-safe: nonce bytes come from `globalThis.crypto.getRandomValues` (Web
195
+ * Crypto — present on Node 18+, Cloudflare Workers, and Vercel Edge), encoded
196
+ * with `jose`'s isomorphic `base64url`. No `node:crypto`, no `Buffer`.
197
+ *
198
+ * @module @kya-os/checkpoint-shared/mcpi/approval-grant
199
+ */
200
+ /** 32 cryptographically-random bytes, base64url-encoded. Edge-safe. */
124
201
  function generateNonce() {
125
- return jose.base64url.encode(globalThis.crypto.getRandomValues(new Uint8Array(32)));
202
+ return jose.base64url.encode(globalThis.crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(32)));
126
203
  }
127
204
  var InMemoryApprovalNonceStore = class {
128
- issued = /* @__PURE__ */ new Map();
129
- issue(requestHash, ttlSec = APPROVAL_GRANT_MAX_LIFETIME_SEC) {
130
- const now = Math.floor(Date.now() / 1e3);
131
- const existing = this.issued.get(requestHash);
132
- if (existing && !existing.consumed && existing.expiresAt >= now) {
133
- return existing.nonce;
134
- }
135
- const nonce = generateNonce();
136
- this.issued.set(requestHash, { nonce, consumed: false, expiresAt: now + ttlSec });
137
- return nonce;
138
- }
139
- current(requestHash) {
140
- const e = this.issued.get(requestHash);
141
- if (!e || e.consumed || e.expiresAt < Math.floor(Date.now() / 1e3)) return null;
142
- return e.nonce;
143
- }
144
- tryConsume(requestHash) {
145
- const e = this.issued.get(requestHash);
146
- if (!e || e.consumed || e.expiresAt < Math.floor(Date.now() / 1e3)) return false;
147
- e.consumed = true;
148
- return true;
149
- }
205
+ issued = /* @__PURE__ */ new Map();
206
+ issue(requestHash, ttlSec = 300) {
207
+ const now = Math.floor(Date.now() / 1e3);
208
+ const existing = this.issued.get(requestHash);
209
+ if (existing && !existing.consumed && existing.expiresAt >= now) return existing.nonce;
210
+ const nonce = generateNonce();
211
+ this.issued.set(requestHash, {
212
+ nonce,
213
+ consumed: false,
214
+ expiresAt: now + ttlSec
215
+ });
216
+ return nonce;
217
+ }
218
+ current(requestHash) {
219
+ const e = this.issued.get(requestHash);
220
+ if (!e || e.consumed || e.expiresAt < Math.floor(Date.now() / 1e3)) return null;
221
+ return e.nonce;
222
+ }
223
+ tryConsume(requestHash) {
224
+ const e = this.issued.get(requestHash);
225
+ if (!e || e.consumed || e.expiresAt < Math.floor(Date.now() / 1e3)) return false;
226
+ e.consumed = true;
227
+ return true;
228
+ }
150
229
  };
151
230
 
231
+ //#endregion
152
232
  exports.APPROVAL_GRANT_MAX_LIFETIME_SEC = APPROVAL_GRANT_MAX_LIFETIME_SEC;
153
233
  exports.APPROVAL_GRANT_TYP = APPROVAL_GRANT_TYP;
154
234
  exports.InMemoryApprovalNonceStore = InMemoryApprovalNonceStore;
155
235
  exports.signApprovalGrant = signApprovalGrant;
156
236
  exports.verifyApprovalGrant = verifyApprovalGrant;
157
- exports.verifySignedApprovalQuorum = verifySignedApprovalQuorum;
237
+ exports.verifySignedApprovalQuorum = verifySignedApprovalQuorum;