11. Security & Ethics
Tool Execution Security
Tool execution security hardens the path from a model-proposed tool call to a real side effect: validate allowlists and schemas, enforce authZ and argument policies, require HITL for risky writes, run in sandboxes with timeouts and egress controls, then audit results. Authorization decides if a tool is allowed; execution security decides how safely it runs. Treat tool output as untrusted when it re-enters the agent loop.
What you will learn
- Describe the propose-validate-HITL-sandbox-audit path
- Design a tool gateway with schema and arg policies
- Match sandbox patterns to API, shell, browser, and files
- Apply timeouts, rate limits, and egress allowlists
- Answer interview questions with short, solid answers
Definition
Tool execution security is the set of controls that sit between an LLM’s tool proposal and the systems that change state or leave the network. It includes allowlisting, schema validation, argument constraints, sandboxes, timeouts, egress filtering, idempotency, HITL, and audit logging.
Even a correctly authorized user can be harmed if the agent runs unconstrained shell, fetches arbitrary URLs, or writes unbounded payloads.
Introduction
This lesson (#5 in Security & Ethics) follows Access Control, Authentication and Permissions. You know who may call which tools; now harden how those calls execute.
Next: AI Hallucinations — when fluent wrongness drives bad tool use even without an attacker.
Why execution needs its own layer
- Model args can be malformed, huge, or hostile
- Injection can steer tool choice and parameters
- Shell, browser, and file tools have high blast radius
- Hung tools stall agents and burn cost
- Tool results can re-inject instructions into context
Secure execution path
- Propose - model emits tool name + arguments
- Validate - allowlist, schema, authZ, arg policy
- HITL - human approve for high-risk classes
- Sandbox - isolate runtime and credentials
- Execute - timeouts, retries, idempotency keys
- Audit - log call, redacted args, result status
Tool gateway checks
Centralize checks so every framework path shares one choke point.
- Allowlist tool names and versions per agent role
- JSON Schema validate types, enums, max lengths
- Arg policy - URL allowlists, path roots, blocked hosts
- Limits - timeout, concurrency, payload size, rate
- Egress - deny SSRF to link-local and metadata IPs
- Strip secrets from args and error messages in logs
Sandbox patterns
Pick isolation strength from the tool’s power.
- API tools - scoped tokens; no ambient cloud admin role
- Code/shell - containers or VMs; command allowlists; no host mounts
- Browser - disposable profile; domain allowlist; no extensions
- Files - chroot/workspace root; block path traversal
- Network - default deny outbound except approved destinations
Side-effect hygiene
- Classify tools read vs write vs irreversible
- Idempotency keys on create/charge/send
- Dry-run or preview modes for risky writes
- Compensating actions documented for partial failure
- Quotas per user/tenant to stop runaway loops
Failure modes
- eval/exec of model-produced code on the host
- Unvalidated redirect URLs in fetch tools (SSRF)
- Logging full tool args including passwords
- Infinite retries without backoff or budget
- Trusting tool stdout as system instructions
Program / implementation notes
Implement a single execute_tool(name, args, ctx) gateway used by all agents. Add schema tests, a denied-SSRF fixture, and timeout tests. Mark write tools with requires_hitl=true in config.
- Replace open shell with typed tools where you can
- Next: AI Hallucinations
Advantages and limitations
- Advantage: one gateway hardens every agent framework path
- Advantage: sandboxes contain coding and browser mistakes
- Advantage: audit trails speed incident response
- Limitation: strict allowlists need product iteration
- Limitation: sandboxes add latency and ops cost
- Limitation: third-party MCP servers need their own review
Key points
- Path: propose → validate → HITL → sandbox → execute → audit.
- Gateway: allowlist, schema, arg policy, limits, egress.
- Sandbox by tool type: API, shell, browser, files.
- Classify read/write/irreversible; use idempotency on writes.
- Treat tool output as untrusted on the next hop.
- Never exec model code on the host without isolation.
FAQs
Is tool allowlisting enough?
Necessary but not sufficient. You still need schema/arg checks, sandboxes, timeouts, and HITL for high-impact tools.
What is a tool gateway?
A trusted service or library that mediates all tool calls — validating policy and executing safely — so the model never talks to raw infrastructure.
How do you stop SSRF via fetch tools?
Allowlist destinations, block link-local/metadata ranges, forbid raw IPs where policy requires, and validate redirects.
Should coding agents get a shell?
Only inside a locked-down workspace container with command allowlists, no host secrets, and strict egress — prefer typed build/test tools when possible.
Interview questions
- What is tool execution security?
Answer Controls that safely turn a model-proposed tool call into a side effect: validation, sandboxing, limits, HITL, and audit — beyond mere permission checks.
- What checks belong in a tool gateway?
Answer Tool allowlist, JSON Schema validation, argument policies, authZ, timeouts/rate limits, egress controls, and redacted logging.
- How do sandboxes differ by tool type?
Answer API tools use scoped tokens; shell/code use containers and command allowlists; browsers use isolated profiles and domain allowlists; files stay under a workspace root.
- Why treat tool results as untrusted?
Answer Results can contain indirect prompt injection or sensitive data; they must be segmented and filtered before influencing the next plan.
- How do you handle irreversible tools?
Answer Require HITL or step-up auth, preview when possible, use idempotency keys, and enforce per-user quotas and kill switches.
- AuthZ vs execution security?
Answer AuthZ answers whether the principal may use the tool. Execution security constrains how the tool runs so allowed calls cannot freely destroy or exfiltrate.
Related topics
Practice task
Complete each numbered practice step in a notebook or doc before opening the next lesson.
- Classify your tools as read, write, or irreversible and mark HITL needs.
- Write gateway rules for one fetch tool (schema + destination policy).
- Pick a sandbox pattern for shell vs API vs browser in your stack.
- Define timeout, max payload, and max calls-per-minute per tool.
- Answer interview questions 1, 2, and 4 in two sentences each.