Sub topic 6
Enforcing authorization in code, not in the prompt
This chapter assumes you know how LLM tool calling works, can read short Python snippets, and understand roles and permissions. It covers where authorization checks must live in an agent.
Why the prompt is not a security boundary
A system prompt such as "Never delete records" is an instruction to the model. It is not a control. The model reads that sentence next to untrusted text, such as web pages, emails, and tool results. Any of that text can contain competing instructions. A prompt injection can persuade the model to ignore your rule, and you cannot prove that it never will.
The model's output is a request, not a command. The runtime decides whether to carry it out. Put every authorization decision in ordinary code that sits between the model's request and the tool's execution.
A per-task allowlist
Each task gets a fixed set of permitted tools. The runtime checks the requested tool name against that set. The model never sees or edits the set.
from pathlib import Path
ALLOWED_ROOT = Path("/srv/agent/workspace").resolve()
def read_file(path: str) -> str:
target = (ALLOWED_ROOT / path).resolve()
if not target.is_relative_to(ALLOWED_ROOT):
raise PermissionError("path is outside the workspace")
return target.read_text()
def search_docs(query: str) -> str:
return f"results for {query}"
TOOLS = {
"read_file": read_file,
"search_docs": search_docs,
}
def run_tool_call(name: str, args: dict, allowed: set[str]):
if name not in allowed:
raise PermissionError(f"tool {name!r} is not allowed for this task")
return TOOLS[name](**args)
# A documentation-answering task gets read-only tools only.
result = run_tool_call("search_docs", {"query": "rotate API keys"}, allowed={"search_docs", "read_file"})
print(result)run_tool_call refuses any tool that is not in allowed, whatever the model says or why. The read_file function repeats the idea at the argument level. It resolves the path first, so .. segments and absolute paths cannot escape the workspace, and then it checks the result.
Checks that belong in code
- Tool allowlist. Decide it from the task type or the user's role, not from model output.
- Argument rules. Validate paths, recipients, record IDs, and amounts after the model has produced them.
- Identity. Take the user's identity from the authenticated session. Never take it from an argument the model fills in.
- Approval gates. Require confirmation for high-risk tools before they run.
Common errors
- Checking the prompt instead of the call. Filtering the user's text for "bad" requests misses injected content that arrives through tools.
- Letting the model pick its own permissions. If a tool accepts a
roleoruser_idargument, the model can set it to anything. Bind these values in the runtime. - Failing open. When a check raises an error or finds no rule, the call must be denied. A missing policy should mean no access.
- Checking once, then trusting. Run the check on every call, because the model's requests can change at each step.
- Validating the raw string. Compare normalized values, as
read_filedoes withresolve(). A string test for".."is easy to bypass.
What this does not solve
Code-level checks limit which calls can happen. They do not limit what a permitted tool can do with the credentials it holds. Pair them with scoped credentials, which a later chapter covers.
Exercise
A tool send_email(to, body) is allowed for a support task. The system prompt says "only email the customer who opened the ticket." An injected instruction in a ticket tells the agent to email an outside address. Where should the fix go, and what should it check?
Show answer
Put it in the runtime, in code that runs before send_email executes. The code should look up the ticket's customer address from the authenticated session or ticket record and reject any to value that does not match. It should deny by default and log the refused call. The prompt sentence can stay as guidance, but it must not be the only control.