@verax-ai/body 0.5.0 → 0.6.0

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 (46) hide show
  1. package/README.md +4 -3
  2. package/dist/approval-signature.d.ts +19 -0
  3. package/dist/approval-signature.js +70 -5
  4. package/dist/approve-cli.js +20 -24
  5. package/dist/cli.d.ts +1 -0
  6. package/dist/cli.js +146 -14
  7. package/dist/config.d.ts +23 -0
  8. package/dist/config.js +41 -0
  9. package/dist/control-signature.d.ts +4 -0
  10. package/dist/control-signature.js +20 -5
  11. package/dist/definitions.d.ts +9 -0
  12. package/dist/definitions.js +53 -0
  13. package/dist/demo.js +15 -12
  14. package/dist/doctor.js +72 -0
  15. package/dist/downstream-pin.d.ts +9 -0
  16. package/dist/downstream-pin.js +273 -0
  17. package/dist/downstream.d.ts +22 -2
  18. package/dist/downstream.js +312 -44
  19. package/dist/heartbeat.d.ts +26 -2
  20. package/dist/heartbeat.js +90 -7
  21. package/dist/held-spend.d.ts +16 -0
  22. package/dist/held-spend.js +20 -0
  23. package/dist/http-io.d.ts +14 -0
  24. package/dist/http-io.js +46 -0
  25. package/dist/init-local.d.ts +11 -0
  26. package/dist/init-local.js +32 -3
  27. package/dist/install.d.ts +5 -0
  28. package/dist/install.js +37 -5
  29. package/dist/main.d.ts +1 -0
  30. package/dist/main.js +34 -5
  31. package/dist/operator-cli.d.ts +49 -1
  32. package/dist/operator-cli.js +132 -7
  33. package/dist/operator-credentials.d.ts +40 -1
  34. package/dist/operator-credentials.js +97 -7
  35. package/dist/operator-door-page.d.ts +3 -0
  36. package/dist/operator-door-page.js +819 -0
  37. package/dist/operator-door.d.ts +22 -0
  38. package/dist/operator-door.js +487 -0
  39. package/dist/operator-handlers.d.ts +28 -0
  40. package/dist/operator-handlers.js +221 -0
  41. package/dist/server.d.ts +2 -0
  42. package/dist/server.js +108 -255
  43. package/dist/verify-cli.js +30 -10
  44. package/dist/webauthn-verify.d.ts +48 -4
  45. package/dist/webauthn-verify.js +183 -25
  46. package/package.json +4 -4
@@ -0,0 +1,221 @@
1
+ import { decisionRecordHash } from "@cedulon/core";
2
+ import { approvalsLogFor, approvePending, createApprovalBudgetGuard, loadApprovalsFromDir, terminalText } from "@verax-ai/proxy";
3
+ import { approvalChallenge, gateApprovalSignature } from "./approval-signature.js";
4
+ import { gateControlSignature, nextControlChallenge } from "./control-signature.js";
5
+ import { haltBody, readHalt, resumeBody } from "./halt.js";
6
+ import { MAX_BODY_BYTES, readJsonBody, send } from "./http-io.js";
7
+ import { loadOrCreateSigners } from "./keys.js";
8
+ import { readCredentials } from "./operator-credentials.js";
9
+ import { readRpConfig } from "./rp-config.js";
10
+ async function signedDefer(ledger, ref) {
11
+ const rows = await ledger.decisions();
12
+ return rows.find((row) => row.claims.ref === ref && row.claims.decision === "defer") ?? null;
13
+ }
14
+ /** `GET|POST halt`, `POST resume`, and the challenge for either. */
15
+ export async function handleControl(ctx, what) {
16
+ const { req, res, stateDir, services, scopes } = ctx;
17
+ const { resuming } = what;
18
+ if (await ctx.refuseStale())
19
+ return;
20
+ const operator = scopes.has("verax:audit") || scopes.has("verax:approve");
21
+ // Stopping is the safe direction, so any operator session may do it.
22
+ // Lifting a halt lets everything through again: that is an approval of
23
+ // everything at once and needs the approve scope. The agent token
24
+ // carries neither, so an agent cannot stop itself being stopped.
25
+ if (!operator || (resuming && !scopes.has("verax:approve"))) {
26
+ send(res, 403, { error: "scope-missing" });
27
+ return;
28
+ }
29
+ if (what.challenge) {
30
+ const rp = readRpConfig(ctx.env);
31
+ if (!rp.ok) {
32
+ send(res, 503, { error: "passkey-closed", reason: rp.reason });
33
+ return;
34
+ }
35
+ send(res, 200, {
36
+ ...nextControlChallenge(stateDir, resuming ? "resume" : "halt"),
37
+ rpId: rp.config.rpID,
38
+ allowCredentials: readCredentials(stateDir).map((row) => ({ id: row.id, type: "public-key" })),
39
+ });
40
+ return;
41
+ }
42
+ if (req.method === "GET") {
43
+ send(res, 200, readHalt(stateDir));
44
+ return;
45
+ }
46
+ const parsed = await readJsonBody(req, MAX_BODY_BYTES);
47
+ if (await ctx.refuseStale())
48
+ return;
49
+ if (!parsed.ok && resuming) {
50
+ send(res, 400, { error: "bad-body" });
51
+ return;
52
+ }
53
+ const asked = parsed.ok && parsed.value && typeof parsed.value === "object" ? parsed.value : {};
54
+ // The tail is read inside the gate and the line is appended right
55
+ // after it, with nothing awaited in between.
56
+ const gated = gateControlSignature({
57
+ stateDir,
58
+ env: ctx.env,
59
+ action: resuming ? "resume" : "halt",
60
+ assertion: asked.assertion,
61
+ });
62
+ // A refused resume writes nothing. A halt is never refused for its
63
+ // signature: it stops unsigned, and the answer says what was refused.
64
+ if (!gated.ok && resuming) {
65
+ send(res, gated.status, { error: gated.error, ...(gated.reason ? { reason: gated.reason } : {}) });
66
+ return;
67
+ }
68
+ const signature = gated.ok ? gated.signature : undefined;
69
+ const refused = !parsed.ok ? "bad-body" : gated.ok ? undefined : gated.error;
70
+ const by = ctx.subject;
71
+ const state = resuming
72
+ ? resumeBody(stateDir, by, "http", Date.now(), signature)
73
+ : {
74
+ ...haltBody(stateDir, by, "http", Date.now(), signature),
75
+ ...(refused ? { signatureRefused: refused } : {}),
76
+ };
77
+ try {
78
+ await services.proxy.syncControlRecords();
79
+ }
80
+ catch (err) {
81
+ // The switch is already on disk. A sync miss must not turn this into a 500.
82
+ const message = (err instanceof Error ? err.message : String(err)).replace(/[\r\n]+/g, " ");
83
+ process.stderr.write(`verax-proxy: control sync failed: ${terminalText(message)}\n`);
84
+ }
85
+ send(res, 200, state);
86
+ }
87
+ export async function handleApproveChallenge(ctx) {
88
+ const { res, stateDir, services } = ctx;
89
+ if (!ctx.scopes.has("verax:approve")) {
90
+ send(res, 403, { error: "scope-missing" });
91
+ return;
92
+ }
93
+ if (await ctx.refuseStale())
94
+ return;
95
+ const ref = ctx.url.searchParams.get("ref") ?? "";
96
+ const deferRecord = ref === "" ? null : await signedDefer(services.ledger, ref);
97
+ if (!deferRecord) {
98
+ send(res, 404, { error: "unknown-ref" });
99
+ return;
100
+ }
101
+ const rp = readRpConfig(ctx.env);
102
+ if (!rp.ok) {
103
+ send(res, 503, { error: "passkey-closed", reason: rp.reason });
104
+ return;
105
+ }
106
+ send(res, 200, {
107
+ challenge: approvalChallenge({
108
+ ref,
109
+ requestHash: deferRecord.claims.requestHash,
110
+ deferRecordHash: decisionRecordHash(deferRecord),
111
+ }),
112
+ rpId: rp.config.rpID,
113
+ allowCredentials: readCredentials(stateDir).map((row) => ({ id: row.id, type: "public-key" })),
114
+ });
115
+ }
116
+ export async function handleApprove(ctx) {
117
+ const { req, res, stateDir, services } = ctx;
118
+ // Reading the ledger is not approving from it. The audit scope opens
119
+ // every door above; this one signs a new decision and lets money go,
120
+ // so it has a scope of its own.
121
+ if (!ctx.scopes.has("verax:approve")) {
122
+ send(res, 403, { error: "scope-missing" });
123
+ return;
124
+ }
125
+ const parsed = await readJsonBody(req, MAX_BODY_BYTES);
126
+ if (await ctx.refuseStale())
127
+ return;
128
+ if (!parsed.ok) {
129
+ send(res, 400, { error: "bad-body" });
130
+ return;
131
+ }
132
+ const asked = (parsed.value ?? {});
133
+ const ref = typeof asked.ref === "string" ? asked.ref : "";
134
+ const sawHash = typeof asked.requestHash === "string" ? asked.requestHash : "";
135
+ if (ref === "" || sawHash === "") {
136
+ send(res, 400, { error: "ref-and-requestHash-required" });
137
+ return;
138
+ }
139
+ const waiting = loadApprovalsFromDir(stateDir).find((row) => row.ref === ref);
140
+ if (!waiting) {
141
+ send(res, 404, { error: "unknown-ref" });
142
+ return;
143
+ }
144
+ // The list on a phone can be ten minutes old. An approval names the
145
+ // request the operator was looking at, and if that is not what is
146
+ // waiting any more, nothing is approved: "I approved what I saw" is
147
+ // only true when something checks it.
148
+ if (waiting.requestHash !== sawHash) {
149
+ send(res, 409, { error: "stale", requestHash: waiting.requestHash });
150
+ return;
151
+ }
152
+ const deferRecord = await signedDefer(services.ledger, ref);
153
+ if (!deferRecord) {
154
+ send(res, 404, { error: "unknown-ref" });
155
+ return;
156
+ }
157
+ const deferRecordHash = decisionRecordHash(deferRecord);
158
+ // The challenge is rebuilt from the defer on disk, not from anything the client sent.
159
+ // A removal the command wrote is only a mark on disk until this copies it
160
+ // into the ledger. The copy has to land before the signature check, or the
161
+ // allow is appended in the window with no removal record ahead of it. A
162
+ // sync miss is one stderr line and is not turned into a 500.
163
+ try {
164
+ await services.proxy.syncControlRecords();
165
+ }
166
+ catch (err) {
167
+ const message = (err instanceof Error ? err.message : String(err)).replace(/[\r\n]+/g, " ");
168
+ process.stderr.write(`verax-proxy: control sync failed: ${terminalText(message)}\n`);
169
+ }
170
+ const gated = await gateApprovalSignature({
171
+ stateDir,
172
+ env: ctx.env,
173
+ assertion: asked.assertion,
174
+ ref,
175
+ requestHash: deferRecord.claims.requestHash,
176
+ deferRecordHash,
177
+ });
178
+ if (!gated.ok) {
179
+ send(res, gated.status, {
180
+ error: gated.error,
181
+ ...(gated.reason ? { reason: gated.reason } : {}),
182
+ });
183
+ return;
184
+ }
185
+ // Who approved, on the record. The CLI writes the machine's login name,
186
+ // which says nothing about the person holding the phone; the session's
187
+ // own subject does.
188
+ const approver = ctx.subject;
189
+ const approvals = approvalsLogFor(services.ledger);
190
+ const outcome = await approvePending({
191
+ ledger: services.ledger,
192
+ recordSigner: loadOrCreateSigners(stateDir).recordSigner,
193
+ now: () => Date.now(),
194
+ nonce: () => crypto.randomUUID(),
195
+ ref,
196
+ approverId: approver,
197
+ via: "http",
198
+ policyHash: deferRecord.claims.policyHash,
199
+ ...(gated.signature ? { signature: gated.signature } : {}),
200
+ approvals,
201
+ budgetGuard: createApprovalBudgetGuard({
202
+ policy: services.policy,
203
+ approvals,
204
+ now: () => Date.now(),
205
+ ledger: services.ledger,
206
+ }),
207
+ });
208
+ if (!outcome.ok) {
209
+ if (outcome.reason === "halted") {
210
+ send(res, 409, { error: "approve-halted" });
211
+ return;
212
+ }
213
+ const code = outcome.reason === "unknown-ref" || outcome.reason === "snapshot-missing" ? 404 : 409;
214
+ send(res, code, {
215
+ error: outcome.reason,
216
+ ...(outcome.allowRef ? { allowRef: outcome.allowRef } : {}),
217
+ });
218
+ return;
219
+ }
220
+ send(res, 200, { allowRef: outcome.allowRef, approver });
221
+ }
package/dist/server.d.ts CHANGED
@@ -198,4 +198,6 @@ export declare function bearerStillLive(payload: {
198
198
  exp?: unknown;
199
199
  }, stateDir: string): boolean;
200
200
  export declare function listen(config: BodyConfig): Promise<Server>;
201
+ /** The operator door opened with this body, or null when it is shut. */
202
+ export declare function operatorDoorOf(server: Server): Server | null;
201
203
  export { TOOL_NAMES };