@verax-ai/body 0.4.1 → 0.4.3
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/dist/anchor-cli.d.ts +2 -0
- package/dist/anchor-cli.js +203 -0
- package/dist/approval-signature.d.ts +47 -0
- package/dist/approval-signature.js +290 -0
- package/dist/cli.js +8 -1
- package/dist/desktop.d.ts +43 -1
- package/dist/desktop.js +335 -34
- package/dist/halt.d.ts +7 -2
- package/dist/install.d.ts +38 -2
- package/dist/install.js +392 -27
- package/dist/operator-credentials.d.ts +1 -0
- package/dist/operator-credentials.js +4 -2
- package/dist/ps-module-imports.d.ts +17 -0
- package/dist/ps-module-imports.js +34 -0
- package/dist/server.js +79 -4
- package/dist/verify-cli.d.ts +2 -2
- package/dist/verify-cli.js +81 -5
- package/dist/webauthn-verify.d.ts +40 -0
- package/dist/webauthn-verify.js +159 -0
- package/dist/wiring.js +3 -0
- package/package.json +3 -3
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* System32 module directory loads for PowerShell started with systemToolEnv().
|
|
3
|
+
*
|
|
4
|
+
* #119, measured on windows-full 5a75875: under that environment PSModulePath is
|
|
5
|
+
* only the System32 Windows PowerShell 5.1 module directory, and resolving an
|
|
6
|
+
* autoloaded cmdlet (ConvertFrom-Json) took 23 to 30 s on a GitHub Windows
|
|
7
|
+
* runner. Import-Module of the same module by its System32 path took 0.27 s.
|
|
8
|
+
* Each statement names the module directory. LocalAccounts lives in a version
|
|
9
|
+
* subdirectory (1.0.0.0 on the measured Windows 11 machine); importing the
|
|
10
|
+
* directory lets Windows PowerShell choose that version and stay inside System32.
|
|
11
|
+
*/
|
|
12
|
+
export const PS_MODULES = [
|
|
13
|
+
"Microsoft.PowerShell.Utility",
|
|
14
|
+
"Microsoft.PowerShell.Management",
|
|
15
|
+
"Microsoft.PowerShell.Security",
|
|
16
|
+
"Microsoft.PowerShell.LocalAccounts",
|
|
17
|
+
"ScheduledTasks",
|
|
18
|
+
];
|
|
19
|
+
/** One Import-Module statement. The expected line shape lives only here. */
|
|
20
|
+
export function psModuleImportLine(module) {
|
|
21
|
+
return `Import-Module "$env:SystemRoot\\System32\\WindowsPowerShell\\v1.0\\Modules\\${module}"`;
|
|
22
|
+
}
|
|
23
|
+
/** One line per module, in the given order. A repeated module is emitted once. */
|
|
24
|
+
export function psModuleImports(modules) {
|
|
25
|
+
const seen = new Set();
|
|
26
|
+
const lines = [];
|
|
27
|
+
for (const module of modules) {
|
|
28
|
+
if (seen.has(module))
|
|
29
|
+
continue;
|
|
30
|
+
seen.add(module);
|
|
31
|
+
lines.push(psModuleImportLine(module));
|
|
32
|
+
}
|
|
33
|
+
return lines.join("\n");
|
|
34
|
+
}
|
package/dist/server.js
CHANGED
|
@@ -4,7 +4,11 @@ import { createServer } from "node:http";
|
|
|
4
4
|
import { Server as McpServer } from "@modelcontextprotocol/sdk/server/index.js";
|
|
5
5
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
6
6
|
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
7
|
+
import { decisionRecordHash } from "@cedulon/core";
|
|
7
8
|
import { isLoopbackHost } from "./config.js";
|
|
9
|
+
import { approvalChallenge, gateApprovalSignature } from "./approval-signature.js";
|
|
10
|
+
import { readCredentials } from "./operator-credentials.js";
|
|
11
|
+
import { readRpConfig } from "./rp-config.js";
|
|
8
12
|
import { approvalsLogFor, approvePending, createApprovalBudgetGuard, explain, LedgerDenyUnrecorded, loadApprovalsFromDir, } from "@verax-ai/proxy";
|
|
9
13
|
import { createVerifier, readBearer, resourceMetadataUrl, wwwAuthenticate } from "./auth.js";
|
|
10
14
|
import { bumpMetric, bumpUnauthenticated } from "./metrics.js";
|
|
@@ -268,6 +272,10 @@ function isProtectedResourcePath(pathname, audience) {
|
|
|
268
272
|
const want = new URL(resourceMetadataUrl(audience)).pathname;
|
|
269
273
|
return pathname === want || pathname === "/.well-known/oauth-protected-resource";
|
|
270
274
|
}
|
|
275
|
+
async function signedDefer(ledger, ref) {
|
|
276
|
+
const rows = await ledger.decisions();
|
|
277
|
+
return rows.find((row) => row.claims.ref === ref && row.claims.decision === "defer") ?? null;
|
|
278
|
+
}
|
|
271
279
|
function send(res, status, body, headers) {
|
|
272
280
|
const text = typeof body === "string" ? body : JSON.stringify(body);
|
|
273
281
|
res.writeHead(status, {
|
|
@@ -362,6 +370,15 @@ export async function listen(config) {
|
|
|
362
370
|
await closeAll(sessions);
|
|
363
371
|
throw err;
|
|
364
372
|
}
|
|
373
|
+
try {
|
|
374
|
+
await services.proxy.syncControlRecords();
|
|
375
|
+
}
|
|
376
|
+
catch (err) {
|
|
377
|
+
// syncControlRecords reports its own failure and does not throw. This
|
|
378
|
+
// catch is the door: a sync miss must not close the ledger or refuse to listen.
|
|
379
|
+
const message = (err instanceof Error ? err.message : String(err)).replace(/[\r\n]+/g, " ");
|
|
380
|
+
process.stderr.write(`verax-proxy: control sync failed: ${message}\n`);
|
|
381
|
+
}
|
|
365
382
|
const toolMeta = [...TOOL_META, ...extraTools.map(downstreamMeta)];
|
|
366
383
|
const verify = createVerifier(config.jwksUrl, config.issuer, config.audience, config.jwksFile, config.jwksPin);
|
|
367
384
|
const attachHandlers = (mcp) => {
|
|
@@ -503,6 +520,7 @@ export async function listen(config) {
|
|
|
503
520
|
const apiInventory = url.pathname === "/api/inventory";
|
|
504
521
|
const contest = req.method === "POST" && url.pathname.startsWith("/api/contest/");
|
|
505
522
|
const apiApprove = req.method === "POST" && url.pathname === "/api/approve";
|
|
523
|
+
const apiApproveChallenge = req.method === "GET" && url.pathname === "/api/approve/challenge";
|
|
506
524
|
const apiAgents = req.method === "GET" && url.pathname === "/api/agents";
|
|
507
525
|
const apiHalt = url.pathname === "/api/halt" && (req.method === "GET" || req.method === "POST");
|
|
508
526
|
const apiResume = req.method === "POST" && url.pathname === "/api/resume";
|
|
@@ -511,6 +529,7 @@ export async function listen(config) {
|
|
|
511
529
|
!apiInventory &&
|
|
512
530
|
!contest &&
|
|
513
531
|
!apiApprove &&
|
|
532
|
+
!apiApproveChallenge &&
|
|
514
533
|
!apiAgents &&
|
|
515
534
|
!apiHalt &&
|
|
516
535
|
!apiResume) {
|
|
@@ -572,7 +591,45 @@ export async function listen(config) {
|
|
|
572
591
|
return;
|
|
573
592
|
}
|
|
574
593
|
const by = verified.principal.brain;
|
|
575
|
-
|
|
594
|
+
const state = apiResume ? resumeBody(config.stateDir, by, "http") : haltBody(config.stateDir, by, "http");
|
|
595
|
+
try {
|
|
596
|
+
await services.proxy.syncControlRecords();
|
|
597
|
+
}
|
|
598
|
+
catch (err) {
|
|
599
|
+
// The switch is already on disk. A sync miss must not turn this into a 500.
|
|
600
|
+
const message = (err instanceof Error ? err.message : String(err)).replace(/[\r\n]+/g, " ");
|
|
601
|
+
process.stderr.write(`verax-proxy: control sync failed: ${message}\n`);
|
|
602
|
+
}
|
|
603
|
+
send(res, 200, state);
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
if (apiApproveChallenge) {
|
|
607
|
+
if (!verified.principal.scopes.has("verax:approve")) {
|
|
608
|
+
send(res, 403, { error: "scope-missing" });
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
if (await refuseStaleBearer())
|
|
612
|
+
return;
|
|
613
|
+
const ref = url.searchParams.get("ref") ?? "";
|
|
614
|
+
const deferRecord = ref === "" ? null : await signedDefer(services.ledger, ref);
|
|
615
|
+
if (!deferRecord) {
|
|
616
|
+
send(res, 404, { error: "unknown-ref" });
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
const rp = readRpConfig(process.env);
|
|
620
|
+
if (!rp.ok) {
|
|
621
|
+
send(res, 503, { error: "passkey-closed", reason: rp.reason });
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
send(res, 200, {
|
|
625
|
+
challenge: approvalChallenge({
|
|
626
|
+
ref,
|
|
627
|
+
requestHash: deferRecord.claims.requestHash,
|
|
628
|
+
deferRecordHash: decisionRecordHash(deferRecord),
|
|
629
|
+
}),
|
|
630
|
+
rpId: rp.config.rpID,
|
|
631
|
+
allowCredentials: readCredentials(config.stateDir).map((row) => ({ id: row.id, type: "public-key" })),
|
|
632
|
+
});
|
|
576
633
|
return;
|
|
577
634
|
}
|
|
578
635
|
if (apiApprove) {
|
|
@@ -610,11 +667,28 @@ export async function listen(config) {
|
|
|
610
667
|
send(res, 409, { error: "stale", requestHash: waiting.requestHash });
|
|
611
668
|
return;
|
|
612
669
|
}
|
|
613
|
-
const
|
|
614
|
-
if (!
|
|
670
|
+
const deferRecord = await signedDefer(services.ledger, ref);
|
|
671
|
+
if (!deferRecord) {
|
|
615
672
|
send(res, 404, { error: "unknown-ref" });
|
|
616
673
|
return;
|
|
617
674
|
}
|
|
675
|
+
const deferRecordHash = decisionRecordHash(deferRecord);
|
|
676
|
+
// The challenge is rebuilt from the defer on disk, not from anything the client sent.
|
|
677
|
+
const gated = await gateApprovalSignature({
|
|
678
|
+
stateDir: config.stateDir,
|
|
679
|
+
env: process.env,
|
|
680
|
+
assertion: asked.assertion,
|
|
681
|
+
ref,
|
|
682
|
+
requestHash: deferRecord.claims.requestHash,
|
|
683
|
+
deferRecordHash,
|
|
684
|
+
});
|
|
685
|
+
if (!gated.ok) {
|
|
686
|
+
send(res, gated.status, {
|
|
687
|
+
error: gated.error,
|
|
688
|
+
...(gated.reason ? { reason: gated.reason } : {}),
|
|
689
|
+
});
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
618
692
|
// Who approved, on the record. The CLI writes the machine's login name,
|
|
619
693
|
// which says nothing about the person holding the phone; the session's
|
|
620
694
|
// own subject does.
|
|
@@ -628,7 +702,8 @@ export async function listen(config) {
|
|
|
628
702
|
ref,
|
|
629
703
|
approverId: approver,
|
|
630
704
|
via: "http",
|
|
631
|
-
policyHash:
|
|
705
|
+
policyHash: deferRecord.claims.policyHash,
|
|
706
|
+
...(gated.signature ? { signature: gated.signature } : {}),
|
|
632
707
|
approvals,
|
|
633
708
|
budgetGuard: createApprovalBudgetGuard({
|
|
634
709
|
policy: services.policy,
|
package/dist/verify-cli.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type VerifyResult } from "@verax-ai/proxy";
|
|
2
|
+
import { type ApprovalSignatureReport } from "./approval-signature.ts";
|
|
2
3
|
export declare const EX_VERIFY_FAILED = 1;
|
|
3
|
-
|
|
4
|
-
export declare function renderVerify(r: VerifyResult): string;
|
|
4
|
+
export declare function renderVerify(r: VerifyResult, approval?: ApprovalSignatureReport): string;
|
|
5
5
|
export declare function runVerify(argv: readonly string[], out?: (s: string) => void): Promise<number>;
|
package/dist/verify-cli.js
CHANGED
|
@@ -15,11 +15,12 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import { readFileSync } from "node:fs";
|
|
17
17
|
import { verifyLedger } from "@verax-ai/proxy";
|
|
18
|
+
import { verifyApprovalSignatures } from "./approval-signature.js";
|
|
18
19
|
import { directoryAccess, unreadableSentence } from "./install.js";
|
|
19
20
|
export const EX_VERIFY_FAILED = 1;
|
|
20
21
|
function usage() {
|
|
21
22
|
return [
|
|
22
|
-
"usage: verax verify <stateDir> [--key <public.pem>] [--effect-key <public.pem>] [--witness-key <public.pem>] [--checkpoint-key <public.pem>] [--json]",
|
|
23
|
+
"usage: verax verify <stateDir> [--key <public.pem>] [--effect-key <public.pem>] [--witness-key <public.pem>] [--checkpoint-key <public.pem>] [--anchor-key <public.pem>] [--operator-credentials <file>] [--json]",
|
|
23
24
|
"",
|
|
24
25
|
" <stateDir> the directory holding decisions.jsonl and effects.jsonl",
|
|
25
26
|
" --key <file> verify decision records against a public key you hold,",
|
|
@@ -42,13 +43,33 @@ function usage() {
|
|
|
42
43
|
" verify every checkpoint row against a public key you",
|
|
43
44
|
" hold, instead of one key taken from the checkpoint",
|
|
44
45
|
" file. Same distinction as --key, for the witness.",
|
|
46
|
+
" --anchor-key <file>",
|
|
47
|
+
" check the transparency-service receipts in",
|
|
48
|
+
" checkpoint-anchors.jsonl under this service key. A",
|
|
49
|
+
" receipt names no key of its own, so without this flag",
|
|
50
|
+
" the receipts are counted and not checked.",
|
|
51
|
+
" --operator-credentials <file>",
|
|
52
|
+
" verify HTTP approval signatures against this operator",
|
|
53
|
+
" credential file, instead of operator-credentials.json",
|
|
54
|
+
" in the state directory. A CLI approval is unsigned and",
|
|
55
|
+
" is counted as such. Same distinction as --key.",
|
|
45
56
|
" --json machine-readable result on stdout",
|
|
46
57
|
"",
|
|
47
58
|
"Exit code is 0 when the ledger verifies and 1 when it does not.",
|
|
48
59
|
].join("\n");
|
|
49
60
|
}
|
|
50
61
|
/** Lines a person reads. The trust line is never omitted. */
|
|
51
|
-
|
|
62
|
+
const APPROVAL_NOT_CHECKED = {
|
|
63
|
+
ok: true,
|
|
64
|
+
signedVerified: 0,
|
|
65
|
+
signedFailed: 0,
|
|
66
|
+
unsignedHttp: 0,
|
|
67
|
+
cli: 0,
|
|
68
|
+
line: "approval signatures not checked",
|
|
69
|
+
trustSource: "none",
|
|
70
|
+
trustNote: "approval signatures were not checked",
|
|
71
|
+
};
|
|
72
|
+
export function renderVerify(r, approval = APPROVAL_NOT_CHECKED) {
|
|
52
73
|
const lines = [];
|
|
53
74
|
lines.push(`ledger ${r.directory}`);
|
|
54
75
|
lines.push(`decisions ${r.decisions}`);
|
|
@@ -86,6 +107,7 @@ export function renderVerify(r) {
|
|
|
86
107
|
lines.push(` ${r.checkpointTrust.note}`);
|
|
87
108
|
lines.push(r.index.line);
|
|
88
109
|
lines.push(r.effectCompleteness);
|
|
110
|
+
lines.push(r.inputs.line);
|
|
89
111
|
if (r.tail.checkpoint) {
|
|
90
112
|
const head = r.tail.checkpoint.chainHeadHash ?? "(no head hash)";
|
|
91
113
|
const holds = r.tail.checkpoint.ledgerHoldsRecord === null
|
|
@@ -99,6 +121,12 @@ export function renderVerify(r) {
|
|
|
99
121
|
lines.push(`checkpoint newest covers ${covered}, head ${head}, ${holds}`);
|
|
100
122
|
}
|
|
101
123
|
lines.push(r.tail.line);
|
|
124
|
+
lines.push(r.control.line);
|
|
125
|
+
for (const warning of r.control.warnings)
|
|
126
|
+
lines.push(`warning ${warning}`);
|
|
127
|
+
lines.push(r.anchors.line);
|
|
128
|
+
lines.push(approval.line);
|
|
129
|
+
lines.push(` ${approval.trustNote}`);
|
|
102
130
|
if (r.problems.length > 0) {
|
|
103
131
|
lines.push("");
|
|
104
132
|
lines.push("problems:");
|
|
@@ -161,6 +189,32 @@ export async function runVerify(argv, out = (s) => process.stdout.write(`${s}\n`
|
|
|
161
189
|
if (!checkpointKey.ok)
|
|
162
190
|
return EX_VERIFY_FAILED;
|
|
163
191
|
checkpointPublicKeyPem = checkpointKey.pem;
|
|
192
|
+
const anchorKey = readKeyFlag("--anchor-key");
|
|
193
|
+
if (!anchorKey.ok)
|
|
194
|
+
return EX_VERIFY_FAILED;
|
|
195
|
+
const anchorPublicKeyPem = anchorKey.pem;
|
|
196
|
+
let credentialsFile;
|
|
197
|
+
const credentialsAt = args.indexOf("--operator-credentials");
|
|
198
|
+
if (credentialsAt !== -1) {
|
|
199
|
+
const path = args[credentialsAt + 1];
|
|
200
|
+
if (!path || path.startsWith("-")) {
|
|
201
|
+
out("verify: --operator-credentials needs a file path");
|
|
202
|
+
return EX_VERIFY_FAILED;
|
|
203
|
+
}
|
|
204
|
+
try {
|
|
205
|
+
const text = readFileSync(path, "utf8");
|
|
206
|
+
if (text.trim() === "") {
|
|
207
|
+
out("verify: operator credentials file is empty");
|
|
208
|
+
return EX_VERIFY_FAILED;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
catch {
|
|
212
|
+
out(`verify: cannot read operator credentials file ${path}`);
|
|
213
|
+
return EX_VERIFY_FAILED;
|
|
214
|
+
}
|
|
215
|
+
args.splice(credentialsAt, 2);
|
|
216
|
+
credentialsFile = path;
|
|
217
|
+
}
|
|
164
218
|
const dir = args.find((a) => !a.startsWith("-"));
|
|
165
219
|
if (!dir) {
|
|
166
220
|
out(usage());
|
|
@@ -177,6 +231,7 @@ export async function runVerify(argv, out = (s) => process.stdout.write(`${s}\n`
|
|
|
177
231
|
...(effectPublicKeyPem ? { effectPublicKeyPem } : {}),
|
|
178
232
|
...(witnessPublicKeyPem ? { witnessPublicKeyPem } : {}),
|
|
179
233
|
...(checkpointPublicKeyPem ? { checkpointPublicKeyPem } : {}),
|
|
234
|
+
...(anchorPublicKeyPem ? { anchorPublicKeyPem } : {}),
|
|
180
235
|
});
|
|
181
236
|
}
|
|
182
237
|
catch (err) {
|
|
@@ -201,11 +256,32 @@ export async function runVerify(argv, out = (s) => process.stdout.write(`${s}\n`
|
|
|
201
256
|
line: "tail: no checkpoint; removing the newest records with their effects is not detectable from these files",
|
|
202
257
|
checkpoint: null,
|
|
203
258
|
},
|
|
259
|
+
control: { line: "control: not checked", windows: 0, warnings: [] },
|
|
260
|
+
inputs: { line: "inputs: not checked", matched: 0, missing: 0, mismatched: 0 },
|
|
261
|
+
anchors: { line: "anchors: not checked", receipts: 0, verified: 0, checked: false },
|
|
204
262
|
problems: [problem],
|
|
205
263
|
};
|
|
206
|
-
|
|
264
|
+
const failedReport = { ...failed, approvalSignatures: APPROVAL_NOT_CHECKED };
|
|
265
|
+
out(json ? JSON.stringify(failedReport, null, 2) : renderVerify(failed, APPROVAL_NOT_CHECKED));
|
|
207
266
|
return EX_VERIFY_FAILED;
|
|
208
267
|
}
|
|
209
|
-
|
|
210
|
-
|
|
268
|
+
let approval;
|
|
269
|
+
try {
|
|
270
|
+
approval = await verifyApprovalSignatures(dir, credentialsFile ? { credentialsFile } : {});
|
|
271
|
+
}
|
|
272
|
+
catch (err) {
|
|
273
|
+
const problem = err instanceof Error ? err.message : "approval signatures could not be read";
|
|
274
|
+
approval = { ...APPROVAL_NOT_CHECKED, ok: false, trustNote: problem };
|
|
275
|
+
result = { ...result, ok: false, problems: [...result.problems, problem] };
|
|
276
|
+
}
|
|
277
|
+
if (approval.signedFailed > 0) {
|
|
278
|
+
result = {
|
|
279
|
+
...result,
|
|
280
|
+
ok: false,
|
|
281
|
+
problems: [...result.problems, `approval signatures: ${approval.signedFailed} did not verify`],
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
const reported = { ...result, approvalSignatures: approval };
|
|
285
|
+
out(json ? JSON.stringify(reported, null, 2) : renderVerify(reported, approval));
|
|
286
|
+
return reported.ok ? 0 : EX_VERIFY_FAILED;
|
|
211
287
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebAuthn assertion checks for an HTTP approval, using only node:crypto
|
|
3
|
+
* and the CBOR decoder already in @cedulon/cose. The body does not take a
|
|
4
|
+
* certificate stack to verify one signature.
|
|
5
|
+
*/
|
|
6
|
+
import { type KeyObject } from "node:crypto";
|
|
7
|
+
export type AssertionVerdict = {
|
|
8
|
+
ok: true;
|
|
9
|
+
counter: number;
|
|
10
|
+
flags: number;
|
|
11
|
+
} | {
|
|
12
|
+
ok: false;
|
|
13
|
+
reason: string;
|
|
14
|
+
};
|
|
15
|
+
type PostedAssertion = {
|
|
16
|
+
response?: {
|
|
17
|
+
clientDataJSON?: unknown;
|
|
18
|
+
authenticatorData?: unknown;
|
|
19
|
+
signature?: unknown;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* COSE public key, base64url, to a node key. EC2 is P-256 / ES256, OKP is
|
|
24
|
+
* Ed25519 / EdDSA, RSA is RS256. Any other kty, alg, or curve is refused.
|
|
25
|
+
* A refusal returns null; this does not throw.
|
|
26
|
+
*/
|
|
27
|
+
export declare function coseKeyToPublicKey(base64urlCose: string): KeyObject | null;
|
|
28
|
+
/**
|
|
29
|
+
* Check one authentication assertion. `expectedOrigins` is the caller's
|
|
30
|
+
* allow-list; a caller that does not have one passes the origin it read
|
|
31
|
+
* back, and says so at the call site. `crossOrigin: true` is refused.
|
|
32
|
+
*/
|
|
33
|
+
export declare function verifyAssertion(opts: {
|
|
34
|
+
response: PostedAssertion;
|
|
35
|
+
expectedChallenge: string;
|
|
36
|
+
expectedOrigins: readonly string[];
|
|
37
|
+
rpId: string;
|
|
38
|
+
publicKeyCose: string;
|
|
39
|
+
}): AssertionVerdict;
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebAuthn assertion checks for an HTTP approval, using only node:crypto
|
|
3
|
+
* and the CBOR decoder already in @cedulon/cose. The body does not take a
|
|
4
|
+
* certificate stack to verify one signature.
|
|
5
|
+
*/
|
|
6
|
+
import { constants, createHash, createPublicKey, verify as cryptoVerify } from "node:crypto";
|
|
7
|
+
import { decodeCbor, isCborMap, mapGet } from "@cedulon/cose";
|
|
8
|
+
function b64url(bytes) {
|
|
9
|
+
return Buffer.from(bytes).toString("base64url");
|
|
10
|
+
}
|
|
11
|
+
function fromBase64Url(value) {
|
|
12
|
+
if (value === "")
|
|
13
|
+
return null;
|
|
14
|
+
const trimmed = value.replace(/=+$/u, "");
|
|
15
|
+
if (trimmed === "" || !/^[A-Za-z0-9_-]+$/u.test(trimmed))
|
|
16
|
+
return null;
|
|
17
|
+
const buf = Buffer.from(trimmed, "base64url");
|
|
18
|
+
return buf.length === 0 ? null : buf;
|
|
19
|
+
}
|
|
20
|
+
function mapNum(map, key) {
|
|
21
|
+
const value = mapGet(map, key);
|
|
22
|
+
return typeof value === "number" && Number.isInteger(value) ? value : null;
|
|
23
|
+
}
|
|
24
|
+
function mapBytes(map, key, length) {
|
|
25
|
+
const value = mapGet(map, key);
|
|
26
|
+
if (!(value instanceof Uint8Array))
|
|
27
|
+
return null;
|
|
28
|
+
if (value.length === 0)
|
|
29
|
+
return null;
|
|
30
|
+
if (length !== undefined && value.length !== length)
|
|
31
|
+
return null;
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* COSE public key, base64url, to a node key. EC2 is P-256 / ES256, OKP is
|
|
36
|
+
* Ed25519 / EdDSA, RSA is RS256. Any other kty, alg, or curve is refused.
|
|
37
|
+
* A refusal returns null; this does not throw.
|
|
38
|
+
*/
|
|
39
|
+
export function coseKeyToPublicKey(base64urlCose) {
|
|
40
|
+
try {
|
|
41
|
+
const bytes = fromBase64Url(base64urlCose);
|
|
42
|
+
if (!bytes)
|
|
43
|
+
return null;
|
|
44
|
+
const decoded = decodeCbor(new Uint8Array(bytes));
|
|
45
|
+
if (!isCborMap(decoded))
|
|
46
|
+
return null;
|
|
47
|
+
const kty = mapNum(decoded, 1);
|
|
48
|
+
const alg = mapNum(decoded, 3);
|
|
49
|
+
if (kty === 2 && alg === -7) {
|
|
50
|
+
const x = mapBytes(decoded, -2, 32);
|
|
51
|
+
const y = mapBytes(decoded, -3, 32);
|
|
52
|
+
if (mapNum(decoded, -1) !== 1 || !x || !y)
|
|
53
|
+
return null;
|
|
54
|
+
return createPublicKey({
|
|
55
|
+
format: "jwk",
|
|
56
|
+
key: { kty: "EC", crv: "P-256", alg: "ES256", x: b64url(x), y: b64url(y) },
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
if (kty === 1 && alg === -8) {
|
|
60
|
+
const x = mapBytes(decoded, -2, 32);
|
|
61
|
+
if (mapNum(decoded, -1) !== 6 || !x)
|
|
62
|
+
return null;
|
|
63
|
+
return createPublicKey({
|
|
64
|
+
format: "jwk",
|
|
65
|
+
key: { kty: "OKP", crv: "Ed25519", alg: "EdDSA", x: b64url(x) },
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (kty === 3 && alg === -257) {
|
|
69
|
+
const n = mapBytes(decoded, -1);
|
|
70
|
+
const e = mapBytes(decoded, -2);
|
|
71
|
+
if (!n || !e)
|
|
72
|
+
return null;
|
|
73
|
+
return createPublicKey({
|
|
74
|
+
format: "jwk",
|
|
75
|
+
key: { kty: "RSA", alg: "RS256", n: b64url(n), e: b64url(e) },
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function signatureHolds(key, data, signature) {
|
|
85
|
+
try {
|
|
86
|
+
const kind = key.asymmetricKeyType;
|
|
87
|
+
if (kind === "ec")
|
|
88
|
+
return cryptoVerify("sha256", data, { key, dsaEncoding: "der" }, signature);
|
|
89
|
+
if (kind === "ed25519")
|
|
90
|
+
return cryptoVerify(null, data, key, signature);
|
|
91
|
+
if (kind === "rsa") {
|
|
92
|
+
return cryptoVerify("sha256", data, { key, padding: constants.RSA_PKCS1_PADDING }, signature);
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Check one authentication assertion. `expectedOrigins` is the caller's
|
|
102
|
+
* allow-list; a caller that does not have one passes the origin it read
|
|
103
|
+
* back, and says so at the call site. `crossOrigin: true` is refused.
|
|
104
|
+
*/
|
|
105
|
+
export function verifyAssertion(opts) {
|
|
106
|
+
try {
|
|
107
|
+
const clientB64 = opts.response.response?.clientDataJSON;
|
|
108
|
+
const authB64 = opts.response.response?.authenticatorData;
|
|
109
|
+
const sigB64 = opts.response.response?.signature;
|
|
110
|
+
if (typeof clientB64 !== "string" || typeof authB64 !== "string" || typeof sigB64 !== "string") {
|
|
111
|
+
return { ok: false, reason: "bad-assertion" };
|
|
112
|
+
}
|
|
113
|
+
const clientBytes = fromBase64Url(clientB64);
|
|
114
|
+
const authenticatorData = fromBase64Url(authB64);
|
|
115
|
+
const signature = fromBase64Url(sigB64);
|
|
116
|
+
if (!clientBytes || !authenticatorData || !signature)
|
|
117
|
+
return { ok: false, reason: "bad-assertion" };
|
|
118
|
+
let client;
|
|
119
|
+
try {
|
|
120
|
+
const parsed = JSON.parse(clientBytes.toString("utf8"));
|
|
121
|
+
if (!parsed || typeof parsed !== "object")
|
|
122
|
+
return { ok: false, reason: "bad-client-data" };
|
|
123
|
+
client = parsed;
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return { ok: false, reason: "bad-client-data" };
|
|
127
|
+
}
|
|
128
|
+
if (client.type !== "webauthn.get")
|
|
129
|
+
return { ok: false, reason: "type" };
|
|
130
|
+
if (client.challenge !== opts.expectedChallenge)
|
|
131
|
+
return { ok: false, reason: "challenge" };
|
|
132
|
+
if (typeof client.origin !== "string" || !opts.expectedOrigins.includes(client.origin)) {
|
|
133
|
+
return { ok: false, reason: "origin" };
|
|
134
|
+
}
|
|
135
|
+
// Absent or false only: a string such as "true" is not a same-origin answer either.
|
|
136
|
+
if (client.crossOrigin !== undefined && client.crossOrigin !== false)
|
|
137
|
+
return { ok: false, reason: "cross-origin" };
|
|
138
|
+
if (authenticatorData.length < 37)
|
|
139
|
+
return { ok: false, reason: "authenticator-data" };
|
|
140
|
+
const rpIdHash = createHash("sha256").update(opts.rpId).digest();
|
|
141
|
+
if (Buffer.compare(authenticatorData.subarray(0, 32), rpIdHash) !== 0)
|
|
142
|
+
return { ok: false, reason: "rp-id" };
|
|
143
|
+
const flags = authenticatorData[32];
|
|
144
|
+
if (flags === undefined || (flags & 0x05) !== 0x05)
|
|
145
|
+
return { ok: false, reason: "user-verification" };
|
|
146
|
+
const counter = authenticatorData.readUInt32BE(33);
|
|
147
|
+
const key = coseKeyToPublicKey(opts.publicKeyCose);
|
|
148
|
+
if (!key)
|
|
149
|
+
return { ok: false, reason: "public-key" };
|
|
150
|
+
const clientHash = createHash("sha256").update(clientBytes).digest();
|
|
151
|
+
const data = Buffer.concat([authenticatorData, clientHash]);
|
|
152
|
+
if (!signatureHolds(key, data, signature))
|
|
153
|
+
return { ok: false, reason: "signature" };
|
|
154
|
+
return { ok: true, counter, flags };
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
return { ok: false, reason: "signature" };
|
|
158
|
+
}
|
|
159
|
+
}
|
package/dist/wiring.js
CHANGED
|
@@ -83,6 +83,9 @@ export function createBodyServices(opts) {
|
|
|
83
83
|
}
|
|
84
84
|
return fn(call, principal, ref);
|
|
85
85
|
};
|
|
86
|
+
// Halt and resume lines are copied into the ledger when the body opens
|
|
87
|
+
// (`listen` awaits `proxy.syncControlRecords` before the socket accepts)
|
|
88
|
+
// and again before each call. A CLI halt waits for one of those.
|
|
86
89
|
const proxy = createProxy({
|
|
87
90
|
policy,
|
|
88
91
|
recordSigner: opts.recordSigner,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verax-ai/body",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"@cedulon/cose": "0.13.1",
|
|
27
27
|
"@cedulon/effect-extract": "0.13.1",
|
|
28
28
|
"@modelcontextprotocol/sdk": "^1.30.0",
|
|
29
|
-
"@verax-ai/inventory": "^0.4.
|
|
30
|
-
"@verax-ai/proxy": "^0.4.
|
|
29
|
+
"@verax-ai/inventory": "^0.4.3",
|
|
30
|
+
"@verax-ai/proxy": "^0.4.3",
|
|
31
31
|
"jose": "^6.2.11"
|
|
32
32
|
},
|
|
33
33
|
"description": "VERAX body: an MCP server that decides, records and proves what an agent did.",
|