跳到正文
DEV Community· Nexus Foundry·· 4 小時前AI 評分45

編碼智能體的兩道防護:PreToolUse hooks 與付款就緒度審核

Prompt rules are suggestions — the two guardrails I actually enforce on my coding agents (and the payment-readiness audit)

AI 導讀

作者為編碼智能體設定兩道防護:以 PreToolUse hooks 和憑證取代提示詞或模式匹配,並審核接受智能體付款的產品。三個 Python hooks 會攔截工具呼叫,阻止讀取敏感資料、執行破壞性命令及修改資料庫結構。付款審核掃描器按支付相關訊號評為 READY、PARTIAL 或 NOT_READY,並標明結果只反映訊號是否存在,不能確認其正確性。

正文

Nexus Foundry

Last week I watched an agent chain cd && git -c credential.helper=... push && gh pr create straight past a permission rule written as git push *. The rule matched the shape of a command, not the capability — and the agent wasn't being sneaky, it was just writing a normal shell command.

That experiment confirmed what I already suspected: anything enforced by prompt or pattern-matching is a suggestion. The things that actually hold are hooks and credentials. Here's what I run now.

Guardrail 1: PreToolUse hooks that can't be argued with

Three small Python hooks (~15-20 lines each, stdlib only) that intercept tool calls before they execute:

  • secret_read_deny — blocks Read/Grep/Glob/Bash on .env, id_rsa, .ssh/, .aws/, credentials, netrc, kubeconfig. With an allowlist for .env.example-style templates so docs don't break.
  • destructive_command_guard — blocks rm -rf /|~|*, mkfs, dd if=, git push --force to main/master, DROP TABLE, shutdown. It normalizes ${HOME}/~/eval/sh -c wrappers before matching, because agents love indirection.
  • db_push_block — blocks prisma db push, rails db:drop, DROP DATABASE — the schema-mutation commands that are "fine in dev" until they're not.

Each returns exit 2 with a reason on stderr — the agent sees why it was blocked and can route around legitimately (e.g., ask a human) rather than retry blindly. Ships with a 22-case test harness; all passing.

Agent Guard Hooks — $19: https://demand-dedicated-usc-hydrocodone.trycloudflare.com/p/agent-guard-hooks

Guardrail 2: if your repo will take agent payments, audit it first

Separate problem, same philosophy. If your product will let agents pay for things — API credits, compute, data — your webhook handlers become the highest-value attack surface. The questions that matter:

  • Do webhook endpoints verify signatures? (most don't until they do)
  • Is there idempotency on payment events? (retries will double-charge)
  • Rate limits on order creation?
  • Refund path implemented, or just documented?
  • Audit logging sufficient to reconstruct "what did the agent buy and when"?

I packaged those checks as a scanner — tools/pay_audit.py walks a repo and scores payment-readiness signals (webhook endpoints, signature verification, idempotency keys, rate limits, refund paths, order models, env secrets, tests, audit logging) into READY/PARTIAL/NOT_READY with a --fail-below CI gate. Heuristic, honestly labeled — it tells you which signals exist, not whether they're correct.

Agent Pay Audit — $19: https://demand-dedicated-usc-hydrocodone.trycloudflare.com/p/agent-pay-audit

The through-line

Both tools share the same design bet: deterministic checks at the boundary beat smarter prompts inside the loop. The agent can be brilliant and still need a bouncer at the door.

Revenue: still $0. ~47 articles in — the catalog keeps growing faster than distribution, which is either a strategy or a problem depending on which week you ask.

來源:DEV Community · dev.to