rungate 0.7.0 → 0.8.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/hooks/ActionVerificationGate.hook.ts +96 -0
- package/hooks/AgentBriefGuard.hook.ts +38 -0
- package/hooks/AgentVerdictCapture.hook.ts +87 -0
- package/hooks/AutoVerifyGate.hook.ts +79 -0
- package/hooks/BashToolGuard.hook.ts +65 -0
- package/hooks/BashWriteGuard.hook.ts +60 -0
- package/hooks/CommitEnforcement.hook.ts +130 -0
- package/hooks/DocHygiene.hook.ts +148 -0
- package/hooks/GateEnforcement.hook.ts +58 -0
- package/hooks/GateResultProtection.hook.ts +69 -0
- package/hooks/HarnessAFKGate.hook.ts +85 -0
- package/hooks/HookCurrency.hook.ts +99 -0
- package/hooks/IssueCloseGuard.hook.ts +149 -0
- package/hooks/MergeGuard.hook.ts +58 -0
- package/hooks/PostCompact.hook.ts +60 -0
- package/hooks/ProjectStateSync.hook.ts +103 -0
- package/hooks/SkillGuard.hook.ts +69 -0
- package/hooks/SpecConformityTrigger.hook.ts +43 -0
- package/hooks/SpecSCGuard.hook.ts +123 -0
- package/hooks/StaleTTLCleanup.hook.ts +141 -0
- package/hooks/TaskCompleted.hook.ts +51 -0
- package/hooks/TeamInit.hook.ts +85 -0
- package/hooks/TestSuiteGuard.hook.ts +70 -0
- package/hooks/TestSuiteRelease.hook.ts +85 -0
- package/hooks/TopLevelCdGuard.hook.ts +108 -0
- package/hooks/VerifyPhaseLock.hook.ts +84 -0
- package/hooks/WorkflowStateGuard.hook.ts +40 -0
- package/hooks/lib/agent-config.json +9 -0
- package/hooks/lib/agentDetection.ts +201 -0
- package/hooks/lib/comment-template.ts +86 -0
- package/hooks/lib/findWorkflow.ts +101 -0
- package/hooks/lib/parseStdin.ts +37 -0
- package/hooks/lib/paths.ts +111 -0
- package/hooks/lib/utils.ts +338 -0
- package/lib/affected-tests.ts +29 -0
- package/lib/bash-write-guard.ts +373 -0
- package/lib/branch-cleanup.ts +279 -7
- package/lib/config-trackable.ts +9 -0
- package/lib/distribution-currency.ts +55 -5
- package/lib/failure-intake.ts +7 -1
- package/lib/gate-enforcement.ts +222 -28
- package/lib/gate-names.ts +72 -0
- package/lib/gate-result-protection.ts +427 -0
- package/lib/intake-sources.ts +380 -0
- package/lib/late-env-spawn.ts +331 -0
- package/lib/launch-base.ts +128 -5
- package/lib/paths.ts +96 -3
- package/lib/phase2-decomposition.ts +1 -1
- package/lib/published-artifact.ts +162 -18
- package/lib/sc-id-cross-tree.ts +84 -8
- package/lib/scaffold/ai-review-findings.ts +7 -1
- package/lib/scaffold/ai-review-step.ts +861 -116
- package/lib/scaffold/defaults.ts +82 -1
- package/lib/scaffold/steps.ts +436 -44
- package/lib/security-verdict.ts +1 -1
- package/lib/shell-path-interpolation.ts +594 -0
- package/lib/text-dependents.ts +875 -0
- package/lib/workflow-security.ts +29 -0
- package/lib/workflow-taint.ts +732 -0
- package/lib/worktree-cleanup.ts +65 -38
- package/lib/worktree-repo.ts +32 -2
- package/package.json +3 -1
- package/scripts/check-installed-hook-currency.ts +825 -0
package/lib/affected-tests.ts
CHANGED
|
@@ -44,6 +44,35 @@ export const SELECTABLE_SOURCE = /\.(ts|tsx|js|jsx|mts|cts)$/;
|
|
|
44
44
|
/** A test file, by the convention this repo uses everywhere. */
|
|
45
45
|
export const TEST_FILE = /\.test\.(ts|tsx)$/;
|
|
46
46
|
|
|
47
|
+
/**
|
|
48
|
+
* The shape a path is allowed to have before it may be PRINTED as part of a
|
|
49
|
+
* `bun test …` selection.
|
|
50
|
+
*
|
|
51
|
+
* Every selector here writes its answer to stdout and a caller interpolates
|
|
52
|
+
* it into a shell command unquoted, because word-splitting is how a list
|
|
53
|
+
* becomes arguments. Rather than trust that a tree walk produced inert
|
|
54
|
+
* paths, anything outside this alphabet refuses the whole selection. The
|
|
55
|
+
* injection surface is removed by construction rather than by assuming the
|
|
56
|
+
* walk is well-behaved.
|
|
57
|
+
*
|
|
58
|
+
* **It lives here, next to `TEST_FILE`, because there is now more than one
|
|
59
|
+
* producer.** It was a `const` inside `scripts/select-affected-tests.ts`
|
|
60
|
+
* until `scripts/text-dependents.ts` (#1054) began emitting into the same
|
|
61
|
+
* composition without it — a guard that one of two producers applies is a
|
|
62
|
+
* guard with a hole, and the hole is invisible from either file. Copying
|
|
63
|
+
* the pattern into the second producer would have made it two deciders that
|
|
64
|
+
* agree, which `.claude/rules/checks-must-be-able-to-fail.md` names as a
|
|
65
|
+
* shape where mutating either leaves the other holding the case up. One
|
|
66
|
+
* binding, both importers, and `test/selection-path-safety.test.ts` fails
|
|
67
|
+
* if a third producer declares its own.
|
|
68
|
+
*
|
|
69
|
+
* The shape of the defect is worth naming, because it is the shape of the
|
|
70
|
+
* issue that introduced it: #1054 is about a dependency expressed in a
|
|
71
|
+
* syntax the checker did not know, and this was the same thing one level
|
|
72
|
+
* up — a validation expressed in one of two producers.
|
|
73
|
+
*/
|
|
74
|
+
export const SAFE_TEST_PATH = /^[A-Za-z0-9._/-]+\.test\.tsx?$/;
|
|
75
|
+
|
|
47
76
|
/**
|
|
48
77
|
* Directories walked when building the graph.
|
|
49
78
|
*
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The decision behind `hooks/BashWriteGuard.hook.ts` (#473 on
|
|
3
|
+
* `hornjason/pai-config`, tuned there in #474, reworked in #1120).
|
|
4
|
+
*
|
|
5
|
+
* Extracted from the hook so the population it refuses can be MEASURED
|
|
6
|
+
* rather than argued about: 5,060 real commands from this machine's
|
|
7
|
+
* transcripts were run through it, which is not something you can do with a
|
|
8
|
+
* decision that only exists inside a subprocess. Same shape as
|
|
9
|
+
* `lib/gate-result-protection.ts`, which #1137 extracted for the same reason.
|
|
10
|
+
*
|
|
11
|
+
* ## The thing this module exists to get right
|
|
12
|
+
*
|
|
13
|
+
* One mechanism was failing in BOTH directions at once.
|
|
14
|
+
*
|
|
15
|
+
* It UNDER-blocked, because a read-only prefix allowlist was consulted before
|
|
16
|
+
* anything looked at what the command wrote, so `gh --version; echo x >
|
|
17
|
+
* .gate-salt` exited clean. A prefix says nothing about what follows a `;`.
|
|
18
|
+
*
|
|
19
|
+
* It OVER-blocked, because the remaining checks matched protected names
|
|
20
|
+
* anywhere in the command STRING, so a read-only `git show … | grep -nE
|
|
21
|
+
* "chmod|chflags"` was refused for quoting the words it was searching for.
|
|
22
|
+
* That happened four times in real work — twice to the session coordinating
|
|
23
|
+
* this fix, twice to a peer staging a comparison copy — and a guard that
|
|
24
|
+
* makes reading its own source awkward is a guard people route around.
|
|
25
|
+
*
|
|
26
|
+
* Both directions are the same root cause: a substring test over a whole
|
|
27
|
+
* command line cannot tell CODE from DATA. So nothing here tests the whole
|
|
28
|
+
* string. The command is split into segments at top-level separators, and
|
|
29
|
+
* each check asks a structural question of a segment:
|
|
30
|
+
*
|
|
31
|
+
* - what does this segment WRITE? → redirects, tee, cp/mv
|
|
32
|
+
* - what does this segment MUTATE in place? → sed -i, dd of=, rm, …
|
|
33
|
+
* - is this segment OPAQUE? → an interpreter body, find -exec
|
|
34
|
+
* - is this segment's COMMAND WORD chmod? → not "does it contain chmod"
|
|
35
|
+
*
|
|
36
|
+
* Quotes are handled two different ways on purpose, and the distinction is
|
|
37
|
+
* the whole fix:
|
|
38
|
+
*
|
|
39
|
+
* `stripQuoted` deletes quoted spans INCLUDING their contents. Used to
|
|
40
|
+
* find a VERB, because text inside quotes is an argument,
|
|
41
|
+
* never the command the shell runs. This is what stops a
|
|
42
|
+
* grep PATTERN from reading as a chmod invocation.
|
|
43
|
+
* `dequote` removes quote characters and KEEPS the contents. Used to
|
|
44
|
+
* find a TARGET, because `chmod 777 "x.hook.ts"` really does
|
|
45
|
+
* name a hook file.
|
|
46
|
+
*
|
|
47
|
+
* Using `stripQuoted` for both would let an attacker quote the path; using
|
|
48
|
+
* `dequote` for both reinstates all four false positives. Each test below
|
|
49
|
+
* names which one it uses and why.
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
/** Gate artifacts. A write to any of these must come from the gate runner. */
|
|
53
|
+
export const PROTECTED_FILES = ['workflow-state.json', '.gate-salt', '/witnesses/'];
|
|
54
|
+
|
|
55
|
+
/** Hook sources. Integrity matters because hooks are what enforce everything else. */
|
|
56
|
+
export const PROTECTED_HOOKS = ['.hook.ts', '.hook.sh'];
|
|
57
|
+
|
|
58
|
+
export interface BashWriteDecision {
|
|
59
|
+
blocked: boolean;
|
|
60
|
+
reason?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const ALLOW: BashWriteDecision = { blocked: false };
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Split a command at top-level `;`, `&&`, `||`, `|` and newline, leaving
|
|
67
|
+
* separators inside quotes alone.
|
|
68
|
+
*
|
|
69
|
+
* Deliberately NOT a shell parser. It does not expand anything, does not
|
|
70
|
+
* track `$(…)` nesting and does not care about redirection grammar. It
|
|
71
|
+
* answers one question — where might a new command begin — and the failure
|
|
72
|
+
* mode of getting that slightly wrong is MORE segments, each of which is
|
|
73
|
+
* then inspected. Over-splitting costs precision in the allow direction and
|
|
74
|
+
* never opens a hole, which is the direction a guard should err in.
|
|
75
|
+
*/
|
|
76
|
+
export function segments(cmd: string): string[] {
|
|
77
|
+
// Heredoc bodies go FIRST, before anything looks for a separator. A
|
|
78
|
+
// heredoc is not a quote, so a `|` or `;` inside an issue body, a commit
|
|
79
|
+
// message or a PR description used to tear the document into fragments
|
|
80
|
+
// and each fragment was then read as a command — which is how a sentence
|
|
81
|
+
// about `workflow-state.json` came to be refused as a write to it.
|
|
82
|
+
cmd = stripHeredocs(cmd);
|
|
83
|
+
const out: string[] = [];
|
|
84
|
+
let cur = '';
|
|
85
|
+
let quote: string | null = null;
|
|
86
|
+
for (let i = 0; i < cmd.length; i++) {
|
|
87
|
+
const c = cmd[i];
|
|
88
|
+
if (quote) {
|
|
89
|
+
cur += c;
|
|
90
|
+
if (c === quote && cmd[i - 1] !== '\\') quote = null;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (c === '"' || c === "'") {
|
|
94
|
+
quote = c;
|
|
95
|
+
cur += c;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (c === ';' || c === '\n' || c === '|' || c === '&') {
|
|
99
|
+
if (cmd[i + 1] === c) i++; // `&&`, `||` are one separator, not two
|
|
100
|
+
out.push(cur);
|
|
101
|
+
cur = '';
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
cur += c;
|
|
105
|
+
}
|
|
106
|
+
out.push(cur);
|
|
107
|
+
return out.map(s => s.trim()).filter(Boolean);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Delete heredoc bodies, then quoted spans and their contents. Use when
|
|
112
|
+
* looking for a VERB.
|
|
113
|
+
*
|
|
114
|
+
* A heredoc body is the single largest source of over-blocking measured on
|
|
115
|
+
* this machine's history: an issue body, a commit message or a PR
|
|
116
|
+
* description written with `cat > f <<'EOF'` routinely QUOTES a protected
|
|
117
|
+
* filename, a `>` redirect or the word `chflags`, because the thing being
|
|
118
|
+
* written about is this guard. 1,860 real commands were newly refused before
|
|
119
|
+
* this stripping existed and the large majority were that shape.
|
|
120
|
+
*/
|
|
121
|
+
export function stripQuoted(s: string): string {
|
|
122
|
+
return stripHeredocs(s).replace(/"(?:[^"\\]|\\.)*"|'[^']*'/g, ' ');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Delete heredoc bodies only, keeping ordinary quoting intact. Separate from
|
|
127
|
+
* `stripQuoted` because a quoted PATH is a real argument while a heredoc
|
|
128
|
+
* body never is — it is the document being written, not a word of the
|
|
129
|
+
* command writing it.
|
|
130
|
+
*/
|
|
131
|
+
export function stripHeredocs(s: string): string {
|
|
132
|
+
return s.replace(/<<-?\s*(['"]?)([A-Za-z_][A-Za-z0-9_]*)\1[\s\S]*?^\s*\2\s*$/gm, ' ');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Remove quote characters, keep the contents. Use when looking for a TARGET. */
|
|
136
|
+
export function dequote(s: string): string {
|
|
137
|
+
return s.replace(/['"]/g, '');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Wrappers that delegate to the real command word rather than being one. */
|
|
141
|
+
const WRAPPERS = /^(?:sudo|env|nohup|time|command|xargs)$/;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The command word of a segment — the first token that is not a wrapper, an
|
|
145
|
+
* assignment or an option. Read off `stripQuoted`, because a quoted token is
|
|
146
|
+
* an argument.
|
|
147
|
+
*/
|
|
148
|
+
export function commandWord(segment: string): string {
|
|
149
|
+
for (const raw of stripQuoted(segment).trim().split(/\s+/)) {
|
|
150
|
+
const tok = raw.replace(/^.*\//, ''); // `/usr/bin/python3` -> `python3`
|
|
151
|
+
if (!tok || tok.includes('=') || tok.startsWith('-')) continue;
|
|
152
|
+
if (WRAPPERS.test(tok)) continue;
|
|
153
|
+
return tok;
|
|
154
|
+
}
|
|
155
|
+
return '';
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Paths a segment writes through shell redirection or a copying verb.
|
|
160
|
+
*
|
|
161
|
+
* Redirections are read off the heredoc-stripped text, because a `>` inside
|
|
162
|
+
* a heredoc body is prose about a redirect, not one. The TARGET itself is
|
|
163
|
+
* then dequoted, because `> "x/.gate-salt"` writes the same file as `>
|
|
164
|
+
* x/.gate-salt`.
|
|
165
|
+
*/
|
|
166
|
+
export function writeTargets(segment: string): string[] {
|
|
167
|
+
const targets: string[] = [];
|
|
168
|
+
const code = stripQuoted(segment);
|
|
169
|
+
// `> file` and `>> file`, but not `2>` or `&>` — those are stderr plumbing.
|
|
170
|
+
for (const m of code.matchAll(/(?<![2&])\s*>{1,2}\s*(\S+)/g)) {
|
|
171
|
+
if (m[1] && m[1] !== '/dev/null') targets.push(dequote(m[1]));
|
|
172
|
+
}
|
|
173
|
+
// A quoted redirect target survives quote-stripping as nothing at all, so
|
|
174
|
+
// it is recovered separately rather than lost.
|
|
175
|
+
for (const m of segment.matchAll(/(?<![2&])\s*>{1,2}\s*(['"])([^'"]+)\1/g)) targets.push(m[2]);
|
|
176
|
+
const tee = code.match(/\btee\s+(?:-a\s+)?(\S+)/);
|
|
177
|
+
if (tee) targets.push(dequote(tee[1]));
|
|
178
|
+
// `cp`/`mv` take the LAST non-option argument as the destination. Reading
|
|
179
|
+
// the second token instead — which this did until #1120 — mis-parses every
|
|
180
|
+
// invocation carrying a flag: `cp -p src /x/workflow-state.json` read the
|
|
181
|
+
// destination as `src` and never examined the real one.
|
|
182
|
+
//
|
|
183
|
+
// Arguments are DEQUOTED rather than stripped. Stripping them was measured
|
|
184
|
+
// to lose the destination entirely for `cp "a/X.hook.ts" "b/X.hook.ts"`,
|
|
185
|
+
// which is a real hook write that then read as having no target.
|
|
186
|
+
const word = commandWord(segment);
|
|
187
|
+
if (word === 'cp' || word === 'mv' || word === 'install' || word === 'rsync') {
|
|
188
|
+
const args = dequote(segment)
|
|
189
|
+
.trim()
|
|
190
|
+
.split(/\s+/)
|
|
191
|
+
.slice(1)
|
|
192
|
+
.filter(a => !a.startsWith('-'));
|
|
193
|
+
if (args.length >= 2) targets.push(args[args.length - 1]);
|
|
194
|
+
}
|
|
195
|
+
return targets;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Non-option arguments of a segment, dequoted. This is what an in-place verb
|
|
200
|
+
* actually operates on — as opposed to its script, which is an argument too
|
|
201
|
+
* but names files only as text.
|
|
202
|
+
*/
|
|
203
|
+
export function argumentsOf(segment: string): string[] {
|
|
204
|
+
const out: string[] = [];
|
|
205
|
+
// Tokens that survive heredoc and quote stripping are bare words; quoted
|
|
206
|
+
// tokens are recovered individually so a quoted PATH still counts while a
|
|
207
|
+
// quoted sed SCRIPT does not reach the protected-name test as a path.
|
|
208
|
+
for (const t of stripQuoted(segment).trim().split(/\s+/)) {
|
|
209
|
+
if (t && !t.startsWith('-')) out.push(t);
|
|
210
|
+
}
|
|
211
|
+
// Heredoc bodies are stripped FIRST: a quoted path inside the document
|
|
212
|
+
// being written is not an argument to the command writing it.
|
|
213
|
+
for (const m of stripHeredocs(segment).matchAll(/(['"])([^'"\s]*[/.][^'"\s]*)\1/g)) out.push(m[2]);
|
|
214
|
+
return out;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Does a protected name sit NEAR a declared write verb in an interpreter
|
|
219
|
+
* body? Proximity, not parsing — the arm needs to tell
|
|
220
|
+
* `open('…/.gate-salt','w')` from a program that writes a `.md` and happens
|
|
221
|
+
* to mention a hook in its data. Associating a call with its argument
|
|
222
|
+
* properly means parsing the language, which this module will not do.
|
|
223
|
+
*/
|
|
224
|
+
function writesNear(body: string, protectedName: string): boolean {
|
|
225
|
+
const WINDOW = 80;
|
|
226
|
+
for (const m of body.matchAll(new RegExp(protectedName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'))) {
|
|
227
|
+
const from = Math.max(0, (m.index ?? 0) - WINDOW);
|
|
228
|
+
const slice = body.slice(from, (m.index ?? 0) + protectedName.length + WINDOW);
|
|
229
|
+
if (WRITE_INTENT.test(slice)) return true;
|
|
230
|
+
}
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Verbs that modify a named file in place, with no redirect for
|
|
236
|
+
* `writeTargets` to see. `sed -i` is the ordinary way to edit a file from a
|
|
237
|
+
* shell, so its absence was a plain hole rather than an exotic one.
|
|
238
|
+
*/
|
|
239
|
+
const IN_PLACE = [
|
|
240
|
+
/\bsed\b[^\n]*\s-i\b/,
|
|
241
|
+
/\bperl\b[^\n]*\s-[a-z]*i\b/,
|
|
242
|
+
/\bdd\b[^\n]*\bof=/,
|
|
243
|
+
/\btruncate\b/,
|
|
244
|
+
/\brm\b/,
|
|
245
|
+
/\bln\b/,
|
|
246
|
+
/\bgit\s+(?:checkout|restore)\b/,
|
|
247
|
+
];
|
|
248
|
+
|
|
249
|
+
/** Interpreters whose argument is a program this module cannot read. */
|
|
250
|
+
const INTERPRETERS = /^(?:python[23]?|perl|ruby|node|bun|deno|osascript|sh|bash|zsh|php)$/;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Declared write intent inside an interpreter body.
|
|
254
|
+
*
|
|
255
|
+
* The honest version of this arm would refuse any interpreter naming a
|
|
256
|
+
* protected artifact, since a program is opaque. That was implemented and
|
|
257
|
+
* MEASURED: it newly refused 1,760 of 5,060 real commands — 35% — and
|
|
258
|
+
* triage found them to be reads. `bun -e '…readFileSync(…workflow-state
|
|
259
|
+
* .json…)…'` and `python3 -I -c 'json.load(open(…))'` are how every agent
|
|
260
|
+
* on this machine inspects gate state, and a guard that refuses the normal
|
|
261
|
+
* way of looking at a thing is a guard that gets switched off.
|
|
262
|
+
*
|
|
263
|
+
* So the arm fires on a declared write VERB instead. The limit that buys is
|
|
264
|
+
* real and is stated rather than hidden: an interpreter can always evade
|
|
265
|
+
* this — build the name from pieces, `eval` it, call through a variable, or
|
|
266
|
+
* put the program in a file instead of `-c`. This arm raises the cost of an
|
|
267
|
+
* accidental write and of the obvious deliberate one. It is not a boundary,
|
|
268
|
+
* and `.claude/rules/checks-must-be-able-to-fail.md` would call it a
|
|
269
|
+
* detector narrower than what it detects. It is the widest setting the
|
|
270
|
+
* measured false-positive rate allows.
|
|
271
|
+
*/
|
|
272
|
+
const WRITE_INTENT =
|
|
273
|
+
/\b(?:writeFileSync|appendFileSync|createWriteStream|copyFileSync|renameSync|unlinkSync|rmSync|truncateSync|write_text|json\.dump|os\.remove|os\.rename|os\.replace|os\.unlink|shutil\.(?:copy|move|rmtree))\b|(?<!stdout)(?<!stderr)\.write\s*\(|\bopen\s*\([^)]*['"][wax]\+?['"]/;
|
|
274
|
+
|
|
275
|
+
/** `find` actions that run or delete rather than print. */
|
|
276
|
+
const FIND_MUTATING = /\s-(?:delete|exec|execdir|ok|okdir)\b/;
|
|
277
|
+
|
|
278
|
+
/** Does this text name a protected artifact? Pass a DEQUOTED segment. */
|
|
279
|
+
function names(text: string, list: string[]): string | null {
|
|
280
|
+
for (const p of list) if (text.includes(p)) return p;
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const ALL_PROTECTED = [...PROTECTED_FILES, ...PROTECTED_HOOKS];
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Decide a Bash tool call.
|
|
288
|
+
*
|
|
289
|
+
* Returns `{ blocked: false }` for anything this module cannot show is a
|
|
290
|
+
* write to a protected artifact. That default is deliberate and is the
|
|
291
|
+
* reason the checks above are structural: a guard tuned so wide that people
|
|
292
|
+
* work around it protects nothing, and four real refusals of read-only work
|
|
293
|
+
* is already a pattern.
|
|
294
|
+
*/
|
|
295
|
+
export function decideBashWrite(cmd: string): BashWriteDecision {
|
|
296
|
+
if (!cmd) return ALLOW;
|
|
297
|
+
|
|
298
|
+
for (const seg of segments(cmd)) {
|
|
299
|
+
const bare = dequote(seg); // TARGET view: contents kept
|
|
300
|
+
const code = stripQuoted(seg); // VERB view: quoted spans gone
|
|
301
|
+
const word = commandWord(seg);
|
|
302
|
+
|
|
303
|
+
// 1. Shell redirection and copying verbs. Never exempted by anything:
|
|
304
|
+
// this is the arm the old allowlist sat above, which is #1120.
|
|
305
|
+
for (const target of writeTargets(seg)) {
|
|
306
|
+
const pf = names(target, PROTECTED_FILES);
|
|
307
|
+
if (pf) {
|
|
308
|
+
return {
|
|
309
|
+
blocked: true,
|
|
310
|
+
reason: `Bash write to protected file "${pf}" blocked. Use gate-runner.ts for gate results.`,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
if (names(target, PROTECTED_HOOKS)) {
|
|
314
|
+
return { blocked: true, reason: 'Bash write to hook file blocked. Hook integrity is protected.' };
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// 2. In-place mutation. The verb is read off `code`, and the target must
|
|
319
|
+
// be an ARGUMENT — not any text in the segment. `sed -i '' "s/|
|
|
320
|
+
// X.hook.ts |/…/" NOTES.md` edits a markdown file and merely mentions
|
|
321
|
+
// a hook in its script; matching the whole segment refused it.
|
|
322
|
+
if (IN_PLACE.some(p => p.test(code))) {
|
|
323
|
+
const pf = names(argumentsOf(seg).join(' '), ALL_PROTECTED);
|
|
324
|
+
if (pf) {
|
|
325
|
+
return {
|
|
326
|
+
blocked: true,
|
|
327
|
+
reason: `Bash command modifies "${pf}" in place. Use gate-runner.ts for gate results.`,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// 3. Interpreter bodies and mutating `find`. These write with no shell
|
|
333
|
+
// redirect, so arm 1 sees nothing at all. See WRITE_INTENT for why
|
|
334
|
+
// this asks for a declared write verb rather than refusing every
|
|
335
|
+
// interpreter that names a protected artifact, and for what that
|
|
336
|
+
// concession costs.
|
|
337
|
+
const isInterpreter = INTERPRETERS.test(word) && /\s-[a-z]*[ce]\b/.test(code);
|
|
338
|
+
const isMutatingFind = word === 'find' && FIND_MUTATING.test(code);
|
|
339
|
+
if (isInterpreter || isMutatingFind) {
|
|
340
|
+
const pf = isMutatingFind
|
|
341
|
+
? names(bare, ALL_PROTECTED)
|
|
342
|
+
: (ALL_PROTECTED.find(p => bare.includes(p) && writesNear(bare, p)) ?? null);
|
|
343
|
+
if (pf) {
|
|
344
|
+
return {
|
|
345
|
+
blocked: true,
|
|
346
|
+
reason:
|
|
347
|
+
`Bash command runs an interpreter or a mutating \`find\` naming "${pf}". ` +
|
|
348
|
+
`What it writes cannot be read off the command line, so it is refused.`,
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// 4. Permission changes on a hook file. The COMMAND WORD must be the
|
|
354
|
+
// verb — not merely a `chmod` somewhere in the string, which is what
|
|
355
|
+
// refused four read-only commands. The target is read off `bare`, so
|
|
356
|
+
// quoting the path does not hide it.
|
|
357
|
+
if ((word === 'chmod' || word === 'chflags') && names(bare, PROTECTED_HOOKS)) {
|
|
358
|
+
return { blocked: true, reason: 'Permission change on hook file blocked. Hook integrity is protected.' };
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// 5. The gate salt in the Keychain. Exempted by nothing — unlike `chmod`,
|
|
363
|
+
// which turns up innocently in docs and greps, `pai-gate-salt` beside
|
|
364
|
+
// `security` has no benign INVOCATION. It has a benign MENTION though,
|
|
365
|
+
// and two real commands hit it: issue comments quoting the very command
|
|
366
|
+
// they were proposing to guard. So it reads the code, not the prose.
|
|
367
|
+
const codeOnly = stripQuoted(cmd);
|
|
368
|
+
if (codeOnly.includes('pai-gate-salt') && codeOnly.includes('security')) {
|
|
369
|
+
return { blocked: true, reason: 'Access to gate salt Keychain entry blocked.' };
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
return ALLOW;
|
|
373
|
+
}
|