hao li 開發 bash-write-targets,靜態列出 Bash 命令的檔案寫入目標
I built the tool my agent-guard README said didn't exist yet
bash-write-targets 可在 Bash 命令執行前,從命令字串列出可能寫入的檔案。它採用純靜態解析,支援重導向、heredoc、tee、cp/mv、sed -i 及多種 -o/--output 選項,不執行命令、不啟動 subshell,也不接觸檔案。作者列出的限制包括 eval、執行時組裝的路徑及 python3 -c 寫檔;19/19 項 smoke tests 通過。
I built the tool my agent-guard README said didn't exist yet
targets 'make 2>&1 | tee build.log' → prints build.log. That's the whole
pitch: give bash-write-targets a bash command string, get back the list
of files it may write to — before anything runs.
pip install bash-write-targets
$ targets 'grep -r foo . | sort -o sorted.txt; mv sorted.txt final.txt'
sorted.txt
final.txt
$ targets --json 'sed -i "s/a/b/g" config.ini && curl -s -o app.tgz https://example.com/app'
["config.ini", "app.tgz"]
Why this exists
My agent-guard project
ships a Bash PreToolUse hook that audits commands before an AI agent runs
them. Its README has an Honest limitations section, and one bullet reads:
Bash write-target extraction is static and approximate. ... This is
the documented frontier for a future version, not a finished parser.
So I built the future version — as a standalone, stdlib-only tool anyone can
use, hook or no hook.
What it catches: > >> &> 2>, heredocs (bodies are stripped so
<<EOF content isn't mistaken for targets), $(...) and backticks
(scanned recursively), tee (all operands, -a aware), cp/mv/install
destinations, sed -i (script vs. file disambiguation), dd of=,
truncate, touch, zip archives, and -o/--output flags for curl,
wget, gcc, sort, pandoc, psql, tar -f.
What it deliberately skips instead of guessing: /dev/null, fd
duplications (2>&1), unresolvable $VARs, bare globs.
The safety property
Pure static parsing over the command string. Nothing is executed, no
subshell is spawned, no file is touched — auditing a destructive command
cannot itself be destructive. And it's conservative by design: a missed
target is a miss, never a crash. Malformed input yields fewer results, not
an exception.
Honest limitations
eval, paths assembled at runtime, and python3 -c "open(p).write(x)"
are known gaps. Network sinks (curl --data POSTing somewhere) are out of
scope — this lists file write targets. 19/19 smoke tests green.
- GitHub: https://github.com/hahahahahahahahah6/bash-write-targets
- PyPI:
pip install bash-write-targets(https://pypi.org/project/bash-write-targets/) - MIT licensed.
來源:DEV Community · dev.to