@schalkneethling/toolkit 0.1.1 → 0.1.3
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.
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
#!/usr/bin/env -S node
|
|
2
|
+
/**
|
|
3
|
+
* block-dangerous-commands: PreToolUse hook for Bash.
|
|
4
|
+
*
|
|
5
|
+
* Reads a Claude Code hook payload from stdin, inspects tool_input.command,
|
|
6
|
+
* and blocks commands that match known-dangerous patterns.
|
|
7
|
+
*
|
|
8
|
+
* Exit 2 = block (with structured JSON on stdout)
|
|
9
|
+
* Exit 0 = allow (including on malformed input — fail open)
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
type Rule = {
|
|
13
|
+
id: string;
|
|
14
|
+
test: (cmd: string) => boolean;
|
|
15
|
+
message: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const rules: Rule[] = [
|
|
19
|
+
{
|
|
20
|
+
id: "rm-rf",
|
|
21
|
+
test: (c) => /\brm\s+(?:-[a-zA-Z]*[rRf][a-zA-Z]*|--recursive|--force)(?:\s|$)/.test(c),
|
|
22
|
+
message:
|
|
23
|
+
"`rm -rf` (and flag variants) is blocked. Delete specific paths with a non-recursive `rm`, or move them to a backup location.",
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
id: "git-push-force",
|
|
27
|
+
test: (c) => /\bgit\s+push\b.*\s(?:--force\b|--force-with-lease\b|-f\b)/.test(c),
|
|
28
|
+
message:
|
|
29
|
+
"`git push --force` is blocked. Use `--force-with-lease` only after coordinating with collaborators, or create a new branch.",
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
id: "git-push-protected",
|
|
33
|
+
test: (c) =>
|
|
34
|
+
/\bgit\s+push\b(?:\s+\S+)*\s+(?:origin\s+)?(?:main|master|production|prod|release)(?:\s|$)/.test(
|
|
35
|
+
c,
|
|
36
|
+
),
|
|
37
|
+
message:
|
|
38
|
+
"Direct push to a protected branch (main/master/production/prod/release) is blocked. Open a pull request instead.",
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
id: "git-reset-hard",
|
|
42
|
+
test: (c) => /\bgit\s+reset\s+(?:\S+\s+)*--hard\b/.test(c),
|
|
43
|
+
message:
|
|
44
|
+
"`git reset --hard` is blocked — it discards uncommitted work. Consider `git stash`, `git restore`, or a soft reset.",
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
id: "chmod-777",
|
|
48
|
+
test: (c) =>
|
|
49
|
+
/\bchmod\s+(?:-[a-zA-Z]*\s+)*(?:777|[ugoa]*[+=][rwx]*w[rwx]*(?:\s|$))/.test(c) &&
|
|
50
|
+
/-R|--recursive|777/.test(c),
|
|
51
|
+
message:
|
|
52
|
+
"`chmod 777` or recursive world-writable chmod is blocked. Grant the minimum permissions required.",
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
id: "dd-if",
|
|
56
|
+
test: (c) => /\bdd\s+if=/.test(c),
|
|
57
|
+
message: "`dd if=` is blocked — it can overwrite disks irrecoverably.",
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
id: "system-redirect",
|
|
61
|
+
test: (c) => /(?:>|>>|tee(?:\s+-[a-zA-Z]*)?)\s+\/(?:etc|boot|usr|bin|sbin)\//.test(c),
|
|
62
|
+
message:
|
|
63
|
+
"Writing into /etc, /boot, /usr, /bin, or /sbin is blocked. These are system directories; use a user-writable path.",
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
id: "fork-bomb",
|
|
67
|
+
test: (c) => /:\(\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;\s*:/.test(c) || /\.\s*\|\s*\.\s*&/.test(c),
|
|
68
|
+
message: "Fork bomb pattern detected and blocked.",
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
id: "curl-pipe-shell",
|
|
72
|
+
test: (c) =>
|
|
73
|
+
/\b(?:curl|wget|fetch)\b[^|;]*\|\s*(?:sudo\s+)?(?:bash|sh|zsh|fish|ksh|dash)\b/.test(c),
|
|
74
|
+
message:
|
|
75
|
+
"Piping remote content directly into a shell is blocked. Download the script, inspect it, then run it.",
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
id: "pkill",
|
|
79
|
+
test: (c) => /\bpkill\b/.test(c),
|
|
80
|
+
message:
|
|
81
|
+
"`pkill` is blocked — it can terminate unrelated processes by name. Kill a specific PID instead.",
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
id: "kill-9",
|
|
85
|
+
test: (c) =>
|
|
86
|
+
/\bkill\s+(?:-[a-zA-Z]*\s+)*-9\b|\bkill\s+-s\s+(?:9|SIGKILL)\b|\bkill\s+-SIGKILL\b/.test(c),
|
|
87
|
+
message: "`kill -9` is blocked — it prevents cleanup. Try SIGTERM (default) first.",
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
id: "npm-publish",
|
|
91
|
+
test: (c) => /\bnpm\s+(?:publish|deprecate|unpublish)\b/.test(c),
|
|
92
|
+
message:
|
|
93
|
+
"`npm publish`/`deprecate`/`unpublish` is blocked. Publishing should be done deliberately, outside of an agent session.",
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
id: "history-clear",
|
|
97
|
+
test: (c) => /\bhistory\s+-c\b/.test(c),
|
|
98
|
+
message: "`history -c` is blocked — erasing shell history hides what happened.",
|
|
99
|
+
},
|
|
100
|
+
];
|
|
101
|
+
|
|
102
|
+
async function readStdin(): Promise<string> {
|
|
103
|
+
const chunks: Buffer[] = [];
|
|
104
|
+
for await (const chunk of process.stdin) chunks.push(chunk as Buffer);
|
|
105
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function deny(reason: string): void {
|
|
109
|
+
const output = {
|
|
110
|
+
hookSpecificOutput: {
|
|
111
|
+
hookEventName: "PreToolUse",
|
|
112
|
+
permissionDecision: "deny",
|
|
113
|
+
permissionDecisionReason: reason,
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
process.stdout.write(JSON.stringify(output));
|
|
117
|
+
process.exit(2);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function main(): Promise<void> {
|
|
121
|
+
let payload: { tool_input?: { command?: string } };
|
|
122
|
+
try {
|
|
123
|
+
const raw = await readStdin();
|
|
124
|
+
if (!raw.trim()) {
|
|
125
|
+
process.exit(0);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
payload = JSON.parse(raw);
|
|
129
|
+
} catch {
|
|
130
|
+
process.exit(0);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const command = payload?.tool_input?.command;
|
|
134
|
+
if (typeof command !== "string" || command.length === 0) {
|
|
135
|
+
process.exit(0);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
for (const rule of rules) {
|
|
139
|
+
if (rule.test(command)) {
|
|
140
|
+
deny(`[${rule.id}] ${rule.message}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
process.exit(0);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
main().catch(() => process.exit(0));
|
package/package.json
CHANGED
|
File without changes
|