Sub topic 5
Pre-approving tools with least privilege
An interactive agent asks before it does anything risky. A headless agent has nobody to ask. If a tool needs approval and no one can give it, the run either stalls until the timeout or fails. If you approve everything to avoid that, the agent has full access to whatever the scheduled user can reach.
This chapter assumes you have used an interactive coding agent and its permission prompts. It also assumes you are comfortable with Linux users and file permissions, and with Git branches. You will decide which tools to pre-approve and how to enforce those limits outside the agent.
Why approval prompts disappear
In an interactive session, the prompt is a safety layer. You see "run rm -rf build?" and you can say no. In a headless run, that layer is gone. You have to replace it with decisions made in advance, and you have to make them before the job's first run.
Pre-approval does not mean "approve everything." It means writing down, ahead of time, the smallest set of actions the job needs, and denying the rest.
Least privilege here has two layers:
- Agent-level permissions. The allow-list the agent itself reads: which tools it may call without asking.
- System-level permissions. What the operating system, the Git host, and the cloud account let the process do, whatever the agent decides.
The agent-level layer is the weaker one. It is software that is trying to follow your rules, and a prompt-injected or confused agent may find a way around a loose rule. The system-level layer holds even if the agent misbehaves. Build both.
Start from the job, not from the tool list
Write one sentence describing the job, then derive the permissions from it. For example: "Each night, run the test suite, and if a test is failing because of an outdated snapshot, fix it on a new branch and push the branch."
That sentence needs:
- reading files in the repository,
- editing files under
tests/, - running the test command,
- creating a branch, committing, and pushing that branch.
It does not need deleting files, installing packages, reading ~/.ssh, calling arbitrary URLs, or pushing to main. Anything you cannot trace back to the sentence is a candidate for denial.
Permission tiers
Tools differ in how much damage they can do. Group them by risk and be stricter as you go down the table.
| Tier | Example capability | Typical decision for a scheduled job |
|---|---|---|
| Read | Read files in the working directory, list directories, search | Allow, scoped to the repository |
| Scoped write | Edit files under named paths | Allow only the paths the job needs |
| Fixed commands | Run `git status`, `git diff`, the test command | Allow by exact command |
| Open shell | Run any command | Deny. This one entry can undo every other rule. |
| Network | Fetch URLs, call APIs | Deny unless required, and then allow named hosts only |
| Destructive or privileged | Delete trees, force-push, `sudo`, change permissions | Deny, and also make impossible at the system level |
The open shell tier needs particular care. If the agent can run any shell command, an allow-list of file paths does not constrain it, because it can write files with sh -c. Allow exact commands such as git commit, or small wrapper scripts you wrote, instead of a general shell.
An example allow-list
Agents differ in how they express permissions, so the file below is a generic illustration. Map its entries onto your agent's own configuration format and syntax, which you should confirm in its documentation.
{
"allowed_tools": [
"read_file",
"list_dir",
"write_file:tests/**",
"shell:git status",
"shell:git diff",
"shell:git switch -c nightly/*",
"shell:git commit -am *",
"shell:git push origin nightly/*",
"shell:npm test"
],
"denied_tools": [
"shell:git push --force*",
"shell:rm *",
"network"
]
}Notice what the entries do. Writes are limited to tests/. Pushes are limited to branches that start with nightly/. The test command is one exact string. Deny entries exist as a second guard, but the allow-list is what carries the policy. Anything not allowed should be refused by default.
Check the policy before every run
A policy file is code, and people edit it. Someone adds a broad entry "to get the job unstuck" and forgets to remove it. A cheap pre-flight check in your wrapper script catches the most common mistakes. This one needs jq.
#!/usr/bin/env bash
# check-policy.sh: fail if the allow-list contains an open shell or bare wildcard.
set -eu
POLICY="${1:-policy.json}"
# Create a sample policy so the script runs as shown.
if [ ! -f "$POLICY" ]; then
cat > "$POLICY" <<'EOF'
{"allowed_tools": ["read_file", "write_file:tests/**", "shell:npm test"]}
EOF
fi
if ! jq -e '
.allowed_tools
| map(select(. == "shell" or . == "*" or . == "network" or test("^shell:\\*")))
| length == 0
' "$POLICY" > /dev/null; then
echo "policy $POLICY allows an open shell, network, or bare wildcard" >&2
exit 1
fi
echo "policy $POLICY passed the broad-permission check"Call it near the top of nightly-agent.sh, before the agent starts. A non-zero exit then stops the job and reaches your alerting, as covered in the chapter on exit codes. The check only catches obvious cases. It does not replace reading the policy during review.
Enforce limits outside the agent
Assume the agent's allow-list will eventually be wrong or bypassed. Put a second boundary beneath it.
Use a dedicated account
Run the job as its own user, for example agent, that owns only the checkout and the log directory. It should have no sudo, no access to your personal home directory, and no SSH keys other than the one the job needs. The credentials chapter covers how to supply that key.
Restrict the process with systemd
If you schedule with a systemd timer, the service unit can apply restrictions that the agent cannot undo.
# /etc/systemd/system/nightly-agent.service (hardening additions)
[Service]
User=agent
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
ReadWritePaths=/srv/repos/my-project /var/log/nightly-agentProtectSystem=strict makes the whole filesystem read-only for this service, except the paths in ReadWritePaths. ProtectHome=true hides home directories. NoNewPrivileges=true blocks privilege escalation through setuid programs. Add these to the unit from the scheduling chapter. If you keep logs under ~/agent-logs as in the earlier wrapper, move them to a path listed in ReadWritePaths, because ProtectHome=true hides the home directory.
Limit the token on the Git host
The most important boundary is what the push credential can do. Use a deploy key or a fine-grained token scoped to one repository. Combine it with branch protection on main. Then even a fully compromised agent can only push to a side branch, and a person still reviews the pull request.
In a CI schedule, set the job token to the least it needs:
# .github/workflows/nightly-agent.yml (permissions block)
jobs:
run:
runs-on: ubuntu-latest
permissions:
contents: write # needed to push the nightly/* branch
pull-requests: write # needed to open the pull requestCompare this with the earlier example that used contents: read. A job that only reports findings can stay read-only. Grant write only when the job delivers changes, and grant nothing beyond that.
Common mistakes
- Approving the shell "temporarily." Temporary permissions in unattended jobs tend to stay.
- Allowing
git pushwithout a branch pattern. The agent can then push tomainif nothing else stops it. - Relying on deny rules alone. A deny-list is a list of things you thought of. An allow-list covers what you did not.
- Testing only the happy path. Run the job once with a prompt that tries a forbidden action, and confirm that it fails safely and that the failure shows up in the log.
- Sharing your own credentials. If the job uses your personal token, it can do everything you can.
Review checklist
Before enabling a schedule, confirm each of these:
- The job is described in one sentence, and every permission traces back to it.
- No open shell and no unrestricted network in the allow-list.
- Writes are limited to named paths, and pushes to a named branch pattern.
- The job runs as a dedicated user with no
sudo. - The Git token is scoped to one repository, and
mainis protected. - The pre-flight policy check runs first and fails the job on a bad policy.
Exercise
A nightly job updates a changelog file and opens a pull request. A teammate proposes allowing the agent to run any shell command "so it never gets blocked." Name two specific problems with this, and describe one narrower configuration that still lets the job work.
Show answer
First, an open shell bypasses path limits, because the agent can write any file or run any program through the shell, so the other allow-list entries stop mattering. Second, a prompt-injected or confused agent could delete files, read secrets, or push to protected branches, and nobody is present to refuse. A narrower setup allows reading the repository, writing only to the changelog file path, running exact git commands (status, diff, switch -c on a nightly/ branch, commit, push to nightly/), and denies network and delete. Back it with a dedicated user and a repository-scoped token with branch protection on main, so the limits hold even if the agent misbehaves.