Sub topic 2
Tools and Agents Expand the Attack Surface
Assume basic Python, chat-style LLM APIs, and the security rule that external content is untrusted. A tool is a program an LLM can ask to run. An agent is an LLM application that selects tools and performs several steps.
A model may be persuaded to call a tool with attacker-controlled arguments. If the tool can send mail, issue refunds, read private files, or make network requests, an injection becomes an application-level security failure.
Excessive Agency and the Confused Deputy
Excessive agency means giving an agent more permissions or autonomy than its task requires. The agent then becomes a confused deputy: a trusted component tricked into using its authority for an attacker.
Use separate permissions for read-only and high-impact operations:
READ_ONLY = {"search_docs", "get_order_status"}
REQUIRES_APPROVAL = {"send_email", "issue_refund"}
def run_tool(name, args, tools, approve):
if name in READ_ONLY:
return tools[name](**args)
if name in REQUIRES_APPROVAL and approve(name, args):
return tools[name](**args)
raise PermissionError(f"Tool call not allowed: {name}")Data Exfiltration and Untrusted Output
An injected model can place stolen data in a URL, Markdown image, email, or tool argument. A client that automatically loads the image may leak data to the attacker's server. Model output can also contain SQL, shell commands, HTML, or tool calls. Treat it as untrusted input and validate it before rendering or execution.
Prompts and Retrieval Sources Are Not Secrets
System prompts guide behavior, but they are delivered to the model and may be inferred or disclosed. Never place API keys, passwords, or authorization decisions in a prompt.
Retrieval-augmented generation (RAG) uses a search system to add documents to the model's context. A poisoned document can contain false instructions or misleading facts. Track document provenance, restrict sources, and validate retrieved content.
Defense in Depth
Assume an injection will sometimes succeed. Limit its impact with:
- least-privilege credentials and isolated tools;
- allowlists for URLs, file paths, and tool arguments;
- output encoding and schema validation;
- human approval for irreversible actions;
- logging of prompts, retrieval sources, and tool calls;
- regression tests containing known injection payloads.
A prompt delimiter helps the model recognize data, but it is not an authorization boundary. Enforcement must happen in application code.
Exercise
Why should a model-generated refund request require application-level approval?
Show answer
The model can be manipulated by untrusted content, so the application must independently authorize the high-impact action.