@verax-ai/body 0.8.2 → 0.10.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.
- package/README.md +9 -6
- package/dist/anchor-cli.d.ts +48 -3
- package/dist/anchor-cli.js +256 -17
- package/dist/auth.d.ts +14 -1
- package/dist/auth.js +25 -4
- package/dist/canary-cli.d.ts +5 -0
- package/dist/canary-cli.js +213 -0
- package/dist/cli.js +93 -6
- package/dist/config.d.ts +6 -0
- package/dist/config.js +1 -0
- package/dist/demo.js +3 -0
- package/dist/doctor.d.ts +11 -0
- package/dist/doctor.js +88 -2
- package/dist/downstream-pin.js +11 -1
- package/dist/downstream.d.ts +51 -0
- package/dist/downstream.js +329 -4
- package/dist/explain-cli.d.ts +5 -0
- package/dist/explain-cli.js +132 -0
- package/dist/init-local.d.ts +5 -0
- package/dist/init-local.js +1 -0
- package/dist/install.js +1 -1
- package/dist/policy-cli.d.ts +8 -0
- package/dist/policy-cli.js +169 -0
- package/dist/policy-rehearsal.d.ts +39 -0
- package/dist/policy-rehearsal.js +512 -0
- package/dist/report-cli.d.ts +163 -0
- package/dist/report-cli.js +1101 -0
- package/dist/server.js +43 -6
- package/dist/taint-cli.d.ts +5 -0
- package/dist/taint-cli.js +80 -0
- package/dist/verify-cli.d.ts +40 -0
- package/dist/verify-cli.js +131 -58
- package/dist/webauthn-verify.d.ts +3 -1
- package/dist/webauthn-verify.js +97 -4
- package/dist/wiring.d.ts +5 -0
- package/dist/wiring.js +2 -0
- package/dist/witness.d.ts +16 -5
- package/dist/witness.js +67 -8
- package/package.json +9 -9
package/dist/downstream.d.ts
CHANGED
|
@@ -19,6 +19,13 @@ export type DownstreamSpec = {
|
|
|
19
19
|
url?: string;
|
|
20
20
|
/** HTTP: headers the operator sends to the child, such as its own bearer. */
|
|
21
21
|
headers?: Record<string, string>;
|
|
22
|
+
/**
|
|
23
|
+
* HTTP only. The operator wrote that this child may be called with no
|
|
24
|
+
* credential. Absent, a body that enforces the check refuses a child that
|
|
25
|
+
* has no `headers`, that answers an initialize without them, or whose
|
|
26
|
+
* probe cannot decide.
|
|
27
|
+
*/
|
|
28
|
+
unauthenticated?: "acknowledged";
|
|
22
29
|
timeoutMs?: number;
|
|
23
30
|
/**
|
|
24
31
|
* stdio only. The operator wrote that this child runs as the same user
|
|
@@ -38,6 +45,11 @@ export type DownstreamSpec = {
|
|
|
38
45
|
* comparing definitions.
|
|
39
46
|
*/
|
|
40
47
|
pins?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Child tool name to labels. A key that is present is a label, including
|
|
50
|
+
* an empty list (neither). A missing key is unlabelled.
|
|
51
|
+
*/
|
|
52
|
+
labels?: Readonly<Record<string, readonly ("reads-untrusted" | "egress")[]>>;
|
|
41
53
|
};
|
|
42
54
|
export type DownstreamTool = {
|
|
43
55
|
name: string;
|
|
@@ -67,6 +79,45 @@ export declare function parseDownstreamJson(raw: string, bodyPublicKeyPems?: rea
|
|
|
67
79
|
* document at all — the operator wrote it, so it is honoured.
|
|
68
80
|
*/
|
|
69
81
|
export declare function parseDownstreamDocument(raw: string, bodyPublicKeyPems?: readonly string[]): DownstreamSpec[];
|
|
82
|
+
/**
|
|
83
|
+
* Talks to a server already running. The headers are the operator's, from the
|
|
84
|
+
* document — the body's own bearer is never forwarded, for the same
|
|
85
|
+
* confused-deputy reason the stdio child does not inherit `VERAX_*`.
|
|
86
|
+
*/
|
|
87
|
+
/** A non-empty header value is the credential the probe must not send. */
|
|
88
|
+
export declare function downstreamHasCredential(spec: DownstreamSpec): boolean;
|
|
89
|
+
/** What the headerless initialize came back as. Only 401 and 403 are `refused`. */
|
|
90
|
+
export type HttpProbeOutcome = "answered" | "refused" | "undecided";
|
|
91
|
+
/**
|
|
92
|
+
* Installed attach refuses this HTTP child. An acknowledgement is the only
|
|
93
|
+
* way past it. No header, or a JSON-RPC result (a 2xx the probe could not
|
|
94
|
+
* read counts the same way), is `downstream-http-credential-required`. Any
|
|
95
|
+
* other outcome is `downstream-http-credential-undecided`. A 401 or 403 with
|
|
96
|
+
* a header is not a finding.
|
|
97
|
+
*/
|
|
98
|
+
export declare function httpCredentialFinding(spec: DownstreamSpec, outcome: HttpProbeOutcome): {
|
|
99
|
+
id: string;
|
|
100
|
+
detail: string;
|
|
101
|
+
} | null;
|
|
102
|
+
/**
|
|
103
|
+
* Local mode only warns. `verax install` sets `installed` (`VERAX_INSTALLED=1`
|
|
104
|
+
* in verax.env). A JWKS file without that flag is `verax init --local`, not an install.
|
|
105
|
+
*/
|
|
106
|
+
export declare function httpCredentialIsLocal(input: {
|
|
107
|
+
jwksFile?: string | null;
|
|
108
|
+
installed?: boolean;
|
|
109
|
+
}): boolean;
|
|
110
|
+
/**
|
|
111
|
+
* One `initialize`, the same one `httpDownstreamClient` sends, and none of
|
|
112
|
+
* the operator's headers. When the answer carries `Mcp-Session-Id`, one
|
|
113
|
+
* `DELETE` for that session, again with none of those headers. A JSON-RPC
|
|
114
|
+
* result means the child is reachable with no credential. A 2xx the probe
|
|
115
|
+
* cannot read counts the same way. Only 401 and 403 are a refusal. Every
|
|
116
|
+
* other outcome (another status, a JSON-RPC error, a connection error, a
|
|
117
|
+
* timeout before the headers) is undecided. Header values are not sent and
|
|
118
|
+
* are not written into an error.
|
|
119
|
+
*/
|
|
120
|
+
export declare function probeHttpDownstream(spec: DownstreamSpec): Promise<HttpProbeOutcome>;
|
|
70
121
|
/**
|
|
71
122
|
* Connects the child once, takes `tools/list` under the same bounds as
|
|
72
123
|
* `openDownstream`, collects stderr, and closes. The returned tools are the
|
package/dist/downstream.js
CHANGED
|
@@ -7,7 +7,7 @@ import { DISPLAY_ESCAPE_CLASS, effectDescriptor, samePublicKey, sha256Canonical,
|
|
|
7
7
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
8
8
|
import { getDefaultEnvironment, StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
9
9
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
10
|
-
import { CallToolResultSchema, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
10
|
+
import { CallToolResultSchema, LATEST_PROTOCOL_VERSION, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
11
11
|
const PREFIX_RE = /^[A-Za-z][A-Za-z0-9_-]{0,31}$/;
|
|
12
12
|
/** `sha256:` plus 64 lowercase hex digits. The capture is the file name. */
|
|
13
13
|
const PINS_RE = /^sha256:([0-9a-f]{64})$/;
|
|
@@ -64,6 +64,29 @@ export function extraToolNameOk(name) {
|
|
|
64
64
|
* The operator wrote `"trust": "same-user"` on a stdio child. Missing ack
|
|
65
65
|
* refuses start. One rule, two call sites — do not duplicate the predicate.
|
|
66
66
|
*/
|
|
67
|
+
function parseDownstreamLabels(raw, tools) {
|
|
68
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
|
69
|
+
throw new Error("downstream-labels-invalid");
|
|
70
|
+
}
|
|
71
|
+
const out = {};
|
|
72
|
+
for (const [name, value] of Object.entries(raw)) {
|
|
73
|
+
if (!CHILD_TOOL_RE.test(name))
|
|
74
|
+
throw new Error("downstream-labels-invalid");
|
|
75
|
+
if (tools !== undefined && !tools.includes(name))
|
|
76
|
+
throw new Error(`downstream-labels-unknown:${name}`);
|
|
77
|
+
if (!Array.isArray(value))
|
|
78
|
+
throw new Error("downstream-labels-invalid");
|
|
79
|
+
const labels = [];
|
|
80
|
+
for (const item of value) {
|
|
81
|
+
if (item !== "reads-untrusted" && item !== "egress")
|
|
82
|
+
throw new Error("downstream-labels-invalid");
|
|
83
|
+
if (!labels.includes(item))
|
|
84
|
+
labels.push(item);
|
|
85
|
+
}
|
|
86
|
+
out[name] = labels;
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
67
90
|
function assertStdioTrusted(spec) {
|
|
68
91
|
if (spec.command !== undefined && spec.trust !== "same-user") {
|
|
69
92
|
// The prefix is the operator's own name for the child and names WHICH entry
|
|
@@ -193,6 +216,16 @@ export function parseDownstreamJson(raw, bodyPublicKeyPems = []) {
|
|
|
193
216
|
}
|
|
194
217
|
spec.pins = rec.pins;
|
|
195
218
|
}
|
|
219
|
+
if (rec.labels !== undefined) {
|
|
220
|
+
spec.labels = parseDownstreamLabels(rec.labels, spec.tools);
|
|
221
|
+
}
|
|
222
|
+
if (rec.unauthenticated !== undefined) {
|
|
223
|
+
// The value is not echoed: a document can put anything in this field.
|
|
224
|
+
if (spec.url === undefined || rec.unauthenticated !== "acknowledged") {
|
|
225
|
+
throw new Error(`downstream-unauthenticated-invalid:${spec.prefix}`);
|
|
226
|
+
}
|
|
227
|
+
spec.unauthenticated = "acknowledged";
|
|
228
|
+
}
|
|
196
229
|
assertStdioTrusted(spec);
|
|
197
230
|
return spec;
|
|
198
231
|
}
|
|
@@ -299,6 +332,290 @@ function collectStderr(stderr, chunks) {
|
|
|
299
332
|
* document — the body's own bearer is never forwarded, for the same
|
|
300
333
|
* confused-deputy reason the stdio child does not inherit `VERAX_*`.
|
|
301
334
|
*/
|
|
335
|
+
/** A non-empty header value is the credential the probe must not send. */
|
|
336
|
+
export function downstreamHasCredential(spec) {
|
|
337
|
+
if (spec.headers === undefined)
|
|
338
|
+
return false;
|
|
339
|
+
return Object.values(spec.headers).some((value) => value.trim() !== "");
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Installed attach refuses this HTTP child. An acknowledgement is the only
|
|
343
|
+
* way past it. No header, or a JSON-RPC result (a 2xx the probe could not
|
|
344
|
+
* read counts the same way), is `downstream-http-credential-required`. Any
|
|
345
|
+
* other outcome is `downstream-http-credential-undecided`. A 401 or 403 with
|
|
346
|
+
* a header is not a finding.
|
|
347
|
+
*/
|
|
348
|
+
export function httpCredentialFinding(spec, outcome) {
|
|
349
|
+
if (spec.url === undefined || spec.unauthenticated === "acknowledged")
|
|
350
|
+
return null;
|
|
351
|
+
const prefix = spec.prefix;
|
|
352
|
+
const required = `downstream-http-credential-required:${prefix}`;
|
|
353
|
+
const notSeen = `child ${prefix} was not seen to refuse an initialize sent with no credential. ` +
|
|
354
|
+
"An agent that can reach it can call it with no decision record.";
|
|
355
|
+
if (!downstreamHasCredential(spec)) {
|
|
356
|
+
return {
|
|
357
|
+
id: required,
|
|
358
|
+
detail: outcome === "answered" ? notSeen : `child ${prefix} has no credential in headers.`,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
if (outcome === "answered")
|
|
362
|
+
return { id: required, detail: notSeen };
|
|
363
|
+
if (outcome === "undecided") {
|
|
364
|
+
return {
|
|
365
|
+
id: `downstream-http-credential-undecided:${prefix}`,
|
|
366
|
+
detail: `child ${prefix}: the probe could not decide whether an initialize sent with no credential was refused. ` +
|
|
367
|
+
"An agent that can reach it can call it with no decision record.",
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Local mode only warns. `verax install` sets `installed` (`VERAX_INSTALLED=1`
|
|
374
|
+
* in verax.env). A JWKS file without that flag is `verax init --local`, not an install.
|
|
375
|
+
*/
|
|
376
|
+
export function httpCredentialIsLocal(input) {
|
|
377
|
+
const file = typeof input.jwksFile === "string" ? input.jwksFile.trim() !== "" : Boolean(input.jwksFile);
|
|
378
|
+
return file && input.installed !== true;
|
|
379
|
+
}
|
|
380
|
+
const PROBE_BODY_CAP = 1024 * 1024;
|
|
381
|
+
/**
|
|
382
|
+
* The first request id a new SDK client uses. `Protocol` starts at 0.
|
|
383
|
+
* Only a message carrying this id is the probe's.
|
|
384
|
+
*/
|
|
385
|
+
const PROBE_ID = 0;
|
|
386
|
+
/**
|
|
387
|
+
* The live HTTP connection (`openDownstream`). The probe's `initialize` is
|
|
388
|
+
* built from this same client, so a child cannot answer one and refuse the
|
|
389
|
+
* other. `StreamableHTTPClientTransport.send` sets the two header values.
|
|
390
|
+
*/
|
|
391
|
+
const HTTP_DOWNSTREAM_CLIENT_INFO = { name: "verax-downstream", version: "0.0.0" };
|
|
392
|
+
const HTTP_DOWNSTREAM_ACCEPT = "application/json, text/event-stream";
|
|
393
|
+
const HTTP_DOWNSTREAM_CONTENT_TYPE = "application/json";
|
|
394
|
+
function httpDownstreamClient() {
|
|
395
|
+
return new Client({ name: HTTP_DOWNSTREAM_CLIENT_INFO.name, version: HTTP_DOWNSTREAM_CLIENT_INFO.version });
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* The object `Client.connect` hands to `Protocol.request` for that client.
|
|
399
|
+
* Field order is the SDK's: the request fields, then `jsonrpc`, then `id`.
|
|
400
|
+
*/
|
|
401
|
+
function httpDownstreamInitializeMessage() {
|
|
402
|
+
return {
|
|
403
|
+
method: "initialize",
|
|
404
|
+
params: {
|
|
405
|
+
protocolVersion: LATEST_PROTOCOL_VERSION,
|
|
406
|
+
capabilities: {},
|
|
407
|
+
clientInfo: { name: HTTP_DOWNSTREAM_CLIENT_INFO.name, version: HTTP_DOWNSTREAM_CLIENT_INFO.version },
|
|
408
|
+
},
|
|
409
|
+
jsonrpc: "2.0",
|
|
410
|
+
id: PROBE_ID,
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* A JSON-RPC result for the probe's id, including `"error": null`. An error
|
|
415
|
+
* on that id is not a refusal. Any other id, or a value that is not an
|
|
416
|
+
* object, is ignored so the decision does not depend on neighbouring messages.
|
|
417
|
+
*/
|
|
418
|
+
function classifyProbeValue(value) {
|
|
419
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
420
|
+
return "ignore";
|
|
421
|
+
const msg = value;
|
|
422
|
+
if (msg.id !== PROBE_ID)
|
|
423
|
+
return "ignore";
|
|
424
|
+
if ("result" in msg && msg.error == null)
|
|
425
|
+
return "answered";
|
|
426
|
+
if ("error" in msg && msg.error != null)
|
|
427
|
+
return "rpc-error";
|
|
428
|
+
return "ignore";
|
|
429
|
+
}
|
|
430
|
+
/** Index of the next LF or bare CR. A CRLF is one break. */
|
|
431
|
+
function lineBreak(text) {
|
|
432
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
433
|
+
const c = text.charCodeAt(i);
|
|
434
|
+
if (c === 0x0a)
|
|
435
|
+
return { at: i, next: i + 1 };
|
|
436
|
+
if (c === 0x0d)
|
|
437
|
+
return { at: i, next: text.charCodeAt(i + 1) === 0x0a ? i + 2 : i + 1 };
|
|
438
|
+
}
|
|
439
|
+
return null;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Reads a 2xx until the message whose id is the probe's request. A result
|
|
443
|
+
* returns immediately, including one whose stream stays open. An error on
|
|
444
|
+
* that id is not a refusal. A message for another id is skipped, including
|
|
445
|
+
* when the next chunk has not arrived yet. Past the cap, on a timeout, or
|
|
446
|
+
* when that message never arrives, a 2xx counts as answered: the child was
|
|
447
|
+
* not seen to refuse.
|
|
448
|
+
*/
|
|
449
|
+
class ProbeBody {
|
|
450
|
+
buf = "";
|
|
451
|
+
total = 0;
|
|
452
|
+
decoder = new TextDecoder();
|
|
453
|
+
sse;
|
|
454
|
+
max;
|
|
455
|
+
constructor(sse, max) {
|
|
456
|
+
this.sse = sse;
|
|
457
|
+
this.max = max;
|
|
458
|
+
}
|
|
459
|
+
push(chunk, eof) {
|
|
460
|
+
const room = this.max - this.total;
|
|
461
|
+
if (room > 0 && chunk.byteLength > 0) {
|
|
462
|
+
const slice = chunk.byteLength > room ? chunk.subarray(0, room) : chunk;
|
|
463
|
+
this.total += slice.byteLength;
|
|
464
|
+
this.buf += this.decoder.decode(slice, { stream: true });
|
|
465
|
+
}
|
|
466
|
+
const end = eof || this.total >= this.max;
|
|
467
|
+
if (end)
|
|
468
|
+
this.buf += this.decoder.decode();
|
|
469
|
+
return this.sse ? this.scanSse(end) : this.scanJson(end);
|
|
470
|
+
}
|
|
471
|
+
scanJson(end) {
|
|
472
|
+
const trimmed = this.buf.trim();
|
|
473
|
+
if (trimmed === "")
|
|
474
|
+
return end ? "no-decision" : "pending";
|
|
475
|
+
try {
|
|
476
|
+
const call = classifyProbeValue(JSON.parse(trimmed));
|
|
477
|
+
if (call === "answered" || call === "rpc-error")
|
|
478
|
+
return call;
|
|
479
|
+
return "no-decision";
|
|
480
|
+
}
|
|
481
|
+
catch {
|
|
482
|
+
return end ? "no-decision" : "pending";
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
scanSse(end) {
|
|
486
|
+
const lines = [];
|
|
487
|
+
let rest = this.buf;
|
|
488
|
+
while (rest.length > 0) {
|
|
489
|
+
const br = lineBreak(rest);
|
|
490
|
+
if (br === null)
|
|
491
|
+
break;
|
|
492
|
+
lines.push(rest.slice(0, br.at));
|
|
493
|
+
rest = rest.slice(br.next);
|
|
494
|
+
}
|
|
495
|
+
if (end && rest.length > 0) {
|
|
496
|
+
lines.push(rest);
|
|
497
|
+
rest = "";
|
|
498
|
+
}
|
|
499
|
+
this.buf = rest;
|
|
500
|
+
for (const line of lines) {
|
|
501
|
+
if (!line.startsWith("data:"))
|
|
502
|
+
continue;
|
|
503
|
+
const payload = line.slice(5).trim();
|
|
504
|
+
if (payload === "" || payload === "[DONE]")
|
|
505
|
+
continue;
|
|
506
|
+
let call;
|
|
507
|
+
try {
|
|
508
|
+
call = classifyProbeValue(JSON.parse(payload));
|
|
509
|
+
}
|
|
510
|
+
catch {
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
if (call === "answered" || call === "rpc-error")
|
|
514
|
+
return call;
|
|
515
|
+
}
|
|
516
|
+
return end ? "no-decision" : "pending";
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
async function readProbe(res, max) {
|
|
520
|
+
if (res.body === null)
|
|
521
|
+
return "no-decision";
|
|
522
|
+
const sse = ((res.headers.get("content-type") ?? "").split(";")[0] ?? "").trim().toLowerCase() === "text/event-stream";
|
|
523
|
+
const reader = res.body.getReader();
|
|
524
|
+
const probe = new ProbeBody(sse, max);
|
|
525
|
+
try {
|
|
526
|
+
while (true) {
|
|
527
|
+
const { done, value } = await reader.read();
|
|
528
|
+
const call = probe.push(value ?? new Uint8Array(), done);
|
|
529
|
+
if (call !== "pending")
|
|
530
|
+
return call;
|
|
531
|
+
if (done)
|
|
532
|
+
return "no-decision";
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
finally {
|
|
536
|
+
await reader.cancel().catch(() => undefined);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* One `initialize`, the same one `httpDownstreamClient` sends, and none of
|
|
541
|
+
* the operator's headers. When the answer carries `Mcp-Session-Id`, one
|
|
542
|
+
* `DELETE` for that session, again with none of those headers. A JSON-RPC
|
|
543
|
+
* result means the child is reachable with no credential. A 2xx the probe
|
|
544
|
+
* cannot read counts the same way. Only 401 and 403 are a refusal. Every
|
|
545
|
+
* other outcome (another status, a JSON-RPC error, a connection error, a
|
|
546
|
+
* timeout before the headers) is undecided. Header values are not sent and
|
|
547
|
+
* are not written into an error.
|
|
548
|
+
*/
|
|
549
|
+
export async function probeHttpDownstream(spec) {
|
|
550
|
+
if (spec.url === undefined)
|
|
551
|
+
return "refused";
|
|
552
|
+
const url = spec.url;
|
|
553
|
+
const timeoutMs = spec.timeoutMs ?? 10_000;
|
|
554
|
+
let res;
|
|
555
|
+
try {
|
|
556
|
+
res = await fetch(url, {
|
|
557
|
+
method: "POST",
|
|
558
|
+
redirect: "error",
|
|
559
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
560
|
+
headers: {
|
|
561
|
+
accept: HTTP_DOWNSTREAM_ACCEPT,
|
|
562
|
+
"content-type": HTTP_DOWNSTREAM_CONTENT_TYPE,
|
|
563
|
+
},
|
|
564
|
+
body: JSON.stringify(httpDownstreamInitializeMessage()),
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
catch {
|
|
568
|
+
return "undecided";
|
|
569
|
+
}
|
|
570
|
+
const sessionId = res.headers.get("mcp-session-id");
|
|
571
|
+
let outcome;
|
|
572
|
+
try {
|
|
573
|
+
outcome = await probeCall(res);
|
|
574
|
+
}
|
|
575
|
+
finally {
|
|
576
|
+
if (sessionId)
|
|
577
|
+
await deleteProbeSession(url, sessionId, timeoutMs);
|
|
578
|
+
}
|
|
579
|
+
return outcome;
|
|
580
|
+
}
|
|
581
|
+
/** The credential decision. The session, if the server opened one, is closed by the caller. */
|
|
582
|
+
async function probeCall(res) {
|
|
583
|
+
if (res.status === 401 || res.status === 403) {
|
|
584
|
+
await res.body?.cancel().catch(() => undefined);
|
|
585
|
+
return "refused";
|
|
586
|
+
}
|
|
587
|
+
if (res.status < 200 || res.status >= 300) {
|
|
588
|
+
await res.body?.cancel().catch(() => undefined);
|
|
589
|
+
return "undecided";
|
|
590
|
+
}
|
|
591
|
+
try {
|
|
592
|
+
const body = await readProbe(res, PROBE_BODY_CAP);
|
|
593
|
+
return body === "rpc-error" ? "undecided" : "answered";
|
|
594
|
+
}
|
|
595
|
+
catch {
|
|
596
|
+
// A 2xx that timed out or broke after the headers was not seen to refuse.
|
|
597
|
+
return "answered";
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Streamable HTTP ends a session with `DELETE` and `Mcp-Session-Id`.
|
|
602
|
+
* The configured headers stay off this request. A failure here does not
|
|
603
|
+
* change the credential decision: the initialize was already classified.
|
|
604
|
+
*/
|
|
605
|
+
async function deleteProbeSession(url, sessionId, timeoutMs) {
|
|
606
|
+
try {
|
|
607
|
+
const res = await fetch(url, {
|
|
608
|
+
method: "DELETE",
|
|
609
|
+
redirect: "error",
|
|
610
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
611
|
+
headers: { "mcp-session-id": sessionId },
|
|
612
|
+
});
|
|
613
|
+
await res.body?.cancel().catch(() => undefined);
|
|
614
|
+
}
|
|
615
|
+
catch {
|
|
616
|
+
/* the initialize outcome stands */
|
|
617
|
+
}
|
|
618
|
+
}
|
|
302
619
|
function httpTransport(spec, overflow) {
|
|
303
620
|
if (spec.url === undefined)
|
|
304
621
|
throw new Error("downstream-url-invalid");
|
|
@@ -434,7 +751,13 @@ function readPinnedManifest(prefix, pins, definitionsDir) {
|
|
|
434
751
|
manifestInvalid(prefix);
|
|
435
752
|
if (rec.tools === null || typeof rec.tools !== "object" || Array.isArray(rec.tools))
|
|
436
753
|
manifestInvalid(prefix);
|
|
437
|
-
|
|
754
|
+
let unauthenticated = null;
|
|
755
|
+
if (rec.unauthenticated !== undefined && rec.unauthenticated !== null) {
|
|
756
|
+
if (rec.unauthenticated !== "acknowledged")
|
|
757
|
+
manifestInvalid(prefix);
|
|
758
|
+
unauthenticated = "acknowledged";
|
|
759
|
+
}
|
|
760
|
+
return { prefix: rec.prefix, selected: rec.selected, tools: rec.tools, unauthenticated };
|
|
438
761
|
}
|
|
439
762
|
/** Both absent (no `tools`, and `selected` null or missing) is the same selection. */
|
|
440
763
|
function sameSelection(specTools, selected) {
|
|
@@ -653,7 +976,7 @@ export async function openDownstream(spec, opts) {
|
|
|
653
976
|
const timeoutMs = spec.timeoutMs ?? 10_000;
|
|
654
977
|
const overflow = { hit: false, detach: () => undefined };
|
|
655
978
|
const transport = spec.url !== undefined ? httpTransport(spec, overflow) : stdioTransport(spec);
|
|
656
|
-
const client =
|
|
979
|
+
const client = httpDownstreamClient();
|
|
657
980
|
// Closing the transport ends every call still waiting on this child at once,
|
|
658
981
|
// an event stream included, instead of at its timeout.
|
|
659
982
|
overflow.detach = () => void transport.close().catch(() => undefined);
|
|
@@ -745,7 +1068,9 @@ export async function openDownstream(spec, opts) {
|
|
|
745
1068
|
if (spec.pins !== undefined) {
|
|
746
1069
|
try {
|
|
747
1070
|
const manifest = readPinnedManifest(spec.prefix, spec.pins, opts?.definitionsDir);
|
|
748
|
-
if (manifest.prefix !== spec.prefix ||
|
|
1071
|
+
if (manifest.prefix !== spec.prefix ||
|
|
1072
|
+
!sameSelection(spec.tools, manifest.selected) ||
|
|
1073
|
+
manifest.unauthenticated !== (spec.unauthenticated ?? null)) {
|
|
749
1074
|
throw new Error(`downstream-manifest-mismatch:${spec.prefix}`);
|
|
750
1075
|
}
|
|
751
1076
|
assertPinnedDefinitions(spec, offered, manifest.tools);
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `verax explain <stateDir> <ref>` reads a ledger without taking its lock.
|
|
3
|
+
* A running body holds `ledger.lock`; this command has to work beside it.
|
|
4
|
+
*/
|
|
5
|
+
import { readFileSync, statSync } from "node:fs";
|
|
6
|
+
import { explain, listPieceFiles, terminalText, } from "@verax-ai/proxy";
|
|
7
|
+
const USAGE = `usage: verax explain <stateDir> <ref>
|
|
8
|
+
|
|
9
|
+
Prints the decision and the reason. When the inputs name a taint source,
|
|
10
|
+
also prints the rule and the record id of the call that tainted the session.
|
|
11
|
+
Does not take the ledger lock.
|
|
12
|
+
`;
|
|
13
|
+
function readJsonl(path) {
|
|
14
|
+
let text;
|
|
15
|
+
try {
|
|
16
|
+
text = readFileSync(path, "utf8");
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
if (err.code === "ENOENT")
|
|
20
|
+
return [];
|
|
21
|
+
throw err;
|
|
22
|
+
}
|
|
23
|
+
const rows = [];
|
|
24
|
+
for (const line of text.split("\n")) {
|
|
25
|
+
if (line === "")
|
|
26
|
+
continue;
|
|
27
|
+
rows.push(JSON.parse(line));
|
|
28
|
+
}
|
|
29
|
+
return rows;
|
|
30
|
+
}
|
|
31
|
+
function asEffect(value) {
|
|
32
|
+
if (value === null || typeof value !== "object")
|
|
33
|
+
return null;
|
|
34
|
+
const rec = value;
|
|
35
|
+
if (rec.row !== null && typeof rec.row === "object")
|
|
36
|
+
return value;
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
function readOnlyLedger(dir) {
|
|
40
|
+
const pieces = () => listPieceFiles(dir);
|
|
41
|
+
const ledger = {
|
|
42
|
+
dir,
|
|
43
|
+
inputsPaths: () => {
|
|
44
|
+
const files = pieces();
|
|
45
|
+
const all = files.map((p) => p.inputs);
|
|
46
|
+
return { active: all[all.length - 1] ?? "", all };
|
|
47
|
+
},
|
|
48
|
+
async decisions() {
|
|
49
|
+
const rows = [];
|
|
50
|
+
for (const piece of pieces()) {
|
|
51
|
+
for (const row of readJsonl(piece.decisions)) {
|
|
52
|
+
rows.push(row);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return rows;
|
|
56
|
+
},
|
|
57
|
+
async effects() {
|
|
58
|
+
const rows = [];
|
|
59
|
+
for (const piece of pieces()) {
|
|
60
|
+
for (const row of readJsonl(piece.effects)) {
|
|
61
|
+
const effect = asEffect(row);
|
|
62
|
+
if (effect)
|
|
63
|
+
rows.push(effect);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return rows;
|
|
67
|
+
},
|
|
68
|
+
async appendDecision() {
|
|
69
|
+
throw new Error("explain-readonly");
|
|
70
|
+
},
|
|
71
|
+
async appendDecisionChained() {
|
|
72
|
+
throw new Error("explain-readonly");
|
|
73
|
+
},
|
|
74
|
+
async appendEffect() {
|
|
75
|
+
throw new Error("explain-readonly");
|
|
76
|
+
},
|
|
77
|
+
async lastDecisionHash() {
|
|
78
|
+
return null;
|
|
79
|
+
},
|
|
80
|
+
async exportExtract() {
|
|
81
|
+
throw new Error("explain-readonly");
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
return ledger;
|
|
85
|
+
}
|
|
86
|
+
export async function runExplain(argv, stdout, stderr) {
|
|
87
|
+
if (argv.includes("--help") || argv.includes("-h")) {
|
|
88
|
+
stdout.write(USAGE);
|
|
89
|
+
return 0;
|
|
90
|
+
}
|
|
91
|
+
const stateDir = argv[0];
|
|
92
|
+
const ref = argv[1];
|
|
93
|
+
if (!stateDir || !ref || stateDir.startsWith("-") || ref.startsWith("-")) {
|
|
94
|
+
stderr.write(USAGE);
|
|
95
|
+
return 78;
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
if (!statSync(stateDir).isDirectory()) {
|
|
99
|
+
stderr.write("explain-failed: not a directory\n");
|
|
100
|
+
return 1;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
const code = err.code;
|
|
105
|
+
stderr.write(code === "ENOENT" ? "explain-failed: no such directory\n" : "explain-failed: not a directory\n");
|
|
106
|
+
return 1;
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
const result = await explain(readOnlyLedger(stateDir), ref);
|
|
110
|
+
const claims = result.record.claims;
|
|
111
|
+
stdout.write(`ref: ${terminalText(String(claims.ref ?? ref))}\n`);
|
|
112
|
+
stdout.write(`decision: ${terminalText(String(claims.decision ?? ""))}\n`);
|
|
113
|
+
stdout.write(`reason: ${terminalText(String(claims.reasonCode ?? ""))}\n`);
|
|
114
|
+
const taint = result.sessionTaint;
|
|
115
|
+
if (taint) {
|
|
116
|
+
stdout.write(`rule: ${terminalText(taint.rule ?? "")}\n`);
|
|
117
|
+
stdout.write(`rule text: ${terminalText(taint.ruleText ?? "")}\n`);
|
|
118
|
+
stdout.write(`taint source: ${terminalText(taint.sourceRecordId)}\n`);
|
|
119
|
+
stdout.write(`taint source ref: ${terminalText(taint.sourceRef)}\n`);
|
|
120
|
+
}
|
|
121
|
+
return 0;
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
const message = err instanceof Error ? err.message : "explain-failed";
|
|
125
|
+
if (message.startsWith("explain-unknown-ref:")) {
|
|
126
|
+
stderr.write(`explain-failed: unknown ref\n`);
|
|
127
|
+
return 1;
|
|
128
|
+
}
|
|
129
|
+
stderr.write(`explain-failed: ${terminalText(message.replace(/[\r\n]+/g, " ").slice(0, 300))}\n`);
|
|
130
|
+
return 1;
|
|
131
|
+
}
|
|
132
|
+
}
|
package/dist/init-local.d.ts
CHANGED
|
@@ -44,6 +44,11 @@ export type InitLocalOpts = {
|
|
|
44
44
|
noOwnerGrant?: boolean;
|
|
45
45
|
/** Install mode. Defaults to process.env. SUDO_USER names the POSIX owner. */
|
|
46
46
|
env?: NodeJS.ProcessEnv;
|
|
47
|
+
/**
|
|
48
|
+
* Install path. Writes `VERAX_INSTALLED=1` into verax.env so the credential
|
|
49
|
+
* check refuses. `verax init --local` leaves the line unset and only warns.
|
|
50
|
+
*/
|
|
51
|
+
installed?: boolean;
|
|
47
52
|
/** Test hook. A throw here runs the rollback after the issuer directory exists and before any file is written. */
|
|
48
53
|
beforeWrite?: () => void;
|
|
49
54
|
};
|
package/dist/init-local.js
CHANGED
|
@@ -276,6 +276,7 @@ async function runInitLocalBody(argv, io, opts) {
|
|
|
276
276
|
`VERAX_STATE_DIR=${stateDir}`,
|
|
277
277
|
`VERAX_POLICY_FILE=${policyDest}`,
|
|
278
278
|
`VERAX_BIND=127.0.0.1:${parsed.port}`,
|
|
279
|
+
...(opts.installed ? ["VERAX_INSTALLED=1"] : []),
|
|
279
280
|
...(parsed.operatorDoor ? [`VERAX_OPERATOR_DOOR=${parsed.operatorDoor.origin}`] : []),
|
|
280
281
|
"",
|
|
281
282
|
].join("\n");
|
package/dist/install.js
CHANGED
|
@@ -4383,7 +4383,7 @@ async function execute(plan, exec, io, platform, copyFile = copyFileSync, instal
|
|
|
4383
4383
|
String(step.port),
|
|
4384
4384
|
...(step.force ? ["--force"] : []),
|
|
4385
4385
|
...(step.operatorDoor ? ["--operator-door", step.operatorDoor] : []),
|
|
4386
|
-
], io, { quiet: true, noOwnerGrant: step.noOwnerGrant, env: installEnv });
|
|
4386
|
+
], io, { quiet: true, noOwnerGrant: step.noOwnerGrant, env: installEnv, installed: true });
|
|
4387
4387
|
if (code !== 0)
|
|
4388
4388
|
return finish(code);
|
|
4389
4389
|
continue;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type RehearsalReport } from "./policy-rehearsal.ts";
|
|
2
|
+
export declare function parseSince(value: string): number;
|
|
3
|
+
export declare function renderRehearsal(report: RehearsalReport, json: boolean): string;
|
|
4
|
+
type Io = {
|
|
5
|
+
write(s: string): unknown;
|
|
6
|
+
};
|
|
7
|
+
export declare function runPolicy(argv: readonly string[], stdout: Io, stderr: Io, now?: () => number): number;
|
|
8
|
+
export {};
|