Sub topic 11
Least Privilege for Agents and Tokens
This chapter assumes you know git and a code hosting platform, can read basic Python and YAML, and understand how a coding agent runs tools in a loop. It builds on the idea that an injected instruction can only do what the agent is allowed to do.
Why privilege matters
You cannot guarantee that an agent will ignore malicious text it reads. You can control what the agent is able to do when it obeys that text. Least privilege means giving the agent only the access its current task needs, and nothing more. A smaller set of permissions shrinks the damage from any single injection.
Agents often inherit more than they need. A developer's shell holds cloud credentials, SSH keys, a personal access token with broad scopes, and write access to many repositories. An agent started from that shell has all of it.
Privileges to review
| Privilege | Common over-grant | Tighter choice |
|---|---|---|
| Shell | Any command, run through a shell | Exact allowlisted argument lists |
| File access | Whole home directory | One working directory |
| Network | Any host | Named hosts only |
| Repository token | Write access to all repos | Read access to one repo |
| Cloud credentials | Admin role | No credentials, or a narrow role |
Scope the tools
Give the agent a wrapper that runs only known commands. Run them without a shell, so injected text cannot add a ; or a && and chain 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)The check matches the whole argument list. Allowing the program name git alone would also allow git push to a remote the attacker chose.
Scope the tokens
A token should be short-lived, limited to one repository, and limited to the actions the task needs. A review agent that comments on pull requests does not need to push code. A summarizing agent that only reads does not need write access at all.
In a GitHub Actions workflow, set permissions explicitly. Anything you leave out is denied once you declare the block.
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"Here the job can read code and write issues, but it cannot push commits. The issue title reaches the script through an environment variable, so the shell treats it as a value and not as code.
Common mistakes
- Reusing your personal token for the agent. Create a separate identity for it.
- Putting secrets in environment variables the agent can read, such as through
envor a file dump. - Granting write access "for convenience" and never removing it.
- Treating an allowlist of program names as safe when arguments can change what a command does.
Exercise
Exercise
An agent summarizes new issues and posts the summary as a comment. Which token permissions does it need, and which should you refuse?
Show answer
It needs read access to the repository and write access to issues, scoped to that one repository. Refuse contents write, pull request write, workflow write, and access to other repositories or to secrets. An injected instruction in an issue then cannot push code or change CI, and the worst it can do is post a misleading comment.