跪拜 Guibai
← All articles
AI · Agent · AIGC

Build the Agent Permission Gate Before Plugging In Tools

By 知行砚 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Without a permission gate, any prompt injection or model error can turn a read-only search into a delete once tools are connected. This demo gives a concrete least-privilege and human-in-the-loop pattern that is easy to copy, and it warns about a logging pitfall that would silently break your audit trail.

Summary

The threat model rating for agent permissions-cum-proxy starts low only because the demo has no tool calling. Once RAG or an agent can query records, modify them, or send email, that risk jumps to critical. The fix is to install a permission framework before writing the first real tool integration: each tool declares its minimum role and a sensitive flag, all calls funnel through one execute_guarded() entry, and sensitive operations require human approval with a timeout that defaults to deny.

The demo covers five scenarios that exercise the permission matrix and HITL branches: a read-only tool passes, an over-permission write is denied, a sensitive write with operator role waits for confirmation and then executes on approval, an insufficient role is denied, and an admin-level sensitive call is terminated when a human denies it. Seven unit tests pass, covering the role matrix, unknown tools, repeated confirmations failing, and denial states; the full regression suite is 128 passing.

A real pitfall appears in the audit log: the structured formatter's field whitelist drops tool, role, and approver, leaving only event and request_id. That makes security auditing of "who did what to which tool" impossible without fixing the whitelist. The article also draws a clear boundary between a permission framework and a sandbox: the former decides whether a tool may be called, the latter where it runs. Production needs both, and the framework is a prerequisite, not a replacement.

Takeaways
— Roles are ranked user (1), operator (2), admin (3); each tool declares a minimum role plus a sensitive flag.
— All tool calls go through execute_guarded(); unknown tools are refused, and permission denials are logged as structured events.
— Sensitive operations (write, delete, external send, spend) enter a pending confirmation queue; human approval or denial is recorded, and a timeout defaults to deny.
— The five-scenario demo covers: read passes, unauthorized write denied, sensitive with operator requires confirmation and approval, role shortage denies, admin sensitive with denial stops execution.
— Seven unit tests in test_permissions.py pass, including repeated confirmation failing on the second call; the full suite has 128 passing tests.
— Audit logs showed only event and request_id because the logging formatter's field whitelist dropped tool, role, and approver; the fix is to add those fields to the whitelist.
— A permission framework is not a sandbox: it governs whether a tool can be called, while a sandbox governs where it runs; production needs both.
Conclusions

Writing permissions declaratively in tool definitions makes security a design-time decision, so adding a tool forces a conscious grant of authority rather than a guess later.

Treating a denied request as a non-event is a common mistake; logging denials as structured events gives security operations a signal for attack detection.

The logging field-whitelist bug is a reminder that audit completeness is a schema problem, not just a capture problem; you must explicitly choose which fields survive.

Concepts & terms
Least privilege
A security principle where each tool is granted only the minimum permissions required for its intended role. In this demo, each tool declares a role level (user, operator, admin) and a sensitive flag; unknown tools get no default permission.
HITL (Human-in-the-loop)
A workflow that inserts a human approval step before a sensitive operation is executed. When a tool is marked sensitive, the guard creates a confirmation request; a human can approve or deny, and the denial is logged as an event.
From the discussion
Featured comments
沉着宇宙

A very solid production-grade breakdown! The points about "permissions being enforced by the system, not by the model's good conscience" and unifying everything through execute_guarded(), with undeclared tools rejected by default, are especially critical. The pitfall in section 3.3 where the audit log whitelist dropped the tool/approver fields is painfully real. At Spark Workshop, when organizing Agent Skills, we increasingly emphasize marking read-only/write boundaries and human-confirmation nodes directly in the tool definitions, rather than relying on prompt-based caution to enforce safety boundaries.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗