@verax-ai/proxy 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/dist/anchors.d.ts +28 -5
- package/dist/anchors.js +21 -8
- 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 +9 -4
- package/dist/index.js +6 -2
- 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/signed-statement.d.ts +26 -0
- package/dist/signed-statement.js +146 -0
- package/dist/tool-labels.d.ts +43 -0
- package/dist/tool-labels.js +94 -0
- package/dist/transparency-receipt.d.ts +38 -0
- package/dist/transparency-receipt.js +103 -1
- package/dist/types.d.ts +82 -1
- package/dist/verify-ledger.d.ts +12 -0
- package/dist/verify-ledger.js +81 -33
- package/package.json +7 -7
package/dist/proxy.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { CANARY_FINDING, haltForCanary, loadActiveCanaries, markCanaryHalted, matchCanary, writeCanaryIncident } from "./canary.js";
|
|
2
|
+
import { isProxyHalted, persistHaltedSwitch } from "./process-halt.js";
|
|
3
|
+
import { decisionRecordHash, signDecisionRecord } from "@cedulon/core";
|
|
3
4
|
import { approvalDispatchArgs, approvalDispatchHash, approvePending, approvalsLogFor, createApprovalBudgetGuard, drainApprovalCommands, signedApprovalDispatch, spentTodayMinorOf, } from "./approvals.js";
|
|
4
5
|
import { SerialQueue } from "./serial-queue.js";
|
|
5
6
|
import { diskProbe } from "./disk.js";
|
|
@@ -13,7 +14,9 @@ import { countedWork, hasPrimaryEffect, lookupDecisionByRef, lookupReauthByHash,
|
|
|
13
14
|
import { argumentDepth, gateDataClasses, MAX_ARGUMENT_DEPTH, scanCallMeta } from "./data-classes.js";
|
|
14
15
|
import { gateResultClasses, gateThrownError } from "./result-classes.js";
|
|
15
16
|
import { terminalText } from "./policy.js";
|
|
17
|
+
import { applyTaintClearFile, createTaintMemory, loadTaintFromLedger, noteTaintReturn, sessionIdOf, taintSourceOf, } from "./session-taint.js";
|
|
16
18
|
import { spokenReason } from "./spoken-reason.js";
|
|
19
|
+
import { resolveToolLabels } from "./tool-labels.js";
|
|
17
20
|
import { tenantKey } from "./tenant.js";
|
|
18
21
|
import { isTextContent } from "./types.js";
|
|
19
22
|
function scopedClaimsRef(principal, raw) {
|
|
@@ -195,6 +198,15 @@ function inputsWithoutSource(inputs) {
|
|
|
195
198
|
};
|
|
196
199
|
}
|
|
197
200
|
export function createProxy(deps) {
|
|
201
|
+
deps = {
|
|
202
|
+
...deps,
|
|
203
|
+
policy: {
|
|
204
|
+
...deps.policy,
|
|
205
|
+
toolLabels: deps.policy.toolLabels ?? {},
|
|
206
|
+
sessionTaint: deps.policy.sessionTaint ?? "hold",
|
|
207
|
+
unlabelledDownstream: deps.policy.unlabelledDownstream ?? "both",
|
|
208
|
+
},
|
|
209
|
+
};
|
|
198
210
|
const inputsLog = deps.inputsLog ?? inputsLogFor(deps.ledger);
|
|
199
211
|
const approvals = approvalsLogFor(deps.ledger);
|
|
200
212
|
const admission = new SerialQueue();
|
|
@@ -214,6 +226,8 @@ export function createProxy(deps) {
|
|
|
214
226
|
const controlMemory = createControlMemory();
|
|
215
227
|
const revokeMemory = createRevokeMemory();
|
|
216
228
|
const operatorRemovalMemory = createOperatorRemovalMemory();
|
|
229
|
+
const taintMemory = createTaintMemory();
|
|
230
|
+
const signedHashByRef = new Map();
|
|
217
231
|
let controlSyncReported = false;
|
|
218
232
|
if (stateDir === null) {
|
|
219
233
|
process.stderr.write("verax-proxy: halt and disk limits are inactive without a ledger directory\n");
|
|
@@ -221,25 +235,29 @@ export function createProxy(deps) {
|
|
|
221
235
|
async function writeRecord(opts) {
|
|
222
236
|
const inputsHash = sha256Canonical(opts.inputs);
|
|
223
237
|
await inputsLog.append(opts.ref, opts.inputs);
|
|
224
|
-
await deps.ledger.appendDecisionChained((prevRecordHash) =>
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
238
|
+
await deps.ledger.appendDecisionChained((prevRecordHash) => {
|
|
239
|
+
const signed = signDecisionRecord({
|
|
240
|
+
decider: opts.decider ?? "verax-proxy",
|
|
241
|
+
subject: opts.subject,
|
|
242
|
+
requestHash: opts.requestHash,
|
|
243
|
+
policyHash: deps.policy.hash,
|
|
244
|
+
inputsHash,
|
|
245
|
+
decision: opts.decision,
|
|
246
|
+
reasonCode: opts.reasonCode,
|
|
247
|
+
ref: opts.ref,
|
|
248
|
+
effectHash: opts.effectHash,
|
|
249
|
+
// The class of effect this decision names, in this channel's own
|
|
250
|
+
// vocabulary: the call it decided about. An allow must carry one, and
|
|
251
|
+
// a refusal names what it refused, so the audit can hold a decision
|
|
252
|
+
// and an effect row to the same word.
|
|
253
|
+
effectClass: opts.subject,
|
|
254
|
+
timestampMs: opts.timestampMs,
|
|
255
|
+
nonce: opts.ref,
|
|
256
|
+
prevRecordHash,
|
|
257
|
+
}, deps.recordSigner.privateKeyPem, deps.recordSigner.publicKeyPem);
|
|
258
|
+
signedHashByRef.set(opts.ref, decisionRecordHash(signed));
|
|
259
|
+
return signed;
|
|
260
|
+
});
|
|
243
261
|
if (opts.inputs.approver?.resolves) {
|
|
244
262
|
const kind = opts.reasonCode === "expired" ? "expired" : opts.decision === "allow" ? "allow" : null;
|
|
245
263
|
if (kind)
|
|
@@ -315,7 +333,73 @@ export function createProxy(deps) {
|
|
|
315
333
|
}
|
|
316
334
|
return denied("halted", allowRef);
|
|
317
335
|
}
|
|
318
|
-
|
|
336
|
+
function sessionTool(name) {
|
|
337
|
+
const labels = resolveToolLabels({
|
|
338
|
+
tool: name,
|
|
339
|
+
policyLabels: deps.policy.toolLabels,
|
|
340
|
+
documentLabels: deps.downstreamLabels,
|
|
341
|
+
downstreamTools: deps.downstreamTools,
|
|
342
|
+
unlabelledDownstream: deps.policy.unlabelledDownstream,
|
|
343
|
+
});
|
|
344
|
+
return labels.readsUntrusted || labels.egress;
|
|
345
|
+
}
|
|
346
|
+
let taintInflight = null;
|
|
347
|
+
async function ensureTaintLoaded() {
|
|
348
|
+
// Only a labelled call replays taint. A full decisions() read on every
|
|
349
|
+
// memory.get would be a second read beside the control sync, which is
|
|
350
|
+
// counted as one for a run of ordinary calls. The read stays outside the
|
|
351
|
+
// admission queue so an unlabelled call is not held behind it.
|
|
352
|
+
if (taintMemory.loaded)
|
|
353
|
+
return;
|
|
354
|
+
if (!taintInflight) {
|
|
355
|
+
taintInflight = (async () => {
|
|
356
|
+
try {
|
|
357
|
+
await loadTaintFromLedger({ ledger: deps.ledger, inputsLog, memory: taintMemory });
|
|
358
|
+
taintMemory.loaded = true;
|
|
359
|
+
taintMemory.replayFailed = false;
|
|
360
|
+
}
|
|
361
|
+
catch {
|
|
362
|
+
// A ledger that cannot be parsed is already fail-closed on the rate
|
|
363
|
+
// and halt paths. Do not throw out of the call, and do not mark the
|
|
364
|
+
// replay loaded: the next labelled call tries again.
|
|
365
|
+
taintMemory.loaded = false;
|
|
366
|
+
taintMemory.replayFailed = true;
|
|
367
|
+
}
|
|
368
|
+
finally {
|
|
369
|
+
taintInflight = null;
|
|
370
|
+
}
|
|
371
|
+
})();
|
|
372
|
+
}
|
|
373
|
+
await taintInflight;
|
|
374
|
+
}
|
|
375
|
+
async function applySessionClears() {
|
|
376
|
+
if (stateDir === null || !taintMemory.loaded || taintMemory.replayFailed)
|
|
377
|
+
return;
|
|
378
|
+
await applyTaintClearFile({
|
|
379
|
+
stateDir,
|
|
380
|
+
now: deps.now,
|
|
381
|
+
nonce: deps.nonce,
|
|
382
|
+
policyHash: deps.policy.hash,
|
|
383
|
+
recordSigner: deps.recordSigner,
|
|
384
|
+
inputsLog,
|
|
385
|
+
ledger: deps.ledger,
|
|
386
|
+
memory: taintMemory,
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
async function noteReturnedTaint(ref, mark) {
|
|
390
|
+
if (!mark)
|
|
391
|
+
return;
|
|
392
|
+
let sourceRecordId = signedHashByRef.get(ref);
|
|
393
|
+
if (!sourceRecordId) {
|
|
394
|
+
// An approved retry's allow was signed in approvePending, not writeRecord.
|
|
395
|
+
const rec = (await deps.ledger.decisions()).find((d) => d.claims.ref === ref);
|
|
396
|
+
if (!rec)
|
|
397
|
+
return;
|
|
398
|
+
sourceRecordId = decisionRecordHash(rec);
|
|
399
|
+
}
|
|
400
|
+
noteTaintReturn(taintMemory, mark.session, { sourceRecordId, sourceRef: ref }, mark.jti);
|
|
401
|
+
}
|
|
402
|
+
async function runInner(call, principal, ref, effectRecorded, bound, taintMark) {
|
|
319
403
|
const dispatchedName = call.name;
|
|
320
404
|
const dispatchedArgs = deepFreeze(structuredClone(call.arguments));
|
|
321
405
|
const dispatchedHash = sha256Canonical(effectDescriptor(dispatchedName, dispatchedArgs));
|
|
@@ -339,6 +423,7 @@ export function createProxy(deps) {
|
|
|
339
423
|
effectClass: `${dispatchedName}:threw`,
|
|
340
424
|
timestampMs: deps.now(),
|
|
341
425
|
actor: principal.brain,
|
|
426
|
+
...(taintMark ? { receivingSession: { jti: taintMark.jti, sessionId: taintMark.session } } : {}),
|
|
342
427
|
};
|
|
343
428
|
try {
|
|
344
429
|
await deps.ledger.appendEffect(row, "self", resultHash);
|
|
@@ -347,6 +432,7 @@ export function createProxy(deps) {
|
|
|
347
432
|
catch (writeErr) {
|
|
348
433
|
throw writeErr;
|
|
349
434
|
}
|
|
435
|
+
await noteReturnedTaint(ref, taintMark);
|
|
350
436
|
// Without result classes the tool's own error leaves, untouched and not awaited.
|
|
351
437
|
if (!deps.policy.resultClasses && !deps.policy.media)
|
|
352
438
|
throw err;
|
|
@@ -364,6 +450,7 @@ export function createProxy(deps) {
|
|
|
364
450
|
effectClass: dispatchedName,
|
|
365
451
|
timestampMs: deps.now(),
|
|
366
452
|
actor: principal.brain,
|
|
453
|
+
...(taintMark ? { receivingSession: { jti: taintMark.jti, sessionId: taintMark.session } } : {}),
|
|
367
454
|
};
|
|
368
455
|
const { effectReceipt, ...normalResult } = result;
|
|
369
456
|
const resultHash = sha256Canonical(normalResult);
|
|
@@ -383,6 +470,7 @@ export function createProxy(deps) {
|
|
|
383
470
|
// stays false) and a restart answers outcome-unknown instead of running again.
|
|
384
471
|
return effectUnrecorded(ref);
|
|
385
472
|
}
|
|
473
|
+
await noteReturnedTaint(ref, taintMark);
|
|
386
474
|
return gateResultClasses(normalResult, dispatchedName, ref, resultHash, principal, {
|
|
387
475
|
policy: deps.policy,
|
|
388
476
|
nonce: deps.nonce,
|
|
@@ -468,14 +556,14 @@ export function createProxy(deps) {
|
|
|
468
556
|
});
|
|
469
557
|
return denied(reasonCode, denyRef);
|
|
470
558
|
}
|
|
471
|
-
function launchInner(dispatchedCall, principal, allowRef, flightKey, bound) {
|
|
559
|
+
function launchInner(dispatchedCall, principal, allowRef, flightKey, bound, taintMark) {
|
|
472
560
|
// Written before the tool runs, removed after the effect is recorded. A
|
|
473
561
|
// mark that survives the process is what tells a restarted body that this
|
|
474
562
|
// ref was already started (in-flight-log.ts).
|
|
475
563
|
if (stateDir !== null)
|
|
476
564
|
markStarted(stateDir, flightKey, dispatchedCall.name);
|
|
477
565
|
const effectRecorded = { ok: false };
|
|
478
|
-
const work = runInner(dispatchedCall, principal, allowRef, effectRecorded, bound);
|
|
566
|
+
const work = runInner(dispatchedCall, principal, allowRef, effectRecorded, bound, taintMark);
|
|
479
567
|
inFlight.set(flightKey, work);
|
|
480
568
|
return { kind: "run", work, key: flightKey, replayRef: allowRef, effectRecorded };
|
|
481
569
|
}
|
|
@@ -633,6 +721,99 @@ export function createProxy(deps) {
|
|
|
633
721
|
});
|
|
634
722
|
return denied("halted", ref);
|
|
635
723
|
}
|
|
724
|
+
async function denyIfCanary() {
|
|
725
|
+
if (!stateDir)
|
|
726
|
+
return null;
|
|
727
|
+
const loaded = loadActiveCanaries(stateDir);
|
|
728
|
+
if (!loaded.ok) {
|
|
729
|
+
const ref = deps.nonce();
|
|
730
|
+
await writeRecord({
|
|
731
|
+
decision: "deny",
|
|
732
|
+
reasonCode: loaded.reason,
|
|
733
|
+
ref,
|
|
734
|
+
requestHash,
|
|
735
|
+
inputs: { principal: resolved.inputs.principal, inputs: [] },
|
|
736
|
+
effectHash: null,
|
|
737
|
+
subject: call.name,
|
|
738
|
+
timestampMs,
|
|
739
|
+
});
|
|
740
|
+
return denied(loaded.reason, ref);
|
|
741
|
+
}
|
|
742
|
+
if (loaded.canaries.length === 0)
|
|
743
|
+
return null;
|
|
744
|
+
const matched = matchCanary(call.arguments, loaded.canaries, call.name);
|
|
745
|
+
if (matched === null)
|
|
746
|
+
return null;
|
|
747
|
+
if (matched === "too-wide") {
|
|
748
|
+
const ref = deps.nonce();
|
|
749
|
+
await writeRecord({
|
|
750
|
+
decision: "deny",
|
|
751
|
+
reasonCode: "canary-unscanned",
|
|
752
|
+
ref,
|
|
753
|
+
requestHash,
|
|
754
|
+
inputs: { principal: resolved.inputs.principal, inputs: [] },
|
|
755
|
+
effectHash: null,
|
|
756
|
+
subject: call.name,
|
|
757
|
+
timestampMs,
|
|
758
|
+
});
|
|
759
|
+
return denied("canary-unscanned", ref);
|
|
760
|
+
}
|
|
761
|
+
const enforcement = deps.policy.canary === "deny" ? "deny" : "halt";
|
|
762
|
+
// The switch goes on before either write. A failed finding or
|
|
763
|
+
// decision record must not leave the agent running.
|
|
764
|
+
if (enforcement === "halt") {
|
|
765
|
+
try {
|
|
766
|
+
haltForCanary(stateDir, timestampMs);
|
|
767
|
+
}
|
|
768
|
+
catch (err) {
|
|
769
|
+
const message = (err instanceof Error ? err.message : String(err)).replace(/[\r\n]+/g, " ");
|
|
770
|
+
process.stderr.write(`verax-proxy: canary halt: ${terminalText(message)}\n`);
|
|
771
|
+
markCanaryHalted(stateDir);
|
|
772
|
+
persistHaltedSwitch(stateDir);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
const failures = [];
|
|
776
|
+
const reportWrite = (note) => {
|
|
777
|
+
process.stderr.write(`verax-proxy: ${terminalText(note)}\n`);
|
|
778
|
+
failures.push(note);
|
|
779
|
+
};
|
|
780
|
+
try {
|
|
781
|
+
writeCanaryIncident(stateDir, {
|
|
782
|
+
id: matched.id,
|
|
783
|
+
where: matched.where,
|
|
784
|
+
enforcement,
|
|
785
|
+
observedAt: timestampMs,
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
catch (err) {
|
|
789
|
+
const message = (err instanceof Error ? err.message : String(err)).replace(/[\r\n]+/g, " ");
|
|
790
|
+
reportWrite(`canary finding write failed: ${message}`);
|
|
791
|
+
}
|
|
792
|
+
const ref = deps.nonce();
|
|
793
|
+
try {
|
|
794
|
+
await writeRecord({
|
|
795
|
+
decision: "deny",
|
|
796
|
+
reasonCode: CANARY_FINDING,
|
|
797
|
+
ref,
|
|
798
|
+
requestHash,
|
|
799
|
+
inputs: {
|
|
800
|
+
principal: resolved.inputs.principal,
|
|
801
|
+
inputs: [],
|
|
802
|
+
canary: { finding: CANARY_FINDING, id: matched.id, where: matched.where },
|
|
803
|
+
},
|
|
804
|
+
effectHash: null,
|
|
805
|
+
subject: matched.subject,
|
|
806
|
+
timestampMs,
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
catch (err) {
|
|
810
|
+
const message = (err instanceof Error ? err.message : String(err)).replace(/[\r\n]+/g, " ");
|
|
811
|
+
reportWrite(`canary decision record write failed: ${message}`);
|
|
812
|
+
}
|
|
813
|
+
if (failures.length > 0)
|
|
814
|
+
throw new Error(failures.join("; "));
|
|
815
|
+
return denied(CANARY_FINDING, ref);
|
|
816
|
+
}
|
|
636
817
|
{
|
|
637
818
|
const halted = await denyIfHalted();
|
|
638
819
|
if (halted)
|
|
@@ -653,16 +834,27 @@ export function createProxy(deps) {
|
|
|
653
834
|
return denied("ledger-disk-low", ref);
|
|
654
835
|
}
|
|
655
836
|
const given = readRef(call.arguments);
|
|
837
|
+
const labelled = sessionTool(dispatched.name);
|
|
838
|
+
if (labelled)
|
|
839
|
+
await ensureTaintLoaded();
|
|
656
840
|
const plan = await admission.enqueue(async () => {
|
|
657
841
|
// A halt that landed while this call waited must be on the chain before the deny.
|
|
658
842
|
if (stateDir)
|
|
659
843
|
await syncControlRecords();
|
|
844
|
+
// The replay already ran outside this queue. The clear file writes, so it stays here.
|
|
845
|
+
if (labelled)
|
|
846
|
+
await applySessionClears();
|
|
660
847
|
// A call that waited here behind another admission reads the halt flag again.
|
|
661
848
|
{
|
|
662
849
|
const halted = await denyIfHalted();
|
|
663
850
|
if (halted)
|
|
664
851
|
return { kind: "done", result: halted };
|
|
665
852
|
}
|
|
853
|
+
{
|
|
854
|
+
const caught = await denyIfCanary();
|
|
855
|
+
if (caught)
|
|
856
|
+
return { kind: "done", result: caught };
|
|
857
|
+
}
|
|
666
858
|
if (tooDeep) {
|
|
667
859
|
const reasonCode = "data-class-unscanned";
|
|
668
860
|
const ref = deps.nonce();
|
|
@@ -886,7 +1078,12 @@ export function createProxy(deps) {
|
|
|
886
1078
|
result: await recordHaltedAfterAllow(allow.ref, toRun, principal),
|
|
887
1079
|
};
|
|
888
1080
|
}
|
|
889
|
-
|
|
1081
|
+
const liveJti = typeof principal.jti === "string" && principal.jti.length > 0 ? principal.jti : undefined;
|
|
1082
|
+
const liveId = liveJti ? sessionIdOf(principal) : null;
|
|
1083
|
+
const taintMark = liveId && liveJti && bound?.toolLabels?.readsUntrusted === true
|
|
1084
|
+
? { session: liveId, jti: liveJti, readsUntrusted: true }
|
|
1085
|
+
: null;
|
|
1086
|
+
return launchInner(toRun, principal, allow.ref, scopedRef, boundRows, taintMark);
|
|
890
1087
|
}
|
|
891
1088
|
return { kind: "done", result: deferred(given) };
|
|
892
1089
|
}
|
|
@@ -969,6 +1166,56 @@ export function createProxy(deps) {
|
|
|
969
1166
|
}
|
|
970
1167
|
}
|
|
971
1168
|
}
|
|
1169
|
+
const session = sessionIdOf(principal);
|
|
1170
|
+
const rawJti = session && typeof principal.jti === "string" ? principal.jti : null;
|
|
1171
|
+
const labels = resolveToolLabels({
|
|
1172
|
+
tool: dispatched.name,
|
|
1173
|
+
policyLabels: deps.policy.toolLabels,
|
|
1174
|
+
documentLabels: deps.downstreamLabels,
|
|
1175
|
+
downstreamTools: deps.downstreamTools,
|
|
1176
|
+
unlabelledDownstream: deps.policy.unlabelledDownstream,
|
|
1177
|
+
});
|
|
1178
|
+
if (session && rawJti && (labels.readsUntrusted || labels.egress)) {
|
|
1179
|
+
recordInputs = {
|
|
1180
|
+
...recordInputs,
|
|
1181
|
+
session: rawJti,
|
|
1182
|
+
toolLabels: {
|
|
1183
|
+
...(labels.readsUntrusted ? { readsUntrusted: true } : {}),
|
|
1184
|
+
...(labels.egress ? { egress: true } : {}),
|
|
1185
|
+
},
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
// An egress call in a tainted session is held, unless the policy says deny.
|
|
1189
|
+
// A call the policy already refused stays refused. A call it already holds
|
|
1190
|
+
// stays held, and the record still names the source.
|
|
1191
|
+
if (session && labels.egress && taintMemory.replayFailed && decision === "allow") {
|
|
1192
|
+
decision = deps.policy.sessionTaint === "deny" ? "deny" : "defer";
|
|
1193
|
+
reasonCode = "taint-unreadable";
|
|
1194
|
+
}
|
|
1195
|
+
let tainted = null;
|
|
1196
|
+
if (session && labels.egress && !taintMemory.replayFailed && (decision === "allow" || decision === "defer")) {
|
|
1197
|
+
tainted = taintSourceOf(taintMemory, session);
|
|
1198
|
+
if (tainted) {
|
|
1199
|
+
const rule = deps.policy.rule(verdict.rule);
|
|
1200
|
+
recordInputs = {
|
|
1201
|
+
...recordInputs,
|
|
1202
|
+
sessionTaint: {
|
|
1203
|
+
sourceRecordId: tainted.sourceRecordId,
|
|
1204
|
+
sourceRef: tainted.sourceRef,
|
|
1205
|
+
rule: verdict.rule,
|
|
1206
|
+
ruleText: rule?.text ?? null,
|
|
1207
|
+
},
|
|
1208
|
+
};
|
|
1209
|
+
if (deps.policy.sessionTaint === "deny") {
|
|
1210
|
+
decision = "deny";
|
|
1211
|
+
reasonCode = "session-tainted";
|
|
1212
|
+
}
|
|
1213
|
+
else if (decision === "allow") {
|
|
1214
|
+
decision = "defer";
|
|
1215
|
+
reasonCode = "session-tainted";
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
972
1219
|
const ref = typeof given === "string" ? scopedClaimsRef(principal, given) : deps.nonce();
|
|
973
1220
|
// What the brain is told: its own `_ref`, so a retry carries the same key back.
|
|
974
1221
|
const shown = typeof given === "string" ? given : ref;
|
|
@@ -1020,7 +1267,8 @@ export function createProxy(deps) {
|
|
|
1020
1267
|
if (stateDir && isProxyHalted(stateDir)) {
|
|
1021
1268
|
return { kind: "done", result: await recordHaltedAfterAllow(ref, forwarded, principal) };
|
|
1022
1269
|
}
|
|
1023
|
-
|
|
1270
|
+
const taintMark = session && rawJti && labels.readsUntrusted ? { session, jti: rawJti, readsUntrusted: true } : null;
|
|
1271
|
+
return launchInner(forwarded, principal, ref, ref, resolved.inputs.inputs, taintMark);
|
|
1024
1272
|
});
|
|
1025
1273
|
if (plan.kind === "done")
|
|
1026
1274
|
return plan.result;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { SignedDecisionRecord } from "@cedulon/core";
|
|
2
|
+
import type { DecisionInputs, InputsLog, Ledger, RecordSigner } from "./types.ts";
|
|
3
|
+
/** One token. The body refuses a call whose JWT has no `jti`. Any other `jti` stays. */
|
|
4
|
+
export declare const TAINT_CLEAR_FILE = "taint-clear.jsonl";
|
|
5
|
+
export declare const TAINT_CLEAR_SUBJECT = "verax.taint-clear";
|
|
6
|
+
export declare const TAINT_CLEAR_REASON = "taint-cleared";
|
|
7
|
+
export type TaintSource = {
|
|
8
|
+
sourceRecordId: string;
|
|
9
|
+
sourceRef: string;
|
|
10
|
+
};
|
|
11
|
+
export type TaintMemory = {
|
|
12
|
+
loaded: boolean;
|
|
13
|
+
/** The ledger could not be read. An egress allow is held until a later read works. */
|
|
14
|
+
replayFailed: boolean;
|
|
15
|
+
tainted: Map<string, TaintSource>;
|
|
16
|
+
/** Session key to the raw `jti`, so a clear of that `jti` can find one session. */
|
|
17
|
+
jtiBySession: Map<string, string>;
|
|
18
|
+
recorded: Set<string>;
|
|
19
|
+
/** Bad clear lines already named on stderr. A later sync does not repeat them. */
|
|
20
|
+
clearWarned: Set<string>;
|
|
21
|
+
};
|
|
22
|
+
export declare function createTaintMemory(): TaintMemory;
|
|
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 declare function sessionIdOf(principal: {
|
|
30
|
+
brain?: string;
|
|
31
|
+
iss?: string;
|
|
32
|
+
tenant?: string;
|
|
33
|
+
org?: string;
|
|
34
|
+
jti?: string;
|
|
35
|
+
} | undefined): string | null;
|
|
36
|
+
export declare function taintSourceOf(memory: TaintMemory, session: string): TaintSource | null;
|
|
37
|
+
/** The first return that taints a clean session is the source. A later one does not replace it. */
|
|
38
|
+
export declare function noteTaintReturn(memory: TaintMemory, session: string, source: TaintSource, jti?: string): void;
|
|
39
|
+
/**
|
|
40
|
+
* `token` is either the session key or the raw `jti`. A raw `jti` shared by
|
|
41
|
+
* two sessions clears neither of them.
|
|
42
|
+
*/
|
|
43
|
+
export declare function noteTaintClear(memory: TaintMemory, token: string): void;
|
|
44
|
+
/**
|
|
45
|
+
* The operator command appends one line. The body copies it into a signed
|
|
46
|
+
* record on the next sync. A tool call does not write this file.
|
|
47
|
+
*/
|
|
48
|
+
export declare function appendTaintClearLine(stateDir: string, line: {
|
|
49
|
+
session: string;
|
|
50
|
+
by: string;
|
|
51
|
+
atMs: number;
|
|
52
|
+
}): void;
|
|
53
|
+
/**
|
|
54
|
+
* Runs after the ledger snapshot is in hand and before it is merged.
|
|
55
|
+
* The race test records a return the snapshot did not include.
|
|
56
|
+
* Production leaves this null.
|
|
57
|
+
*/
|
|
58
|
+
export declare const taintReplayHooks: {
|
|
59
|
+
afterRead: ((memory: TaintMemory) => void) | null;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Replay taint from the ledger in durable effect order. A reads-untrusted
|
|
63
|
+
* allow taints only once its effect row is there, including `:threw`, and
|
|
64
|
+
* not when the only effect is `:halted`. A clear effect lifts it. A
|
|
65
|
+
* decision with no effect is not a completed read and is not a completed
|
|
66
|
+
* clear. The source stays the first completed return until the clear.
|
|
67
|
+
*
|
|
68
|
+
* The replay is built into fresh maps and then merged. A session the
|
|
69
|
+
* snapshot has keeps the ledger's source, because that is the first
|
|
70
|
+
* return. A live entry the snapshot does not have stays: it is newer
|
|
71
|
+
* than every clear in the snapshot. A clear is signed only after a
|
|
72
|
+
* replay has succeeded, and a retry runs only while that has not
|
|
73
|
+
* happened yet.
|
|
74
|
+
*/
|
|
75
|
+
export declare function loadTaintFromLedger(opts: {
|
|
76
|
+
ledger: Ledger;
|
|
77
|
+
inputsLog: InputsLog;
|
|
78
|
+
memory: TaintMemory;
|
|
79
|
+
}): Promise<void>;
|
|
80
|
+
export declare function applyTaintClearFile(opts: {
|
|
81
|
+
stateDir: string;
|
|
82
|
+
now: () => number;
|
|
83
|
+
nonce: () => string;
|
|
84
|
+
policyHash: string;
|
|
85
|
+
recordSigner: RecordSigner;
|
|
86
|
+
inputsLog: InputsLog;
|
|
87
|
+
ledger: Ledger;
|
|
88
|
+
memory: TaintMemory;
|
|
89
|
+
}): Promise<void>;
|
|
90
|
+
/** Used by explain, which already holds the inputs document. */
|
|
91
|
+
export declare function taintOnInputs(inputs: DecisionInputs | null): DecisionInputs["sessionTaint"] | undefined;
|
|
92
|
+
export declare function isTaintClearRecord(rec: SignedDecisionRecord): boolean;
|