@schalkneethling/calavera-hook-block-dangerous-commands 0.2.0-next.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Schalk Neethling
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,12 @@
1
+ {
2
+ "$schema": "https://calavera.schalkneethling.com/schemas/calavera-artifact.schema.json",
3
+ "schemaVersion": 1,
4
+ "id": "hook-block-dangerous-commands",
5
+ "type": "hook",
6
+ "displayName": "Block dangerous commands",
7
+ "payload": "payload/block-dangerous-commands",
8
+ "targets": ["claude-code", "codex", "cursor", "opencode"],
9
+ "compatibility": {
10
+ "calavera": ">=2.2.0 <3"
11
+ }
12
+ }
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@schalkneethling/calavera-hook-block-dangerous-commands",
3
+ "version": "0.2.0-next.2",
4
+ "description": "Block dangerous commands artifact for Calavera.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+ssh://git@github.com/schalkneethling/create-project-calavera.git",
9
+ "directory": "packages/artifacts/hook-block-dangerous-commands"
10
+ },
11
+ "files": [
12
+ "calavera-artifact.json",
13
+ "payload"
14
+ ],
15
+ "type": "module",
16
+ "exports": {
17
+ ".": "./calavera-artifact.json",
18
+ "./package.json": "./package.json"
19
+ },
20
+ "publishConfig": {
21
+ "access": "public",
22
+ "provenance": true
23
+ }
24
+ }
@@ -0,0 +1,121 @@
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
+ const rules = [
12
+ {
13
+ id: "rm-rf",
14
+ test: (c) => /\brm\s+(?:-[a-zA-Z]*[rRf][a-zA-Z]*|--recursive|--force)(?:\s|$)/.test(c),
15
+ message: "`rm -rf` (and flag variants) is blocked. Delete specific paths with a non-recursive `rm`, or move them to a backup location.",
16
+ },
17
+ {
18
+ id: "git-push-force",
19
+ test: (c) => /\bgit\s+push\b.*\s(?:--force(?!-with-lease)\b|-f\b)/.test(c),
20
+ message: "`git push --force` is blocked. Use `--force-with-lease` only after coordinating with collaborators, or create a new branch.",
21
+ },
22
+ {
23
+ id: "git-push-protected",
24
+ test: (c) => /\bgit\s+push\b(?:\s+\S+)*\s+(?:origin\s+)?\+?(?:main|master|production|prod|release)(?:\s|$)/.test(c) ||
25
+ /\bgit\s+push\b.*(?:^|\s)\+?\S+:(?:refs\/heads\/)?(?:main|master|production|prod|release)(?:\s|$)/.test(c),
26
+ message: "Direct push to a protected branch (main/master/production/prod/release) is blocked. Open a pull request instead.",
27
+ },
28
+ {
29
+ id: "git-reset-hard",
30
+ test: (c) => /\bgit\s+reset\s+(?:\S+\s+)*--hard\b/.test(c),
31
+ message: "`git reset --hard` is blocked — it discards uncommitted work. Consider `git stash`, `git restore`, or a soft reset.",
32
+ },
33
+ {
34
+ id: "chmod-777",
35
+ test: (c) => /\bchmod\s+(?:(?:-[a-zA-Z]+|--[a-zA-Z-]+)\s+)*(?:777|[ugoa]*[+=][rwx]*w[rwx]*(?:\s|$))/.test(c) &&
36
+ /-R|--recursive|777/.test(c),
37
+ message: "`chmod 777` or recursive world-writable chmod is blocked. Grant the minimum permissions required.",
38
+ },
39
+ {
40
+ id: "dd-if",
41
+ test: (c) => /\bdd\s+if=/.test(c),
42
+ message: "`dd if=` is blocked — it can overwrite disks irrecoverably.",
43
+ },
44
+ {
45
+ id: "system-redirect",
46
+ test: (c) => /(?:>{1,2}\s*|tee(?:\s+-[a-zA-Z]*)?\s+)\/(?:etc|boot|usr|bin|sbin)\//.test(c),
47
+ message: "Writing into /etc, /boot, /usr, /bin, or /sbin is blocked. These are system directories; use a user-writable path.",
48
+ },
49
+ {
50
+ id: "fork-bomb",
51
+ test: (c) => /:\(\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;\s*:/.test(c) || /\.\s*\|\s*\.\s*&/.test(c),
52
+ message: "Fork bomb pattern detected and blocked.",
53
+ },
54
+ {
55
+ id: "curl-pipe-shell",
56
+ test: (c) => /\b(?:curl|wget|fetch)\b[^|;]*\|\s*(?:sudo\s+)?(?:(?:\/usr\/bin\/env\s+)|(?:\/(?:usr\/)?bin\/))?(?:bash|sh|zsh|fish|ksh|dash)\b/.test(c),
57
+ message: "Piping remote content directly into a shell is blocked. Download the script, inspect it, then run it.",
58
+ },
59
+ {
60
+ id: "pkill",
61
+ test: (c) => /\bpkill\b/.test(c),
62
+ message: "`pkill` is blocked — it can terminate unrelated processes by name. Kill a specific PID instead.",
63
+ },
64
+ {
65
+ id: "kill-9",
66
+ test: (c) => /\bkill\s+(?:-[a-zA-Z]*\s+)*-9\b|\bkill\s+-s\s+(?:9|SIGKILL)\b|\bkill\s+-SIGKILL\b/.test(c),
67
+ message: "`kill -9` is blocked — it prevents cleanup. Try SIGTERM (default) first.",
68
+ },
69
+ {
70
+ id: "npm-publish",
71
+ test: (c) => /\bnpm\s+(?:publish|deprecate|unpublish)\b/.test(c),
72
+ message: "`npm publish`/`deprecate`/`unpublish` is blocked. Publishing should be done deliberately, outside of an agent session.",
73
+ },
74
+ {
75
+ id: "history-clear",
76
+ test: (c) => /\bhistory\s+-c\b/.test(c),
77
+ message: "`history -c` is blocked — erasing shell history hides what happened.",
78
+ },
79
+ ];
80
+ async function readStdin() {
81
+ const chunks = [];
82
+ for await (const chunk of process.stdin)
83
+ chunks.push(chunk);
84
+ return Buffer.concat(chunks).toString("utf8");
85
+ }
86
+ function deny(reason) {
87
+ const output = {
88
+ hookSpecificOutput: {
89
+ hookEventName: "PreToolUse",
90
+ permissionDecision: "deny",
91
+ permissionDecisionReason: reason,
92
+ },
93
+ };
94
+ process.stdout.write(JSON.stringify(output));
95
+ process.exitCode = 2;
96
+ }
97
+ async function main() {
98
+ let payload;
99
+ try {
100
+ const raw = await readStdin();
101
+ if (!raw.trim()) {
102
+ return;
103
+ }
104
+ payload = JSON.parse(raw);
105
+ }
106
+ catch {
107
+ return;
108
+ }
109
+ const command = payload?.tool_input?.command;
110
+ if (typeof command !== "string" || command.length === 0) {
111
+ return;
112
+ }
113
+ for (const rule of rules) {
114
+ if (rule.test(command)) {
115
+ deny(`[${rule.id}] ${rule.message}`);
116
+ return;
117
+ }
118
+ }
119
+ }
120
+ main().catch(() => undefined);
121
+ export {};
@@ -0,0 +1,153 @@
1
+ #!/usr/bin/env -S node
2
+
3
+ /**
4
+ * block-dangerous-commands: PreToolUse hook for Bash.
5
+ *
6
+ * Reads a Claude Code hook payload from stdin, inspects tool_input.command,
7
+ * and blocks commands that match known-dangerous patterns.
8
+ *
9
+ * Exit 2 = block (with structured JSON on stdout)
10
+ * Exit 0 = allow (including on malformed input — fail open)
11
+ */
12
+
13
+ type Rule = {
14
+ id: string;
15
+ test: (cmd: string) => boolean;
16
+ message: string;
17
+ };
18
+
19
+ const rules: Rule[] = [
20
+ {
21
+ id: "rm-rf",
22
+ test: (c) => /\brm\s+(?:-[a-zA-Z]*[rRf][a-zA-Z]*|--recursive|--force)(?:\s|$)/.test(c),
23
+ message:
24
+ "`rm -rf` (and flag variants) is blocked. Delete specific paths with a non-recursive `rm`, or move them to a backup location.",
25
+ },
26
+ {
27
+ id: "git-push-force",
28
+ test: (c) => /\bgit\s+push\b.*\s(?:--force(?!-with-lease)\b|-f\b)/.test(c),
29
+ message:
30
+ "`git push --force` is blocked. Use `--force-with-lease` only after coordinating with collaborators, or create a new branch.",
31
+ },
32
+ {
33
+ id: "git-push-protected",
34
+ test: (c) =>
35
+ /\bgit\s+push\b(?:\s+\S+)*\s+(?:origin\s+)?\+?(?:main|master|production|prod|release)(?:\s|$)/.test(
36
+ c,
37
+ ) ||
38
+ /\bgit\s+push\b.*(?:^|\s)\+?\S+:(?:refs\/heads\/)?(?:main|master|production|prod|release)(?:\s|$)/.test(
39
+ c,
40
+ ),
41
+ message:
42
+ "Direct push to a protected branch (main/master/production/prod/release) is blocked. Open a pull request instead.",
43
+ },
44
+ {
45
+ id: "git-reset-hard",
46
+ test: (c) => /\bgit\s+reset\s+(?:\S+\s+)*--hard\b/.test(c),
47
+ message:
48
+ "`git reset --hard` is blocked — it discards uncommitted work. Consider `git stash`, `git restore`, or a soft reset.",
49
+ },
50
+ {
51
+ id: "chmod-777",
52
+ test: (c) =>
53
+ /\bchmod\s+(?:(?:-[a-zA-Z]+|--[a-zA-Z-]+)\s+)*(?:777|[ugoa]*[+=][rwx]*w[rwx]*(?:\s|$))/.test(
54
+ c,
55
+ ) && /-R|--recursive|777/.test(c),
56
+ message:
57
+ "`chmod 777` or recursive world-writable chmod is blocked. Grant the minimum permissions required.",
58
+ },
59
+ {
60
+ id: "dd-if",
61
+ test: (c) => /\bdd\s+if=/.test(c),
62
+ message: "`dd if=` is blocked — it can overwrite disks irrecoverably.",
63
+ },
64
+ {
65
+ id: "system-redirect",
66
+ test: (c) => /(?:>{1,2}\s*|tee(?:\s+-[a-zA-Z]*)?\s+)\/(?:etc|boot|usr|bin|sbin)\//.test(c),
67
+ message:
68
+ "Writing into /etc, /boot, /usr, /bin, or /sbin is blocked. These are system directories; use a user-writable path.",
69
+ },
70
+ {
71
+ id: "fork-bomb",
72
+ test: (c) => /:\(\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;\s*:/.test(c) || /\.\s*\|\s*\.\s*&/.test(c),
73
+ message: "Fork bomb pattern detected and blocked.",
74
+ },
75
+ {
76
+ id: "curl-pipe-shell",
77
+ test: (c) =>
78
+ /\b(?:curl|wget|fetch)\b[^|;]*\|\s*(?:sudo\s+)?(?:(?:\/usr\/bin\/env\s+)|(?:\/(?:usr\/)?bin\/))?(?:bash|sh|zsh|fish|ksh|dash)\b/.test(
79
+ c,
80
+ ),
81
+ message:
82
+ "Piping remote content directly into a shell is blocked. Download the script, inspect it, then run it.",
83
+ },
84
+ {
85
+ id: "pkill",
86
+ test: (c) => /\bpkill\b/.test(c),
87
+ message:
88
+ "`pkill` is blocked — it can terminate unrelated processes by name. Kill a specific PID instead.",
89
+ },
90
+ {
91
+ id: "kill-9",
92
+ test: (c) =>
93
+ /\bkill\s+(?:-[a-zA-Z]*\s+)*-9\b|\bkill\s+-s\s+(?:9|SIGKILL)\b|\bkill\s+-SIGKILL\b/.test(c),
94
+ message: "`kill -9` is blocked — it prevents cleanup. Try SIGTERM (default) first.",
95
+ },
96
+ {
97
+ id: "npm-publish",
98
+ test: (c) => /\bnpm\s+(?:publish|deprecate|unpublish)\b/.test(c),
99
+ message:
100
+ "`npm publish`/`deprecate`/`unpublish` is blocked. Publishing should be done deliberately, outside of an agent session.",
101
+ },
102
+ {
103
+ id: "history-clear",
104
+ test: (c) => /\bhistory\s+-c\b/.test(c),
105
+ message: "`history -c` is blocked — erasing shell history hides what happened.",
106
+ },
107
+ ];
108
+
109
+ async function readStdin(): Promise<string> {
110
+ const chunks: Buffer[] = [];
111
+ for await (const chunk of process.stdin) chunks.push(chunk as Buffer);
112
+ return Buffer.concat(chunks).toString("utf8");
113
+ }
114
+
115
+ function deny(reason: string): void {
116
+ const output = {
117
+ hookSpecificOutput: {
118
+ hookEventName: "PreToolUse",
119
+ permissionDecision: "deny",
120
+ permissionDecisionReason: reason,
121
+ },
122
+ };
123
+ process.stdout.write(JSON.stringify(output));
124
+ process.exitCode = 2;
125
+ }
126
+
127
+ async function main(): Promise<void> {
128
+ let payload: { tool_input?: { command?: string } };
129
+ try {
130
+ const raw = await readStdin();
131
+ if (!raw.trim()) {
132
+ return;
133
+ }
134
+
135
+ payload = JSON.parse(raw);
136
+ } catch {
137
+ return;
138
+ }
139
+
140
+ const command = payload?.tool_input?.command;
141
+ if (typeof command !== "string" || command.length === 0) {
142
+ return;
143
+ }
144
+
145
+ for (const rule of rules) {
146
+ if (rule.test(command)) {
147
+ deny(`[${rule.id}] ${rule.message}`);
148
+ return;
149
+ }
150
+ }
151
+ }
152
+
153
+ main().catch(() => undefined);
@@ -0,0 +1,15 @@
1
+ {
2
+ "hooks": {
3
+ "PreToolUse": [
4
+ {
5
+ "matcher": "Bash",
6
+ "hooks": [
7
+ {
8
+ "type": "command",
9
+ "command": "node .agents/hooks/claude-code/block-dangerous-commands.mjs"
10
+ }
11
+ ]
12
+ }
13
+ ]
14
+ }
15
+ }