-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathallow-readonly-git_test.sh
More file actions
executable file
·87 lines (77 loc) · 2.18 KB
/
Copy pathallow-readonly-git_test.sh
File metadata and controls
executable file
·87 lines (77 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
# Tests for allow-readonly-git.sh
#
# Usage: bash hooks/scripts/allow-readonly-git_test.sh
#
# The hook reads a JSON payload from stdin containing tool_input.command,
# then prints a deny JSON on stdout if denied, or exits silently if allowed.
# We check stdout for the deny marker to determine the result.
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOOK="$SCRIPT_DIR/allow-readonly-git.sh"
PASSED=0
FAILED=0
run_hook() {
local cmd="$1"
local payload
payload=$(jq -n --arg c "$cmd" '{"tool_input":{"command":$c}}')
local output
output=$(echo "$payload" | bash "$HOOK" 2>/dev/null) || true
if echo "$output" | grep -q '"permissionDecision"'; then
echo "blocked"
else
echo "allowed"
fi
}
assert_blocked() {
local cmd="$1"
local result
result=$(run_hook "$cmd")
if [ "$result" = "blocked" ]; then
printf " \033[32mPASS\033[0m blocked: %s\n" "$cmd"
PASSED=$((PASSED + 1))
else
printf " \033[31mFAIL\033[0m expected blocked but allowed: %s\n" "$cmd"
FAILED=$((FAILED + 1))
fi
}
assert_allowed() {
local cmd="$1"
local result
result=$(run_hook "$cmd")
if [ "$result" = "allowed" ]; then
printf " \033[32mPASS\033[0m allowed: %s\n" "$cmd"
PASSED=$((PASSED + 1))
else
printf " \033[31mFAIL\033[0m expected allowed but blocked: %s\n" "$cmd"
FAILED=$((FAILED + 1))
fi
}
echo "=== allow-readonly-git tests ==="
echo ""
echo "--- Should be ALLOWED ---"
assert_allowed "git diff"
assert_allowed "git status"
assert_allowed "git status -s"
assert_allowed "git diff --stat"
assert_allowed "git diff main...HEAD"
assert_allowed " git diff HEAD~1"
echo ""
echo "--- Should be BLOCKED ---"
assert_blocked "git checkout ."
assert_blocked "git apply patch.diff"
assert_blocked "git commit -m wip"
assert_blocked "git diff > out.txt"
assert_blocked "git status; rm -rf x"
assert_blocked "git diff && rm x"
assert_blocked "git diff | tee out.txt"
assert_blocked 'git diff $(rm x)'
assert_blocked "rm -rf /"
assert_blocked "sed -i s/a/b/ file"
assert_blocked "echo hi > file"
assert_blocked "diff a b"
echo ""
echo "=== Results: $PASSED passed, $FAILED failed ==="
if [ "$FAILED" -gt 0 ]; then
exit 1
fi