Commit f5772f4
committed
hooks: strip heredoc bodies before dangerous-command scan
Closes #100.
The dangerous-command blocker regex-matches against the raw `command`
string. Bodies of bash heredocs (cat > note.md <<EOF ... EOF) are data,
not commands, so a journal entry or repro that quotes a forbidden
phrase like "git push --force" or the docker-compose teardown invocation
was tripping the blocker and refusing benign Bash invocations.
Strip heredoc bodies from the scannable text before the pattern loop:
/<<-?\s*['"]?(\w+)['"]?\n[\s\S]*?\n[ \t]*\1\s*$/gm
Handles `<<DELIM`, `<<-DELIM`, `<<"DELIM"`, `<<'DELIM'`. The `[ \t]*`
before the closing delimiter covers `<<-` heredocs where bash strips
leading tabs from the terminator line. Real destructive commands
outside the heredoc still match (verified by test).
Tests added (hooks.test.ts):
- allows heredoc body that names a forbidden phrase as data
- allows heredoc with quoted delimiter and dangerous-looking body
- blocks real dangerous command outside the heredoc
- blocks dash-stripped heredoc opener with real dangerous trailing command
Stash-bisect: 13/2 fail without the fix, 15/0 pass with the fix.
This is option 2 from #100 (heredoc-strip, no new dep). Echo/printf
false-positives still exist; the long-term option 1 (shell-token-aware
scan) is left for a future, larger PR. This narrow patch addresses the
highest-frequency case.1 parent ff74713 commit f5772f4
2 files changed
Lines changed: 92 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
210 | 210 | | |
211 | 211 | | |
212 | 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 | + | |
213 | 290 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
28 | 41 | | |
29 | 42 | | |
30 | 43 | | |
| |||
59 | 72 | | |
60 | 73 | | |
61 | 74 | | |
| 75 | + | |
62 | 76 | | |
63 | | - | |
| 77 | + | |
64 | 78 | | |
65 | 79 | | |
66 | 80 | | |
| |||
0 commit comments