Sub topic 2
Mapping trust boundaries in an LLM application
This chapter assumes you can read basic Python and have called a chat-style LLM API. It also assumes you know the web security idea that user input is untrusted. You will build an inventory of everything that enters a model's context, label each input by how far you trust it, and find the places where a mistake would cause harm.
What a trust boundary is
A trust boundary is a line in a system where data or control passes between components that deserve different levels of trust. In a web app, the line between the browser and your server is one. You validate what crosses it, because the browser can send anything.
LLM applications have the same lines, plus one that is easy to miss. The model reads instructions and data in one stream of text, so you cannot enforce a boundary inside the context window. A sentence from a web page and a sentence from your system prompt look the same to the model. You must enforce boundaries outside the model, in the code that assembles the prompt and the code that acts on the output.
Step 1: list every input to the context
Start by writing down everything that can place text in the prompt. People usually remember the user's message and the system prompt. They forget the rest.
| Source | Who controls it | Typical trust level |
|---|---|---|
| System prompt | You, the developer | Trusted, but see the leakage chapter |
| User message | The person using the app | Untrusted, even if authenticated |
| Retrieved documents (RAG) | Whoever wrote or edited the corpus | Depends on the corpus, often untrusted |
| Web pages fetched by a tool | Any site owner | Untrusted |
| Emails, tickets, chat logs | Senders, often external | Untrusted |
| Tool results (API responses) | The API and the data behind it | Untrusted unless fully owned |
| Conversation history | Earlier turns, which include all of the above | Inherits the lowest trust of its contents |
The last row matters. Once untrusted text enters the history, every later turn carries it. A context is only as trustworthy as its least trusted part.
Step 2: list every output and what it can reach
Next, list where model output goes. Output is a second set of boundaries, because the model's text can become an action.
- Rendered in a browser or chat client. Markdown, links and images can trigger network requests.
- Passed to a tool as arguments, such as a SQL query, an email recipient or a file path.
- Stored and later read back into another prompt.
- Sent to another model or agent.
For each destination, ask what the worst thing is that attacker-chosen text could do there. This is the same question you ask about any injection sink in web security.
Step 3: mark the boundaries on a diagram
Draw the components and mark where untrusted data enters and where output gains power. The diagram below shows a document assistant with one read tool and one email tool.
Two crossings deserve the most attention. The first is every arrow into the prompt builder from the untrusted group. The second is every arrow out of the model into something with side effects, here the renderer and the email tool.
Step 4: encode the inventory in code
A written inventory goes stale. Keeping it in code lets you review it in pull requests and test it. The sketch below tags each context item with a source and trust level, then flags a dangerous combination: untrusted text in the context while a high-impact tool is available.
from dataclasses import dataclass
@dataclass(frozen=True)
class ContextItem:
source: str
trusted: bool
text: str
HIGH_IMPACT_TOOLS = {"send_email", "issue_refund"}
def audit(items: list[ContextItem], enabled_tools: set[str]) -> list[str]:
"""Return warnings about risky combinations of inputs and tools."""
warnings = []
untrusted = [i.source for i in items if not i.trusted]
risky_tools = sorted(enabled_tools & HIGH_IMPACT_TOOLS)
if untrusted and risky_tools:
warnings.append(
f"Untrusted sources {untrusted} can influence "
f"high-impact tools {risky_tools}."
)
return warnings
context = [
ContextItem("system_prompt", True, "You summarize documents."),
ContextItem("user_message", False, "Summarize the attached file."),
ContextItem("uploaded_document", False, "Q3 revenue grew 4%."),
]
for w in audit(context, {"search_docs", "send_email"}):
print(w)Running it prints one warning, because the user message and the uploaded document sit in the same context as send_email. The fix is not to delete the check. You either remove the tool from this flow, require human approval for it, or split the work so the model that reads untrusted text has no high-impact tools.
Common errors in threat modeling
- Treating the authenticated user as trusted. A logged-in user can paste a malicious document or be tricked into doing so. Authentication tells you who they are, not that their input is safe.
- Trusting your own database. If customers, partners or the public can write to a table, then rows read from it are untrusted. The same applies to support tickets and wiki pages.
- Forgetting second-order flows. Model output stored today and fed into a prompt tomorrow crosses a boundary again. Label stored model output with the trust level of the inputs that produced it.
- Assuming the system prompt enforces a boundary. Telling the model to ignore instructions in data is a request, not a control. Later chapters cover why it can fail.
- Mapping only the first model call. Agents loop. Each tool result becomes new input, so the map must include the loop.
Using the map
A good map gives you a short list of decisions:
- Which untrusted inputs can reach a model that also has powerful tools or private data?
- Which output sinks need sanitization, such as the renderer in the diagram?
- Where can you remove a connection entirely? Deleting an arrow is stronger than guarding it.
Revisit the map when you add a tool, a data source or a second model. Each addition changes the arrows.
Exercise
A support assistant reads incoming customer emails, searches an internal knowledge base that any employee can edit, and can call a close_ticket tool. List the untrusted inputs, name the output sink with side effects, and say one change that reduces risk.
Show answer
Untrusted inputs: the customer emails (external senders) and the knowledge base, because any employee can edit it, so a compromised or careless account can plant text. The sink with side effects is close_ticket. One change: require human approval before close_ticket runs, or remove the tool from the flow that reads raw emails. Either way, injected text in an email can no longer close tickets on its own.