Sub topic 13
Approval gates and their limits
An approval gate is a point where an agent must stop and wait for a person to say yes before it takes an action. Most coding agents ship with gates for shell commands, file writes, and network calls. This chapter explains what a gate protects against, why people stop reading prompts, and how to design gates that still work when a tired human is clicking through them.
This chapter assumes you know the agent tool loop, the difference between direct and indirect prompt injection, and the combination of private data, untrusted input, and an outbound channel described earlier in this tutorial. It also assumes you can read short Python and YAML examples.
What a gate does
An agent proposes an action, such as running git push or writing a file. A gate sits between the proposal and the execution. A person sees the proposal, approves or rejects it, and only then does the tool run.
A gate is a control that sits on the action. It does not stop the injection. The model has already read the hostile text and has already decided to act on it. The gate only gives a person one chance to notice.
That distinction shapes everything else in this chapter. A gate is useful when the action is visible, understandable, and rare enough that the person reads it. It fails when the action is hidden, confusing, or constant.
Note that a rejected action returns to the model. The model may then propose a different action that reaches the same goal. A gate that blocks one command does not end the attack.
Three kinds of gate
Gates differ in who decides and when.
| Kind | Who decides | When | Typical use |
|---|---|---|---|
| Per-action prompt | A person | Each time the action is proposed | Shell commands, file writes |
| Pre-approved rule | A person, once | When configuring the agent | Allowing `git status` without asking |
| Policy gate | Code | Before the tool runs | Blocking hosts not on an allowlist |
A pre-approved rule removes a gate. A policy gate is not a gate in the human sense, but it is the most reliable of the three because it does not get tired. Later sections return to this point.
Why gates fail
Approval fatigue
If an agent asks for approval forty times in a session, the person starts approving without reading. This is the most common failure. It is a human-factors problem and no wording of the prompt fixes it.
Fatigue grows with the number of prompts and shrinks with their relevance. A gate that fires on every ls trains the person to approve the one curl that matters.
The prompt shows less than the action does
A gate is only as good as what it displays. Consider a command that looks harmless:
python -m pytest -q && echo doneIf the tool displays only the first token, the person sees python and approves. If the test suite itself is attacker-controlled, running pytest executes attacker code. The approval was for "run the tests," but the effect was "run whatever the repository says."
This is a common edge case. Many approved commands run code the repository supplies: make, npm test, pip install ., a conftest.py file, a build script. Approving the command approves everything it executes.
The model writes the explanation
Many agents show a short description next to the command. The model writes that description. If the model has been steered by injected text, the description can be misleading. Treat the description as a claim, and read the command.
Allow-lists that grow
Most tools offer "always allow this command." Each click widens the policy permanently. Over months, a policy can end up allowing shells, interpreters, or package managers, all of which can run arbitrary code. An allowed python or bash is equivalent to allowing everything.
Chained and indirect actions
An injection rarely needs one big action. It can split work into steps that each look fine:
- Write a script file (a file write).
- Run the script (a shell command that looks like a normal run).
- The script reads a secret and posts it to a URL.
Each step may pass review on its own. Only the combination is harmful.
Designing gates that hold up
Gate the dangerous capabilities, not everything
Put gates on actions that change the world or leave the machine: pushing commits, posting comments, calling the network, reading files outside the project, and modifying configuration. Let read-only, local actions run without a prompt. Fewer prompts means each one gets read.
Use exact matches, not prefixes
The allowlist wrapper shown in the spine's earlier example matches whole argument lists. The same idea applies to approvals. Approving git diff should not approve git diff --output=/some/path, which writes a file.
import subprocess
ALLOWED = {
('git', 'status'),
('git', 'diff'),
('pytest', '-q'),
}
def run_tool(argv):
if tuple(argv) not in ALLOWED:
raise PermissionError(f'command not allowed: {argv}')
return subprocess.run(argv, capture_output=True, text=True, timeout=60, check=False)The wrapper uses no shell, so injected text cannot add pipes, redirects, or a second command. A prefix match such as startswith('git') would allow git config or git push.
Show the real action
A good gate displays the full argument list, the working directory, and the target of any network call or write. For a file write, show the diff, not only the path. For a network call, show the host.
Combine gates with policy
Human approval handles what code cannot judge, such as whether a commit message looks right. Code handles what people judge badly, such as whether a hostname is on a list. A policy check runs first, and only actions that pass it reach the person.
from urllib.parse import urlparse
ALLOWED_HOSTS = {'pypi.org', 'files.pythonhosted.org'}
def url_allowed(url):
parts = urlparse(url)
return parts.scheme == 'https' and parts.hostname in ALLOWED_HOSTSIf the URL fails this check, the request never reaches a person, so the person cannot approve it by mistake.
Make approvals narrow and short-lived
Approve one action once, or for one session, not forever. Scope an approval to a directory or a host where the tool allows it. Review the saved allow rules from time to time and remove those you no longer need.
Gates in unattended agents
A CI agent triggered by an issue has no person watching. There is no gate to click. Two options exist:
- Remove the dangerous capability, so there is nothing to approve. This is the safer choice.
- Add a human step outside the agent, such as requiring a reviewer to approve the pull request the agent opens before anything merges or deploys.
A pull request is a gate too. It works when reviewers read the diff, including changes to workflow files, configuration, and dependency lists. It fails when changes are large or reviewers approve by habit.
Edge cases to check
- Auto-approve modes. Flags and settings that skip prompts turn every gate off. Never enable them in a repository you do not trust, or on a machine with credentials you cannot afford to lose.
- Repository-supplied settings. A configuration file inside the repository might pre-approve commands. Treat these as untrusted, as discussed in the chapter on repository configuration.
- Prompts that appear inside tool output. Text such as "type yes to continue" in output is data. The gate that matters is the one your tool shows, not text the agent reads.
- Denied actions. Check that a rejection actually stops the action, and that the agent cannot retry it through another tool.
Summary
Gates catch some attacks and miss others. They work best on rare, readable, high-impact actions, and best when backed by code-enforced limits. Use them as one layer alongside least privilege, sandboxing, and egress control.
Exercise
An agent's policy includes "always allow npm test". A malicious pull request edits the test script in package.json to send ~/.npmrc to an outside server. Explain why the approval gate fails here, and name two controls that would still limit the damage.
Show answer
The approval covered the command name, but npm test runs whatever the repository's test script says, so the approved command executes attacker-controlled code. The person was never shown that code. Two controls that still help: run the tests in a sandbox with no network (or a host allowlist) so the script cannot send the file out, and keep credentials such as ~/.npmrc out of the environment the agent runs in. Narrowing the approval so it is not permanent, and reviewing changes to package.json before running tests, also help.
Exercise
Why is a code-enforced allowlist for hostnames usually more reliable than asking a person to approve each network request?
Show answer
Code checks every request the same way and does not tire. A person approving many prompts begins to approve without reading, and may not recognize a lookalike hostname. The code check also removes disallowed requests before a person sees them, so the person cannot approve one by mistake.