Sub topic 5
Pre-approving tools with least privilege
This chapter assumes you can write short shell scripts, have used an interactive coding agent, and know Git branches and pull requests. It also assumes you have read the earlier chapters on headless mode and self-contained prompts.
Why approvals change when no one is watching
In an interactive session, the agent asks before it runs a shell command or edits a file, and you answer. A scheduled run has nobody to answer. The agent will do one of two things:
- Stop and wait for an approval that never arrives, until the timeout kills it.
- Run with broad approval already granted, so it can do anything it decides to do.
The first wastes the run. The second is the real risk. The fix is to decide in advance exactly which tools the agent may use, and to grant nothing else.
The least-privilege rule
Least privilege means the job gets the smallest set of permissions that lets it finish its task. Decide this at three layers, because each one limits damage if the layer above it fails.
- Agent tool permissions. An allowlist of tools and commands the agent may use without asking. Allow by name, such as reading files, running the test command, or
git diff. Avoid blanket approval of every tool. - Operating system account. Run the job as a dedicated user that owns only the files it needs.
- Remote access. Give the Git token or deploy key the narrowest scope, such as push access to branches but not to the protected main branch.
The exact setting names for the first layer differ between agents. Look for an allowed-tools list or a permission mode in your agent's documentation.
Matching permissions to the task
Start from what the task needs, not from what is convenient.
| Task | Allow | Do not allow |
|---|---|---|
| Summarize yesterday's commits | Read files, `git log` | Editing files, network access |
| Fix lint errors and open a PR | Read and edit files, run linter, commit to a new branch | Pushing to main, deleting branches |
| Update a dependency | Read and edit manifest files, run tests | Publishing packages, reading deploy secrets |
Enforcing the OS layer
Agent allowlists are a policy inside the agent. A systemd sandbox is enforced by the kernel and still holds if the agent misbehaves. Add these lines to the [Service] section from the scheduling chapter:
# /etc/systemd/system/nightly-agent.service ([Service] section additions)
User=agent-runner
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=read-only
ReadWritePaths=/srv/repos/my-project /home/agent-runner/agent-logsWith ProtectSystem=strict, the whole file system is read-only except the paths listed in ReadWritePaths. A job that tries to write elsewhere fails with a permission error instead of succeeding quietly.
Common errors
- Allowing a whole shell. Permitting any command defeats the allowlist, because the agent can run anything through it. Allow specific commands.
- Reusing your personal account. The job then inherits everything you can do. Create a separate user and token.
- Testing only the happy path. Run the job once and confirm that a forbidden action, such as writing outside the repository, is actually refused.
Exercise
A nightly job fixes lint errors and opens a pull request. The run stalls and times out because the agent asked to run the linter and nobody answered. What should you change, and what should you avoid doing?
Show answer
Add the specific linter command to the agent's allowed tools so it no longer needs approval. Avoid switching on a blanket "approve everything" option, because that would also let the agent run any other command, such as pushing to main, without a check.