A Copilot agent session in VS Code set to Allow all ran a cleanup script that deleted files in your home directory — which VS Code control would have contained it?
answer
- a second layer beneath approvals
- off until you turn it on
- independent of the permission level
- writes confined to the working directory
- close the run-outside-sandbox escape
basics
~20 sVS Code's agent sandboxing for terminal commands. It is off by default and independent of the permission level, so once enabled it still confines commands under Allow all, limiting writes to the working directory and blocking sensitive home-directory paths.
solid answer
~40 sAllow all skips every approval prompt, including the built-in `rm: false` rule, so the approval layer could not have stopped the script. The containing control is **agent sandboxing**: turn on `chat.agent.sandbox.enabled` (macOS, Linux and WSL2, Preview; Linux needs `bubblewrap` and `socat`) or `chat.agent.sandbox.enabledWindows` (Experimental). Sandboxed terminal commands and their child processes can write to the current working directory and below (plus paths you add with `allowWrite`), and cannot read sensitive home-directory locations by default — and that holds under Allow all and Autopilot. Then close the gaps: set `chat.agent.sandbox.allowUnsandboxedCommands` to `false` so blocked commands cannot be rerun outside the sandbox, and note `chat.agent.sandbox.allowNetwork` defaults to `true`. In a Copilot Agent Host session, `/sandbox-policy` shows the effective policy. A checkpoint would not have brought the files back: it does not reverse terminal commands.
go deeper
Recall that approvals and sandboxing are separate, that Allow all skips prompts, and that checkpoints do not undo terminal commands.
Explain what the sandbox restricts by default — writes to the working directory, no sensitive home paths — and that it is off until enabled.
Show the hardening: sandbox on, allowUnsandboxedCommands false, allowNetwork decided deliberately, /sandbox-policy to verify, edits to sensitive files gated separately.
Decide whether to mandate sandboxing through managed settings, weighing Preview and Experimental status and platform prerequisites against developer friction.
## Why the approval layer did not stop it VS Code protects agent actions with **two independent layers**. **Approvals** decide whether an action runs automatically or waits for you. **Sandboxing** restricts the file-system and network resources a terminal command can reach *after* it has been approved. The session was on **Allow all**, which runs every tool call without confirmation. That removes the approval layer entirely, including the built-in `chat.tools.terminal.autoApprove` rules that would normally make `rm`, `del` or `Remove-Item` ask. Even under **Manual permissions**, those rules would only have *asked* — a `false` rule never blocks — and the docs call terminal auto-approval best-effort parsing, not a security boundary. A cleanup script is exactly the case parsing misses: the dangerous `rm` sits inside the script, not on the command line the agent proposed. ## The sandbox layer Agent sandboxing adds an operating-system boundary around terminal commands and **their child processes**, so it sees what the script does, not just how it was invoked. With the default file-system configuration, sandboxed commands: - can read workspace folders, the sandbox runtime temporary folder and paths VS Code adds for common developer tools; - can **write to the current working directory and its subdirectories** — other write locations have to be added with `allowWrite`; - **cannot read sensitive locations in your home directory** by default; - pass the same restrictions to build scripts and package managers they start. It is **independent of the permission level**: an enabled sandbox still restricts terminal commands under **Allow all** and **Autopilot**. | Platform | Setting | Status | Prerequisite | |---|---|---|---| | macOS | `chat.agent.sandbox.enabled` | Preview | None | | Linux, WSL2 | `chat.agent.sandbox.enabled` | Preview | `bubblewrap` and `socat` installed | | Windows | `chat.agent.sandbox.enabledWindows` | Experimental | The applicable September 8, 2026 Windows security update | ## Turning it on and checking it 1. Install the prerequisites for the platform where the commands run. If they are missing, VS Code does not silently run the command unsandboxed; it notifies you to install them. 2. Set the platform's setting to `on`. It applies to Local sessions and Copilot Agent Host sessions. 3. Start a new session, or use **Sandboxing for terminal** in the permissions picker for an existing Agent Host session. 4. In a Copilot Agent Host session, run `/sandbox-policy` and open the report to confirm the effective file-system and network policy. Use `chat.agent.sandbox.fileSystem.mac`, `.linux` or `.windows` to add `allowRead`, `allowWrite`, `denyRead` (and, outside Windows, `denyWrite`) paths. ## Closing the escape hatches The sandbox ships with defaults that favour getting work done: - **`chat.agent.sandbox.allowUnsandboxedCommands`** defaults to `true`: when a command cannot run inside the sandbox, the agent can request to run it outside. Set it to `false` and such requests fail instead of prompting. - **`chat.agent.sandbox.allowNetwork`** defaults to `true`: enabling the sandbox does not block the network. Set it to `false` to apply network isolation; in the Copilot Agent Host built-in shell and on Windows that is all-or-nothing, while Local sessions on macOS and Linux can filter by domain. - **`chat.agent.sandbox.retryWithAllowNetworkRequests`** defaults to `true` in Local sessions, letting the agent ask to retry a network-blocked command with unrestricted network. - **`chat.agent.sandbox.allowAutoApprove`** defaults to `true` in Local sessions, so commands inside the sandbox run without prompts. Organizations can require sandboxing and prevent bypass through managed settings, which lock the toggle. Where a bypass is permitted in an Agent Host session, approving **Allow in this Session** on the prompt turns the sandbox off for every later terminal command in that session, not only the one that was blocked — approving a single operation does not. ## What it does not cover, and recovery - **Built-in file tools are outside the sandbox.** It applies to terminal commands only; protect files such as `.env` from the agent's edit tools with `chat.tools.edits.autoApprove` patterns set to `false`. - **Deletions inside the working directory are still allowed.** The boundary protects your home directory, not the repository itself. - **A worktree is not a security boundary.** It separates code changes but does not restrict commands or network access. - **Checkpoints will not undo it.** A checkpoint restores affected workspace files and chat history but does not reverse completed terminal commands; recovery for those effects is Git and your backups.
- Would restoring a checkpoint in VS Code have brought the deleted files back?No. Checkpoints snapshot affected workspace files before each request and restore those files and the chat history, but the docs state they do not reverse completed terminal commands, network requests, deployments or changes to external services. Files a script deleted have to come back from Git or a backup.
- The sandbox is on, yet Copilot's agent still downloaded a package from the internet. Why?Enabling the sandbox does not block the network: `chat.agent.sandbox.allowNetwork` defaults to `true`. Set it to `false` to isolate the network. In the Copilot Agent Host built-in shell that blocks all outbound access, because domain lists are not used there; Local sessions on macOS and Linux can allow specific domains through `chat.agent.allowedNetworkDomains`.
- Does the terminal sandbox stop the agent's file-edit tool from changing `.env`?No. Sandboxing applies to terminal commands and their child processes, not to built-in file tools. To make edits to such files wait for approval, add a pattern like `"**/.env": false` to `chat.tools.edits.autoApprove`.
Approvals are the doorman deciding who may enter; the sandbox is the walls of the room. Allow all sends the doorman home, but the walls are still there — provided someone built them before the party.
- Selecting Allow all switches the terminal sandbox off.
- Enabling the sandbox blocks outbound network access by default.
- Restoring a checkpoint undoes files that terminal commands deleted.
- The built-in rm rule would have stopped the script under Allow all.
- If bubblewrap is missing, VS Code quietly runs the command unsandboxed.
- Running the session in a new worktree is a security boundary for commands.