Files
java-ai-agents/claude-code-spring/.claude/hooks/GuardBash.java
T

16 lines
1.0 KiB
Java

// The same deny rules as guard-bash.sh, written as a JDK 25 compact source file (JEP 512, final in 25).
// Run by the hook as: java ${CLAUDE_PROJECT_DIR}/.claude/hooks/GuardBash.java
// It exists to measure what a JVM hook costs next to bash + jq. It does not parse JSON properly.
void main() throws Exception {
String in = new String(System.in.readAllBytes());
var m = java.util.regex.Pattern.compile("\"command\"\\s*:\\s*\"((?:[^\"\\\\]|\\\\.)*)\"").matcher(in);
String cmd = m.find() ? m.group(1) : "";
String reason = null;
if (cmd.contains("mvn deploy") || cmd.contains("mvn release:")) reason = "Publishing artifacts is a human decision.";
else if (cmd.contains("git push --force") || cmd.contains("git push -f")) reason = "Force-pushing is blocked in this project.";
else if (cmd.contains("rm -rf")) reason = "rm -rf is blocked.";
if (reason != null) {
System.out.println("{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"deny\",\"permissionDecisionReason\":\"" + reason + "\"}}");
}
}