Sub topic 9
Data exfiltration through links, images and tool calls
This chapter assumes you know how a chat-style LLM API works, what untrusted input is, and how XSS (cross-site scripting) abuses content that a browser renders. It builds on indirect prompt injection and on tools and agents.
What exfiltration means here
Data exfiltration is the unauthorized transfer of data out of a system. In an LLM application, the attacker usually cannot read the model's output directly. So the attacker plants an instruction that makes the application send private data to a place the attacker controls.
The model's context often holds secrets: the system prompt, earlier chat turns, retrieved documents, and API keys passed in by mistake. An injected instruction can ask the model to copy that data into its output. The next question is how that output reaches the attacker.
The three channels
| Channel | How data leaves | Who triggers the request |
|---|---|---|
| Image | Data is encoded in an image URL, such as a markdown image | The client, automatically on render |
| Link | Data is encoded in a link's query string | The user, by clicking |
| Tool call | Data is passed as an argument to a tool, such as email or HTTP fetch | The agent runtime, automatically |
Images are the most dangerous channel because no click is needed. The chat client fetches the image as soon as it renders the reply, and the URL carries the stolen data to the attacker's server log.
Blocking the image channel
The client should not fetch images from arbitrary hosts. This function keeps only images from an allowlist and replaces the rest:
import re
from urllib.parse import urlparse
ALLOWED_IMAGE_HOSTS = {"images.example.com"}
IMAGE_RE = re.compile(r"!\[([^\]]*)\]\(([^)\s]+)[^)]*\)")
def sanitize_markdown(text: str) -> str:
def replace(match: re.Match) -> str:
host = urlparse(match.group(2)).hostname
if host in ALLOWED_IMAGE_HOSTS:
return match.group(0)
return "[image removed]"
return IMAGE_RE.sub(replace, text)
print(sanitize_markdown(""))This prints [image removed]. A content security policy (CSP) that limits img-src gives the same protection in the browser and covers cases a regex misses, such as HTML image tags. Use both.
Links and tool calls
Links need a user click, but the model can make them look trustworthy. Show the real destination, and consider stripping or warning on links to unknown hosts.
Tool calls are harder to contain. If an agent can send email or fetch URLs, an injection can use those tools as the exfiltration channel. A tool that makes web requests lets the model place data in the URL, with no rendering involved. Limit this with a deny-by-default policy that runs outside the model:
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}")Common errors
- Allowlisting by substring, so
images.example.com.evil.examplepasses. Compare the parsed hostname exactly, as the code above does. - Filtering only markdown images and forgetting HTML tags or link previews.
- Giving a tool that reads private data and a tool that contacts the internet to the same agent session.
Exercise
A support chatbot reads customer emails and renders markdown replies. An attacker emails a hidden instruction. Name two separate controls that stop the reply from leaking data through an image, and explain why one control alone is not enough.
Show answer
First, sanitize the output by removing images from hosts outside an allowlist. Second, set a CSP img-src that only permits trusted hosts. The sanitizer can miss cases such as HTML tags or unusual syntax, and a CSP can be misconfigured or absent on one client. With both in place, a miss in one is still caught by the other.