agent-sanitizer 2.69.0 → 2.70.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 +5 -0
- package/claude-hooks/lib/hook-fault.mjs +21 -0
- package/claude-hooks/lib/hook-io.mjs +19 -1
- package/claude-hooks/lib/invisible-alert.mjs +59 -18
- package/claude-hooks/scan-invisible-chars.mjs +64 -41
- package/claude-hooks/scan-loaded-instructions.mjs +16 -13
- package/package.json +6 -2
- package/types/claude-hooks/lib/hook-fault.d.mts +12 -0
- package/types/claude-hooks/lib/hook-io.d.mts +9 -0
- package/types/claude-hooks/lib/invisible-alert.d.mts +15 -4
package/README.md
CHANGED
|
@@ -258,6 +258,11 @@ To wire them yourself instead, one entry dispatches every mode on `--hook=`:
|
|
|
258
258
|
|
|
259
259
|
`InstructionsLoaded` needs Claude Code **2.1.69 or newer** — 2.1.68 does not emit the event, so the hook never fires however you wire it. Check with `claude --version`.
|
|
260
260
|
|
|
261
|
+
The hooks load `agent-control-plane-core`, a peer dependency
|
|
262
|
+
(`>=0.11.2 <1.0.0`): npm 7+ and pnpm install it for you, but on Yarn classic,
|
|
263
|
+
npm 6, `--legacy-peer-deps` or pnpm with `auto-install-peers=false`, add it to
|
|
264
|
+
your project yourself, or every hook reports it unavailable.
|
|
265
|
+
|
|
261
266
|
`require.resolve("agent-sanitizer/claude-hooks")` gives the path without
|
|
262
267
|
hardcoding a layout. Importing the module rather than spawning it is a no-op.
|
|
263
268
|
|
|
@@ -169,6 +169,27 @@ export function defaultOpen(ctx) {
|
|
|
169
169
|
return { fields: { additionalContext: ctx.openContext } };
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
+
/**
|
|
173
|
+
* The parts of a warning for an event that cannot block (SessionStart,
|
|
174
|
+
* InstructionsLoaded): `text` goes to the operator's stderr, the user
|
|
175
|
+
* (`systemMessage`) and the model (`additionalContext`), and the hook exits 0.
|
|
176
|
+
* Exit 0 because the hook ran and reached its answer. A non-zero exit with no
|
|
177
|
+
* envelope reads to the launcher as a bundle that died, which it reports as
|
|
178
|
+
* "reinstall the plugin".
|
|
179
|
+
* @param {string} event the hook's event name
|
|
180
|
+
* @param {string} text
|
|
181
|
+
* @returns {FaultParts}
|
|
182
|
+
*/
|
|
183
|
+
export function advisoryParts(event, text) {
|
|
184
|
+
return {
|
|
185
|
+
stderr: `${text}\n`,
|
|
186
|
+
envelope: {
|
|
187
|
+
systemMessage: text,
|
|
188
|
+
hookSpecificOutput: { hookEventName: event, additionalContext: text },
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
172
193
|
/**
|
|
173
194
|
* Resolve `hook`'s response to its own failure under the caller's posture. This
|
|
174
195
|
* is the ONLY place {@link failOpenEnabled} is consulted on a hook fault, so the
|
|
@@ -789,7 +789,7 @@ export function resolveStateRoot(env) {
|
|
|
789
789
|
// A relative path resolves against each hook process's cwd, so two processes
|
|
790
790
|
// would disagree on the root, and it could land inside a scanned tree.
|
|
791
791
|
if (!data || !isAbsolute(data)) return tmpdir();
|
|
792
|
-
const root =
|
|
792
|
+
const root = pluginStateRoot(data);
|
|
793
793
|
for (const dir of [data, root]) {
|
|
794
794
|
try {
|
|
795
795
|
mkdirSync(dir, { mode: 0o700 });
|
|
@@ -810,6 +810,24 @@ export function resolveStateRoot(env) {
|
|
|
810
810
|
return root;
|
|
811
811
|
}
|
|
812
812
|
|
|
813
|
+
/** @param {string} data an absolute plugin data dir */
|
|
814
|
+
const pluginStateRoot = (data) => join(data, "session-state");
|
|
815
|
+
|
|
816
|
+
/**
|
|
817
|
+
* Every root a hook process given `env` can resolve STATE_ROOT to, deduplicated.
|
|
818
|
+
* Each process resolves its own root, and one whose check faulted takes $TMPDIR
|
|
819
|
+
* while the rest keep the plugin data dir, so a reader of state another hook
|
|
820
|
+
* wrote looks under each. Unchecked: the reader's own trust checks apply.
|
|
821
|
+
* @param {Record<string, string | undefined>} env
|
|
822
|
+
* @returns {string[]}
|
|
823
|
+
*/
|
|
824
|
+
export function stateRootCandidates(env) {
|
|
825
|
+
const data = env.CLAUDE_PLUGIN_DATA;
|
|
826
|
+
return data && isAbsolute(data)
|
|
827
|
+
? [pluginStateRoot(data), tmpdir()]
|
|
828
|
+
: [tmpdir()];
|
|
829
|
+
}
|
|
830
|
+
|
|
813
831
|
/** {@link resolveStateRoot} for this process, resolved once at load. */
|
|
814
832
|
export const STATE_ROOT = resolveStateRoot(process.env);
|
|
815
833
|
|
|
@@ -36,6 +36,7 @@ import {
|
|
|
36
36
|
PROJECT_HASH,
|
|
37
37
|
scrubUntrustedText,
|
|
38
38
|
STATE_ROOT,
|
|
39
|
+
stateRootCandidates,
|
|
39
40
|
touchLiveState,
|
|
40
41
|
writeFileNoFollow,
|
|
41
42
|
writeSentinelFile,
|
|
@@ -78,6 +79,19 @@ const ALERT_STEM = ".claude-invisible-char-alert-";
|
|
|
78
79
|
*/
|
|
79
80
|
export const ALERT_BASE = join(STATE_ROOT, `${ALERT_STEM}${PROJECT_HASH}`);
|
|
80
81
|
|
|
82
|
+
/**
|
|
83
|
+
* {@link ALERT_BASE} under every root a hook of this session can resolve,
|
|
84
|
+
* this process's own first. A hook that wrote state and the gate that reads it
|
|
85
|
+
* can each have degraded to $TMPDIR while the other kept the plugin data dir.
|
|
86
|
+
*/
|
|
87
|
+
const ALERT_BASES = [
|
|
88
|
+
...new Set(
|
|
89
|
+
[STATE_ROOT, ...stateRootCandidates(process.env)].map((root) =>
|
|
90
|
+
join(root, `${ALERT_STEM}${PROJECT_HASH}`),
|
|
91
|
+
),
|
|
92
|
+
),
|
|
93
|
+
];
|
|
94
|
+
|
|
81
95
|
/**
|
|
82
96
|
* Whether `name` is a session artifact of ANY project: {@link ALERT_BASE}'s
|
|
83
97
|
* name, whatever its project hash, then `.s-`.
|
|
@@ -100,25 +114,27 @@ const isAnyProjectSessionEntry = (name) =>
|
|
|
100
114
|
* files needs neither the clear nor the ordering — past sessions' artifacts
|
|
101
115
|
* simply age out through {@link sweepStaleSessions}.
|
|
102
116
|
* @param {string} [sessionId] the harness's session identity
|
|
117
|
+
* @param {string} [base] the store's root prefix
|
|
103
118
|
* @returns {string}
|
|
104
119
|
*/
|
|
105
|
-
export function sessionPrefix(sessionId) {
|
|
120
|
+
export function sessionPrefix(sessionId, base = ALERT_BASE) {
|
|
106
121
|
// The id becomes a path component, so anything outside this class — a `/` in
|
|
107
122
|
// a hostile session id above all — is folded away rather than escaping
|
|
108
123
|
// STATE_ROOT. A host that exports no session id falls back to one shared name,
|
|
109
124
|
// whose findings and ack are bounded by FALLBACK_TTL_MS instead.
|
|
110
125
|
const key =
|
|
111
126
|
(sessionId ?? "").replace(/[^A-Za-z0-9._-]/gu, "_") || "no-session";
|
|
112
|
-
return `${
|
|
127
|
+
return `${base}.s-${key}`;
|
|
113
128
|
}
|
|
114
129
|
|
|
115
130
|
/**
|
|
116
131
|
* The directory holding this session's alert findings, one file per finding.
|
|
117
132
|
* @param {string} [sessionId]
|
|
133
|
+
* @param {string} [base] the store's root prefix
|
|
118
134
|
* @returns {string}
|
|
119
135
|
*/
|
|
120
|
-
export function alertDir(sessionId) {
|
|
121
|
-
return `${sessionPrefix(sessionId)}.alerts`;
|
|
136
|
+
export function alertDir(sessionId, base = ALERT_BASE) {
|
|
137
|
+
return `${sessionPrefix(sessionId, base)}.alerts`;
|
|
122
138
|
}
|
|
123
139
|
|
|
124
140
|
/**
|
|
@@ -137,10 +153,11 @@ export function alertAckFile(sessionId) {
|
|
|
137
153
|
* Marker the InstructionsLoaded scanner writes on every fire, so another hook
|
|
138
154
|
* can tell whether that event is being scanned at all this session.
|
|
139
155
|
* @param {string} [sessionId]
|
|
156
|
+
* @param {string} [base] the store's root prefix
|
|
140
157
|
* @returns {string}
|
|
141
158
|
*/
|
|
142
|
-
export function instructionsLoadedFile(sessionId) {
|
|
143
|
-
return `${sessionPrefix(sessionId)}.instructions-loaded`;
|
|
159
|
+
export function instructionsLoadedFile(sessionId, base = ALERT_BASE) {
|
|
160
|
+
return `${sessionPrefix(sessionId, base)}.instructions-loaded`;
|
|
144
161
|
}
|
|
145
162
|
|
|
146
163
|
/**
|
|
@@ -161,15 +178,30 @@ export function instructionsLoadedNoticeFile(sessionId) {
|
|
|
161
178
|
* it is written once at launch, so otherwise a session outliving
|
|
162
179
|
* MARKER_TTL_MS would have it swept by another session's SessionStart and read
|
|
163
180
|
* its absence as the event never having fired.
|
|
181
|
+
*
|
|
182
|
+
* A session's marker is also looked for under the other {@link ALERT_BASES},
|
|
183
|
+
* where a scanner that resolved a different STATE_ROOT wrote it. The shared
|
|
184
|
+
* no-session marker is read only under this process's root, which this
|
|
185
|
+
* process's sweep ages out; read elsewhere it could silence the notice for
|
|
186
|
+
* every later session-less run on the machine.
|
|
164
187
|
* @param {string} [sessionId]
|
|
165
188
|
* @returns {boolean}
|
|
166
189
|
*/
|
|
167
190
|
export function instructionsLoadedSeen(sessionId) {
|
|
168
191
|
const marker = instructionsLoadedFile(sessionId);
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
192
|
+
const keyed = sessionPrefix(sessionId) !== sessionPrefix();
|
|
193
|
+
if (markerIsTrusted(marker)) {
|
|
194
|
+
// Not the shared no-session marker: every session-less host would refresh
|
|
195
|
+
// it, and MARKER_TTL_MS is what keeps it from silencing every later session.
|
|
196
|
+
if (keyed) touchLiveState(marker);
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
if (!keyed) return false;
|
|
200
|
+
const elsewhere = ALERT_BASES.slice(1)
|
|
201
|
+
.map((base) => instructionsLoadedFile(sessionId, base))
|
|
202
|
+
.find((path) => markerIsTrusted(path));
|
|
203
|
+
if (elsewhere === undefined) return false;
|
|
204
|
+
touchLiveState(elsewhere);
|
|
173
205
|
return true;
|
|
174
206
|
}
|
|
175
207
|
|
|
@@ -617,28 +649,37 @@ export function recordInstructionsLoadedNotice(sessionId) {
|
|
|
617
649
|
* the one report of an unscanned instruction file unreachable. The ack stays
|
|
618
650
|
* strictly session-keyed, so the gate still asks exactly once per session. A
|
|
619
651
|
* fallback finding is read only while it is inside FALLBACK_TTL_MS, which is
|
|
620
|
-
* what keeps it from re-arming the gate for a later session.
|
|
652
|
+
* what keeps it from re-arming the gate for a later session. Both stores are
|
|
653
|
+
* read under every one of {@link ALERT_BASES}, where a writer that resolved a
|
|
654
|
+
* different STATE_ROOT left its finding.
|
|
621
655
|
* @param {string} [sessionId]
|
|
622
656
|
* @returns {string | null}
|
|
623
657
|
*/
|
|
624
658
|
export function invisibleCharAlert(sessionId) {
|
|
625
|
-
const
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
// fallback
|
|
629
|
-
|
|
659
|
+
const sessionDirs = new Set(
|
|
660
|
+
ALERT_BASES.map((base) => alertDir(sessionId, base)),
|
|
661
|
+
);
|
|
662
|
+
// A fallback store belongs to no session, so only age says whether it is
|
|
663
|
+
// still this session's business. Where the fallback IS the session's store
|
|
664
|
+
// there is nothing inherited and nothing to expire.
|
|
665
|
+
const dirs = new Set(
|
|
666
|
+
ALERT_BASES.flatMap((base) => [
|
|
667
|
+
alertDir(sessionId, base),
|
|
668
|
+
alertDir(undefined, base),
|
|
669
|
+
]),
|
|
670
|
+
);
|
|
630
671
|
const parts = [];
|
|
631
672
|
for (const dir of dirs) {
|
|
632
673
|
if (!privateDirAt(dir)) continue;
|
|
633
674
|
// A live session's findings must outlast any sweep that ages the store by
|
|
634
675
|
// its directory's timestamp: losing them lifts the gate mid-session.
|
|
635
|
-
if (dir
|
|
676
|
+
if (sessionDirs.has(dir)) touchLiveState(dir);
|
|
636
677
|
// Sorted so a multi-finding report reads the same on every call; the names
|
|
637
678
|
// are random, so the order carries no meaning beyond being stable.
|
|
638
679
|
for (const name of readdirSync(dir).sort()) {
|
|
639
680
|
const path = join(dir, name);
|
|
640
681
|
if (!markerIsTrusted(path)) continue;
|
|
641
|
-
if (dir
|
|
682
|
+
if (!sessionDirs.has(dir) && !withinFallbackTtl(path)) continue;
|
|
642
683
|
const text = readFileSync(path, "utf-8").trim();
|
|
643
684
|
if (text !== "") parts.push(text);
|
|
644
685
|
}
|
|
@@ -38,6 +38,7 @@ import {
|
|
|
38
38
|
readStdinJson,
|
|
39
39
|
} from "./lib/hook-io.mjs";
|
|
40
40
|
import {
|
|
41
|
+
advisoryParts,
|
|
41
42
|
registerFaultPolicy,
|
|
42
43
|
hookFaultOutcome,
|
|
43
44
|
writeFaultOutcome,
|
|
@@ -159,28 +160,17 @@ function faultLine(ctx) {
|
|
|
159
160
|
|
|
160
161
|
/**
|
|
161
162
|
* The open-posture parts for instruction files the scan could not READ, or null
|
|
162
|
-
* when the fault is anything else.
|
|
163
|
-
* bare exit 1: the hook ran and is reporting what it could not check, so the
|
|
164
|
-
* user (systemMessage) and the model (additionalContext) are both told, and the
|
|
165
|
-
* launcher forwards it rather than reporting a broken bundle.
|
|
163
|
+
* when the fault is anything else.
|
|
166
164
|
* @param {import("./lib/hook-fault.mjs").FaultContext} ctx
|
|
167
165
|
* @returns {import("./lib/hook-fault.mjs").FaultParts | null}
|
|
168
166
|
*/
|
|
169
167
|
function openGapParts(ctx) {
|
|
170
168
|
if (ctx.gapNotice === undefined) return null;
|
|
171
|
-
|
|
169
|
+
return advisoryParts(
|
|
170
|
+
HookEvent.SESSION_START,
|
|
172
171
|
`${ctx.gapNotice}\n${HOOK_NAME}: failing open — nothing is blocked. Set ` +
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
stderr: `${warning}\n`,
|
|
176
|
-
envelope: {
|
|
177
|
-
systemMessage: warning,
|
|
178
|
-
hookSpecificOutput: {
|
|
179
|
-
hookEventName: HookEvent.SESSION_START,
|
|
180
|
-
additionalContext: warning,
|
|
181
|
-
},
|
|
182
|
-
},
|
|
183
|
-
};
|
|
172
|
+
"AGENT_SANITIZER_FAIL_OPEN=0 to have the next tool call ask instead.",
|
|
173
|
+
);
|
|
184
174
|
}
|
|
185
175
|
|
|
186
176
|
// This hook's entry in the one posture table (lib/hook-fault.mjs). SessionStart
|
|
@@ -193,18 +183,20 @@ registerFaultPolicy(HOOK_NAME, {
|
|
|
193
183
|
event: HookEvent.SESSION_START,
|
|
194
184
|
guarded: "instruction files",
|
|
195
185
|
open: (ctx) =>
|
|
196
|
-
openGapParts(ctx) ??
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
}
|
|
207
|
-
|
|
186
|
+
openGapParts(ctx) ??
|
|
187
|
+
advisoryParts(
|
|
188
|
+
HookEvent.SESSION_START,
|
|
189
|
+
`${faultLine(ctx)} Passing through unguarded; set AGENT_SANITIZER_FAIL_OPEN=0 to arm the tool-call gate instead.`,
|
|
190
|
+
),
|
|
191
|
+
closed: (ctx) => ({
|
|
192
|
+
...advisoryParts(
|
|
193
|
+
HookEvent.SESSION_START,
|
|
194
|
+
ctx.gapNotice === undefined
|
|
195
|
+
? `${faultLine(ctx)} Arming the tool-call gate (AGENT_SANITIZER_FAIL_OPEN=0).`
|
|
196
|
+
: `${ctx.gapNotice}\n${HOOK_NAME}: the next tool call asks first (AGENT_SANITIZER_FAIL_OPEN=0).`,
|
|
197
|
+
),
|
|
198
|
+
armAlert: true,
|
|
199
|
+
}),
|
|
208
200
|
});
|
|
209
201
|
|
|
210
202
|
/**
|
|
@@ -502,23 +494,14 @@ async function runScanCli({ trace: sink, scan: runScan, sessionId }, out) {
|
|
|
502
494
|
),
|
|
503
495
|
);
|
|
504
496
|
persistAlert(alertParts, sessionId);
|
|
505
|
-
// process.exit skips cliMain's `finally`, which would otherwise flush.
|
|
497
|
+
// process.exit skips cliMain's `finally`, which would otherwise flush. No
|
|
498
|
+
// code: the exit is the one reportFault set from the posture table.
|
|
506
499
|
out.flush();
|
|
507
|
-
process.exit(
|
|
500
|
+
process.exit();
|
|
508
501
|
}
|
|
509
502
|
/* c8 ignore stop */
|
|
510
503
|
|
|
511
|
-
|
|
512
|
-
// construction and cannot inherit a previous session's ack. Age out what past
|
|
513
|
-
// sessions left instead — SessionStart is the once-per-session touchpoint the
|
|
514
|
-
// sweep belongs on.
|
|
515
|
-
sweepStaleSessions(sessionId);
|
|
516
|
-
// The other two stores these hooks own (reveal under STATE_ROOT, confirm in
|
|
517
|
-
// $TMPDIR). Both are content- or fingerprint-addressed with no natural owner
|
|
518
|
-
// to delete them, so without a sweep they grow for the life of the machine;
|
|
519
|
-
// SessionStart is the only touchpoint that runs once per session.
|
|
520
|
-
sweepStaleReveals();
|
|
521
|
-
sweepStaleConfirms();
|
|
504
|
+
sweepStaleStores(sessionId);
|
|
522
505
|
|
|
523
506
|
// Only a non-errno throw reaches here — a bug in the scanner, not a file it
|
|
524
507
|
// could not read (those are accounted for in `skipped`). It is a fault of THIS
|
|
@@ -571,6 +554,46 @@ async function runScanCli({ trace: sink, scan: runScan, sessionId }, out) {
|
|
|
571
554
|
persistAlert(alertParts, sessionId);
|
|
572
555
|
}
|
|
573
556
|
|
|
557
|
+
/**
|
|
558
|
+
* Age out what past sessions left in the three stores these hooks own: the
|
|
559
|
+
* session-keyed alert store, and the reveal and confirm stores, which are
|
|
560
|
+
* content- or fingerprint-addressed with no natural owner to delete them.
|
|
561
|
+
* SessionStart is the one touchpoint that runs once per session. The alert
|
|
562
|
+
* store needs no clear: it is session-keyed, so this session starts empty and
|
|
563
|
+
* cannot inherit a past session's ack.
|
|
564
|
+
*
|
|
565
|
+
* An I/O error here is reported and the scan still runs. A sweep that fails
|
|
566
|
+
* leaves stale files behind and costs no coverage, but a throw would abort the
|
|
567
|
+
* scan before the posture table sees it: the bundle dies, and under
|
|
568
|
+
* AGENT_SANITIZER_FAIL_OPEN=0 nothing arms the gate. A non-errno throw is a bug
|
|
569
|
+
* in the sweep and propagates.
|
|
570
|
+
* @param {string} [sessionId]
|
|
571
|
+
* @returns {void}
|
|
572
|
+
*/
|
|
573
|
+
function sweepStaleStores(sessionId) {
|
|
574
|
+
/** @type {string[]} */
|
|
575
|
+
const failed = [];
|
|
576
|
+
for (const sweep of [
|
|
577
|
+
() => sweepStaleSessions(sessionId),
|
|
578
|
+
sweepStaleReveals,
|
|
579
|
+
sweepStaleConfirms,
|
|
580
|
+
]) {
|
|
581
|
+
try {
|
|
582
|
+
sweep();
|
|
583
|
+
} catch (err) {
|
|
584
|
+
const code = /** @type {NodeJS.ErrnoException} */ (err).code;
|
|
585
|
+
if (typeof code !== "string") throw err;
|
|
586
|
+
failed.push(code);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
if (failed.length === 0) return;
|
|
590
|
+
// errno codes only: the error messages embed paths under a shared $TMPDIR.
|
|
591
|
+
const codes = [...new Set(failed)].join(", ");
|
|
592
|
+
process.stderr.write(
|
|
593
|
+
`${HOOK_NAME}: could not age out stale session state (${codes}); scanning anyway.\n`,
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
|
|
574
597
|
/**
|
|
575
598
|
* Auto-clean the contaminated files so the session proceeds without blocking
|
|
576
599
|
* every tool call, and return the alert text for whatever could not be cleaned
|
|
@@ -31,6 +31,7 @@ import {
|
|
|
31
31
|
setupRunning,
|
|
32
32
|
} from "./lib/hook-io.mjs";
|
|
33
33
|
import {
|
|
34
|
+
advisoryParts,
|
|
34
35
|
registerFaultPolicy,
|
|
35
36
|
hookFaultOutcome,
|
|
36
37
|
writeFaultOutcome,
|
|
@@ -104,18 +105,17 @@ function faultLine(ctx) {
|
|
|
104
105
|
*/
|
|
105
106
|
function emptyPayloadParts(ctx) {
|
|
106
107
|
if (!(ctx.err instanceof EmptyStdinError)) return null;
|
|
107
|
-
return
|
|
108
|
-
|
|
109
|
-
|
|
108
|
+
return advisoryParts(
|
|
109
|
+
HookEvent.INSTRUCTIONS_LOADED,
|
|
110
|
+
`${HOOK_NAME} hook error: ${ctx.message}. No instruction file was named, so nothing ` +
|
|
110
111
|
"was scanned and nothing was left unguarded. Check how this hook is invoked: it reads " +
|
|
111
112
|
"its InstructionsLoaded event as JSON on stdin, and one wired to a channel that " +
|
|
112
|
-
"delivers nothing scans nothing for the whole session
|
|
113
|
-
|
|
114
|
-
};
|
|
113
|
+
"delivers nothing scans nothing for the whole session.",
|
|
114
|
+
);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
// This hook's entry in the one posture table (lib/hook-fault.mjs). Like
|
|
118
|
-
// scan-invisible-chars it has no
|
|
118
|
+
// scan-invisible-chars it has no verdict to give — InstructionsLoaded
|
|
119
119
|
// cannot block, and its exit code is ignored — so both arms are stated
|
|
120
120
|
// explicitly and the only enforcement either can reach is the cross-hook alert,
|
|
121
121
|
// which makes the PreToolUse gate ask once on the next tool call.
|
|
@@ -123,14 +123,17 @@ registerFaultPolicy(HOOK_NAME, {
|
|
|
123
123
|
event: HookEvent.INSTRUCTIONS_LOADED,
|
|
124
124
|
guarded: "a loaded instruction file",
|
|
125
125
|
open: (ctx) =>
|
|
126
|
-
emptyPayloadParts(ctx) ??
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
126
|
+
emptyPayloadParts(ctx) ??
|
|
127
|
+
advisoryParts(
|
|
128
|
+
HookEvent.INSTRUCTIONS_LOADED,
|
|
129
|
+
`${faultLine(ctx)} Passing through unguarded; set AGENT_SANITIZER_FAIL_OPEN=0 to arm the tool-call gate instead.`,
|
|
130
|
+
),
|
|
130
131
|
closed: (ctx) =>
|
|
131
132
|
emptyPayloadParts(ctx) ?? {
|
|
132
|
-
|
|
133
|
-
|
|
133
|
+
...advisoryParts(
|
|
134
|
+
HookEvent.INSTRUCTIONS_LOADED,
|
|
135
|
+
`${faultLine(ctx)} Arming the tool-call gate (AGENT_SANITIZER_FAIL_OPEN=0).`,
|
|
136
|
+
),
|
|
134
137
|
armAlert: true,
|
|
135
138
|
},
|
|
136
139
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agent-sanitizer",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.70.0",
|
|
4
4
|
"description": "Defend an agent against hidden-content injection: strip payload-capable invisible Unicode and ANSI, splice out human-invisible HTML, and flag data-exfil URLs in untrusted text before any model sees it.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
"@unicode/unicode-17.0.0": "2.0.7",
|
|
56
56
|
"acorn": "^8.18.0",
|
|
57
57
|
"acorn-walk": "^8.3.5",
|
|
58
|
+
"agent-control-plane-core": "0.12.0",
|
|
58
59
|
"agent-sanitizer": "link:.",
|
|
59
60
|
"bun": "1.3.11",
|
|
60
61
|
"c8": "11.0.0",
|
|
@@ -71,6 +72,7 @@
|
|
|
71
72
|
"recheck": "4.5.0",
|
|
72
73
|
"rehype-parse": "9.0.1",
|
|
73
74
|
"remark-gfm": "4.0.1",
|
|
75
|
+
"semver": "7.8.6",
|
|
74
76
|
"smol-toml": "^1.7.1",
|
|
75
77
|
"typescript": "6.0.3",
|
|
76
78
|
"typescript-eslint": "8.61.0",
|
|
@@ -227,7 +229,6 @@
|
|
|
227
229
|
"SECURITY.md"
|
|
228
230
|
],
|
|
229
231
|
"dependencies": {
|
|
230
|
-
"agent-control-plane-core": "0.11.2",
|
|
231
232
|
"css-tree": "^3.2.1",
|
|
232
233
|
"hast-util-from-parse5": "8.0.3",
|
|
233
234
|
"mdast-util-gfm": "3.1.0",
|
|
@@ -245,6 +246,9 @@
|
|
|
245
246
|
"unist-util-visit": "5.1.0",
|
|
246
247
|
"vfile": "6.0.3"
|
|
247
248
|
},
|
|
249
|
+
"peerDependencies": {
|
|
250
|
+
"agent-control-plane-core": ">=0.11.2 <1.0.0"
|
|
251
|
+
},
|
|
248
252
|
"scripts": {
|
|
249
253
|
"test": "node scripts/coverage.mjs",
|
|
250
254
|
"coverage": "node scripts/coverage.mjs",
|
|
@@ -29,6 +29,18 @@ export function faultPolicy(hook: string): FaultPolicy;
|
|
|
29
29
|
* @returns {FaultParts}
|
|
30
30
|
*/
|
|
31
31
|
export function defaultOpen(ctx: FaultContext): FaultParts;
|
|
32
|
+
/**
|
|
33
|
+
* The parts of a warning for an event that cannot block (SessionStart,
|
|
34
|
+
* InstructionsLoaded): `text` goes to the operator's stderr, the user
|
|
35
|
+
* (`systemMessage`) and the model (`additionalContext`), and the hook exits 0.
|
|
36
|
+
* Exit 0 because the hook ran and reached its answer. A non-zero exit with no
|
|
37
|
+
* envelope reads to the launcher as a bundle that died, which it reports as
|
|
38
|
+
* "reinstall the plugin".
|
|
39
|
+
* @param {string} event the hook's event name
|
|
40
|
+
* @param {string} text
|
|
41
|
+
* @returns {FaultParts}
|
|
42
|
+
*/
|
|
43
|
+
export function advisoryParts(event: string, text: string): FaultParts;
|
|
32
44
|
/**
|
|
33
45
|
* Resolve `hook`'s response to its own failure under the caller's posture. This
|
|
34
46
|
* is the ONLY place {@link failOpenEnabled} is consulted on a hook fault, so the
|
|
@@ -326,6 +326,15 @@ export function createEnvelopeBuffer(write?: (chunk: string) => void): EnvelopeB
|
|
|
326
326
|
* @returns {string}
|
|
327
327
|
*/
|
|
328
328
|
export function resolveStateRoot(env: Record<string, string | undefined>): string;
|
|
329
|
+
/**
|
|
330
|
+
* Every root a hook process given `env` can resolve STATE_ROOT to, deduplicated.
|
|
331
|
+
* Each process resolves its own root, and one whose check faulted takes $TMPDIR
|
|
332
|
+
* while the rest keep the plugin data dir, so a reader of state another hook
|
|
333
|
+
* wrote looks under each. Unchecked: the reader's own trust checks apply.
|
|
334
|
+
* @param {Record<string, string | undefined>} env
|
|
335
|
+
* @returns {string[]}
|
|
336
|
+
*/
|
|
337
|
+
export function stateRootCandidates(env: Record<string, string | undefined>): string[];
|
|
329
338
|
/**
|
|
330
339
|
* Adopt a host's own cold-start marker path in place of the derived one, so a
|
|
331
340
|
* host whose setup script already writes a marker under its own convention can
|
|
@@ -10,15 +10,17 @@
|
|
|
10
10
|
* files needs neither the clear nor the ordering — past sessions' artifacts
|
|
11
11
|
* simply age out through {@link sweepStaleSessions}.
|
|
12
12
|
* @param {string} [sessionId] the harness's session identity
|
|
13
|
+
* @param {string} [base] the store's root prefix
|
|
13
14
|
* @returns {string}
|
|
14
15
|
*/
|
|
15
|
-
export function sessionPrefix(sessionId?: string): string;
|
|
16
|
+
export function sessionPrefix(sessionId?: string, base?: string): string;
|
|
16
17
|
/**
|
|
17
18
|
* The directory holding this session's alert findings, one file per finding.
|
|
18
19
|
* @param {string} [sessionId]
|
|
20
|
+
* @param {string} [base] the store's root prefix
|
|
19
21
|
* @returns {string}
|
|
20
22
|
*/
|
|
21
|
-
export function alertDir(sessionId?: string): string;
|
|
23
|
+
export function alertDir(sessionId?: string, base?: string): string;
|
|
22
24
|
/**
|
|
23
25
|
* Companion marker the PreToolUse gate writes once it has surfaced the alert
|
|
24
26
|
* this session, so the gate asks ONCE then degrades to a passive reminder
|
|
@@ -32,9 +34,10 @@ export function alertAckFile(sessionId?: string): string;
|
|
|
32
34
|
* Marker the InstructionsLoaded scanner writes on every fire, so another hook
|
|
33
35
|
* can tell whether that event is being scanned at all this session.
|
|
34
36
|
* @param {string} [sessionId]
|
|
37
|
+
* @param {string} [base] the store's root prefix
|
|
35
38
|
* @returns {string}
|
|
36
39
|
*/
|
|
37
|
-
export function instructionsLoadedFile(sessionId?: string): string;
|
|
40
|
+
export function instructionsLoadedFile(sessionId?: string, base?: string): string;
|
|
38
41
|
/**
|
|
39
42
|
* Companion marker: the notice below has been surfaced this session.
|
|
40
43
|
* @param {string} [sessionId]
|
|
@@ -50,6 +53,12 @@ export function instructionsLoadedNoticeFile(sessionId?: string): string;
|
|
|
50
53
|
* it is written once at launch, so otherwise a session outliving
|
|
51
54
|
* MARKER_TTL_MS would have it swept by another session's SessionStart and read
|
|
52
55
|
* its absence as the event never having fired.
|
|
56
|
+
*
|
|
57
|
+
* A session's marker is also looked for under the other {@link ALERT_BASES},
|
|
58
|
+
* where a scanner that resolved a different STATE_ROOT wrote it. The shared
|
|
59
|
+
* no-session marker is read only under this process's root, which this
|
|
60
|
+
* process's sweep ages out; read elsewhere it could silence the notice for
|
|
61
|
+
* every later session-less run on the machine.
|
|
53
62
|
* @param {string} [sessionId]
|
|
54
63
|
* @returns {boolean}
|
|
55
64
|
*/
|
|
@@ -155,7 +164,9 @@ export function recordInstructionsLoadedNotice(sessionId?: string): void;
|
|
|
155
164
|
* the one report of an unscanned instruction file unreachable. The ack stays
|
|
156
165
|
* strictly session-keyed, so the gate still asks exactly once per session. A
|
|
157
166
|
* fallback finding is read only while it is inside FALLBACK_TTL_MS, which is
|
|
158
|
-
* what keeps it from re-arming the gate for a later session.
|
|
167
|
+
* what keeps it from re-arming the gate for a later session. Both stores are
|
|
168
|
+
* read under every one of {@link ALERT_BASES}, where a writer that resolved a
|
|
169
|
+
* different STATE_ROOT left its finding.
|
|
159
170
|
* @param {string} [sessionId]
|
|
160
171
|
* @returns {string | null}
|
|
161
172
|
*/
|