-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_guard.sh
More file actions
executable file
·337 lines (241 loc) · 10.5 KB
/
Copy pathtest_guard.sh
File metadata and controls
executable file
·337 lines (241 loc) · 10.5 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env bash
# Automated test matrix for secrets-guard hook (guard.sh)
# Tests various tool inputs against the guard and asserts expected outcomes.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
GUARD="$SCRIPT_DIR/hooks/guard.sh"
passed=0
failed=0
errors=()
# ── Helpers ──────────────────────────────────────────────────────────────────
run_guard() {
local input="$1"
printf '%s' "$input" | bash "$GUARD" 2>/dev/null
}
run_guard_exitcode() {
local input="$1"
printf '%s' "$input" | bash "$GUARD" 2>/dev/null
return $?
}
make_read() {
jq -nc --arg path "$1" '{"tool_name":"Read","tool_input":{"file_path":$path}}'
}
make_write() {
jq -nc --arg path "$1" '{"tool_name":"Write","tool_input":{"file_path":$path}}'
}
make_edit() {
jq -nc --arg path "$1" '{"tool_name":"Edit","tool_input":{"file_path":$path}}'
}
make_notebook() {
jq -nc --arg path "$1" '{"tool_name":"NotebookEdit","tool_input":{"notebook_path":$path}}'
}
make_bash() {
jq -nc --arg cmd "$1" '{"tool_name":"Bash","tool_input":{"command":$cmd}}'
}
make_glob() {
jq -nc --arg pattern "$1" --arg path "$2" '{"tool_name":"Glob","tool_input":{"pattern":$pattern,"path":$path}}'
}
make_grep() {
jq -nc --arg path "$1" --arg glob "$2" '{"tool_name":"Grep","tool_input":{"pattern":"search","path":$path,"glob":$glob}}'
}
assert_deny() {
local label="$1"
local input="$2"
local output
output="$(run_guard "$input")" || true
local decision
decision="$(printf '%s' "$output" | jq -r '.hookSpecificOutput.permissionDecision // empty' 2>/dev/null)" || true
if [[ "$decision" == "deny" ]]; then
passed=$((passed + 1))
else
failed=$((failed + 1))
errors+=("FAIL [DENY expected]: $label — got: ${output:-<empty>}")
fi
}
assert_allow() {
local label="$1"
local input="$2"
local output
output="$(run_guard "$input")" || true
# Allow = empty output (exit 0 with no stdout)
if [[ -z "$output" ]]; then
passed=$((passed + 1))
else
failed=$((failed + 1))
errors+=("FAIL [ALLOW expected]: $label — got: $output")
fi
}
assert_deny_json_valid() {
local label="$1"
local input="$2"
local output
output="$(run_guard "$input")" || true
# Check JSON structure has all required fields
local has_event has_decision has_reason
has_event="$(printf '%s' "$output" | jq -r '.hookSpecificOutput.hookEventName // empty' 2>/dev/null)" || true
has_decision="$(printf '%s' "$output" | jq -r '.hookSpecificOutput.permissionDecision // empty' 2>/dev/null)" || true
has_reason="$(printf '%s' "$output" | jq -r '.hookSpecificOutput.permissionDecisionReason // empty' 2>/dev/null)" || true
if [[ "$has_event" == "PreToolUse" && "$has_decision" == "deny" && -n "$has_reason" ]]; then
passed=$((passed + 1))
else
failed=$((failed + 1))
errors+=("FAIL [valid deny JSON]: $label — got: ${output:-<empty>}")
fi
}
# ── Test Matrix ──────────────────────────────────────────────────────────────
echo "secrets-guard test matrix"
echo "========================="
echo ""
# ── 1. Environment files (DENY) ─────────────────────────────────────────────
echo "--- Environment files ---"
assert_deny "Read .env" \
"$(make_read "/app/.env")"
assert_deny "Read .env.local" \
"$(make_read "/app/.env.local")"
assert_deny "Read .env.production" \
"$(make_read "/app/.env.production")"
assert_deny "Read nested .env" \
"$(make_read "/project/apps/web/.env")"
assert_deny "Write .env" \
"$(make_write "/app/.env")"
assert_deny "Edit .env" \
"$(make_edit "/app/.env")"
assert_deny "Read .flaskenv" \
"$(make_read "/app/.flaskenv")"
# ── 2. Private keys & certificates (DENY) ───────────────────────────────────
echo "--- Private keys ---"
assert_deny "Read .pem file" \
"$(make_read "/certs/server.pem")"
assert_deny "Read .key file" \
"$(make_read "/ssl/private.key")"
assert_deny "Read .p12 file" \
"$(make_read "/certs/cert.p12")"
assert_deny "Read id_rsa" \
"$(make_read "/home/user/.ssh/id_rsa")"
assert_deny "Read id_ed25519" \
"$(make_read "/home/user/.ssh/id_ed25519")"
# ── 3. SSH directory (DENY) ─────────────────────────────────────────────────
echo "--- SSH ---"
assert_deny "Read .ssh/config" \
"$(make_read "/home/user/.ssh/config")"
assert_deny "Read .ssh/known_hosts" \
"$(make_read "/home/user/.ssh/known_hosts")"
# ── 4. App secrets (DENY) ───────────────────────────────────────────────────
echo "--- App secrets ---"
assert_deny "Read credentials.json" \
"$(make_read "/app/credentials.json")"
assert_deny "Read token.json" \
"$(make_read "/app/token.json")"
assert_deny "Read secrets.yml" \
"$(make_read "/app/secrets.yml")"
assert_deny "Read secrets.yaml" \
"$(make_read "/app/secrets.yaml")"
assert_deny "Read master.key" \
"$(make_read "/app/master.key")"
# ── 5. Cloud credentials (DENY) ─────────────────────────────────────────────
echo "--- Cloud credentials ---"
assert_deny "Read .aws/credentials" \
"$(make_read "/home/user/.aws/credentials")"
assert_deny "Read .kube/config" \
"$(make_read "/home/user/.kube/config")"
assert_deny "Read .docker/config.json" \
"$(make_read "/home/user/.docker/config.json")"
# ── 6. Infrastructure (DENY) ────────────────────────────────────────────────
echo "--- Infrastructure ---"
assert_deny "Read .tfvars" \
"$(make_read "/infra/prod.tfvars")"
assert_deny "Read terraform.tfstate" \
"$(make_read "/infra/terraform.tfstate")"
# ── 7. Shell history (DENY) ─────────────────────────────────────────────────
echo "--- Shell history ---"
assert_deny "Read .bash_history" \
"$(make_read "/home/user/.bash_history")"
assert_deny "Read .zsh_history" \
"$(make_read "/home/user/.zsh_history")"
# ── 8. Bash commands referencing sensitive files (DENY) ─────────────────────
echo "--- Bash commands ---"
assert_deny "Bash cat .env" \
"$(make_bash "cat /app/.env")"
assert_deny "Bash cat .pem" \
"$(make_bash "cat /certs/server.pem")"
assert_deny "Bash source .env" \
"$(make_bash "source .env.local")"
assert_deny "Bash grep in .aws/credentials" \
"$(make_bash "grep key ~/.aws/credentials")"
assert_deny "Bash scp id_rsa" \
"$(make_bash "scp user@host:~/.ssh/id_rsa .")"
# ── 9. Glob patterns (DENY) ─────────────────────────────────────────────────
echo "--- Glob patterns ---"
assert_deny "Glob for .env files" \
"$(make_glob "**/.env" "/app")"
assert_deny "Glob path in .ssh" \
"$(make_glob "*.pub" "/home/user/.ssh/")"
# ── 10. Grep patterns (DENY) ────────────────────────────────────────────────
echo "--- Grep patterns ---"
assert_deny "Grep in .env path" \
"$(make_grep "/app/.env" "")"
assert_deny "Grep with .pem glob" \
"$(make_grep "/app" "*.pem")"
# ── 11. NotebookEdit (DENY) ─────────────────────────────────────────────────
echo "--- Notebook ---"
assert_deny "NotebookEdit .env path" \
"$(make_notebook "/app/.env.ipynb")"
# ── 12. Safe files (ALLOW) ──────────────────────────────────────────────────
echo "--- Safe files (should ALLOW) ---"
assert_allow "Read README.md" \
"$(make_read "/app/README.md")"
assert_allow "Read package.json" \
"$(make_read "/app/package.json")"
assert_allow "Read src/index.ts" \
"$(make_read "/app/src/index.ts")"
assert_allow "Read .gitignore" \
"$(make_read "/app/.gitignore")"
assert_allow "Write src/app.py" \
"$(make_write "/app/src/app.py")"
assert_allow "Edit tsconfig.json" \
"$(make_edit "/app/tsconfig.json")"
assert_allow "Bash ls" \
"$(make_bash "ls -la /app")"
assert_allow "Bash git status" \
"$(make_bash "git status")"
assert_allow "Bash npm install" \
"$(make_bash "npm install express")"
assert_allow "Glob for ts files" \
"$(make_glob "**/*.ts" "/app")"
assert_allow "Grep in src" \
"$(make_grep "/app/src" "*.ts")"
assert_deny "Read .env.example (intentionally blocked)" \
"$(make_read "/app/.env.example")"
# ── 13. JSON output validation (for deny cases) ─────────────────────────────
echo "--- JSON output validation ---"
assert_deny_json_valid "Deny JSON has required fields" \
"$(make_read "/app/.env")"
assert_deny_json_valid "Deny JSON for .pem" \
"$(make_read "/certs/server.pem")"
# ── 14. Edge cases ──────────────────────────────────────────────────────────
echo "--- Edge cases ---"
assert_allow "Read file named 'environment'" \
"$(make_read "/app/environment")"
assert_allow "Read .envrc (not in patterns)" \
"$(make_read "/app/.envrc")"
assert_deny "Read .secret file" \
"$(make_read "/app/.secret")"
assert_deny "Read .secrets file" \
"$(make_read "/app/.secrets")"
assert_allow "Unknown tool (should allow)" \
'{"tool_name":"WebSearch","tool_input":{"query":"test"}}'
assert_allow "Empty file_path" \
'{"tool_name":"Read","tool_input":{"file_path":""}}'
# ── Results ──────────────────────────────────────────────────────────────────
echo ""
echo "========================="
echo "Results: $passed passed, $failed failed"
echo ""
if [[ ${#errors[@]} -gt 0 ]]; then
for err in "${errors[@]}"; do
echo " $err"
done
echo ""
exit 1
fi
echo "All tests passed."