@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
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { loadPolicy, terminalText } from "@verax-ai/proxy";
|
|
3
|
+
import { PolicyRehearsalError, rehearsePolicy } from "./policy-rehearsal.js";
|
|
4
|
+
const USAGE = `usage: verax policy rehearse --policy <file> [--since <duration>] [--brain <id>] [--json] <stateDir>
|
|
5
|
+
|
|
6
|
+
--policy <file> policy to rehearse. It is not installed and the file is not written.
|
|
7
|
+
--since <duration>
|
|
8
|
+
keep decisions whose timestamp is at or after the moment
|
|
9
|
+
the command starts, minus this duration. A duration is a
|
|
10
|
+
number and a unit: ms, s, m or h. For example 30m or 2h.
|
|
11
|
+
--brain <id> keep decisions whose inputs row names that brain.
|
|
12
|
+
A decision with no matching inputs row is left out.
|
|
13
|
+
--json one JSON object on stdout. The values are the ledger's.
|
|
14
|
+
<stateDir> ledger directory. Nothing in it is written. No tool runs.
|
|
15
|
+
|
|
16
|
+
held is the ledger's defer. A decision that stays allow, deny or held is
|
|
17
|
+
unchanged, including when the rule id differs. When the rehearsed policy's
|
|
18
|
+
hash is the hash on the record, the decision is unchanged. Otherwise a
|
|
19
|
+
decision the rehearsal cannot reproduce is not rehearsable: it is not counted as
|
|
20
|
+
unchanged or as changed. That includes a rule that needs an argument, a
|
|
21
|
+
counter, free space or argument text the record does not keep, a
|
|
22
|
+
data-class policy whose depth check runs before any rule and reads
|
|
23
|
+
arguments the record does not keep, a higher disk limit than the recorded
|
|
24
|
+
policy, which the proxy checks before any rule, an
|
|
25
|
+
allow or a hold whose recorded policy snapshot is missing or unreadable, a
|
|
26
|
+
deny for memory.get or audit.explain that would become an allow or a hold,
|
|
27
|
+
and a timestamp outside -8.64e15..8.64e15.
|
|
28
|
+
|
|
29
|
+
Exit 0 when no rehearsed decision changes. Decisions that cannot be
|
|
30
|
+
rehearsed are counted on their own and do not change this exit code.
|
|
31
|
+
Exit 1 when some rehearsed decision changes.
|
|
32
|
+
Exit 2 on usage or when the policy or the ledger cannot be read.
|
|
33
|
+
|
|
34
|
+
A rehearsal shows what the recorded calls would get.
|
|
35
|
+
It says nothing about calls that were never made.
|
|
36
|
+
`;
|
|
37
|
+
const UNITS = { ms: 1, s: 1000, m: 60_000, h: 3_600_000 };
|
|
38
|
+
export function parseSince(value) {
|
|
39
|
+
const match = /^(\d+(?:\.\d+)?)(ms|s|m|h)$/.exec(value);
|
|
40
|
+
if (!match)
|
|
41
|
+
throw new PolicyRehearsalError("duration needs ms, s, m or h");
|
|
42
|
+
const ms = Number(match[1]) * UNITS[match[2]];
|
|
43
|
+
if (!Number.isFinite(ms) || ms < 1 || ms > Number.MAX_SAFE_INTEGER) {
|
|
44
|
+
throw new PolicyRehearsalError("duration out of range");
|
|
45
|
+
}
|
|
46
|
+
return ms;
|
|
47
|
+
}
|
|
48
|
+
function cell(value) {
|
|
49
|
+
return terminalText(value);
|
|
50
|
+
}
|
|
51
|
+
export function renderRehearsal(report, json) {
|
|
52
|
+
if (json)
|
|
53
|
+
return `${JSON.stringify(report)}\n`;
|
|
54
|
+
const lines = [`unchanged\t${report.unchanged}`];
|
|
55
|
+
for (const [pair, count] of Object.entries(report.changes))
|
|
56
|
+
lines.push(`${pair}\t${count}`);
|
|
57
|
+
const skipped = report.skipped.length;
|
|
58
|
+
lines.push(`not rehearsable\t${skipped}`);
|
|
59
|
+
for (const [reason, count] of Object.entries(report.notRehearsable)) {
|
|
60
|
+
lines.push(["reason", reason, String(count)].join("\t"));
|
|
61
|
+
}
|
|
62
|
+
if (report.changed.length > 0) {
|
|
63
|
+
lines.push("changed");
|
|
64
|
+
lines.push(["id", "time", "brain", "tool", "from", "rule", "to", "rule"].join("\t"));
|
|
65
|
+
for (const row of report.changed) {
|
|
66
|
+
lines.push([row.id, row.time, row.brain, row.tool, row.from, row.rule ?? "-", row.to, row.newRule ?? "-"].map(cell).join("\t"));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (skipped > 0) {
|
|
70
|
+
lines.push("not rehearsed");
|
|
71
|
+
lines.push(["id", "time", "brain", "tool", "reason"].join("\t"));
|
|
72
|
+
for (const row of report.skipped) {
|
|
73
|
+
lines.push([row.id, row.time ?? "-", row.brain ?? "-", row.tool ?? "-", row.reason].map(cell).join("\t"));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return `${lines.join("\n")}\n`;
|
|
77
|
+
}
|
|
78
|
+
function fail(stderr, message) {
|
|
79
|
+
stderr.write(`${terminalText(message)}\n`);
|
|
80
|
+
return 2;
|
|
81
|
+
}
|
|
82
|
+
function usage(stderr) {
|
|
83
|
+
stderr.write(USAGE);
|
|
84
|
+
return 2;
|
|
85
|
+
}
|
|
86
|
+
export function runPolicy(argv, stdout, stderr, now = Date.now) {
|
|
87
|
+
if (argv.length === 0 || argv[0] === "--help" || argv[0] === "-h") {
|
|
88
|
+
stdout.write(USAGE);
|
|
89
|
+
return argv.length === 0 ? 2 : 0;
|
|
90
|
+
}
|
|
91
|
+
if (argv[0] !== "rehearse")
|
|
92
|
+
return usage(stderr);
|
|
93
|
+
const args = argv.slice(1);
|
|
94
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
95
|
+
stdout.write(USAGE);
|
|
96
|
+
return 0;
|
|
97
|
+
}
|
|
98
|
+
const seen = new Set();
|
|
99
|
+
let policyPath;
|
|
100
|
+
let since;
|
|
101
|
+
let brain;
|
|
102
|
+
let json = false;
|
|
103
|
+
let stateDir;
|
|
104
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
105
|
+
const arg = args[i];
|
|
106
|
+
if (arg === "--json") {
|
|
107
|
+
if (seen.has(arg))
|
|
108
|
+
return fail(stderr, "duplicate option: --json");
|
|
109
|
+
seen.add(arg);
|
|
110
|
+
json = true;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (!arg.startsWith("-")) {
|
|
114
|
+
if (stateDir !== undefined)
|
|
115
|
+
return fail(stderr, "one state directory");
|
|
116
|
+
stateDir = arg;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (arg !== "--policy" && arg !== "--since" && arg !== "--brain")
|
|
120
|
+
return fail(stderr, `unknown option: ${arg}`);
|
|
121
|
+
if (seen.has(arg))
|
|
122
|
+
return fail(stderr, `duplicate option: ${arg}`);
|
|
123
|
+
seen.add(arg);
|
|
124
|
+
const value = args[i + 1];
|
|
125
|
+
if (value === undefined || value.startsWith("-"))
|
|
126
|
+
return fail(stderr, `missing value for ${arg}`);
|
|
127
|
+
i += 1;
|
|
128
|
+
if (arg === "--policy")
|
|
129
|
+
policyPath = value;
|
|
130
|
+
if (arg === "--since")
|
|
131
|
+
since = value;
|
|
132
|
+
if (arg === "--brain")
|
|
133
|
+
brain = value;
|
|
134
|
+
}
|
|
135
|
+
if (policyPath === undefined || stateDir === undefined)
|
|
136
|
+
return usage(stderr);
|
|
137
|
+
let text;
|
|
138
|
+
try {
|
|
139
|
+
text = readFileSync(policyPath, "utf8");
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
return fail(stderr, "cannot read policy file");
|
|
143
|
+
}
|
|
144
|
+
let policy;
|
|
145
|
+
try {
|
|
146
|
+
policy = loadPolicy(text);
|
|
147
|
+
}
|
|
148
|
+
catch (err) {
|
|
149
|
+
if (err instanceof SyntaxError)
|
|
150
|
+
return fail(stderr, err.message);
|
|
151
|
+
return fail(stderr, err instanceof Error && err.message !== "" ? err.message : "policy cannot be read");
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
let notBeforeMs;
|
|
155
|
+
if (since !== undefined)
|
|
156
|
+
notBeforeMs = now() - parseSince(since);
|
|
157
|
+
const report = rehearsePolicy(stateDir, policy, {
|
|
158
|
+
...(notBeforeMs !== undefined ? { notBeforeMs } : {}),
|
|
159
|
+
...(brain !== undefined ? { brain } : {}),
|
|
160
|
+
});
|
|
161
|
+
stdout.write(renderRehearsal(report, json));
|
|
162
|
+
return report.changed.length > 0 ? 1 : 0;
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
const message = err instanceof Error && err.message !== "" ? err.message : "rehearsal failed";
|
|
166
|
+
const line = message.split(/\r?\n/, 1)[0];
|
|
167
|
+
return fail(stderr, line === undefined || line === "" ? "rehearsal failed" : line);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type Policy } from "@verax-ai/proxy";
|
|
2
|
+
/**
|
|
3
|
+
* Read a ledger and say what a different policy would have decided.
|
|
4
|
+
* The state directory is only read. No tool is called.
|
|
5
|
+
*/
|
|
6
|
+
export declare class PolicyRehearsalError extends Error {
|
|
7
|
+
constructor(message: string);
|
|
8
|
+
}
|
|
9
|
+
export declare const ARGUMENT_REASON = "the rule reads arguments the record does not keep";
|
|
10
|
+
export type RehearsalChange = {
|
|
11
|
+
id: string;
|
|
12
|
+
time: string;
|
|
13
|
+
brain: string;
|
|
14
|
+
tool: string;
|
|
15
|
+
from: "allow" | "deny" | "held";
|
|
16
|
+
rule: string | null;
|
|
17
|
+
to: "allow" | "deny" | "held";
|
|
18
|
+
newRule: string | null;
|
|
19
|
+
};
|
|
20
|
+
export type RehearsalSkip = {
|
|
21
|
+
id: string;
|
|
22
|
+
time: string | null;
|
|
23
|
+
brain: string | null;
|
|
24
|
+
tool: string | null;
|
|
25
|
+
reason: string;
|
|
26
|
+
};
|
|
27
|
+
export type RehearsalReport = {
|
|
28
|
+
unchanged: number;
|
|
29
|
+
changes: Record<string, number>;
|
|
30
|
+
notRehearsable: Record<string, number>;
|
|
31
|
+
changed: RehearsalChange[];
|
|
32
|
+
skipped: RehearsalSkip[];
|
|
33
|
+
};
|
|
34
|
+
export type RehearseOptions = {
|
|
35
|
+
/** Keep a decision whose timestampMs is at least this instant. */
|
|
36
|
+
notBeforeMs?: number;
|
|
37
|
+
brain?: string;
|
|
38
|
+
};
|
|
39
|
+
export declare function rehearsePolicy(stateDir: string, policy: Policy, opts?: RehearseOptions): RehearsalReport;
|