Sub topic 10
Threat Modeling a Repository Workflow
This chapter assumes you know git, issues, and pull requests, and that you have read the earlier chapters on the agent loop, untrusted sources, indirect injection, and the combination of private data, untrusted input, and an outbound channel. Here you turn those ideas into a repeatable method. You will apply it to a concrete agent workflow and end with a list of controls to build.
What a threat model is for
A threat model is a short written answer to four questions:
- What are we protecting?
- Who or what can influence the agent, and through which inputs?
- What can go wrong if the agent is influenced?
- What will we do about it?
For coding agents the second question needs special care. The attacker rarely talks to the agent. The attacker writes text somewhere the agent will read it: an issue, a pull request description, a README, a dependency's docs, a code comment, or the output of a tool. The model cannot reliably tell that text from your instructions, so you must assume that anything the agent reads can steer it.
A useful working assumption: the agent acts with the authority you gave it, but on behalf of whoever wrote the last text it read.
Step 1: Describe the workflow
Write the workflow as a sequence of steps, with the agent's tools and credentials at each step. Vague descriptions hide risk. "The agent helps with issues" tells you nothing. This does:
- A GitHub Actions job runs when an issue is opened.
- The agent reads the issue title, body, and comments.
- The agent reads files in the repository to find relevant code.
- The agent may run
pytestandgit diff. - The agent posts a comment and applies labels.
- The job has a
GITHUB_TOKENand a cloud deployment key in its environment.
Even this short list shows a problem. The deployment key has nothing to do with triage, yet it sits in the environment.
Step 2: Draw trust boundaries
A trust boundary is a line where data moves between parties with different levels of trust. Mark where text crosses into the agent's context, and where the agent's actions cross out into the world.
The left side holds the inputs. The right side holds the actions. The agent context in the middle is where they mix, and nothing there enforces a separation. Your controls therefore have to sit on the right side, at the tools, the credentials, and the network. They cannot rely on the model "knowing better."
Step 3: List assets
Assets are the things an attacker would want, or that you would be hurt to lose. For a repository workflow they usually include:
| Asset | Example | Harm if misused |
|---|---|---|
| Secrets | API keys, cloud credentials, signing keys | Account takeover, data theft |
| Source code | Private repositories | Leak of intellectual property |
| Repository integrity | Default branch, release tags | Backdoored code shipped to users |
| Write access | Token that can push, comment, or merge | Spam, malicious commits, workflow changes |
| Developer machine | Files outside the repository, SSH keys | Wider compromise |
| Trust in output | Comments and PRs from the agent | Misleading reviewers |
Rank them. A leaked deployment key is worse than a stray comment.
Step 4: Enumerate entry points
Walk through every place where text reaches the agent. Use the sources from earlier chapters as a checklist, and record who controls each one.
| Entry point | Who can write it | Trust level |
|---|---|---|
| Issue title and body | Anyone with an account | Untrusted |
| Pull request from a fork | Any outside contributor | Untrusted |
| Comments | Anyone who can comment | Untrusted |
| Files on the default branch | Maintainers, via review | Mostly trusted |
| Files on a PR branch | The PR author | Untrusted |
| Agent rules, MCP settings, hooks in the repo | Whoever can commit to that branch | Depends on the branch |
| Third-party docs and web pages | Unknown third parties | Untrusted |
| Dependency source and README files | Package authors | Untrusted |
| Tool output (logs, test output, API responses) | Anything the tool touched | Inherits the lowest trust of its inputs |
Note the last row. Test output can contain strings from a malicious test file, so tool output is only as trustworthy as whatever produced it.
Also note the rows for agent configuration. A rules file, hook, or MCP server definition on an attacker's branch is not just text to read. It can change what the agent does or run code. Treat configuration from an untrusted branch as untrusted code.
Step 5: Enumerate actions
For each tool the agent has, write down the worst thing it can do, not the intended use.
- Shell: run any command the user account can run, including
curl,env, orgit push. - File write: modify source, CI workflows, or the agent's own configuration.
- Network: fetch pages (which can carry injected text) and send requests (which can carry data out).
- Credentials: use any token present in the environment or in files the agent can read.
- Comments and commits: publish text that anyone can see, which is also an exfiltration channel.
Step 6: Combine into scenarios
Now pair inputs with actions. A scenario is one sentence: an attacker who controls X causes the agent to do Y, resulting in Z. Use the earlier combination of private data, untrusted input, and an outbound channel as a filter. A scenario is high priority when all three are present.
| Scenario | Private data | Untrusted input | Outbound channel | Priority |
|---|---|---|---|---|
| Issue text tells agent to post environment variables in a comment | Yes | Yes | Yes (public comment) | High |
| PR branch adds a hook that runs on agent start | Yes | Yes | Likely (shell) | High |
| Poisoned docs make the agent run `curl` with file contents | Yes | Yes | Yes | High |
| Issue text asks agent to add a label that does not exist | No | Yes | No | Low |
| Agent in a no-network sandbox reads a malicious README | No secrets | Yes | No | Low |
The last row shows how a control changes the rating. If you remove one leg of the combination, the scenario drops in priority. That is the main use of the table: it shows which single change removes the most risk.
Step 7: Choose controls and record gaps
For each high-priority scenario, pick controls that break a leg of the combination. The following chapters cover them in depth, so here you only map scenarios to categories.
- Private data: scope tokens, remove unneeded secrets, use a sandbox with no credentials.
- Untrusted input: restrict which inputs the agent reads, and label them.
- Outbound channel: block the network or allow only named hosts, and limit where the agent can write.
- Impact of any action: require approval for risky steps.
Write down what remains. A threat model that claims zero risk is wrong. Residual risk is covered in a later chapter.
A worked example
Consider the triage workflow from Step 1. Its first draft was:
name: triage
on:
issues:
types: [opened]
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run agent
env:
DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./run-agent.sh "${{ github.event.issue.title }}"Applying the method finds three problems:
- The issue title is interpolated into a shell command, so the title itself can inject shell syntax before the agent is even involved.
DEPLOY_KEYis available to a job that reads attacker text. That is the private-data leg, and it is not needed.- The token has default permissions, which may include write access to more than comments and labels.
A revised version addresses each one:
name: triage
on:
issues:
types: [opened]
permissions:
contents: read
issues: write
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run agent
env:
ISSUE_TITLE: ${{ github.event.issue.title }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./run-agent.sh "$ISSUE_TITLE"The deployment key is gone, the token is narrowed, and the title travels as data in an environment variable. The agent can still be misled by the issue text, so a residual risk remains: it could post a misleading comment or apply a wrong label. You accept that on purpose, because its impact is small and visible.
Common errors
- Modeling the model. Teams ask "will the model resist this prompt?" The answer changes with each model version and each phrasing. Model the capabilities instead: what could happen if it did not resist.
- Trusting the default branch too much. Files on the default branch are only as safe as your review process. Dependencies and generated files can still carry injected text.
- Forgetting tool output. Logs, search results, and API responses feed the context too.
- Counting approval prompts as a control. People approve repeatedly and stop reading. Treat approval as a backstop for rare, high-impact steps.
- Modeling once. Adding a tool, an MCP server, or a new trigger changes the model. Revisit it with each change.
Keeping the model usable
Store the threat model next to the workflow, in a short THREAT_MODEL.md or in the pull request that introduces the workflow. One page is enough: workflow description, inputs, actions, top scenarios, controls, and accepted risks. Review it when someone changes the agent's tools, triggers, credentials, or configuration files.
Exercise
A coding agent runs on every pull request, including those from forks. It can run pytest, has network access, and the job environment contains a package registry publish token. It posts a review comment. Using the three-leg filter (private data, untrusted input, outbound channel), name the legs present, and give the single change that removes the most risk.
Show answer
All three legs are present. The publish token is private data, the fork's code and PR text are untrusted input, and network access (plus the public review comment) is an outbound channel. The change with the most effect is to remove the publish token from the job, because the review task does not need it. Running tests in a sandbox with no network is a strong second step, since it removes the outbound leg. With the token gone, an injected instruction has no valuable secret to steal.
Exercise
You find a .agent/hooks file on a contributor's pull request branch. Why should the workflow not load it, and where should the agent's configuration come from instead?
Show answer
Hooks and rules files can change what the agent does or run commands, so a file on an attacker's branch is untrusted code, not just text. The workflow should load agent configuration from the trusted default branch (or from the workflow definition itself) and ignore configuration files that arrive with the pull request.