@verax-ai/proxy 0.9.0 → 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/dist/approvals.js +2 -0
- package/dist/canary.d.ts +118 -0
- package/dist/canary.js +755 -0
- package/dist/explain.js +7 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/inputs.d.ts +5 -0
- package/dist/inputs.js +38 -0
- package/dist/ledger.js +17 -1
- package/dist/policy.d.ts +9 -1
- package/dist/policy.js +38 -1
- package/dist/proxy.js +274 -26
- package/dist/session-taint.d.ts +92 -0
- package/dist/session-taint.js +343 -0
- package/dist/tool-labels.d.ts +43 -0
- package/dist/tool-labels.js +94 -0
- package/dist/types.d.ts +82 -1
- package/dist/verify-ledger.js +28 -11
- package/package.json +1 -1
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { appendFileSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { decisionRecordHash, signDecisionRecord } from "@cedulon/core";
|
|
5
|
+
import { sha256Canonical } from "./hash.js";
|
|
6
|
+
import { inputsMatchingHash, readInputsIndex } from "./inputs.js";
|
|
7
|
+
import { tenantKey } from "./tenant.js";
|
|
8
|
+
/** One token. The body refuses a call whose JWT has no `jti`. Any other `jti` stays. */
|
|
9
|
+
export const TAINT_CLEAR_FILE = "taint-clear.jsonl";
|
|
10
|
+
export const TAINT_CLEAR_SUBJECT = "verax.taint-clear";
|
|
11
|
+
export const TAINT_CLEAR_REASON = "taint-cleared";
|
|
12
|
+
const CONTROL_EFFECT = "verax.control";
|
|
13
|
+
export function createTaintMemory() {
|
|
14
|
+
return {
|
|
15
|
+
loaded: false,
|
|
16
|
+
replayFailed: false,
|
|
17
|
+
tainted: new Map(),
|
|
18
|
+
jtiBySession: new Map(),
|
|
19
|
+
recorded: new Set(),
|
|
20
|
+
clearWarned: new Set(),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Session key for one token: SHA-256 of the tenant key concatenated with the
|
|
25
|
+
* JWT `jti`. The body already requires a `jti` and refuses only an empty one,
|
|
26
|
+
* so a long `jti` or one that contains a control character is still a session.
|
|
27
|
+
* Absent or empty is not a session.
|
|
28
|
+
*/
|
|
29
|
+
export function sessionIdOf(principal) {
|
|
30
|
+
if (!principal)
|
|
31
|
+
return null;
|
|
32
|
+
const jti = principal.jti;
|
|
33
|
+
if (typeof jti !== "string" || jti.length < 1)
|
|
34
|
+
return null;
|
|
35
|
+
const key = tenantKey({
|
|
36
|
+
brain: principal.brain ?? "",
|
|
37
|
+
iss: principal.iss,
|
|
38
|
+
tenant: principal.tenant,
|
|
39
|
+
org: principal.org,
|
|
40
|
+
});
|
|
41
|
+
return createHash("sha256").update(key + jti, "utf8").digest("hex");
|
|
42
|
+
}
|
|
43
|
+
/** What an operator may write as `--session`. A newline would be a second line. */
|
|
44
|
+
function clearSessionToken(value) {
|
|
45
|
+
if (typeof value !== "string" || value.length < 1 || value.length > 4096)
|
|
46
|
+
return null;
|
|
47
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
48
|
+
const c = value.charCodeAt(i);
|
|
49
|
+
if (c < 0x20 || c === 0x7f)
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Cedulon accepts a decision time only when it is a non-negative safe integer.
|
|
56
|
+
* The effect extract's window ends one millisecond later, so that sum has to
|
|
57
|
+
* be a safe integer too. `9007199254740991` is safe, and the next integer is not.
|
|
58
|
+
*/
|
|
59
|
+
function clearAtMs(value) {
|
|
60
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0)
|
|
61
|
+
return null;
|
|
62
|
+
if (!Number.isSafeInteger(value + 1))
|
|
63
|
+
return null;
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
export function taintSourceOf(memory, session) {
|
|
67
|
+
return memory.tainted.get(session) ?? null;
|
|
68
|
+
}
|
|
69
|
+
/** The first return that taints a clean session is the source. A later one does not replace it. */
|
|
70
|
+
export function noteTaintReturn(memory, session, source, jti) {
|
|
71
|
+
if (memory.tainted.has(session))
|
|
72
|
+
return;
|
|
73
|
+
memory.tainted.set(session, source);
|
|
74
|
+
if (typeof jti === "string" && jti.length > 0)
|
|
75
|
+
memory.jtiBySession.set(session, jti);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* `token` is either the session key or the raw `jti`. A raw `jti` shared by
|
|
79
|
+
* two sessions clears neither of them.
|
|
80
|
+
*/
|
|
81
|
+
export function noteTaintClear(memory, token) {
|
|
82
|
+
if (memory.tainted.delete(token)) {
|
|
83
|
+
memory.jtiBySession.delete(token);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const hits = [];
|
|
87
|
+
for (const [id, jti] of memory.jtiBySession) {
|
|
88
|
+
if (jti === token && memory.tainted.has(id))
|
|
89
|
+
hits.push(id);
|
|
90
|
+
}
|
|
91
|
+
if (hits.length !== 1)
|
|
92
|
+
return;
|
|
93
|
+
const id = hits[0];
|
|
94
|
+
memory.tainted.delete(id);
|
|
95
|
+
memory.jtiBySession.delete(id);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The operator command appends one line. The body copies it into a signed
|
|
99
|
+
* record on the next sync. A tool call does not write this file.
|
|
100
|
+
*/
|
|
101
|
+
export function appendTaintClearLine(stateDir, line) {
|
|
102
|
+
const session = clearSessionToken(line.session);
|
|
103
|
+
if (!session)
|
|
104
|
+
throw new Error("taint-clear-session");
|
|
105
|
+
const by = typeof line.by === "string" && line.by !== "" && line.by.length <= 256 ? line.by : "unknown";
|
|
106
|
+
const atMs = clearAtMs(line.atMs);
|
|
107
|
+
if (atMs === null)
|
|
108
|
+
throw new Error("taint-clear-time");
|
|
109
|
+
const row = { session, by, via: "cli", atMs };
|
|
110
|
+
appendFileSync(join(stateDir, TAINT_CLEAR_FILE), `${JSON.stringify(row)}\n`, { encoding: "utf8", mode: 0o600 });
|
|
111
|
+
}
|
|
112
|
+
function readClearLines(stateDir, warned) {
|
|
113
|
+
let text;
|
|
114
|
+
try {
|
|
115
|
+
text = readFileSync(join(stateDir, TAINT_CLEAR_FILE), "utf8");
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
if (err.code === "ENOENT")
|
|
119
|
+
return [];
|
|
120
|
+
throw err;
|
|
121
|
+
}
|
|
122
|
+
const out = [];
|
|
123
|
+
const lines = text.split("\n");
|
|
124
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
125
|
+
const line = lines[i] ?? "";
|
|
126
|
+
if (line === "")
|
|
127
|
+
continue;
|
|
128
|
+
try {
|
|
129
|
+
const row = JSON.parse(line);
|
|
130
|
+
const atMs = clearAtMs(row.atMs);
|
|
131
|
+
if (atMs === null) {
|
|
132
|
+
const warnKey = `${i + 1}:${line}`;
|
|
133
|
+
if (!warned.has(warnKey)) {
|
|
134
|
+
warned.add(warnKey);
|
|
135
|
+
process.stderr.write(`verax-proxy: taint-clear skipped ${TAINT_CLEAR_FILE}:${i + 1}\n`);
|
|
136
|
+
}
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
const session = clearSessionToken(row.session);
|
|
140
|
+
if (!session)
|
|
141
|
+
continue;
|
|
142
|
+
const by = typeof row.by === "string" && row.by !== "" && row.by.length <= 256 ? row.by : "unknown";
|
|
143
|
+
out.push({ session, by, via: "cli", atMs, lineNo: i + 1 });
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
// A torn last line is skipped. The next append is a new line.
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return out;
|
|
150
|
+
}
|
|
151
|
+
function storedSession(inputs) {
|
|
152
|
+
if (typeof inputs.session !== "string" || inputs.session.length < 1)
|
|
153
|
+
return null;
|
|
154
|
+
const brain = inputs.principal?.brain;
|
|
155
|
+
if (typeof brain !== "string")
|
|
156
|
+
return null;
|
|
157
|
+
const id = sessionIdOf({
|
|
158
|
+
brain,
|
|
159
|
+
iss: inputs.principal.iss,
|
|
160
|
+
tenant: inputs.principal.tenant,
|
|
161
|
+
org: inputs.principal.org,
|
|
162
|
+
jti: inputs.session,
|
|
163
|
+
});
|
|
164
|
+
if (!id)
|
|
165
|
+
return null;
|
|
166
|
+
return { id, jti: inputs.session };
|
|
167
|
+
}
|
|
168
|
+
function clearRequestHash(line) {
|
|
169
|
+
return sha256Canonical({ action: "taint-clear", session: line.session, by: line.by, via: line.via, atMs: line.atMs });
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Runs after the ledger snapshot is in hand and before it is merged.
|
|
173
|
+
* The race test records a return the snapshot did not include.
|
|
174
|
+
* Production leaves this null.
|
|
175
|
+
*/
|
|
176
|
+
export const taintReplayHooks = {
|
|
177
|
+
afterRead: null,
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* Replay taint from the ledger in durable effect order. A reads-untrusted
|
|
181
|
+
* allow taints only once its effect row is there, including `:threw`, and
|
|
182
|
+
* not when the only effect is `:halted`. A clear effect lifts it. A
|
|
183
|
+
* decision with no effect is not a completed read and is not a completed
|
|
184
|
+
* clear. The source stays the first completed return until the clear.
|
|
185
|
+
*
|
|
186
|
+
* The replay is built into fresh maps and then merged. A session the
|
|
187
|
+
* snapshot has keeps the ledger's source, because that is the first
|
|
188
|
+
* return. A live entry the snapshot does not have stays: it is newer
|
|
189
|
+
* than every clear in the snapshot. A clear is signed only after a
|
|
190
|
+
* replay has succeeded, and a retry runs only while that has not
|
|
191
|
+
* happened yet.
|
|
192
|
+
*/
|
|
193
|
+
export async function loadTaintFromLedger(opts) {
|
|
194
|
+
const decisions = await opts.ledger.decisions();
|
|
195
|
+
const effects = await opts.ledger.effects();
|
|
196
|
+
const index = await readInputsIndex(opts.inputsLog);
|
|
197
|
+
taintReplayHooks.afterRead?.(opts.memory);
|
|
198
|
+
const replay = createTaintMemory();
|
|
199
|
+
const byRef = new Map();
|
|
200
|
+
for (const rec of decisions) {
|
|
201
|
+
const ref = rec.claims.ref;
|
|
202
|
+
if (typeof ref !== "string")
|
|
203
|
+
continue;
|
|
204
|
+
const list = byRef.get(ref);
|
|
205
|
+
if (list)
|
|
206
|
+
list.push(rec);
|
|
207
|
+
else
|
|
208
|
+
byRef.set(ref, [rec]);
|
|
209
|
+
if (rec.claims.reasonCode === TAINT_CLEAR_REASON && typeof rec.claims.requestHash === "string") {
|
|
210
|
+
replay.recorded.add(rec.claims.requestHash);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
for (const effect of effects) {
|
|
214
|
+
const effectClass = effect.row.effectClass;
|
|
215
|
+
if (effectClass === "duplicate-effect" || effectClass.endsWith(":halted"))
|
|
216
|
+
continue;
|
|
217
|
+
const ref = effect.row.ref;
|
|
218
|
+
if (typeof ref !== "string")
|
|
219
|
+
continue;
|
|
220
|
+
const recs = byRef.get(ref);
|
|
221
|
+
if (!recs)
|
|
222
|
+
continue;
|
|
223
|
+
for (const rec of recs) {
|
|
224
|
+
if (typeof rec.claims.inputsHash !== "string")
|
|
225
|
+
continue;
|
|
226
|
+
const inputs = index
|
|
227
|
+
? (index.get(`${rec.claims.inputsHash}:${ref}`) ?? null)
|
|
228
|
+
: await inputsMatchingHash(opts.inputsLog, ref, rec.claims.inputsHash);
|
|
229
|
+
if (!inputs)
|
|
230
|
+
continue;
|
|
231
|
+
if (inputs.taintClear && rec.claims.reasonCode === TAINT_CLEAR_REASON && effectClass === CONTROL_EFFECT) {
|
|
232
|
+
noteTaintClear(replay, inputs.taintClear.session);
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
const receiving = effect.receivingSession;
|
|
236
|
+
const fromEffect = receiving &&
|
|
237
|
+
typeof receiving.jti === "string" &&
|
|
238
|
+
receiving.jti !== "" &&
|
|
239
|
+
typeof receiving.sessionId === "string" &&
|
|
240
|
+
receiving.sessionId !== ""
|
|
241
|
+
? { id: receiving.sessionId, jti: receiving.jti }
|
|
242
|
+
: null;
|
|
243
|
+
const stored = fromEffect ?? storedSession(inputs);
|
|
244
|
+
const subject = rec.claims.subject;
|
|
245
|
+
if (stored &&
|
|
246
|
+
rec.claims.decision === "allow" &&
|
|
247
|
+
inputs.toolLabels?.readsUntrusted === true &&
|
|
248
|
+
typeof subject === "string" &&
|
|
249
|
+
(effectClass === subject || effectClass === `${subject}:threw`) &&
|
|
250
|
+
!replay.tainted.has(stored.id)) {
|
|
251
|
+
replay.tainted.set(stored.id, {
|
|
252
|
+
sourceRecordId: decisionRecordHash(rec),
|
|
253
|
+
sourceRef: ref,
|
|
254
|
+
});
|
|
255
|
+
replay.jtiBySession.set(stored.id, stored.jti);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
for (const [session, source] of replay.tainted) {
|
|
260
|
+
opts.memory.tainted.set(session, source);
|
|
261
|
+
const jti = replay.jtiBySession.get(session);
|
|
262
|
+
if (jti !== undefined)
|
|
263
|
+
opts.memory.jtiBySession.set(session, jti);
|
|
264
|
+
}
|
|
265
|
+
for (const hash of replay.recorded)
|
|
266
|
+
opts.memory.recorded.add(hash);
|
|
267
|
+
}
|
|
268
|
+
export async function applyTaintClearFile(opts) {
|
|
269
|
+
// The request time stays in the signed inputs (`atMs`). The decision and
|
|
270
|
+
// the effect are stamped when they are written, so a late clear cannot
|
|
271
|
+
// hide an earlier row from a window read.
|
|
272
|
+
for (const line of readClearLines(opts.stateDir, opts.memory.clearWarned)) {
|
|
273
|
+
const writtenAt = opts.now();
|
|
274
|
+
const requestHash = clearRequestHash(line);
|
|
275
|
+
if (opts.memory.recorded.has(requestHash))
|
|
276
|
+
continue;
|
|
277
|
+
try {
|
|
278
|
+
const ref = opts.nonce();
|
|
279
|
+
const inputs = {
|
|
280
|
+
principal: { brain: "verax-proxy", scopes: [] },
|
|
281
|
+
inputs: [],
|
|
282
|
+
taintClear: { session: line.session, by: line.by, via: line.via, atMs: line.atMs },
|
|
283
|
+
};
|
|
284
|
+
const inputsHash = sha256Canonical(inputs);
|
|
285
|
+
const effectHash = sha256Canonical({
|
|
286
|
+
action: "taint-clear",
|
|
287
|
+
session: line.session,
|
|
288
|
+
by: line.by,
|
|
289
|
+
via: line.via,
|
|
290
|
+
atMs: line.atMs,
|
|
291
|
+
});
|
|
292
|
+
await opts.inputsLog.append(ref, inputs);
|
|
293
|
+
await opts.ledger.appendDecisionChained((prevRecordHash) => signDecisionRecord({
|
|
294
|
+
decider: "verax-proxy",
|
|
295
|
+
subject: TAINT_CLEAR_SUBJECT,
|
|
296
|
+
requestHash,
|
|
297
|
+
policyHash: opts.policyHash,
|
|
298
|
+
inputsHash,
|
|
299
|
+
decision: "allow",
|
|
300
|
+
reasonCode: TAINT_CLEAR_REASON,
|
|
301
|
+
ref,
|
|
302
|
+
effectHash,
|
|
303
|
+
effectClass: CONTROL_EFFECT,
|
|
304
|
+
timestampMs: writtenAt,
|
|
305
|
+
nonce: ref,
|
|
306
|
+
prevRecordHash,
|
|
307
|
+
}, opts.recordSigner.privateKeyPem, opts.recordSigner.publicKeyPem));
|
|
308
|
+
// Seen before the effect row. A throw there must not sign this line again.
|
|
309
|
+
opts.memory.recorded.add(requestHash);
|
|
310
|
+
const row = {
|
|
311
|
+
ref,
|
|
312
|
+
effectHash,
|
|
313
|
+
effectClass: CONTROL_EFFECT,
|
|
314
|
+
timestampMs: writtenAt,
|
|
315
|
+
actor: line.by,
|
|
316
|
+
};
|
|
317
|
+
await opts.ledger.appendEffect(row, "self", effectHash);
|
|
318
|
+
noteTaintClear(opts.memory, line.session);
|
|
319
|
+
}
|
|
320
|
+
catch (err) {
|
|
321
|
+
const message = (err instanceof Error ? err.message : "taint-clear-failed").replace(/[\r\n]+/g, " ").slice(0, 200);
|
|
322
|
+
const warnKey = `apply:${line.lineNo}:${requestHash}`;
|
|
323
|
+
if (!opts.memory.clearWarned.has(warnKey)) {
|
|
324
|
+
opts.memory.clearWarned.add(warnKey);
|
|
325
|
+
process.stderr.write(`verax-proxy: taint-clear failed ${TAINT_CLEAR_FILE}:${line.lineNo}: ${message}\n`);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
/** Used by explain, which already holds the inputs document. */
|
|
331
|
+
export function taintOnInputs(inputs) {
|
|
332
|
+
const taint = inputs?.sessionTaint;
|
|
333
|
+
if (!taint)
|
|
334
|
+
return undefined;
|
|
335
|
+
if (typeof taint.sourceRecordId !== "string" || taint.sourceRecordId === "")
|
|
336
|
+
return undefined;
|
|
337
|
+
if (typeof taint.sourceRef !== "string" || taint.sourceRef === "")
|
|
338
|
+
return undefined;
|
|
339
|
+
return taint;
|
|
340
|
+
}
|
|
341
|
+
export function isTaintClearRecord(rec) {
|
|
342
|
+
return rec.claims.subject === TAINT_CLEAR_SUBJECT && rec.claims.reasonCode === TAINT_CLEAR_REASON;
|
|
343
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ToolLabelName } from "./types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Built-in tools whose results or effects cross the operator's boundary.
|
|
4
|
+
* A policy `toolLabels` entry for the same name replaces this list, including
|
|
5
|
+
* an empty list (the operator labelled it as neither).
|
|
6
|
+
*
|
|
7
|
+
* `message.read` returns the inbox: messages that arrived from outside.
|
|
8
|
+
* `message.send` queues a message for delivery. `spend` authorizes a payment.
|
|
9
|
+
* `memory.get`, `memory.put` and `audit.explain` stay on this machine's own
|
|
10
|
+
* store and ledger, so they carry neither label.
|
|
11
|
+
*/
|
|
12
|
+
export declare const BUILTIN_TOOL_LABELS: Readonly<Record<string, readonly ToolLabelName[]>>;
|
|
13
|
+
export type ResolvedToolLabels = {
|
|
14
|
+
readsUntrusted: boolean;
|
|
15
|
+
egress: boolean;
|
|
16
|
+
/** False only for a downstream tool nobody labelled. Doctor lists those. */
|
|
17
|
+
labelled: boolean;
|
|
18
|
+
};
|
|
19
|
+
export declare function parseToolLabelMap(raw: unknown, code: string): Readonly<Record<string, readonly ToolLabelName[]>>;
|
|
20
|
+
export declare function parseToolLabelList(value: unknown, code: string): ToolLabelName[];
|
|
21
|
+
/**
|
|
22
|
+
* Policy labels win, then the downstream document, then the built-in defaults.
|
|
23
|
+
* A downstream tool with no entry in either place is both labels, unless the
|
|
24
|
+
* policy sets `unlabelledDownstream` to `neither`. A key that is present and
|
|
25
|
+
* empty is a label: the operator said neither.
|
|
26
|
+
*/
|
|
27
|
+
export declare function resolveToolLabels(opts: {
|
|
28
|
+
tool: string;
|
|
29
|
+
policyLabels?: Readonly<Record<string, readonly ToolLabelName[]>>;
|
|
30
|
+
documentLabels?: Readonly<Record<string, readonly ToolLabelName[]>>;
|
|
31
|
+
downstreamTools?: ReadonlySet<string>;
|
|
32
|
+
unlabelledDownstream?: "both" | "neither";
|
|
33
|
+
}): ResolvedToolLabels;
|
|
34
|
+
/**
|
|
35
|
+
* Downstream tools the operator did not label, prefixed (`web.fetch`).
|
|
36
|
+
* A child with no `tools` list is named by its prefix: the body cannot see
|
|
37
|
+
* the child's list without attaching it, and doctor does not attach.
|
|
38
|
+
*/
|
|
39
|
+
export declare function unlabelledDownstreamNames(specs: readonly {
|
|
40
|
+
prefix: string;
|
|
41
|
+
tools?: readonly string[];
|
|
42
|
+
labels?: Readonly<Record<string, readonly string[]>>;
|
|
43
|
+
}[], policyLabels: Readonly<Record<string, readonly ToolLabelName[]>>): string[];
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in tools whose results or effects cross the operator's boundary.
|
|
3
|
+
* A policy `toolLabels` entry for the same name replaces this list, including
|
|
4
|
+
* an empty list (the operator labelled it as neither).
|
|
5
|
+
*
|
|
6
|
+
* `message.read` returns the inbox: messages that arrived from outside.
|
|
7
|
+
* `message.send` queues a message for delivery. `spend` authorizes a payment.
|
|
8
|
+
* `memory.get`, `memory.put` and `audit.explain` stay on this machine's own
|
|
9
|
+
* store and ledger, so they carry neither label.
|
|
10
|
+
*/
|
|
11
|
+
export const BUILTIN_TOOL_LABELS = {
|
|
12
|
+
"message.read": ["reads-untrusted"],
|
|
13
|
+
"message.send": ["egress"],
|
|
14
|
+
spend: ["egress"],
|
|
15
|
+
};
|
|
16
|
+
const TOOL_NAME_RE = /^[A-Za-z][A-Za-z0-9._-]{0,96}$/;
|
|
17
|
+
export function parseToolLabelMap(raw, code) {
|
|
18
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw))
|
|
19
|
+
throw new Error(code);
|
|
20
|
+
const out = {};
|
|
21
|
+
for (const [tool, value] of Object.entries(raw)) {
|
|
22
|
+
if (!TOOL_NAME_RE.test(tool))
|
|
23
|
+
throw new Error(`${code}:${tool}`);
|
|
24
|
+
out[tool] = parseToolLabelList(value, `${code}:${tool}`);
|
|
25
|
+
}
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
export function parseToolLabelList(value, code) {
|
|
29
|
+
if (!Array.isArray(value))
|
|
30
|
+
throw new Error(code);
|
|
31
|
+
const out = [];
|
|
32
|
+
for (const item of value) {
|
|
33
|
+
if (item !== "reads-untrusted" && item !== "egress")
|
|
34
|
+
throw new Error(code);
|
|
35
|
+
if (!out.includes(item))
|
|
36
|
+
out.push(item);
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
function flags(labels, labelled) {
|
|
41
|
+
return {
|
|
42
|
+
readsUntrusted: labels.includes("reads-untrusted"),
|
|
43
|
+
egress: labels.includes("egress"),
|
|
44
|
+
labelled,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Policy labels win, then the downstream document, then the built-in defaults.
|
|
49
|
+
* A downstream tool with no entry in either place is both labels, unless the
|
|
50
|
+
* policy sets `unlabelledDownstream` to `neither`. A key that is present and
|
|
51
|
+
* empty is a label: the operator said neither.
|
|
52
|
+
*/
|
|
53
|
+
export function resolveToolLabels(opts) {
|
|
54
|
+
const policyLabels = opts.policyLabels ?? {};
|
|
55
|
+
const unlabelledDownstream = opts.unlabelledDownstream ?? "both";
|
|
56
|
+
if (Object.prototype.hasOwnProperty.call(policyLabels, opts.tool)) {
|
|
57
|
+
return flags(policyLabels[opts.tool] ?? [], true);
|
|
58
|
+
}
|
|
59
|
+
if (opts.documentLabels && Object.prototype.hasOwnProperty.call(opts.documentLabels, opts.tool)) {
|
|
60
|
+
return flags(opts.documentLabels[opts.tool] ?? [], true);
|
|
61
|
+
}
|
|
62
|
+
if (Object.prototype.hasOwnProperty.call(BUILTIN_TOOL_LABELS, opts.tool)) {
|
|
63
|
+
return flags(BUILTIN_TOOL_LABELS[opts.tool] ?? [], true);
|
|
64
|
+
}
|
|
65
|
+
if (opts.downstreamTools?.has(opts.tool)) {
|
|
66
|
+
if (unlabelledDownstream === "neither") {
|
|
67
|
+
return { readsUntrusted: false, egress: false, labelled: false };
|
|
68
|
+
}
|
|
69
|
+
return { readsUntrusted: true, egress: true, labelled: false };
|
|
70
|
+
}
|
|
71
|
+
return { readsUntrusted: false, egress: false, labelled: true };
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Downstream tools the operator did not label, prefixed (`web.fetch`).
|
|
75
|
+
* A child with no `tools` list is named by its prefix: the body cannot see
|
|
76
|
+
* the child's list without attaching it, and doctor does not attach.
|
|
77
|
+
*/
|
|
78
|
+
export function unlabelledDownstreamNames(specs, policyLabels) {
|
|
79
|
+
const names = [];
|
|
80
|
+
for (const spec of specs) {
|
|
81
|
+
if (spec.tools === undefined) {
|
|
82
|
+
names.push(spec.prefix);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
for (const child of spec.tools) {
|
|
86
|
+
const prefixed = `${spec.prefix}.${child}`;
|
|
87
|
+
const inDoc = spec.labels !== undefined && Object.prototype.hasOwnProperty.call(spec.labels, child);
|
|
88
|
+
const inPolicy = Object.prototype.hasOwnProperty.call(policyLabels, prefixed);
|
|
89
|
+
if (!inDoc && !inPolicy)
|
|
90
|
+
names.push(prefixed);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return names;
|
|
94
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { DecisionKind, SignedDecisionRecord } from "@cedulon/core";
|
|
2
2
|
import type { EffectRow, SignedEffectExtract } from "@cedulon/effect-extract";
|
|
3
|
+
/** A tool label. `reads-untrusted` taints the session when the call returns. `egress` is held once it is tainted. */
|
|
4
|
+
export type ToolLabelName = "reads-untrusted" | "egress";
|
|
3
5
|
/** Who asked. G2 fills `brain` from JWT `sub` and `scopes` from `scope`. */
|
|
4
6
|
export type Principal = {
|
|
5
7
|
brain: string;
|
|
@@ -10,6 +12,11 @@ export type Principal = {
|
|
|
10
12
|
tenant?: string;
|
|
11
13
|
/** Explicit JWT `org` claim, when the issuer sends one and `tenant` is absent. */
|
|
12
14
|
org?: string;
|
|
15
|
+
/**
|
|
16
|
+
* JWT `jti`. With the tenant key it names the session. Absent on a direct
|
|
17
|
+
* `proxy.call` that is not a token. An egress call with no `jti` is held.
|
|
18
|
+
*/
|
|
19
|
+
jti?: string;
|
|
13
20
|
};
|
|
14
21
|
/** Same shape as `@cedulon/mcp-guard` so a later wrap can sit on this type. */
|
|
15
22
|
export type ToolCall = {
|
|
@@ -99,6 +106,17 @@ export type Policy = {
|
|
|
99
106
|
media?: MediaConfig;
|
|
100
107
|
/** Document `scanCharCap`. Results read it even when no `dataClasses` key is set. */
|
|
101
108
|
scanCharCap?: number;
|
|
109
|
+
/** Absent means a canary match halts the body. `deny` records the deny and leaves the switch alone. */
|
|
110
|
+
canary?: "halt" | "deny";
|
|
111
|
+
/**
|
|
112
|
+
* Operator labels. A key that is present replaces the built-in default,
|
|
113
|
+
* including an empty list. Absent keys keep the default.
|
|
114
|
+
*/
|
|
115
|
+
toolLabels?: Readonly<Record<string, readonly ToolLabelName[]>>;
|
|
116
|
+
/** What an egress call does in a tainted session. Absent in the file means hold. */
|
|
117
|
+
sessionTaint?: "hold" | "deny";
|
|
118
|
+
/** What an unlabelled downstream tool is. Absent in the file means both labels. */
|
|
119
|
+
unlabelledDownstream?: "both" | "neither";
|
|
102
120
|
};
|
|
103
121
|
export type WitnessClass = "self" | "same-org" | "third-party" | "regulated";
|
|
104
122
|
export type EffectAttestation = {
|
|
@@ -111,8 +129,20 @@ export type LedgerEffect = {
|
|
|
111
129
|
resultHash?: string;
|
|
112
130
|
/** One-row extract signed at call time. */
|
|
113
131
|
receipt?: SignedEffectExtract;
|
|
114
|
-
/**
|
|
132
|
+
/**
|
|
133
|
+
* Signed `{ ref, effectHash, witnessClass, resultHash }` via COSE Sign1.
|
|
134
|
+
* When `receivingSession` is present, the payload also carries `session`
|
|
135
|
+
* and `sessionId`. Rows written before that field omit it and still verify.
|
|
136
|
+
*/
|
|
115
137
|
attestation?: EffectAttestation;
|
|
138
|
+
/**
|
|
139
|
+
* The session that ran this effect. Absent on rows written before the field,
|
|
140
|
+
* on `:halted` rows, and on a third-party receipt.
|
|
141
|
+
*/
|
|
142
|
+
receivingSession?: {
|
|
143
|
+
jti: string;
|
|
144
|
+
sessionId: string;
|
|
145
|
+
};
|
|
116
146
|
};
|
|
117
147
|
export type RecordSigner = {
|
|
118
148
|
privateKeyPem: string;
|
|
@@ -259,6 +289,40 @@ export type DecisionInputs = {
|
|
|
259
289
|
withheld: Record<string, number>;
|
|
260
290
|
returnedHash: string;
|
|
261
291
|
};
|
|
292
|
+
/** A canary match. The id and the place in the call, never the value. */
|
|
293
|
+
canary?: {
|
|
294
|
+
finding: "canary-exfiltration";
|
|
295
|
+
id: string;
|
|
296
|
+
where: string;
|
|
297
|
+
};
|
|
298
|
+
/**
|
|
299
|
+
* JWT `jti` of the token that made this call. The gate's session key is the
|
|
300
|
+
* SHA-256 of the tenant key and this `jti`. Set only when the tool carries
|
|
301
|
+
* a session label, so an unlabelled call's inputs stay as they were.
|
|
302
|
+
*/
|
|
303
|
+
session?: string;
|
|
304
|
+
/** Labels that applied when this call was decided. Replay does not re-read the policy. */
|
|
305
|
+
toolLabels?: {
|
|
306
|
+
readsUntrusted?: true;
|
|
307
|
+
egress?: true;
|
|
308
|
+
};
|
|
309
|
+
/**
|
|
310
|
+
* On an egress decision made while the session was tainted. `sourceRecordId`
|
|
311
|
+
* is `decisionRecordHash` of the call that tainted the session.
|
|
312
|
+
*/
|
|
313
|
+
sessionTaint?: {
|
|
314
|
+
sourceRecordId: string;
|
|
315
|
+
sourceRef: string;
|
|
316
|
+
rule: string | null;
|
|
317
|
+
ruleText: string | null;
|
|
318
|
+
};
|
|
319
|
+
/** The operator clear. A tool call cannot write this. */
|
|
320
|
+
taintClear?: {
|
|
321
|
+
session: string;
|
|
322
|
+
by: string;
|
|
323
|
+
via: "cli";
|
|
324
|
+
atMs: number;
|
|
325
|
+
};
|
|
262
326
|
};
|
|
263
327
|
/** How an operator's approval reached the ledger: a person at `verax approve`, a non-interactive `--from-script` run, or the body's `/api/approve`. */
|
|
264
328
|
export type ApprovalChannel = "cli" | "cli-script" | "http";
|
|
@@ -286,6 +350,13 @@ export type ProxyDeps = {
|
|
|
286
350
|
resolveInput?: (id: string, principal?: Principal) => Promise<ResolvedInput | null>;
|
|
287
351
|
/** True when the call names another tenant's memory id or decision ref. */
|
|
288
352
|
checkTenantMismatch?: (call: ToolCall, principal: Principal) => Promise<boolean>;
|
|
353
|
+
/** Prefixed downstream tool names attached for this process. */
|
|
354
|
+
downstreamTools?: ReadonlySet<string>;
|
|
355
|
+
/**
|
|
356
|
+
* Labels from the downstream document, keyed by the prefixed tool name.
|
|
357
|
+
* A key that is present is labelled, including an empty list.
|
|
358
|
+
*/
|
|
359
|
+
downstreamLabels?: Readonly<Record<string, readonly ToolLabelName[]>>;
|
|
289
360
|
};
|
|
290
361
|
export type ExplainFinding = {
|
|
291
362
|
code: string | null;
|
|
@@ -336,6 +407,16 @@ export type ExplainResult = {
|
|
|
336
407
|
guarantee: "unconditional" | "conditional";
|
|
337
408
|
warnings: ExplainWarning[];
|
|
338
409
|
trustRoot: ExplainTrustRoot;
|
|
410
|
+
/**
|
|
411
|
+
* Set when this held or denied egress call was decided in a tainted session.
|
|
412
|
+
* `sourceRecordId` names the call that tainted it. `rule` is the egress rule.
|
|
413
|
+
*/
|
|
414
|
+
sessionTaint?: {
|
|
415
|
+
sourceRecordId: string;
|
|
416
|
+
sourceRef: string;
|
|
417
|
+
rule: string | null;
|
|
418
|
+
ruleText: string | null;
|
|
419
|
+
};
|
|
339
420
|
scope?: {
|
|
340
421
|
accountId: string;
|
|
341
422
|
railId: string;
|