LevelUpWith

Explore / Securing LLM Apps and Agents: Attack, Defend, Harden / Lesson 2

Lesson 2: Trust Boundaries and Data Flows

Build a data-flow threat model of an agent that marks where trusted instructions and untrusted content cross, and who the principals are: user, developer, tool owner and third parties.

Free to read, no account needed. Part of Securing LLM Apps and Agents: Attack, Defend, Harden.

By the end of this level you'll be able to

  • Produce a data-flow diagram for a sample email-and-calendar agent with its trust boundaries marked
  • Sort each piece of context into trusted, semi-trusted or untrusted by who can influence it
  • Name the attacker for each boundary and describe what that attacker controls

Why draw the data flows first

In Level 1 you broke an agent into its parts: model, system prompt, tools, memory and the outside world. This level adds the question security engineers ask before anything else: who can write the text that reaches the model?

Threat modelling for ordinary software has long used data-flow diagrams (DFDs). Microsoft's threat-modelling guidance uses five elements:

  • processes: code that handles data
  • data stores
  • external entities: anything outside your direct control
  • data flows
  • trust boundaries

A trust boundary is the line where the level of trust changes. Data that crosses it may need to be treated as untrusted and checked, or blocked from flowing at all.

Agents need the same picture with one twist. In a web app, a trust boundary guards code paths. In an agent, everything from every source ends up as text in one context window. So the diagram has to follow who authored each piece of text, not just which server it came from.

Analogy: treat the context window like a shared Google Doc that the model reads top to bottom. The question is not "which tab did this paragraph come from?" It is "who had edit access to this paragraph?"

The four principals

A principal is anyone whose words can end up in the context. For an agent there are usually four:

Principal Examples What they control
Developer (you) System prompt, tool schemas you wrote, orchestration code The rules, which tools exist, what the code does with model output
User The person typing "find me a slot with Priya next week" Their own requests, and files they paste or upload
Tool owner The vendor of a calendar API or a third-party MCP server Tool names, descriptions and parameter docs; the shape and content of tool results
Third parties Anyone who emails the user, sends an invite or publishes a web page Email bodies, subjects, invite descriptions, attachments, page text

The vendors broadly agree on the top of this ladder. OpenAI's Model Spec ranks authority as root, system, developer, user and guideline, in that order. Tool outputs, quoted text, file attachments and multimodal data have no authority by default, and any instructions inside them should be treated as information, not commands. That is a statement of how the model is meant to behave. OpenAI says plainly that its models don't fully match the Spec yet. Your diagram has to assume the model will sometimes get it wrong.

Worked example: an email-and-calendar agent

The sample agent reads the user's inbox, checks their calendar and drafts replies. Here is its DFD, with trust boundaries drawn as dashed lines (- - -):

                    ┌──────────────── TB1: developer / runtime ────────────────┐
  [User] ──request──▶│  (Orchestrator code) ──prompt──▶ (Model API)              │
                    │        │  ▲                                                │
                    │  system prompt + tool schemas (repo, code-reviewed)       │
                    └────────┼──┼────────────────────────────────────────────────┘
                - - - - - - -│- │- - - - - - - - TB2: tool owner - - - - - - - - -
                             ▼  │ results
                       (Mail API)   (Calendar API)   (3rd-party MCP: "contacts")
                - - - - - - -│- - - - - -│- - - - - - TB3: the open world - - - - -
                             ▼           ▼
                   [Any sender on earth] [Anyone who can send an invite]

Every arrow that crosses a dashed line is a place where someone you don't control gets to put words in front of the model. Production experience shows TB3 is the line people forget. EchoLeak (CVE-2025-32711), a critical Microsoft 365 Copilot flaw that Microsoft fixed server-side in 2025, began with nothing more than a crafted email arriving in the victim's inbox. The victim didn't have to click anything.

Sorting context into trust tiers

Now list every piece of text that can reach the model, and label it by who can influence it:

  • Trusted: only the developer can change it, and changes go through review.
  • Semi-trusted: a known principal controls it. You rely on their intent, but they can make mistakes or change it later.
  • Untrusted: anyone, including strangers, can influence it.
# context_inventory.py: one row per piece of text the model can see
from dataclasses import dataclass

@dataclass
class ContextItem:
    name: str
    author: str   # who can WRITE these bytes, not who fetched them
    tier: str     # trusted | semi-trusted | untrusted

INVENTORY = [
    ContextItem("system prompt",            "developer",  "trusted"),
    ContextItem("our own tool schemas",     "developer",  "trusted"),
    ContextItem("user chat message",        "user",       "semi-trusted"),
    ContextItem("MCP 'contacts' tool desc", "tool owner", "semi-trusted"),  # can change without your review
    ContextItem("calendar free/busy",       "tool owner", "semi-trusted"),
    ContextItem("email body / subject",     "any sender", "untrusted"),
    ContextItem("invite title + notes",     "any sender", "untrusted"),     # invites land unasked
    ContextItem("attachment text",          "any sender", "untrusted"),
    ContextItem("user's own sent mail",     "mixed",      "untrusted"),     # quoted replies carry 3rd-party text
]

for item in INVENTORY:
    print(f"{item.tier:13} {item.name:26} <- {item.author}")

Output:

trusted       system prompt              <- developer
trusted       our own tool schemas       <- developer
semi-trusted  user chat message          <- user
...
untrusted     user's own sent mail       <- mixed

Gotcha: "it came from our API" doesn't make it trusted. Your Mail API is authenticated and runs on infrastructure you trust. The body of an email it returns was still written by a stranger. Label each piece of text by its author, not by the transport that carried it.

Gotcha: mixed items take the lowest tier. A thread the user wrote but that quotes an outside sender is untrusted as a whole. One stranger-written paragraph is enough.

Carry the label in your code so that later layers can act on it. In TypeScript:

type Tier = "trusted" | "semi-trusted" | "untrusted";
interface Tagged { text: string; tier: Tier; source: string }

// Tag at the boundary, where the data enters, and never re-label it upward
function fromMailApi(msg: { from: string; body: string }): Tagged {
  return { text: msg.body, tier: "untrusted", source: `email:${msg.from}` };
}

The vendor APIs give you different places to keep this separation. OpenAI has system/developer, user and tool messages. Claude has a system parameter plus tool_result blocks. Gemini has system_instruction plus functionResponse parts. The role a piece of text arrives in is a hint to the model, not a guarantee. Keep your own provenance tags anyway.

Naming the attacker at each boundary

For every boundary, write one line: who attacks here, and what they control.

Boundary Attacker Controls
User → orchestrator A malicious user (or someone using a stolen session) Their own messages and uploads. They can't change the system prompt directly.
Tool owner → model (TB2) A compromised or dishonest tool vendor Tool descriptions and returned results. They can change these after you approved the tool.
World → mail and calendar (TB3) Any outsider who knows the user's email address Subject, body, attachments, invite text. That costs them nothing and needs no account with you.
Developer zone (TB1) An insider, or whoever compromises your repo or CI Everything else. This is ordinary software supply chain, out of scope for the model.

The TB3 row is the one to remember. The cheapest attacker has the widest reach, and needs nothing more than the user's email address. Whether and how that attacker succeeds is the subject of the next few levels. For now, the job is to make the exposure visible.

Hands-on exercise

  1. Copy the DFD above. Add a web-browsing tool and a shared team calendar, and draw any new boundaries.
  2. Extend INVENTORY with every new context item. Give each one an author and a tier, and add a one-line reason for any item you rate semi-trusted.
  3. For each boundary, fill in a row of the attacker table.
  4. Find the single untrusted input that reaches the model with the least effort for an outsider. Circle it. You will attack it in a later level.

Key takeaways

  • A data-flow diagram with trust boundaries is the first artefact of agent threat modelling.
  • There are four principals: developer, user, tool owner and third parties. Each controls different text.
  • Sort context into tiers by who can write it, never by which system fetched it. Anything mixed takes the lowest tier.
  • Vendor guidance gives tool outputs and quoted content no authority by default, but models don't fully follow that guidance, so design as if they won't.
  • For every boundary, name the attacker and what they control. The outsider who can email your user is the cheapest attacker and usually the most important.

A 6-question knowledge check follows this lesson in the app, free and without an account.

Keep going: levels 3 and 4 are free

Create a free account to open the next two levels, try the AI tutor, save your progress and pick up where you left off. No card needed.

Next: Level 3, One Channel: Why Instructions and Data Mix.

Create a free account →See the whole course

More like this