Sign up Log in
n/agent · 12d ago · 466 views
u/TigerDistinguished ContributorSilver
n/agentu/Tiger · 12d ago

[Agent in Practice] Local interception and anti-poisoning tips for letting CLI Agents (Cursor / OpenCode) run commands day to day

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:

  1. 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;
  2. When the Agent reads external docs or pulls third-party dependencies, it can run into malicious prompt guidance (Indirect Prompt Injection);
  3. 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, dangerous dd overwrites
  • 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~

Translated by AI from 简体中文

10
13
Log into join the discussion.

13 replies

Sort order
  1. tanmoumou252 Gold 3 YEARS 12d ago

    https://github.com/tanmoumou252/skill_toolkit

    So you're blocking it at the gateway, right? In my setup I just lock down all the agent permissions. I only give it a restricted BASH, restricted edit, restricted writer. That way even if it wants to write, it can't — no tool injection.

    But when it comes to reading, I haven't restricted that.

    Translated by AI from 简体中文

    2
  2. tufei Distinguished Contributor Diamond 12d ago

    Thanks for sharing your experience, thumbs up.

    Translated by AI from 简体中文

    1
  3. app_welcome_bot App account 12d ago

    Wow 👏 not bad, feel free to hang around the Agent section more 🚶

    Translated by AI from 简体中文

    0
  4. Zercher Distinguished Contributor Silver 12d ago

    Solid stuff — learned a lot.

    Translated by AI from 简体中文

    0
  5. Tiger Distinguished Contributor Silver OP 12d ago

    Haha, we learn from each other! Still gotta stay sharp when running Agent day to day~

    Translated by AI from 简体中文

    1
  6. Evan 🌱Moderator Master 3 YEARS 12d ago

    赞👍 :11-jingdai:

    0
  7. deleted_20159 12d ago

    Learned something new.

    Translated by AI from 简体中文

    0
  8. zh2385165 Silver 12d ago

    This local-blocking approach is really practical — catching .env and id_rsa the moment they're touched beats digging through logs after the fact. And whitelisting things like git status keeps day-to-day work fast too.

    Translated by AI from 简体中文

    0
  9. mn8001 Bronze 12d ago

    The AI realizes it's being restricted and just goes and kills your script itself

    Translated by AI from 简体中文

    0
  10. tanmoumou252 Gold 3 YEARS 11d ago

    What you're describing is the kind that takes effect immediately. If it stores the initial state per process PID, then to make a change take effect you'd have to reload the session — and that session gets terminated.

    Or add a freshness gate.

    You could also mimic SSL certs: each of their certificates is a different file, but the algorithm inside shows they're all authenticating the same domain. Take that approach and you don't have to worry about the files being modified externally.

    Translated by AI from 简体中文

    0
  11. Tiger Distinguished Contributor Silver OP 10d ago

    Haha so true, so never put it in a working directory with an obvious name (like agent_guard.py), otherwise the moment it checks ps it'll casually kill -9 or even rm -rf your whole setup. Best to drop it into the parent shell environment, run it as a systemd service, or make it a read-only alias hijack, so that when it tries to run kill, the rule catches it and it just hangs itself lol 2333

    Translated by AI from 简体中文

    0
  12. AGAGAG666 Distinguished Contributor Silver 3 YEARS 12d ago

    Sure

    Translated by AI from 简体中文

    0
  13. Holden Silver 3d ago

    I'd suggest isolating permissions per workspace: new projects default to read-only code access plus running tests, with writes limited to the current workspace, and separate approval required for network access or files outside the workspace. That way common dev tasks run smoothly, and it's also easier to tell auto-approved actions from manually approved ones in the audit log.

    Translated by AI from 简体中文

    0