keepcalculator

06 Numbers and Powers

Secure-by-Default AI Coding Agents: A Practical Guide

Card No. 06·24457 · Updated · Standard formula, shown below

A secure-by-default AI coding agent can help with development while limiting what it can read, change, execute, and send outside your environment. This guide is for developers and teams setting up an agent; start with a test repository, a separate low-privilege account, and a way to review its proposed changes.

What You’ll Need

  • A test repository with no production credentials or sensitive data.
  • An agent that supports configurable tools, permissions, or execution environments.
  • A separate operating-system account, container, or virtual machine for agent work.
  • Version control, a clean working tree, and a human review process.
  • A way to inspect logs and manage secrets without exposing them to the agent.

Step-by-Step

  1. Define the agent’s allowed work. Write down the tasks it may perform, the files it may access, and the actions that always require approval. For example, it might edit application code and run local tests, but not deploy, alter access controls, or access customer data. Treat this as a boundary, not merely a prompt instruction.
  2. Use a dedicated, low-privilege environment. Run the agent as a non-administrator account in a disposable container or virtual machine where practical. Mount only the repository and necessary tools. Keep the host filesystem, other projects, browser profiles, SSH directories, and cloud configuration directories inaccessible. Do not assume a container alone is a complete security boundary; configure its user, mounts, network, and capabilities deliberately.
  3. Start with read-only access. Give the agent permission to inspect files and propose patches before allowing writes. Then allow writes only to the working copy or designated paths. Deny access to sensitive files such as private keys, credential stores, production configuration, and unrelated repositories. Check that symlinks or path traversal cannot turn an approved directory into access to a forbidden one.
  4. Require approval for consequential actions. Make shell commands, package installation, network access, deletion, permission changes, commits, pushes, deployments, and database operations restricted or approval-gated according to risk. Prefer explicit allowlists for common safe commands over broad permission to run arbitrary commands. Inspect the full command and its effects before approving it; a familiar command can invoke scripts with unexpected behavior.
  5. Keep secrets out of the agent’s reach. Do not place long-lived tokens, passwords, private keys, or production credentials in prompts, repository files, environment variables available to the agent, or command output. If a task genuinely needs a credential, use a narrowly scoped, short-lived secret supplied through a controlled mechanism, and revoke it when the task ends. Check logs and generated patches for accidental disclosure.
  6. Limit network access. Disable outbound access unless the task needs it. If downloads are necessary, allow only the required destinations through a controlled proxy or network policy. Treat content from web pages, package metadata, issues, and repository files as untrusted input: it may contain instructions intended to manipulate the agent. Do not let those instructions override your permission rules.
  7. Make review and testing mandatory. Have the agent work on a branch or disposable copy. Review the diff for unexpected files, changed permissions, hidden scripts, dependency changes, and embedded secrets. Run tests, linters, and security checks in the restricted environment. A passing test suite does not prove a change is safe, so a person should approve consequential changes before they enter a shared or production branch.
  8. Log activity and set a stop condition. Record tool calls, approvals, command results, and files changed, while ensuring logs do not capture secrets. Set limits for runtime, tool calls, and resource use if the platform supports them. Stop the agent if it requests broader access than the task requires, behaves unexpectedly, or attempts an unapproved action.
  9. Test the controls before relying on them. Use a harmless test repository to confirm that denied files cannot be read, disallowed commands cannot run, network restrictions work, and approval gates actually pause execution. Test with both ordinary requests and hostile-looking instructions embedded in sample files. Recheck controls after changing agent settings, tools, or execution environments.

Common Mistakes

  • Relying on a system prompt as a security boundary. Instructions help shape behavior, but they do not replace operating-system permissions, tool restrictions, and network controls.
  • Granting broad access for convenience. Giving an agent administrator rights, a home-directory mount, or an unrestricted shell makes a mistake or manipulated action much more damaging.
  • Allowing secrets into context. A secret can leak through summaries, logs, patches, or tool output even if the agent was not asked to reveal it.
  • Approving commands without understanding them. Review scripts, chained commands, and package lifecycle actions, not just the first command name.
  • Assuming review is optional after tests pass. Tests check specified behavior; they may miss harmful side effects, secret exposure, and unsafe dependency changes.

Tips

  • Use a fresh worktree or disposable environment for each task, then discard it when finished.
  • Separate low-risk read and edit tasks from actions that need network access, deployment, or privileged credentials.
  • Ask the agent to explain its planned changes and list affected files before it acts; verify the result independently.
  • Keep permission policies in version control where appropriate, and have another person review changes to those policies.
  • Use pinned, reviewed dependencies and existing project scripts where possible; avoid unreviewed downloads and install commands.

Troubleshooting

The agent cannot read or edit a needed file: Confirm the file is inside an explicitly mounted, permitted path. Grant the narrowest additional access needed, then test it. Do not solve a path issue by mounting the whole host or home directory.

A command fails in the sandbox: Check the recorded error and identify the exact missing capability, such as a required executable or network destination. Add only that capability if the task warrants it. If the agent asks for administrator access, pause and investigate whether the task can be completed another way.

The agent makes unrelated changes: Stop it, preserve the logs, and inspect the diff. Restore the clean baseline or discard the disposable worktree, narrow the task and writable paths, and retry. Do not merge changes until they have been reviewed.

A credential appears in output or a patch: Treat it as exposed. Revoke or rotate it through the system that issued it, remove it from affected files and logs where possible, and check whether it was transmitted elsewhere. Then trace how it entered the agent’s environment and close that access path.

Network controls block a required task: Verify the destination and purpose independently. Allow only the necessary access using the available network controls, monitor the request, and remove the exception when finished. If you cannot limit or audit access, keep the agent offline for that task.

Frequently Asked Questions

Can a prompt alone make a coding agent secure?
No. Prompts can communicate boundaries, but enforceable controls must restrict files, tools, network access, and operating-system privileges.

Should an agent have access to production credentials?
Usually not. Prefer a task-specific, narrowly scoped, short-lived credential only when genuinely necessary, and require human approval for consequential use.

Is a container enough to isolate an agent?
Not automatically. Review its user privileges, mounted paths, network access, exposed sockets, and available capabilities. Use additional isolation where the risk warrants it.

Should every shell command require approval?
That is a reasonable starting point for sensitive repositories. Teams can permit well-understood, low-risk commands later, while keeping arbitrary commands and consequential operations gated.

How often should permissions be reviewed?
Review them when the agent, tools, repository, or task changes, and periodically during ongoing use. Remove permissions that are no longer needed.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →