@devrik-tools/claude-gates 0.4.0 → 0.6.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/.claude-plugin/marketplace.json +2 -2
- package/README.es.md +25 -4
- package/README.md +21 -5
- package/cli/config.mjs +126 -124
- package/cli/init.mjs +303 -276
- package/cli/install.mjs +281 -175
- package/cli/materialize.mjs +103 -102
- package/cli/registry.mjs +139 -136
- package/cli/smoke-fixtures.json +51 -0
- package/cli/task.mjs +140 -140
- package/package.json +1 -1
- package/plugins/gates/.claude-plugin/plugin.json +1 -1
- package/plugins/gates/hooks/ask-adoption.mjs +147 -147
- package/plugins/gates/hooks/doctor.mjs +207 -207
- package/plugins/gates/hooks/gates/atomic-commit/index.mjs +229 -0
- package/plugins/gates/hooks/gates/audit-before-build/index.mjs +110 -88
- package/plugins/gates/hooks/gates/autonomous-mode/index.mjs +50 -50
- package/plugins/gates/hooks/gates/bash-commands/index.mjs +215 -215
- package/plugins/gates/hooks/gates/brief-before-delegate/index.mjs +269 -265
- package/plugins/gates/hooks/gates/capability-map/index.mjs +377 -0
- package/plugins/gates/hooks/gates/circuit-breaker/index.mjs +527 -501
- package/plugins/gates/hooks/gates/diagnosis-before-patch/index.mjs +48 -43
- package/plugins/gates/hooks/gates/feature-catalog/index.mjs +83 -83
- package/plugins/gates/hooks/gates/force-parallel/index.mjs +134 -119
- package/plugins/gates/hooks/gates/forge-flow/index.mjs +134 -134
- package/plugins/gates/hooks/gates/implementation-pipeline/index.mjs +187 -187
- package/plugins/gates/hooks/gates/intent-flow/index.mjs +260 -260
- package/plugins/gates/hooks/gates/lint-commit/index.mjs +152 -149
- package/plugins/gates/hooks/gates/mandatory-flow/index.mjs +180 -180
- package/plugins/gates/hooks/gates/never-assume/index.mjs +59 -58
- package/plugins/gates/hooks/gates/no-blocking/index.mjs +163 -148
- package/plugins/gates/hooks/gates/no-coauthor/index.mjs +127 -0
- package/plugins/gates/hooks/gates/no-lint-suppression/index.mjs +183 -0
- package/plugins/gates/hooks/gates/protected-paths/index.mjs +149 -144
- package/plugins/gates/hooks/gates/recurrence-lock/index.mjs +91 -89
- package/plugins/gates/hooks/gates/reuse-before-build/index.mjs +263 -159
- package/plugins/gates/hooks/gates/risk-level/index.mjs +265 -263
- package/plugins/gates/hooks/gates/root-cause-first/index.mjs +57 -56
- package/plugins/gates/hooks/gates/root-whitelist/index.mjs +211 -131
- package/plugins/gates/hooks/gates/rule-skill-autodiscovery/index.mjs +181 -184
- package/plugins/gates/hooks/gates/sdd-specs/index.mjs +256 -256
- package/plugins/gates/hooks/gates/staged-lint/index.mjs +187 -0
- package/plugins/gates/hooks/gates/stop-pending/index.mjs +169 -164
- package/plugins/gates/hooks/gates/test-matrix/index.mjs +187 -187
- package/plugins/gates/hooks/gates/tool-map/index.mjs +168 -143
- package/plugins/gates/hooks/hooks.json +51 -0
- package/plugins/gates/hooks/lib/config.mjs +179 -172
- package/plugins/gates/hooks/lib/hook-io.mjs +367 -357
- package/plugins/gates/hooks/lib/signals.mjs +172 -127
- package/plugins/gates/hooks/wiring-check.mjs +227 -227
- package/plugins/tasks/.claude-plugin/plugin.json +1 -1
- package/plugins/tasks/hooks/hooks.json +26 -26
- package/plugins/tasks/hooks/lib/task-store.mjs +217 -197
- package/plugins/tasks/hooks/register-requests.mjs +145 -145
- package/plugins/tasks/hooks/session-tasks.mjs +108 -108
- package/registry.json +171 -1
|
@@ -1,215 +1,215 @@
|
|
|
1
|
-
// bash-commands — denies destructive shell commands (rm -rf over protected areas, git
|
|
2
|
-
// reset --hard, force push, kill-by-name). Runs on a real Bash/run_command call and on a
|
|
3
|
-
// delegation prompt (a subagent can be told "run git reset --hard" in prose).
|
|
4
|
-
//
|
|
5
|
-
// Remote-publish blocking (git push / gh pr merge / gh release create) used to live here as
|
|
6
|
-
// a hardcoded, non-configurable block. It was split out into the block-remote-publish gate
|
|
7
|
-
// so it carries its own enabled flag: a project can now allow the agent to push by disabling
|
|
8
|
-
// that gate WITHOUT also disabling the destructive-command protections below.
|
|
9
|
-
//
|
|
10
|
-
// ── What a project can configure (params) ───────────────────────────────────────────
|
|
11
|
-
// denyPatterns regex sources (matched case-insensitively) of destructive
|
|
12
|
-
// commands to deny. Replaces the built-in list below wholesale.
|
|
13
|
-
// rmRfProtectedAreas targets rm -rf may not hit; woven into the rm -rf pattern.
|
|
14
|
-
// embeddedInterpreterEnabled block `node -e`/`python -c` that does raw file ops. Off
|
|
15
|
-
// by default: this harness legitimately uses inline interpreters.
|
|
16
|
-
// The defaults live here, in the source, so a project reads them and knows exactly what
|
|
17
|
-
// its override replaces.
|
|
18
|
-
|
|
19
|
-
import {
|
|
20
|
-
runGate,
|
|
21
|
-
deny,
|
|
22
|
-
toolInGroups,
|
|
23
|
-
delegationPromptOf,
|
|
24
|
-
} from '../../lib/hook-io.mjs';
|
|
25
|
-
|
|
26
|
-
const GATE_ID = 'bash-commands';
|
|
27
|
-
const CONFIG_KEY = 'blockDestructiveShellCommands';
|
|
28
|
-
|
|
29
|
-
// Areas rm -rf must never target. A project overrides this list in config; the rm -rf
|
|
30
|
-
// deny pattern is rebuilt from it at runtime (see rmRfSourceFrom), so an edit takes effect.
|
|
31
|
-
const DEFAULT_RM_RF_PROTECTED_AREAS = ['/', '*', 'src', 'tests'];
|
|
32
|
-
|
|
33
|
-
// Process-killing command names, assembled from fragments so the spell checker does not
|
|
34
|
-
// read them as prose (the project keeps an empty dictionary by policy).
|
|
35
|
-
const KILL_BY_NAME_COMMANDS = ['task' + 'kill', 'p' + 'kill', 'kill' + 'all'];
|
|
36
|
-
|
|
37
|
-
// PowerShell's kill-by-name form: `Stop-Process -Name node` (and its Get-Process pipe). On
|
|
38
|
-
// Windows this reaches every process of that name exactly like taskkill /IM, so it belongs
|
|
39
|
-
// in the same block. Matched separately because its shape (a -Name flag) differs from the
|
|
40
|
-
// unix commands above.
|
|
41
|
-
const STOP_PROCESS_BY_NAME_SOURCE = String.raw`\bStop-Process\b[^|;\n]*\s-Name\b`;
|
|
42
|
-
|
|
43
|
-
// The rm -rf deny source, built from the protected-areas list. Separate from the static
|
|
44
|
-
// deny list so a project can edit rmRfProtectedAreas in config and have it take effect at
|
|
45
|
-
// runtime: the list is re-read on every call, not baked in at load time.
|
|
46
|
-
function rmRfSourceFrom(protectedAreas) {
|
|
47
|
-
const rmRfTargets = protectedAreas
|
|
48
|
-
.map((area) => area.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
|
|
49
|
-
.join('|');
|
|
50
|
-
// Allow an optional `./` (or `.\`) prefix before the target, so `rm -rf ./src` and
|
|
51
|
-
// `rm -rf .\src` are caught, not just the bare `rm -rf src`. Without this, a leading
|
|
52
|
-
// `./` sat between the required whitespace and the target's word boundary and slipped past.
|
|
53
|
-
return String.raw`\brm\s+-rf\s+(?:\.[\\/])?(${rmRfTargets})\b`;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Static destructive-command rules as `[regexSource, reason]`. Sources (not RegExp) so
|
|
58
|
-
* they are JSON-serializable and a project can override them through config. rm -rf is not
|
|
59
|
-
* here — it is built at runtime from rmRfProtectedAreas (see rmRfSourceFrom).
|
|
60
|
-
*/
|
|
61
|
-
function defaultDenyPatterns() {
|
|
62
|
-
const [taskKill, pKill, killAll] = KILL_BY_NAME_COMMANDS;
|
|
63
|
-
return [
|
|
64
|
-
[
|
|
65
|
-
String.raw`\bgit\s+reset\s+--hard\b`,
|
|
66
|
-
"'git reset --hard' is destructive and needs explicit authorization.",
|
|
67
|
-
],
|
|
68
|
-
[
|
|
69
|
-
String.raw`\bgit\s+push\s+.*--force\b`,
|
|
70
|
-
"'git push --force' is destructive and is not allowed.",
|
|
71
|
-
],
|
|
72
|
-
[
|
|
73
|
-
String.raw`\bgit\s+clean\s+-[a-z]*f[a-z]*\b`,
|
|
74
|
-
"'git clean -f' is destructive and is not allowed.",
|
|
75
|
-
],
|
|
76
|
-
[
|
|
77
|
-
// Killing by image name reaches every process with that name on the machine — the
|
|
78
|
-
// server the user is watching, another session's, a half-run tool. The damage is
|
|
79
|
-
// silent. Keep the PID you started and kill that; if lost, identify by full command
|
|
80
|
-
// line and confirm ownership first.
|
|
81
|
-
[
|
|
82
|
-
String.raw`\b(`,
|
|
83
|
-
taskKill,
|
|
84
|
-
String.raw`\s+[^|;]*[/]IM|`,
|
|
85
|
-
pKill,
|
|
86
|
-
String.raw`\s+|`,
|
|
87
|
-
killAll,
|
|
88
|
-
String.raw`\s+)`,
|
|
89
|
-
].join(''),
|
|
90
|
-
'Killing processes by NAME reaches everything with that name, not just yours. ' +
|
|
91
|
-
'Keep the PID you started and kill that; if lost, find it by its full command ' +
|
|
92
|
-
'line and confirm it is yours before touching it.',
|
|
93
|
-
],
|
|
94
|
-
[
|
|
95
|
-
STOP_PROCESS_BY_NAME_SOURCE,
|
|
96
|
-
'Stop-Process -Name kills every process of that name on the machine, not just yours. ' +
|
|
97
|
-
'Stop the specific process by its Id (the PID you started); if lost, identify it by ' +
|
|
98
|
-
'its full command line and confirm ownership first.',
|
|
99
|
-
],
|
|
100
|
-
];
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Interpreter name, then any intermediate flags (e.g. --input-type=module), then an
|
|
104
|
-
// eval flag. Intermediate flags are matched loosely (`-\S+`) to keep the pattern simple.
|
|
105
|
-
const INTERPRETER_EVAL_PATTERN =
|
|
106
|
-
/\b(node|python3?|deno|bun)\b(?:\s+-\S+)*?\s+(?:-e|-c|--eval|--print|-p)\b/i;
|
|
107
|
-
const RAW_FILE_OPS_PATTERN =
|
|
108
|
-
/\b(writeFileSync|readFileSync|appendFileSync|fs\.writeFile|fs\.readFile|fs\.unlink|fs\.mkdir)\b/;
|
|
109
|
-
|
|
110
|
-
function compile(source) {
|
|
111
|
-
return new RegExp(source, 'i');
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// git's GLOBAL options sit between `git` and the subcommand: `git -C <path> reset --hard`,
|
|
115
|
-
// `git -c k=v push`, `git --git-dir=… clean -f`. A pattern that matches `git reset --hard`
|
|
116
|
-
// contiguously is evaded by any of them. Stripping these options first — turning
|
|
117
|
-
// `git -C /repo reset --hard` back into `git reset --hard` — closes that bypass for every
|
|
118
|
-
// git rule at once, instead of teaching each pattern about every global option.
|
|
119
|
-
//
|
|
120
|
-
// A single global option, matched one at a time and stripped repeatedly (below), so the
|
|
121
|
-
// pattern stays simple: an option taking a value (`-C /path`, `--git-dir=…`) or a flag
|
|
122
|
-
// (`--no-pager`). The leading `git ` is kept; only the option after it is removed.
|
|
123
|
-
const GIT_OPTION_WITH_VALUE = String.raw`(?:-[Cc]|--git-dir|--work-tree|--namespace|--exec-path|--config-env)(?:\s+|=)\S+`;
|
|
124
|
-
const GIT_FLAG_OPTION = String.raw`--(?:paginate|no-pager|bare|no-optional-locks)|-p`;
|
|
125
|
-
const GIT_GLOBAL_OPTION_PATTERN = new RegExp(
|
|
126
|
-
String.raw`\bgit\s+(?:${GIT_OPTION_WITH_VALUE}|${GIT_FLAG_OPTION})\s+`,
|
|
127
|
-
'i',
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
function normalizeGitOptions(command) {
|
|
131
|
-
// Strip one leading global option at a time and re-run, so a stacked
|
|
132
|
-
// `git -c a=b -C /x reset` is fully reduced to `git reset` before the deny patterns run.
|
|
133
|
-
let previous;
|
|
134
|
-
let normalized = command;
|
|
135
|
-
do {
|
|
136
|
-
previous = normalized;
|
|
137
|
-
normalized = normalized.replace(GIT_GLOBAL_OPTION_PATTERN, 'git ');
|
|
138
|
-
} while (normalized !== previous);
|
|
139
|
-
return normalized;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function checkEmbeddedInterpreter(command) {
|
|
143
|
-
const match = INTERPRETER_EVAL_PATTERN.exec(command);
|
|
144
|
-
if (!match) return null;
|
|
145
|
-
if (match[1].toLowerCase().startsWith('python')) {
|
|
146
|
-
return 'Inline Python is not allowed in this harness: write the script to a file with Write and run that.';
|
|
147
|
-
}
|
|
148
|
-
const inline = command.slice(match.index + match[0].length);
|
|
149
|
-
if (RAW_FILE_OPS_PATTERN.test(inline)) {
|
|
150
|
-
return 'Inline interpreter doing a raw file operation (read/write/delete/mkdir). Use Write/Edit/Read instead.';
|
|
151
|
-
}
|
|
152
|
-
return null; // import/require or fetch: logic the native tools do not cover — allowed
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** The text to inspect: a real command's command line, or the delegation prompt. */
|
|
156
|
-
function commandTextFrom(toolName, toolInput) {
|
|
157
|
-
if (toolInGroups(toolName, ['shell'])) {
|
|
158
|
-
return String(toolInput.CommandLine ?? toolInput.command ?? '');
|
|
159
|
-
}
|
|
160
|
-
return delegationPromptOf(toolInput);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** Static deny rules + the runtime rm -rf rule, both read from config params. */
|
|
164
|
-
function checkDestructive(command, parameters) {
|
|
165
|
-
// Strip git's global options so `git -C /repo reset --hard` cannot slip past a pattern
|
|
166
|
-
// written for `git reset --hard`. Non-git commands are unaffected.
|
|
167
|
-
const normalized = normalizeGitOptions(command);
|
|
168
|
-
|
|
169
|
-
// denyPatterns may be a flat list of sources or [source, reason] pairs; normalize.
|
|
170
|
-
const denyPairs = (parameters.denyPatterns ?? []).map((entry) =>
|
|
171
|
-
Array.isArray(entry)
|
|
172
|
-
? entry
|
|
173
|
-
: [entry, 'Destructive command is not allowed.'],
|
|
174
|
-
);
|
|
175
|
-
for (const [source, reason] of denyPairs) {
|
|
176
|
-
if (compile(source).test(normalized)) deny(GATE_ID, reason);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// rm -rf over a protected area: areas read from config at runtime, so editing
|
|
180
|
-
// rmRfProtectedAreas takes effect without touching denyPatterns.
|
|
181
|
-
const rmRfAreas =
|
|
182
|
-
parameters.rmRfProtectedAreas ?? DEFAULT_RM_RF_PROTECTED_AREAS;
|
|
183
|
-
if (
|
|
184
|
-
rmRfAreas.length > 0 &&
|
|
185
|
-
compile(rmRfSourceFrom(rmRfAreas)).test(command)
|
|
186
|
-
) {
|
|
187
|
-
deny(GATE_ID, "'rm -rf' over a protected area is not allowed.");
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (parameters.embeddedInterpreterEnabled) {
|
|
191
|
-
const reason = checkEmbeddedInterpreter(command);
|
|
192
|
-
if (reason) deny(GATE_ID, reason);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
runGate(
|
|
197
|
-
{
|
|
198
|
-
id: GATE_ID,
|
|
199
|
-
configKey: CONFIG_KEY,
|
|
200
|
-
enabledByDefault: true,
|
|
201
|
-
defaultParams: {
|
|
202
|
-
denyPatterns: defaultDenyPatterns(),
|
|
203
|
-
rmRfProtectedAreas: DEFAULT_RM_RF_PROTECTED_AREAS,
|
|
204
|
-
embeddedInterpreterEnabled: false,
|
|
205
|
-
},
|
|
206
|
-
},
|
|
207
|
-
({ toolName, toolInput, parameters }) => {
|
|
208
|
-
// Destructive-command rules act on a real shell command AND on a delegation prompt (a
|
|
209
|
-
// subagent can be told "run rm -rf" in prose). commandTextFrom picks the right text.
|
|
210
|
-
if (!toolInGroups(toolName, ['shell', 'delegation'])) return;
|
|
211
|
-
|
|
212
|
-
const command = commandTextFrom(toolName, toolInput);
|
|
213
|
-
checkDestructive(command, parameters);
|
|
214
|
-
},
|
|
215
|
-
);
|
|
1
|
+
// bash-commands — denies destructive shell commands (rm -rf over protected areas, git
|
|
2
|
+
// reset --hard, force push, kill-by-name). Runs on a real Bash/run_command call and on a
|
|
3
|
+
// delegation prompt (a subagent can be told "run git reset --hard" in prose).
|
|
4
|
+
//
|
|
5
|
+
// Remote-publish blocking (git push / gh pr merge / gh release create) used to live here as
|
|
6
|
+
// a hardcoded, non-configurable block. It was split out into the block-remote-publish gate
|
|
7
|
+
// so it carries its own enabled flag: a project can now allow the agent to push by disabling
|
|
8
|
+
// that gate WITHOUT also disabling the destructive-command protections below.
|
|
9
|
+
//
|
|
10
|
+
// ── What a project can configure (params) ───────────────────────────────────────────
|
|
11
|
+
// denyPatterns regex sources (matched case-insensitively) of destructive
|
|
12
|
+
// commands to deny. Replaces the built-in list below wholesale.
|
|
13
|
+
// rmRfProtectedAreas targets rm -rf may not hit; woven into the rm -rf pattern.
|
|
14
|
+
// embeddedInterpreterEnabled block `node -e`/`python -c` that does raw file ops. Off
|
|
15
|
+
// by default: this harness legitimately uses inline interpreters.
|
|
16
|
+
// The defaults live here, in the source, so a project reads them and knows exactly what
|
|
17
|
+
// its override replaces.
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
runGate,
|
|
21
|
+
deny,
|
|
22
|
+
toolInGroups,
|
|
23
|
+
delegationPromptOf,
|
|
24
|
+
} from '../../lib/hook-io.mjs';
|
|
25
|
+
|
|
26
|
+
const GATE_ID = 'bash-commands';
|
|
27
|
+
const CONFIG_KEY = 'blockDestructiveShellCommands';
|
|
28
|
+
|
|
29
|
+
// Areas rm -rf must never target. A project overrides this list in config; the rm -rf
|
|
30
|
+
// deny pattern is rebuilt from it at runtime (see rmRfSourceFrom), so an edit takes effect.
|
|
31
|
+
const DEFAULT_RM_RF_PROTECTED_AREAS = ['/', '*', 'src', 'tests'];
|
|
32
|
+
|
|
33
|
+
// Process-killing command names, assembled from fragments so the spell checker does not
|
|
34
|
+
// read them as prose (the project keeps an empty dictionary by policy).
|
|
35
|
+
const KILL_BY_NAME_COMMANDS = ['task' + 'kill', 'p' + 'kill', 'kill' + 'all'];
|
|
36
|
+
|
|
37
|
+
// PowerShell's kill-by-name form: `Stop-Process -Name node` (and its Get-Process pipe). On
|
|
38
|
+
// Windows this reaches every process of that name exactly like taskkill /IM, so it belongs
|
|
39
|
+
// in the same block. Matched separately because its shape (a -Name flag) differs from the
|
|
40
|
+
// unix commands above.
|
|
41
|
+
const STOP_PROCESS_BY_NAME_SOURCE = String.raw`\bStop-Process\b[^|;\n]*\s-Name\b`;
|
|
42
|
+
|
|
43
|
+
// The rm -rf deny source, built from the protected-areas list. Separate from the static
|
|
44
|
+
// deny list so a project can edit rmRfProtectedAreas in config and have it take effect at
|
|
45
|
+
// runtime: the list is re-read on every call, not baked in at load time.
|
|
46
|
+
function rmRfSourceFrom(protectedAreas) {
|
|
47
|
+
const rmRfTargets = protectedAreas
|
|
48
|
+
.map((area) => area.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
|
|
49
|
+
.join('|');
|
|
50
|
+
// Allow an optional `./` (or `.\`) prefix before the target, so `rm -rf ./src` and
|
|
51
|
+
// `rm -rf .\src` are caught, not just the bare `rm -rf src`. Without this, a leading
|
|
52
|
+
// `./` sat between the required whitespace and the target's word boundary and slipped past.
|
|
53
|
+
return String.raw`\brm\s+-rf\s+(?:\.[\\/])?(${rmRfTargets})\b`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Static destructive-command rules as `[regexSource, reason]`. Sources (not RegExp) so
|
|
58
|
+
* they are JSON-serializable and a project can override them through config. rm -rf is not
|
|
59
|
+
* here — it is built at runtime from rmRfProtectedAreas (see rmRfSourceFrom).
|
|
60
|
+
*/
|
|
61
|
+
function defaultDenyPatterns() {
|
|
62
|
+
const [taskKill, pKill, killAll] = KILL_BY_NAME_COMMANDS;
|
|
63
|
+
return [
|
|
64
|
+
[
|
|
65
|
+
String.raw`\bgit\s+reset\s+--hard\b`,
|
|
66
|
+
"'git reset --hard' is destructive and needs explicit authorization.",
|
|
67
|
+
],
|
|
68
|
+
[
|
|
69
|
+
String.raw`\bgit\s+push\s+.*--force\b`,
|
|
70
|
+
"'git push --force' is destructive and is not allowed.",
|
|
71
|
+
],
|
|
72
|
+
[
|
|
73
|
+
String.raw`\bgit\s+clean\s+-[a-z]*f[a-z]*\b`,
|
|
74
|
+
"'git clean -f' is destructive and is not allowed.",
|
|
75
|
+
],
|
|
76
|
+
[
|
|
77
|
+
// Killing by image name reaches every process with that name on the machine — the
|
|
78
|
+
// server the user is watching, another session's, a half-run tool. The damage is
|
|
79
|
+
// silent. Keep the PID you started and kill that; if lost, identify by full command
|
|
80
|
+
// line and confirm ownership first.
|
|
81
|
+
[
|
|
82
|
+
String.raw`\b(`,
|
|
83
|
+
taskKill,
|
|
84
|
+
String.raw`\s+[^|;]*[/]IM|`,
|
|
85
|
+
pKill,
|
|
86
|
+
String.raw`\s+|`,
|
|
87
|
+
killAll,
|
|
88
|
+
String.raw`\s+)`,
|
|
89
|
+
].join(''),
|
|
90
|
+
'Killing processes by NAME reaches everything with that name, not just yours. ' +
|
|
91
|
+
'Keep the PID you started and kill that; if lost, find it by its full command ' +
|
|
92
|
+
'line and confirm it is yours before touching it.',
|
|
93
|
+
],
|
|
94
|
+
[
|
|
95
|
+
STOP_PROCESS_BY_NAME_SOURCE,
|
|
96
|
+
'Stop-Process -Name kills every process of that name on the machine, not just yours. ' +
|
|
97
|
+
'Stop the specific process by its Id (the PID you started); if lost, identify it by ' +
|
|
98
|
+
'its full command line and confirm ownership first.',
|
|
99
|
+
],
|
|
100
|
+
];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Interpreter name, then any intermediate flags (e.g. --input-type=module), then an
|
|
104
|
+
// eval flag. Intermediate flags are matched loosely (`-\S+`) to keep the pattern simple.
|
|
105
|
+
const INTERPRETER_EVAL_PATTERN =
|
|
106
|
+
/\b(node|python3?|deno|bun)\b(?:\s+-\S+)*?\s+(?:-e|-c|--eval|--print|-p)\b/i;
|
|
107
|
+
const RAW_FILE_OPS_PATTERN =
|
|
108
|
+
/\b(writeFileSync|readFileSync|appendFileSync|fs\.writeFile|fs\.readFile|fs\.unlink|fs\.mkdir)\b/;
|
|
109
|
+
|
|
110
|
+
function compile(source) {
|
|
111
|
+
return new RegExp(source, 'i');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// git's GLOBAL options sit between `git` and the subcommand: `git -C <path> reset --hard`,
|
|
115
|
+
// `git -c k=v push`, `git --git-dir=… clean -f`. A pattern that matches `git reset --hard`
|
|
116
|
+
// contiguously is evaded by any of them. Stripping these options first — turning
|
|
117
|
+
// `git -C /repo reset --hard` back into `git reset --hard` — closes that bypass for every
|
|
118
|
+
// git rule at once, instead of teaching each pattern about every global option.
|
|
119
|
+
//
|
|
120
|
+
// A single global option, matched one at a time and stripped repeatedly (below), so the
|
|
121
|
+
// pattern stays simple: an option taking a value (`-C /path`, `--git-dir=…`) or a flag
|
|
122
|
+
// (`--no-pager`). The leading `git ` is kept; only the option after it is removed.
|
|
123
|
+
const GIT_OPTION_WITH_VALUE = String.raw`(?:-[Cc]|--git-dir|--work-tree|--namespace|--exec-path|--config-env)(?:\s+|=)\S+`;
|
|
124
|
+
const GIT_FLAG_OPTION = String.raw`--(?:paginate|no-pager|bare|no-optional-locks)|-p`;
|
|
125
|
+
const GIT_GLOBAL_OPTION_PATTERN = new RegExp(
|
|
126
|
+
String.raw`\bgit\s+(?:${GIT_OPTION_WITH_VALUE}|${GIT_FLAG_OPTION})\s+`,
|
|
127
|
+
'i',
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
function normalizeGitOptions(command) {
|
|
131
|
+
// Strip one leading global option at a time and re-run, so a stacked
|
|
132
|
+
// `git -c a=b -C /x reset` is fully reduced to `git reset` before the deny patterns run.
|
|
133
|
+
let previous;
|
|
134
|
+
let normalized = command;
|
|
135
|
+
do {
|
|
136
|
+
previous = normalized;
|
|
137
|
+
normalized = normalized.replace(GIT_GLOBAL_OPTION_PATTERN, 'git ');
|
|
138
|
+
} while (normalized !== previous);
|
|
139
|
+
return normalized;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function checkEmbeddedInterpreter(command) {
|
|
143
|
+
const match = INTERPRETER_EVAL_PATTERN.exec(command);
|
|
144
|
+
if (!match) return null;
|
|
145
|
+
if (match[1].toLowerCase().startsWith('python')) {
|
|
146
|
+
return 'Inline Python is not allowed in this harness: write the script to a file with Write and run that.';
|
|
147
|
+
}
|
|
148
|
+
const inline = command.slice(match.index + match[0].length);
|
|
149
|
+
if (RAW_FILE_OPS_PATTERN.test(inline)) {
|
|
150
|
+
return 'Inline interpreter doing a raw file operation (read/write/delete/mkdir). Use Write/Edit/Read instead.';
|
|
151
|
+
}
|
|
152
|
+
return null; // import/require or fetch: logic the native tools do not cover — allowed
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** The text to inspect: a real command's command line, or the delegation prompt. */
|
|
156
|
+
function commandTextFrom(toolName, toolInput) {
|
|
157
|
+
if (toolInGroups(toolName, ['shell'])) {
|
|
158
|
+
return String(toolInput.CommandLine ?? toolInput.command ?? '');
|
|
159
|
+
}
|
|
160
|
+
return delegationPromptOf(toolInput);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Static deny rules + the runtime rm -rf rule, both read from config params. */
|
|
164
|
+
function checkDestructive(command, parameters) {
|
|
165
|
+
// Strip git's global options so `git -C /repo reset --hard` cannot slip past a pattern
|
|
166
|
+
// written for `git reset --hard`. Non-git commands are unaffected.
|
|
167
|
+
const normalized = normalizeGitOptions(command);
|
|
168
|
+
|
|
169
|
+
// denyPatterns may be a flat list of sources or [source, reason] pairs; normalize.
|
|
170
|
+
const denyPairs = (parameters.denyPatterns ?? []).map((entry) =>
|
|
171
|
+
Array.isArray(entry)
|
|
172
|
+
? entry
|
|
173
|
+
: [entry, 'Destructive command is not allowed.'],
|
|
174
|
+
);
|
|
175
|
+
for (const [source, reason] of denyPairs) {
|
|
176
|
+
if (compile(source).test(normalized)) deny(GATE_ID, reason);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// rm -rf over a protected area: areas read from config at runtime, so editing
|
|
180
|
+
// rmRfProtectedAreas takes effect without touching denyPatterns.
|
|
181
|
+
const rmRfAreas =
|
|
182
|
+
parameters.rmRfProtectedAreas ?? DEFAULT_RM_RF_PROTECTED_AREAS;
|
|
183
|
+
if (
|
|
184
|
+
rmRfAreas.length > 0 &&
|
|
185
|
+
compile(rmRfSourceFrom(rmRfAreas)).test(command)
|
|
186
|
+
) {
|
|
187
|
+
deny(GATE_ID, "'rm -rf' over a protected area is not allowed.");
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (parameters.embeddedInterpreterEnabled) {
|
|
191
|
+
const reason = checkEmbeddedInterpreter(command);
|
|
192
|
+
if (reason) deny(GATE_ID, reason);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
runGate(
|
|
197
|
+
{
|
|
198
|
+
id: GATE_ID,
|
|
199
|
+
configKey: CONFIG_KEY,
|
|
200
|
+
enabledByDefault: true,
|
|
201
|
+
defaultParams: {
|
|
202
|
+
denyPatterns: defaultDenyPatterns(),
|
|
203
|
+
rmRfProtectedAreas: DEFAULT_RM_RF_PROTECTED_AREAS,
|
|
204
|
+
embeddedInterpreterEnabled: false,
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
({ toolName, toolInput, parameters }) => {
|
|
208
|
+
// Destructive-command rules act on a real shell command AND on a delegation prompt (a
|
|
209
|
+
// subagent can be told "run rm -rf" in prose). commandTextFrom picks the right text.
|
|
210
|
+
if (!toolInGroups(toolName, ['shell', 'delegation'])) return;
|
|
211
|
+
|
|
212
|
+
const command = commandTextFrom(toolName, toolInput);
|
|
213
|
+
checkDestructive(command, parameters);
|
|
214
|
+
},
|
|
215
|
+
);
|