@schalkneethling/calavera-hook-auto-approve-safe-commands 0.2.0-next.1
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-auto-approve-safe-commands",
|
|
5
|
+
"type": "hook",
|
|
6
|
+
"displayName": "Auto-approve safe commands",
|
|
7
|
+
"payload": "payload/auto-approve-safe-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-auto-approve-safe-commands",
|
|
3
|
+
"version": "0.2.0-next.1",
|
|
4
|
+
"description": "Auto-approve safe 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-auto-approve-safe-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,111 @@
|
|
|
1
|
+
#!/usr/bin/env -S node
|
|
2
|
+
/**
|
|
3
|
+
* auto-approve-safe-commands: PermissionRequest hook
|
|
4
|
+
*
|
|
5
|
+
* Reads a Claude Code hook payload from stdin, inspects tool_input.command,
|
|
6
|
+
* and allows safe commands that match known-safe patterns.
|
|
7
|
+
*
|
|
8
|
+
* Exit 0 = defers to permission prompt
|
|
9
|
+
*/
|
|
10
|
+
// --- Safe command patterns ---
|
|
11
|
+
//
|
|
12
|
+
// Each entry is a pattern and a label for logging purposes.
|
|
13
|
+
// Order matters — more specific patterns should come before broader ones.
|
|
14
|
+
const SAFE_PATTERNS = [
|
|
15
|
+
// Git read operations (no side effects)
|
|
16
|
+
{ pattern: /^git\s+status(?:\s+--(?:short|branch|porcelain(?:=v[12])?))*$/, label: "git status" },
|
|
17
|
+
{ pattern: /^git\s+log(?:\s+--oneline)?$/, label: "git log" },
|
|
18
|
+
{ pattern: /^git\s+diff(?:\s+--(?:stat|name-only|name-status|check|cached|staged))*$/, label: "git diff" },
|
|
19
|
+
{ pattern: /^git\s+branch(?:\s+--show-current)?$/, label: "git branch" },
|
|
20
|
+
{ pattern: /^git\s+show(?:\s+--(?:stat|oneline))?$/, label: "git show" },
|
|
21
|
+
// Filesystem reads
|
|
22
|
+
{ pattern: /^cat\b/, label: "cat" },
|
|
23
|
+
{ pattern: /^ls\b/, label: "ls" },
|
|
24
|
+
{ pattern: /^find\b/, label: "find" },
|
|
25
|
+
{ pattern: /^grep\b/, label: "grep" },
|
|
26
|
+
{ pattern: /^rg\b/, label: "ripgrep" },
|
|
27
|
+
// Environment checks
|
|
28
|
+
{ pattern: /^node\s+--version\b/, label: "node --version" },
|
|
29
|
+
{ pattern: /^node\s+-v\b/, label: "node -v" },
|
|
30
|
+
{ pattern: /^bun\s+--version\b/, label: "bun --version" },
|
|
31
|
+
{ pattern: /^npm\s+--version\b/, label: "npm --version" },
|
|
32
|
+
{ pattern: /^git\s+--version\b/, label: "git --version" },
|
|
33
|
+
];
|
|
34
|
+
const UNSAFE_SHELL_SYNTAX = /[;&|<>`]|\$\(|\r|\n/;
|
|
35
|
+
const SAFE_COMMAND_WORDS = /^[\w@%+=:,./\s-]+$/;
|
|
36
|
+
const DESTRUCTIVE_OPTIONS = /(?:^|\s)(?:--force|--fix|--write|--delete|-delete|-f|-r|-R|-rf|-fr|--recursive)(?:\s|$)/;
|
|
37
|
+
const COMMAND_SPECIFIC_UNSAFE_PATTERNS = [
|
|
38
|
+
/^git\s+branch\s+-(?:d|D)\b/,
|
|
39
|
+
/^find\b.*\s-exec(?:dir)?\b.*(?:\+|\\;)\s*$/,
|
|
40
|
+
];
|
|
41
|
+
// --- Helpers ---
|
|
42
|
+
function isSafeCommandShape(command) {
|
|
43
|
+
return (!UNSAFE_SHELL_SYNTAX.test(command) &&
|
|
44
|
+
SAFE_COMMAND_WORDS.test(command) &&
|
|
45
|
+
!DESTRUCTIVE_OPTIONS.test(command) &&
|
|
46
|
+
!COMMAND_SPECIFIC_UNSAFE_PATTERNS.some((pattern) => pattern.test(command)));
|
|
47
|
+
}
|
|
48
|
+
function approve() {
|
|
49
|
+
const output = {
|
|
50
|
+
hookSpecificOutput: {
|
|
51
|
+
hookEventName: "PermissionRequest",
|
|
52
|
+
decision: {
|
|
53
|
+
behavior: "allow",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
process.stdout.write(JSON.stringify(output) + "\n");
|
|
58
|
+
process.exitCode = 0;
|
|
59
|
+
}
|
|
60
|
+
function defer() {
|
|
61
|
+
// Exit 0 with no output — falls through to the normal permission prompt
|
|
62
|
+
process.exitCode = 0;
|
|
63
|
+
}
|
|
64
|
+
// --- Main ---
|
|
65
|
+
async function readStdin() {
|
|
66
|
+
const chunks = [];
|
|
67
|
+
for await (const chunk of process.stdin)
|
|
68
|
+
chunks.push(chunk);
|
|
69
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
70
|
+
}
|
|
71
|
+
async function main() {
|
|
72
|
+
const raw = (await readStdin()).trim();
|
|
73
|
+
let input;
|
|
74
|
+
try {
|
|
75
|
+
input = JSON.parse(raw);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
process.stderr.write(`[auto-approve-safe-commands] Failed to parse stdin JSON\n`);
|
|
79
|
+
defer();
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
// Only handle Bash tool permission requests
|
|
83
|
+
if (input.tool_name !== "Bash") {
|
|
84
|
+
defer();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (typeof input.tool_input !== "object" || input.tool_input === null) {
|
|
88
|
+
defer();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const { command } = input.tool_input;
|
|
92
|
+
if (typeof command !== "string") {
|
|
93
|
+
defer();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const trimmed = command.trim();
|
|
97
|
+
if (!isSafeCommandShape(trimmed)) {
|
|
98
|
+
defer();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
for (const { pattern, label } of SAFE_PATTERNS) {
|
|
102
|
+
if (pattern.test(trimmed)) {
|
|
103
|
+
process.stderr.write(`[auto-approve-safe-commands] Auto-approved: ${label}\n`);
|
|
104
|
+
approve();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// Not on the allowlist — fall through to normal permission prompt
|
|
109
|
+
defer();
|
|
110
|
+
}
|
|
111
|
+
main().catch(() => defer());
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
#!/usr/bin/env -S node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* auto-approve-safe-commands: PermissionRequest hook
|
|
5
|
+
*
|
|
6
|
+
* Reads a Claude Code hook payload from stdin, inspects tool_input.command,
|
|
7
|
+
* and allows safe commands that match known-safe patterns.
|
|
8
|
+
*
|
|
9
|
+
* Exit 0 = defers to permission prompt
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// --- Types ---
|
|
13
|
+
|
|
14
|
+
interface BashToolInput {
|
|
15
|
+
command: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface PermissionRequestInput {
|
|
19
|
+
hook_event_name: "PermissionRequest";
|
|
20
|
+
session_id: string;
|
|
21
|
+
cwd: string;
|
|
22
|
+
transcript_path: string;
|
|
23
|
+
tool_name: string;
|
|
24
|
+
tool_input: BashToolInput | Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface ApproveOutput {
|
|
28
|
+
hookSpecificOutput: {
|
|
29
|
+
hookEventName: "PermissionRequest";
|
|
30
|
+
decision: {
|
|
31
|
+
behavior: "allow";
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// --- Safe command patterns ---
|
|
37
|
+
//
|
|
38
|
+
// Each entry is a pattern and a label for logging purposes.
|
|
39
|
+
// Order matters — more specific patterns should come before broader ones.
|
|
40
|
+
|
|
41
|
+
const SAFE_PATTERNS: { pattern: RegExp; label: string }[] = [
|
|
42
|
+
// Git read operations (no side effects)
|
|
43
|
+
{ pattern: /^git\s+status(?:\s+--(?:short|branch|porcelain(?:=v[12])?))*$/, label: "git status" },
|
|
44
|
+
{ pattern: /^git\s+log(?:\s+--oneline)?$/, label: "git log" },
|
|
45
|
+
{
|
|
46
|
+
pattern: /^git\s+diff(?:\s+--(?:stat|name-only|name-status|check|cached|staged))*$/,
|
|
47
|
+
label: "git diff",
|
|
48
|
+
},
|
|
49
|
+
{ pattern: /^git\s+branch(?:\s+--show-current)?$/, label: "git branch" },
|
|
50
|
+
{ pattern: /^git\s+show(?:\s+--(?:stat|oneline))?$/, label: "git show" },
|
|
51
|
+
|
|
52
|
+
// Filesystem reads
|
|
53
|
+
{ pattern: /^cat\b/, label: "cat" },
|
|
54
|
+
{ pattern: /^ls\b/, label: "ls" },
|
|
55
|
+
{ pattern: /^find\b/, label: "find" },
|
|
56
|
+
{ pattern: /^grep\b/, label: "grep" },
|
|
57
|
+
{ pattern: /^rg\b/, label: "ripgrep" },
|
|
58
|
+
|
|
59
|
+
// Environment checks
|
|
60
|
+
{ pattern: /^node\s+--version\b/, label: "node --version" },
|
|
61
|
+
{ pattern: /^node\s+-v\b/, label: "node -v" },
|
|
62
|
+
{ pattern: /^bun\s+--version\b/, label: "bun --version" },
|
|
63
|
+
{ pattern: /^npm\s+--version\b/, label: "npm --version" },
|
|
64
|
+
{ pattern: /^git\s+--version\b/, label: "git --version" },
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
const UNSAFE_SHELL_SYNTAX = /[;&|<>`]|\$\(|\r|\n/;
|
|
68
|
+
const SAFE_COMMAND_WORDS = /^[\w@%+=:,./\s-]+$/;
|
|
69
|
+
const DESTRUCTIVE_OPTIONS =
|
|
70
|
+
/(?:^|\s)(?:--force|--fix|--write|--delete|-delete|-f|-r|-R|-rf|-fr|--recursive)(?:\s|$)/;
|
|
71
|
+
|
|
72
|
+
const COMMAND_SPECIFIC_UNSAFE_PATTERNS = [
|
|
73
|
+
/^git\s+branch\s+-(?:d|D)\b/,
|
|
74
|
+
/^find\b.*\s-exec(?:dir)?\b.*(?:\+|\\;)\s*$/,
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
// --- Helpers ---
|
|
78
|
+
|
|
79
|
+
function isSafeCommandShape(command: string): boolean {
|
|
80
|
+
return (
|
|
81
|
+
!UNSAFE_SHELL_SYNTAX.test(command) &&
|
|
82
|
+
SAFE_COMMAND_WORDS.test(command) &&
|
|
83
|
+
!DESTRUCTIVE_OPTIONS.test(command) &&
|
|
84
|
+
!COMMAND_SPECIFIC_UNSAFE_PATTERNS.some((pattern) => pattern.test(command))
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function approve(): void {
|
|
89
|
+
const output: ApproveOutput = {
|
|
90
|
+
hookSpecificOutput: {
|
|
91
|
+
hookEventName: "PermissionRequest",
|
|
92
|
+
decision: {
|
|
93
|
+
behavior: "allow",
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
process.stdout.write(JSON.stringify(output) + "\n");
|
|
99
|
+
process.exitCode = 0;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function defer(): void {
|
|
103
|
+
// Exit 0 with no output — falls through to the normal permission prompt
|
|
104
|
+
process.exitCode = 0;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// --- Main ---
|
|
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
|
+
async function main(): Promise<void> {
|
|
116
|
+
const raw = (await readStdin()).trim();
|
|
117
|
+
|
|
118
|
+
let input: PermissionRequestInput;
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
input = JSON.parse(raw) as PermissionRequestInput;
|
|
122
|
+
} catch {
|
|
123
|
+
process.stderr.write(`[auto-approve-safe-commands] Failed to parse stdin JSON\n`);
|
|
124
|
+
defer();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Only handle Bash tool permission requests
|
|
129
|
+
if (input.tool_name !== "Bash") {
|
|
130
|
+
defer();
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (typeof input.tool_input !== "object" || input.tool_input === null) {
|
|
135
|
+
defer();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const { command } = input.tool_input as BashToolInput;
|
|
140
|
+
|
|
141
|
+
if (typeof command !== "string") {
|
|
142
|
+
defer();
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const trimmed = command.trim();
|
|
147
|
+
|
|
148
|
+
if (!isSafeCommandShape(trimmed)) {
|
|
149
|
+
defer();
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
for (const { pattern, label } of SAFE_PATTERNS) {
|
|
154
|
+
if (pattern.test(trimmed)) {
|
|
155
|
+
process.stderr.write(`[auto-approve-safe-commands] Auto-approved: ${label}\n`);
|
|
156
|
+
approve();
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Not on the allowlist — fall through to normal permission prompt
|
|
162
|
+
defer();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
main().catch(() => defer());
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"hooks": {
|
|
3
|
+
"PermissionRequest": [
|
|
4
|
+
{
|
|
5
|
+
"matcher": "Bash",
|
|
6
|
+
"hooks": [
|
|
7
|
+
{
|
|
8
|
+
"type": "command",
|
|
9
|
+
"command": "node .agents/hooks/claude-code/auto-approve-safe-commands.mjs",
|
|
10
|
+
"timeout": 5,
|
|
11
|
+
"statusMessage": "Checking if command can be auto-approved..."
|
|
12
|
+
}
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
]
|
|
16
|
+
}
|
|
17
|
+
}
|