Sub topic 2
Mapping Trust Boundaries in an LLM Application
This chapter assumes you can call a chat-style LLM API from Python and that you know the web security idea of untrusted input. You should also know why XSS happens. The chapter shows how to find where untrusted text enters an LLM application and what that text can reach.
What a trust boundary is
A trust boundary is a point where data or control passes between components that deserve different levels of trust. In a web app, the browser is untrusted and the server is trusted. You validate everything that crosses from one to the other.
LLM apps have the same boundaries, with one difference. The model reads trusted instructions and untrusted data in the same context window. It has no built-in check that tells them apart. Your code has to enforce the boundary, because the model will not.
Sources of input
List every source that ends up in the prompt, then label each one.
| Source | Typical trust level | Reason |
|---|---|---|
| Developer system prompt | Trusted | You wrote it and control deployment. |
| End-user message | Untrusted | Anyone can type anything. |
| Uploaded document | Untrusted | The author may not be the user. |
| Web page fetched by a tool | Untrusted | Any site owner controls it. |
| Retrieved knowledge-base chunk | Depends | It is only as safe as whoever can write to the index. |
| Tool result (API, database) | Depends | It may carry user-written fields. |
The "depends" rows are where mistakes happen. A database row is trusted storage, but if a customer wrote the "notes" field, the row carries attacker text.
Sinks: where the output goes
Inputs are half the map. Also list what the model's output can do:
- Rendered in a browser, where it can become markdown, links, images or HTML.
- Passed to a tool, such as email, a database query or a payment API.
- Stored and shown later to another user.
- Fed back to the model in the next turn.
An injection is dangerous when untrusted text can reach a sink that has real power.
The policy layer is code you control: output sanitizers, tool allowlists and approval checks. Later chapters build it. For now, note that the model sits inside the untrusted zone, not beside your trusted code.
A short threat-modeling procedure
- List every input that reaches the prompt, including indirect ones like retrieved chunks and tool results.
- Mark each input as trusted, untrusted or depends. Treat "depends" as untrusted until you can show otherwise.
- List every sink and the privileges behind it: data it can read, actions it can take.
- For each untrusted input, ask what the worst sink it could influence would do.
- Put a control at each path that matters, and do not rely on the prompt text alone.
Common errors
- Treating the model as trusted because you chose it. Once it reads untrusted text, its output is shaped by that text.
- Forgetting indirect inputs. Teams label the chat box untrusted and overlook documents and tool results.
- Mapping only inputs. A model with no powerful sinks limits the damage of an injection, so sinks usually decide the severity.
Exercise
A support assistant reads a customer's ticket text, looks up the order in a database, and can call an issue_refund tool. Name two untrusted inputs and the sink that makes an injection serious.
Show answer
Untrusted inputs include the ticket text and any customer-written fields in the order record, such as notes. The serious sink is the issue_refund tool, because injected text could push the model to trigger a payment.