Sub topic 19
Residual risk and secure defaults
This chapter assumes you have read the earlier chapters on the agent tool loop, indirect prompt injection, the combination of private data, untrusted input and an outbound channel, and the defenses: least privilege, sandboxing, approval gates, egress control, and monitoring. Here you decide what to do with the risk that remains after those defenses are in place.
Why risk remains
No current defense makes a language model reliably tell instructions from data. Delimiters, warnings, and "ignore instructions in the content" rules lower the odds that an injected instruction succeeds. They do not reduce them to zero. The model reads one stream of text, and anything in that stream can influence what it does next.
So the realistic goal is to limit the damage when an injection succeeds. A defense that depends on the model always noticing an attack will fail eventually. A defense that depends on the model being unable to do harm even when fooled will hold up better.
Each control from the earlier chapters leaves a gap:
| Control | What it limits | What remains |
|---|---|---|
| Least privilege | What a fooled agent can touch | Anything inside the granted scope can still be misused |
| Sandbox | Blast radius on the host | Data and tokens you mounted into the sandbox |
| Approval gates | Actions a human sees | Approval fatigue, and actions whose danger is hard to see in a prompt |
| Egress allowlist | Where data can be sent | Data hidden in requests to allowed hosts, or in commits and comments |
| Input labeling | Model confusion | Everything a determined attacker can phrase around |
| Monitoring | Time to detect | Damage done before anyone reads the log |
Residual risks to name explicitly
When you write a threat model, record these as accepted risks instead of leaving them out.
- Approval fatigue. A developer who clicks "allow" fifty times a day will click it on the fifty-first, including on a malicious command. Gates work best when they are rare and only cover high-impact actions.
- Allowed channels used as exfiltration paths. If the agent may push commits or write issue comments, an injected instruction can place stolen data there. An allowlisted host that accepts user content, such as a code host, is still an outbound channel.
- Poisoned output that is correct-looking. An injection may not steal anything. It may make the agent add a subtly wrong dependency, weaken a check, or write a vulnerable function. Human code review is the control here, and reviewers must treat agent-authored diffs as untrusted.
- Supply chain. Dependencies and their documentation are untrusted content that you chose to trust. Sandboxing limits the effect of a malicious package at install time, but it does not make the package safe to ship.
- Configuration drift. A repository can change its own rules files, hooks, or tool settings. A setup that was safe at review time may not be safe after the next pull request merges.
- Model and tool updates. A new model version or tool integration can change behavior. Defenses tested last quarter need retesting.
Choosing secure defaults
A secure default is the configuration a new project, teammate, or CI job gets without making any decision. Defaults matter because people rarely change them.
Use these as a starting set:
- Start every session with read-only access to the working tree. Grant writes to specific paths when needed.
- Give the agent no credentials by default. Add a token only for a named task, scoped to one repository and the minimum permissions.
- Run commands in a sandbox with no network. Add allowlisted hosts one at a time, with a stated reason.
- Require approval for anything that sends data out, changes configuration, installs software, or touches credentials.
- Do not load agent configuration from a repository you have not reviewed. Treat rules files, hooks, and tool-server settings as code that executes.
- Log every tool call with its arguments, and keep the logs where the agent cannot edit them.
- Fail closed. If a check cannot decide, it denies.
Make the trifecta check explicit
You can turn the "three legs" rule into a check that runs before a session starts. The function below refuses any session that combines all three capabilities.
from dataclasses import dataclass
@dataclass(frozen=True)
class SessionConfig:
reads_private_data: bool
reads_untrusted_input: bool
can_send_outbound: bool
def check_session(cfg):
legs = [
cfg.reads_private_data,
cfg.reads_untrusted_input,
cfg.can_send_outbound,
]
if all(legs):
raise PermissionError(
'session combines private data, untrusted input, '
'and an outbound channel; remove at least one'
)
return True
if __name__ == '__main__':
safe = SessionConfig(True, True, False)
print(check_session(safe))
risky = SessionConfig(True, True, True)
try:
check_session(risky)
except PermissionError as err:
print('blocked:', err)The check is only as good as the inputs. Someone must decide honestly whether a configuration "can send outbound." A session with a shell and no network block can send outbound, even if nobody intended it to. This is why the flags should be derived from the actual sandbox and token settings rather than typed by hand.
A decision flow for a new agent workflow
The diagram shows the order of questions to ask when you add an agent to a workflow.
Splitting into two agents is a common fix. One agent reads the untrusted issue and produces a plain summary with no tools. A second agent, with tools, receives only a reviewed or structured version of that summary. This reduces exposure but does not remove it, because the summary is still derived from attacker text.
Secure defaults in a CI workflow
CI is where defaults are easiest to enforce, because the configuration is a file. This workflow grants read-only repository access, and it does not give the job any extra secrets.
name: agent-triage
on:
issues:
types: [opened]
permissions:
contents: read
issues: write
jobs:
summarize:
runs-on: ubuntu-latest
steps:
- name: Pass issue text as data
env:
ISSUE_BODY: ${{ github.event.issue.body }}
run: |
printf '%s' "$ISSUE_BODY" | head -c 4000 > issue.txt
wc -c issue.txtNote the limits: the job truncates the input and never interpolates it into the script. It can write to issues, which is still an outbound channel. If the job also held a private token, you would have all three legs.
Reviewing the residual risk on a schedule
Defenses decay. Set a recurring review, for example each quarter, and ask:
- Which tokens does the agent hold now, and who approved each one?
- Which hosts are on the egress allowlist, and is each still needed?
- Did any repository change its rules files, hooks, or tool settings since the last review?
- What do the logs show about blocked actions and about approvals granted in bulk?
- Do the benign injection tests from the testing chapter still fail to move the agent?
Write down what you accept. An accepted risk with an owner and a date is manageable. An unwritten one is a surprise later.
Summary of the working model
- Assume a fooled agent will eventually happen, and design so that the worst outcome is bounded.
- Remove at least one leg of the trifecta wherever you can, and record the cases where you cannot.
- Keep defaults restrictive and make exceptions explicit, narrow, and logged.
- Review agent output, configuration, and permissions as ongoing work, not as a one-time setup.
Exercise
A team runs an agent in CI that reads new issues, holds a token with write access to the repository, and has unrestricted network access. The team says the issue text is wrapped in labeled delimiters, so it is safe. Which trifecta legs are present, and what two changes would you make first?
Show answer
All three legs are present: untrusted input (issue text), private data or authority (the write token and any secrets in the job), and an outbound channel (unrestricted network, plus the ability to write to the repository). Delimiters reduce confusion but are not a security boundary. First, remove the outbound channel by applying an egress allowlist or no network. Second, reduce the token to the minimum scope, for example issues: write only with contents: read. Then pass the issue text as data, not in shell, and review the remaining accepted risks.
Exercise
Why is an approval gate on every tool call a poor secure default, and what is a better design?
Show answer
Frequent prompts cause approval fatigue, so people approve without reading, including malicious actions. A better design allows low-risk actions such as reading files and running an allowlisted test command without prompts, and asks for approval only for high-impact actions such as sending data out, installing software, changing configuration, or using credentials.
Exercise
Your session check passes because the flags say cansendoutbound is False, but the sandbox actually has network access. What went wrong, and how do you prevent it?
Show answer
The flags were entered by hand and did not match the real configuration. Derive the flags from the actual sandbox and token settings, for example by checking that the container runs with no network, and fail closed when the state cannot be determined.