|
| 1 | +// src/pre-tool-use.ts |
| 2 | +import process from "node:process"; |
| 3 | +var DENY_RULES = [ |
| 4 | + { pattern: /^rm\s+-rf\s+\/(?:\s|$)/i, reason: "Filesystem root deletion blocked" }, |
| 5 | + { pattern: /^rm\s+-rf\s+\/\*(?:\s|$)/i, reason: "Destructive wildcard deletion from root blocked" }, |
| 6 | + { pattern: /^rm\s+-rf\s+~(?:\/|$)/i, reason: "Home directory deletion blocked" }, |
| 7 | + { pattern: /^mkfs\./i, reason: "Disk format command blocked" }, |
| 8 | + { |
| 9 | + pattern: /^dd\s+if=\/dev\/zero\s+of=\/dev\//i, |
| 10 | + reason: "Disk zeroing blocked" |
| 11 | + } |
| 12 | +]; |
| 13 | +var ALLOW_RULES = [ |
| 14 | + { |
| 15 | + pattern: /^(npm|yarn|pnpm|bun)\s+(test|run|install|ci|add|remove|ls|info|outdated|audit|why)\b/i, |
| 16 | + reason: "Safe package manager command" |
| 17 | + }, |
| 18 | + { |
| 19 | + pattern: /^git\s+(status|log|diff|branch|fetch|remote|tag|show|stash\s+list|rev-parse)\b/i, |
| 20 | + reason: "Safe git read operation" |
| 21 | + }, |
| 22 | + { |
| 23 | + pattern: /^git\s+(add|commit|checkout|switch|merge|rebase|stash|pull|cherry-pick)\b/i, |
| 24 | + reason: "Safe git write operation" |
| 25 | + }, |
| 26 | + { |
| 27 | + pattern: /^(node|npx|tsx|python3?|ruby|go\s+run|cargo\s+(build|run|test|check|clippy)|make|gradle|mvn)\b/i, |
| 28 | + reason: "Safe build/runtime command" |
| 29 | + }, |
| 30 | + { |
| 31 | + pattern: /^(ls|pwd|cat|head|tail|wc|file|which|type|env|echo|printf|grep|find|rg|fd|ag|tree)\b/i, |
| 32 | + reason: "Safe file inspection command" |
| 33 | + }, |
| 34 | + { |
| 35 | + pattern: /^docker\s+(ps|logs|images|inspect|version)\b/i, |
| 36 | + reason: "Safe docker read operation" |
| 37 | + } |
| 38 | +]; |
| 39 | +function isGitPushNonForce(cmd) { |
| 40 | + return /^git\s+push\b/i.test(cmd) && !/--force(?:-with-lease)?\b|\s-(?!-)\S*f/i.test(cmd); |
| 41 | +} |
| 42 | +function makeDecision(decision, reason) { |
| 43 | + return { |
| 44 | + hookSpecificOutput: { |
| 45 | + hookEventName: "PreToolUse", |
| 46 | + permissionDecision: decision, |
| 47 | + permissionDecisionReason: reason |
| 48 | + } |
| 49 | + }; |
| 50 | +} |
| 51 | +function evaluate(input) { |
| 52 | + if (input.tool_name !== "Bash") { |
| 53 | + return null; |
| 54 | + } |
| 55 | + const cmd = input.tool_input?.command ?? ""; |
| 56 | + if (!cmd) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + for (const rule of DENY_RULES) { |
| 60 | + if (rule.pattern.test(cmd)) { |
| 61 | + return makeDecision("deny", rule.reason); |
| 62 | + } |
| 63 | + } |
| 64 | + if (/[;&|`\n]|\$\(/.test(cmd)) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + for (const rule of ALLOW_RULES) { |
| 68 | + if (rule.pattern.test(cmd)) { |
| 69 | + return makeDecision("allow", rule.reason); |
| 70 | + } |
| 71 | + } |
| 72 | + if (isGitPushNonForce(cmd)) { |
| 73 | + return makeDecision("allow", "Safe git push (non-force)"); |
| 74 | + } |
| 75 | + return null; |
| 76 | +} |
| 77 | +function readStdin() { |
| 78 | + return new Promise((resolve, reject) => { |
| 79 | + let data = ""; |
| 80 | + process.stdin.setEncoding("utf8"); |
| 81 | + process.stdin.on("data", (chunk) => { |
| 82 | + data += chunk; |
| 83 | + }); |
| 84 | + process.stdin.on("end", () => resolve(data)); |
| 85 | + process.stdin.on("error", reject); |
| 86 | + }); |
| 87 | +} |
| 88 | +async function main() { |
| 89 | + const raw = await readStdin(); |
| 90 | + if (!raw.trim()) { |
| 91 | + process.exit(0); |
| 92 | + } |
| 93 | + let parsed; |
| 94 | + try { |
| 95 | + parsed = JSON.parse(raw); |
| 96 | + } catch (err) { |
| 97 | + process.stderr.write(`gatekeeper: invalid JSON input: ${err instanceof Error ? err.message : String(err)} |
| 98 | +`); |
| 99 | + process.exit(1); |
| 100 | + } |
| 101 | + if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) { |
| 102 | + process.stderr.write(`gatekeeper: expected JSON object, got ${parsed === null ? "null" : typeof parsed} |
| 103 | +`); |
| 104 | + process.exit(1); |
| 105 | + } |
| 106 | + const input = parsed; |
| 107 | + const decision = evaluate(input); |
| 108 | + if (decision) { |
| 109 | + process.stdout.write(JSON.stringify(decision)); |
| 110 | + } |
| 111 | + process.exit(0); |
| 112 | +} |
| 113 | +main().catch((err) => { |
| 114 | + process.stderr.write(`gatekeeper: unexpected error: ${err instanceof Error ? err.message : String(err)} |
| 115 | +`); |
| 116 | + process.exit(1); |
| 117 | +}); |
| 118 | +export { |
| 119 | + makeDecision, |
| 120 | + isGitPushNonForce, |
| 121 | + evaluate, |
| 122 | + DENY_RULES, |
| 123 | + ALLOW_RULES |
| 124 | +}; |
0 commit comments