@ericrisco/rsc 4.1.9 → 4.1.10
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/manifest.json +1 -1
- package/package.json +1 -1
- package/targets/claude.js +3 -3
- package/targets/danger-guard.mjs +74 -8
package/manifest.json
CHANGED
package/package.json
CHANGED
package/targets/claude.js
CHANGED
|
@@ -316,15 +316,15 @@ export function wireHook(paths, sourceMd, policy = {}) {
|
|
|
316
316
|
settings.hooks.PreToolUse ||= [];
|
|
317
317
|
settings.hooks.PreToolUse = settings.hooks.PreToolUse.filter((e) => !hookWiringOf(e).includes('.rsc/danger-guard.'));
|
|
318
318
|
if (policy.dangerGuard !== false) {
|
|
319
|
-
// Danger guard: a PreToolUse
|
|
319
|
+
// Danger guard: a PreToolUse shell hook that DENIES irreversible foot-gun commands
|
|
320
320
|
// (rm -rf, git push --force, DROP/TRUNCATE, DELETE/UPDATE without WHERE, dd to /dev,
|
|
321
321
|
// curl|bash, …) when the user-profile says the user is NON-technical (default-safe
|
|
322
|
-
// when no profile exists yet
|
|
322
|
+
// when no profile exists yet). Technical profiles confirm the high-risk subset. Materialized +
|
|
323
323
|
// node-run (Windows-safe), idempotent, fail-open, opt-out via .rsc/.no-danger-guard.
|
|
324
324
|
const dgDest = join(paths.projectRoot, '.rsc', 'danger-guard.mjs');
|
|
325
325
|
copyFileSync(join(HERE, 'danger-guard.mjs'), dgDest);
|
|
326
326
|
const dgCmd = viaBootstrap('guard', at('.rsc', 'danger-guard.mjs'), `"${P}"`);
|
|
327
|
-
settings.hooks.PreToolUse.push({ matcher: 'Bash', hooks: [{ type: 'command', command: dgCmd }] });
|
|
327
|
+
settings.hooks.PreToolUse.push({ matcher: 'Bash|PowerShell', hooks: [{ type: 'command', command: dgCmd }] });
|
|
328
328
|
written.push(dgDest);
|
|
329
329
|
} else {
|
|
330
330
|
rmSync(join(paths.projectRoot, '.rsc', 'danger-guard.mjs'), { force: true });
|
package/targets/danger-guard.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// rsc Danger guard (claude). Wired by targets/claude.js onto PreToolUse (matcher Bash)
|
|
2
|
+
// rsc Danger guard (claude). Wired by targets/claude.js onto PreToolUse (matcher Bash|PowerShell)
|
|
3
3
|
// as `node ...` so it runs on every platform including Windows.
|
|
4
4
|
// argv[2] = absolute project root stdin = PreToolUse hook JSON
|
|
5
5
|
//
|
|
6
6
|
// For a NON-TECHNICAL user (per 02-DOCS/wiki/harness/user-profile.md → technical_level),
|
|
7
|
-
// it DENIES irreversible, foot-gun
|
|
7
|
+
// it DENIES irreversible, foot-gun shell commands and tells the agent to find a safer,
|
|
8
8
|
// scoped alternative. Default-safe: if there is no profile yet, the harness convention is
|
|
9
9
|
// "assume non-technical", so the guard is ON.
|
|
10
10
|
//
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
// Disable per project with .rsc/.no-danger-guard — but only when the USER explicitly asks
|
|
18
18
|
// for it (the deny message says so). Fail-open on any internal error (never brick a shell).
|
|
19
19
|
import { existsSync, readFileSync } from 'node:fs';
|
|
20
|
-
import {
|
|
20
|
+
import { homedir } from 'node:os';
|
|
21
|
+
import { join, win32 } from 'node:path';
|
|
21
22
|
|
|
22
23
|
const root = process.argv[2] || process.cwd();
|
|
23
24
|
|
|
@@ -68,12 +69,75 @@ function technicalLevel() {
|
|
|
68
69
|
}
|
|
69
70
|
const technical = technicalLevel() === 'technical';
|
|
70
71
|
|
|
71
|
-
//
|
|
72
|
+
// Claude sends the same command field for either shell.
|
|
72
73
|
let input = {};
|
|
73
74
|
try { input = JSON.parse(readFileSync(0, 'utf8') || '{}'); } catch { allow(); }
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
const tool = input.tool_name || input.toolName;
|
|
76
|
+
if (tool !== 'Bash' && tool !== 'PowerShell') allow();
|
|
77
|
+
const command = input.tool_input?.command || input.toolInput?.command || '';
|
|
78
|
+
if (typeof command !== 'string' || !command) allow();
|
|
79
|
+
|
|
80
|
+
// Read literal arguments of direct PowerShell commands. A backslash is a path separator in
|
|
81
|
+
// this shell. Quoted paths stay together and quoted text is never split at a semicolon.
|
|
82
|
+
function powerShellCommands(line) {
|
|
83
|
+
const commands = [];
|
|
84
|
+
let words = [];
|
|
85
|
+
let invoke = false;
|
|
86
|
+
for (const [token] of line.matchAll(/`\r?\n|#[^\r\n]*|'(?:''|[^'])*'|"(?:`[\s\S]|""|[^"`])*"|[;|&\r\n]|(?:`[\s\S]|[^\s;|&'"`])+/g)) {
|
|
87
|
+
if (/^`\r?\n$/.test(token) || token.startsWith('#')) continue;
|
|
88
|
+
if (/^[;|&\r\n]$/.test(token)) {
|
|
89
|
+
if (words.length) commands.push(words);
|
|
90
|
+
words = [];
|
|
91
|
+
invoke = token === '&';
|
|
92
|
+
} else {
|
|
93
|
+
words.push(invoke ? token.replace(/^(['"])(.*)\1$/, '$2') : token);
|
|
94
|
+
invoke = false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (words.length) commands.push(words);
|
|
98
|
+
return commands;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function protectedPowerShellPath(raw) {
|
|
102
|
+
const path = raw.replace(/^(['"])(.*)\1$/, '$2').replace(/\//g, '\\');
|
|
103
|
+
const target = win32.normalize(path).replace(/\\(?:\*)?$/, '').toLowerCase();
|
|
104
|
+
if (['.', '..', '*', '.git', '~'].includes(target)) return true;
|
|
105
|
+
if (!raw.startsWith("'") && ['$home', '$env:userprofile'].includes(target)) return true;
|
|
106
|
+
const absolute = win32.isAbsolute(path);
|
|
107
|
+
if (!absolute) return false;
|
|
108
|
+
const same = (value) => target === win32.normalize(value).replace(/\\$/, '').toLowerCase();
|
|
109
|
+
return same(win32.parse(path).root) || same(root) || same(homedir());
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Option values such as the include filter or the output variable are not deletion targets.
|
|
113
|
+
const PS_REMOVE_VALUE = /^-(?:filter|include|exclude|credential|erroraction|warningaction|informationaction|progressaction|errorvariable|warningvariable|informationvariable|outvariable|pipelinevariable|outbuffer)$/i;
|
|
114
|
+
function protectedRemoval(args) {
|
|
115
|
+
for (let i = 0; i < args.length; i++) {
|
|
116
|
+
const arg = args[i];
|
|
117
|
+
if (PS_REMOVE_VALUE.test(arg)) { i++; continue; }
|
|
118
|
+
if (!arg.startsWith('-') && protectedPowerShellPath(arg)) return true;
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const psCommands = tool === 'PowerShell' ? powerShellCommands(command) : null;
|
|
124
|
+
if (psCommands) {
|
|
125
|
+
for (const [name, ...args] of psCommands) {
|
|
126
|
+
if (!/^(?:Microsoft\.PowerShell\.Management\\)?(?:Remove-Item|rm|ri|del|erase|rd|rmdir)$/i.test(name)) continue;
|
|
127
|
+
const flag = (key) => args.some((arg) => arg.toLowerCase() === key || arg.toLowerCase() === key + ':$true');
|
|
128
|
+
if (!flag('-recurse') || !flag('-force') || flag('-whatif')) continue;
|
|
129
|
+
const why = 'recursively deletes files and folders with PowerShell';
|
|
130
|
+
if (!technical) deny(why);
|
|
131
|
+
if (protectedRemoval(args)) ask(why);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// PowerShell output commands and standalone string literals only display their text. Other
|
|
136
|
+
// segments still reach the shared rules, including external database clients and disk tools.
|
|
137
|
+
const cmd = psCommands
|
|
138
|
+
? psCommands.filter(([name]) => !/^(?:['"]|(?:echo|write-(?:output|host|verbose|warning|information|debug|error))$)/i.test(name))
|
|
139
|
+
.map((words) => words.join(' ')).join('; ')
|
|
140
|
+
: command;
|
|
77
141
|
|
|
78
142
|
// --- the dangerous-command list ---------------------------------------------
|
|
79
143
|
// High-signal, low-false-positive. Each rule: what it catches and the plain why.
|
|
@@ -138,11 +202,13 @@ const runSegments = () => {
|
|
|
138
202
|
|
|
139
203
|
// A git rule judges the segment that RUNS git, so `echo 'git reset --hard'` is a sentence, not a reset.
|
|
140
204
|
function gitSegment(re) {
|
|
205
|
+
if (psCommands) return psCommands.some(([name, ...args]) =>
|
|
206
|
+
/(?:^|[\\/])git(?:\.exe)?$/i.test(name) && re.test(['git', ...args].join(' ')));
|
|
141
207
|
return runSegments().some((segment) => runs(segment, 'git') && re.test(segment));
|
|
142
208
|
}
|
|
143
209
|
|
|
144
210
|
function isRmRecursiveForce() {
|
|
145
|
-
if (!/\brm\b/.test(cmd)) return false;
|
|
211
|
+
if (psCommands || !/\brm\b/.test(cmd)) return false;
|
|
146
212
|
for (const segment of runSegments()) {
|
|
147
213
|
if (!runsRm(segment)) continue;
|
|
148
214
|
const flags = rmFlagsIn(segment);
|