Sub topic 11
Safety, permissions, and data handling
This chapter assumes you have used agent mode in VS Code, can read a Git diff, and know how tool approval prompts work. It covers how to limit what the agent can do and what happens to your data.
Why agents need guardrails
An agent can edit files and run terminal commands. A wrong guess, a misleading file, or a bad tool result can therefore cause real damage. The risks fall into three groups:
- Unwanted changes: edits to files you did not mean to touch, or commands that delete or overwrite data.
- Data exposure: secrets or private code sent to a model provider or an external tool.
- Prompt injection: text in a file, web page, or tool result that tries to steer the agent. The agent reads this text as part of its context, so it can mistake it for your instructions.
Layer your defenses
No single setting covers everything. Combine these layers.
Scope permissions
Give the agent the least access it needs for the task.
- Open only the project folder, not your home directory.
- Point file-based MCP servers at the workspace folder, as in the
${workspaceFolder}argument in the earlier MCP example. - Use credentials with read-only or limited rights for any tool that touches a database or cloud account.
- Install only MCP servers you trust. A server runs code on your machine and returns text the agent will read.
Keep approvals on for risky actions
Approve terminal commands one at a time until you trust the pattern. Read the full command before you accept it. Be careful with commands that delete files, change permissions, push to a remote, install packages, or pipe downloaded content into a shell. Avoid broad auto-approval settings, especially in repositories you did not write.
Use version control as a checkpoint
Commit or stash your work before a large task. Then any bad run is one command away from being undone.
git status
git add -A && git commit -m "Checkpoint before agent task"
git switch -c agent/add-validationWorking on a separate branch keeps the agent's edits apart from your main line. After the run, review git diff before you merge. Git only protects tracked files. Ignored files and changes made outside the repository, such as a command that edits your shell profile, are not covered.
Handle secrets
Treat anything in the agent's context as something that may leave your machine.
- Keep keys and tokens in environment variables or a secrets manager, not in source files.
- Do not attach
.envfiles or paste credentials into chat. - Add secret files to
.gitignoreand, where your editor supports it, to its content exclusion list. - If a secret reaches the chat or a commit, rotate it. Deleting it afterward does not make it safe.
# .gitignore
.env
.env.*
*.pemUnderstand data handling
When you send a request, the editor sends your prompt, the attached files, and tool results to the model service. Policies differ by provider and plan. Check these points in your provider's documentation or with your administrator:
| Question | Why it matters |
|---|---|
| Is my code used to train models? | Business plans often exclude it. Free plans may not. |
| How long are prompts retained? | Affects compliance and incident response. |
| Which MCP servers send data elsewhere? | A third-party server has its own data policy. |
| Can an admin restrict models or tools? | Organizations often enforce this centrally. |
Defend against prompt injection
Be suspicious when the agent reads content you did not write, such as a downloaded dependency, an issue comment, or a web page. If the agent suddenly proposes an unrelated command or tries to send data to a URL, deny the call and look at what it just read.
Exercise
You plan to let the agent refactor a large module. List three steps you would take before starting, and one thing you would check afterward.
Show answer
Before: commit or stash current work, create a new branch, and make sure no secrets or .env files are attached or in the open workspace scope. Keep terminal approvals on. Afterward: review the full git diff and run the tests before merging.
Exercise
Why is deleting a leaked API key from a chat message or commit not enough?
Show answer
The key may already have been sent to a model service or stored in history, so you must rotate it to make the old value useless.