Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .claude/hooks/block_dangerous_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
# ── Dangerous patterns (regex) ──────────────────────────────────
DANGEROUS_PATTERNS = [
# === Git destructive operations ===
(r"git\s+push\s+.*--force", "git push --force (force push)"),
(r"git\s+push\s+.*-f\b", "git push -f (force push)"),
(r"git\s+push\s+.*--force-with-lease", "git push --force-with-lease"),
# Bound the wildcard to the push invocation's own args: [^|&;<>]* stops at a
# pipe/redirect/separator so a later `-f` in a chained command or a flattened
# heredoc body (newlines collapse to spaces) cannot trigger a false positive.
(r"git\s+push\s+[^|&;<>]*--force", "git push --force (force push)"),
(r"git\s+push\s+[^|&;<>]*-f\b", "git push -f (force push)"),
(r"git\s+push\s+[^|&;<>]*--force-with-lease", "git push --force-with-lease"),
Comment on lines +27 to +29

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Block force pushes with inline redirections

When a redirection appears before the force option in the same simple command, this negated class stops scanning at >/< and misses the later force flag. Bash still treats later words as arguments after a redirection (e.g. f 2>/dev/null --force origin main passes --force), and the hook now exits 0 for git push 2>/dev/null --force origin main, allowing the force-push policy to be bypassed while still executing a real force push.

Useful? React with 👍 / 👎.

(r"git\s+push\s+\S+\s+\+", "git push origin +branch (force push)"),
(r"git\s+clean\s+.*-f", "git clean -f (delete untracked files)"),
(r"git\s+reflog\s+expire", "git reflog expire (destroy recovery data)"),
Expand Down
10 changes: 8 additions & 2 deletions test/hooks-integrity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,17 @@ describe('Claude Code Hooks integrity', () => {
});

test('should block git force push (--force)', () => {
expect(content).toContain('git\\s+push\\s+.*--force');
expect(content).toContain('git\\s+push\\s+[^|&;<>]*--force');
});

test('should block git force push (-f)', () => {
expect(content).toContain('git\\s+push\\s+.*-f\\b');
expect(content).toContain('git\\s+push\\s+[^|&;<>]*-f\\b');
});

test('should bound force-push wildcard to avoid chained-command false positives', () => {
// [^|&;<>]* stops at a pipe/redirect/separator so a later -f in a chained
// command or flattened heredoc body does not trigger a false positive.
expect(content).not.toContain('git\\s+push\\s+.*-f\\b');
});

test('should block git reset --hard', () => {
Expand Down
Loading