Lately I've been leaning heavily on Agent tools in my daily work (Cursor's Agent mode, plus a few open-source CLI Agents I run in the terminal), and it really is a huge productivity boost — writing scripts and fixing bugs goes way faster.
But after using them for a while, my biggest worry is letting the Agent run terminal commands on its own (Terminal Auto-run). We all run into these situations sooner or later:
- Using third-party API relay/proxy services — cheap, but you can't guarantee the relay layer or the prompts haven't been injected into / poisoned;
- When the Agent reads external docs or pulls third-party dependencies, it can run into malicious prompt guidance (Indirect Prompt Injection);
- The model occasionally glitches or hallucinates and spits out a destructive command (deleting a directory by accident, reading sensitive config files directly, etc.).
If it leaks or overwrites my local .env secrets, SSH private keys, or important code, the cost is way too high.
So that I can hand things off to the Agent with peace of mind, I hacked together an extremely lightweight local command security interception and zero-trust gateway in Python. The core idea is pretty simple — just lock down a few key lines of defense:
1. Absolute blocking of sensitive files and directories
Strictly intercept any read/write probing against sensitive paths:
.env,.git/config,id_rsa,id_ed25519,credentials- Critical system paths like
/etc/shadow,/etc/passwd
The moment the Agent tries to touch these files with cat, curl -d @.env, grep, etc., it throws an interception error and aborts before execution.
2. Hard blocking of high-risk and destructive commands
Regex and syntax-tree pattern matching for irreversible destructive commands:
- Dangerous deletions:
rm -rf /,rm -rf *,mkfs, dangerousddoverwrites - Remote execution pipes:
curl ... | bash,wget ... | sh - Malicious reverse shell signatures (
/dev/tcp/...,nc -e, etc.)
3. Smooth pass-through for safe commands
For everyday dev and test commands like git status, git diff, npm run build, python -m pytest, ls -la, it goes straight through the whitelist in milliseconds, so it doesn't get in the way of day-to-day coding.
If you also run various Agents, or even multi-Agent setups, on a regular basis, I'd suggest adding a local protection layer like this too. I've open-sourced the code on GitHub — zero heavy third-party dependencies, a single file you can just run:
GitHub repo: https://github.com/03viceboll/ai-agent-firewall
Have you hit similar pitfalls when running automation with Agents? Or are there dangerous command patterns you think are easy to miss? Feel free to share in this thread~
最近日常重度依赖各种 Agent 工具(比如 Cursor 的 Agent 模式,以及平时终端里跑的一些开源 CLI Agent),确实极大地解放了生产力,写点脚本、改改 bug 速度飞快。
但用久了以后,最大的顾虑就是让 Agent 自主执行终端命令(Terminal Auto-run)。平时我们免不了会遇到下面几种情况:
- 用了一些第三方的 API 中转渠道,虽然便宜,但保不齐中转层或者提示词被注入/投毒;
- Agent 在查阅外部文档或者爬取第三方依赖时,可能遇到恶意的提示词引导(Indirect Prompt Injection);
- 模型偶尔自己抽风或幻觉,给出带有破坏性的命令(比如误删目录、直接读取敏感配置文件)。
万一把本地的 .env 密钥、SSH 私钥或者重要代码给泄露或覆盖了,代价就太大了。
为了能安心放权给 Agent 自动干活,我自己用 Python 搓了一套极其轻量的本地命令安全拦截与零信任网关,核心思路其实很简单,主要卡死几个核心防线:
1. 敏感文件与目录绝对阻断
严格拦截所有针对敏感路径的读写探测:
.env,.git/config,id_rsa,id_ed25519,credentials- 系统关键路径
/etc/shadow,/etc/passwd等
只要 Agent 试图用 cat, curl -d @.env, grep 等命令去碰这些文件,直接在执行前抛出拦截中断。
2. 高危与破坏性命令强拦截
对于不可逆的破坏性命令进行正则与语法树模式匹配:
- 危险删除:
rm -rf /,rm -rf *,mkfs,dd危险覆盖 - 远程执行管道:
curl ... | bash,wget ... | sh - 恶意的反弹 shell 特征(
/dev/tcp/...,nc -e等)
3. 安全命令平滑放行
像常规的 git status, git diff, npm run build, python -m pytest, ls -la 这类日常开发和测试命令,直接毫秒级白名单放行,不影响日常写代码的流畅度。
如果大家平时也经常跑各类 Agent 甚至多 Agent 协作,建议也尽量加上这类本地保护层。代码我也顺手开源在 GitHub 上了,完全零第三方重依赖,单文件就能直接跑起来:
GitHub 项目地址:https://github.com/03viceboll/ai-agent-firewall
大家平时用 Agent 跑自动化有没有踩过类似的坑?或者觉得还有哪些容易被漏掉的危险命令模式,欢迎在帖子里一起交流交流~
Translated by AI from 简体中文Original (简体中文)
13 replies
Sort order