Sub topic 14
Least Privilege, Scoped Credentials and Isolation
This chapter assumes you can call an LLM through a chat-style API, write basic Python, and know why untrusted input is dangerous in web applications. It also assumes you have read the earlier chapters on excessive agency and data exfiltration.
Why privilege limits matter more than prompts
Filters and prompt wording lower the odds that an injection works. They cannot guarantee it fails. Least privilege answers a different question: if the injection succeeds, what can the attacker do? Design so the answer is "very little."
The principle is the same as in conventional security. Give the application only the permissions the current task needs, and enforce those limits in code the model cannot rewrite.
Scope the tools
Start with the tool list. Each tool the model can call is a capability an attacker can request through an injected instruction.
- Expose only the tools the feature needs. A document summarizer needs no email tool.
- Prefer read-only tools. Separate read and write into different tools so you can allow one without the other.
- Narrow the parameters. A tool that takes a free-form SQL string is far riskier than
get_order_status(order_id). - Deny by default. Anything not on an allowlist is refused.
READ_ONLY_TOOLS = {"search_docs", "get_order_status"}
NEEDS_APPROVAL = {"send_email", "issue_refund"}
def run_tool_call(name, args, tools, approve):
"""Run a model-requested tool call under a deny-by-default policy."""
if name in READ_ONLY_TOOLS:
return tools[name](**args)
if name in NEEDS_APPROVAL and approve(name, args):
return tools[name](**args)
raise PermissionError(f"Tool call not permitted: {name}")The policy lives in run_tool_call, not in the prompt. The model can ask for anything, but the code decides what runs.
Scope the credentials
A tool is only as safe as the credential behind it. Avoid giving the agent your own admin token or a shared service account.
- Issue a separate credential for the LLM feature.
- Limit it to the needed resources, such as one database table or one storage bucket path.
- Make it short-lived, so a leaked token expires quickly.
- Where possible, act with the end user's permissions rather than a broader service identity. This prevents the confused deputy problem, where the model uses its own authority to do something the user was never allowed to do.
Isolate the environment
Isolation limits the damage when a tool or generated code misbehaves.
- Run code execution in a sandbox with no access to your secrets or internal network.
- Block outbound network access by default, or allow only named hosts. This also cuts off exfiltration paths.
- Keep one user's data out of another user's session. Do not share conversation memory or retrieval indexes across tenants without per-user filtering.
- Use separate model calls for separate trust levels. A call that reads untrusted web pages should not hold tools that act on private data.
Common mistakes
- Putting "only use safe tools" in the system prompt and treating it as a control.
- Reusing a developer's broad API key because it was convenient.
- Granting write access "just in case" a later feature needs it.
- Allowing unrestricted outbound requests from the sandbox.
Exercise
A support chatbot reads customer emails and can call lookup_order, send_email and delete_account, all using one admin API key. Name three changes that apply least privilege.
Show answer
(1) Remove delete_account, or put it behind human approval, and require approval for send_email. (2) Replace the admin key with a short-lived credential scoped to read orders for the current customer. (3) Restrict lookup_order parameters to an order ID belonging to the signed-in user, and block outbound network access except to required hosts.