@verax-ai/body 0.5.1 → 0.6.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.
- package/README.md +4 -3
- package/dist/approval-signature.d.ts +19 -0
- package/dist/approval-signature.js +70 -5
- package/dist/approve-cli.js +20 -24
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +146 -14
- package/dist/config.d.ts +27 -0
- package/dist/config.js +72 -1
- package/dist/control-signature.d.ts +4 -0
- package/dist/control-signature.js +20 -5
- package/dist/definitions.d.ts +9 -0
- package/dist/definitions.js +53 -0
- package/dist/demo.js +11 -11
- package/dist/doctor.js +72 -0
- package/dist/downstream-pin.d.ts +9 -0
- package/dist/downstream-pin.js +273 -0
- package/dist/downstream.d.ts +22 -2
- package/dist/downstream.js +312 -44
- package/dist/heartbeat.d.ts +26 -2
- package/dist/heartbeat.js +90 -7
- package/dist/held-spend.d.ts +16 -0
- package/dist/held-spend.js +20 -0
- package/dist/http-io.d.ts +14 -0
- package/dist/http-io.js +46 -0
- package/dist/init-local.d.ts +11 -0
- package/dist/init-local.js +32 -3
- package/dist/install.d.ts +5 -0
- package/dist/install.js +37 -5
- package/dist/main.d.ts +1 -0
- package/dist/main.js +40 -6
- package/dist/operator-cli.d.ts +49 -1
- package/dist/operator-cli.js +136 -8
- package/dist/operator-credentials.d.ts +40 -1
- package/dist/operator-credentials.js +97 -7
- package/dist/operator-door-page.d.ts +3 -0
- package/dist/operator-door-page.js +819 -0
- package/dist/operator-door.d.ts +22 -0
- package/dist/operator-door.js +487 -0
- package/dist/operator-handlers.d.ts +28 -0
- package/dist/operator-handlers.js +221 -0
- package/dist/operator-pairing.d.ts +2 -0
- package/dist/operator-pairing.js +11 -0
- package/dist/reconcile-cli.js +3 -1
- package/dist/server.d.ts +2 -0
- package/dist/server.js +133 -256
- package/dist/verify-cli.js +30 -10
- package/dist/webauthn-verify.d.ts +48 -4
- package/dist/webauthn-verify.js +183 -25
- package/dist/wiring.js +14 -1
- 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
|
+
}
|
|
@@ -17,6 +17,8 @@ export declare function hashPairingCode(code: string): string;
|
|
|
17
17
|
export declare function mintPairingCode(): string;
|
|
18
18
|
export declare function writePairing(stateDir: string, record: PairingRecord): void;
|
|
19
19
|
export declare function readPairing(stateDir: string): PairingRecord | null;
|
|
20
|
+
/** A pairing the door would still accept. Enrolling again replaces that code. */
|
|
21
|
+
export declare function livePairing(stateDir: string, nowMs?: number): boolean;
|
|
20
22
|
export declare function beginPairing(stateDir: string, nowMs?: number): {
|
|
21
23
|
code: string;
|
|
22
24
|
};
|
package/dist/operator-pairing.js
CHANGED
|
@@ -32,6 +32,17 @@ export function readPairing(stateDir) {
|
|
|
32
32
|
return null;
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
+
/** A pairing the door would still accept. Enrolling again replaces that code. */
|
|
36
|
+
export function livePairing(stateDir, nowMs = Date.now()) {
|
|
37
|
+
const row = readPairing(stateDir);
|
|
38
|
+
if (!row)
|
|
39
|
+
return false;
|
|
40
|
+
if (row.attempts >= PAIRING_MAX_ATTEMPTS)
|
|
41
|
+
return false;
|
|
42
|
+
if (nowMs > row.expiresAtMs)
|
|
43
|
+
return false;
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
35
46
|
export function beginPairing(stateDir, nowMs = Date.now()) {
|
|
36
47
|
const code = mintPairingCode();
|
|
37
48
|
writePairing(stateDir, {
|
package/dist/reconcile-cli.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
-
import { loadApprovalsFromDir, loadDecisionRefsFromDir, loadEffectsFromDir, loadPolicy, parseCardCsv, parseChannelJsonl, reconcile, } from "@verax-ai/proxy";
|
|
3
|
+
import { loadApprovalsFromDir, loadDecisionRefsFromDir, loadDecisionRequestHashesFromDir, loadEffectsFromDir, loadPolicy, parseCardCsv, parseChannelJsonl, reconcile, } from "@verax-ai/proxy";
|
|
4
4
|
const SNAPSHOT_NAME = /^[0-9a-f]{64}\.json$/;
|
|
5
5
|
// The descriptors come from the parsed policy, never from the stored document: a snapshot is
|
|
6
6
|
// read with unknown keys ignored, and a block the hash does not cover must not decide a match.
|
|
@@ -220,12 +220,14 @@ export function runReconcile(argv, writeErr = (s) => process.stderr.write(s)) {
|
|
|
220
220
|
const channel = card ?? parseChannelJsonl(raw);
|
|
221
221
|
const effects = loadEffectsFromDir(parsed.stateDir);
|
|
222
222
|
const decisionRefs = loadDecisionRefsFromDir(parsed.stateDir);
|
|
223
|
+
const decisionRequestHashes = loadDecisionRequestHashesFromDir(parsed.stateDir);
|
|
223
224
|
const loaded = parsed.channel === "card" ? loadDescriptorsFromDir(parsed.stateDir) : undefined;
|
|
224
225
|
const descriptorsByPayee = loaded?.descriptorsByPayee;
|
|
225
226
|
const report = reconcile(channel, effects, {
|
|
226
227
|
toleranceMs: parsed.toleranceMs,
|
|
227
228
|
window: parsed.window,
|
|
228
229
|
decisionRefs,
|
|
230
|
+
decisionRequestHashes,
|
|
229
231
|
approvals: parsed.channel === "card" ? loadApprovalsFromDir(parsed.stateDir) : undefined,
|
|
230
232
|
skipped: card?.skipped,
|
|
231
233
|
...(descriptorsByPayee && Object.keys(descriptorsByPayee).length > 0
|
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 };
|