Cybersecurity

The Core Principle

Python Docker AI Agents CI/CD Git GitHub AWS Cloud Rust Credentials Passwords Hashing
1,307 words Includes Code
🎯 Key Takeaway
AI coding agents are powerful tools, but they need guardrails. Before running any agent: create a feature branch, scan for secrets, ensure tests pass, restrict the workspace, review dependencies, and always review the diff before merging. The agent suggests — you decide.

An AI coding agent just fixed your bug. The test passes. The code looks right. You commit and push.

But did you check:

  • Were you on a safe branch?
  • Did the agent read any secrets?
  • Did it install new packages?
  • Did it access files outside your project?
  • Did it make network requests?
  • Did it modify files you didn't expect?

AI coding agents are remarkably capable — and that's exactly why they need guardrails. This tutorial gives you a practical safety framework: a pre-flight checklist, defensive practices for both terminal agents and AI IDEs, and a Python safety checker you can run before every agent session.

This tutorial connects to AI Coding Agents Evolution, AI Security Risks, and AI Coding Agent Security Checklist.

1. The Core Principle

Agent suggests → You decide.
The agent proposes changes. You review. You approve. You commit.

This principle applies to every interaction — whether you're using a terminal agent (Claude Code, Aider) or an AI IDE (Cursor, Copilot). The agent has capabilities you don't fully control. Your job is to set boundaries and verify results.

2. The Pre-Flight Checklist

Run this checklist before every agent session. Every item is a safety layer.

Check Command / Action Why It Matters
Feature branch git checkout -b feature/ai-work Never let the agent touch main directly
Clean working dir git status Avoid mixing agent changes with yours
No secrets in repo grep -r "sk-" . && ls .env* Agent may read and expose secrets
Tests pass pytest / npm test Baseline — know what worked before
Note commit hash git rev-parse HEAD Rollback point if agent breaks things
Safe directory pwd Agent shouldn't operate in /etc, /var, ~/.ssh

3. Git Branch Protection

The single most important safety measure: never run an agent on main.

# Before running the agent
git status # Check current branch
git checkout -b feature/fix-auth # Create safe branch
git commit -am "pre-agent snapshot" # Save current state

# Run the agent here

# If something goes wrong
git diff main..feature/fix-auth # Review changes
git checkout main # Switch back
git branch -D feature/fix-auth # Delete if needed
Never skip this step. An agent that makes 15 file changes on main is a crisis. The same 15 changes on a feature branch are a pull request.

4. Secrets Scanning

AI agents read files. If your repository contains secrets, the agent will read them — and potentially include them in its context, responses, or logs.

Secret Type Files to Check Action
API keys .env, config.py, settings.py Move to environment variables
SSH keys *.pem, *.key, id_rsa* Add to .gitignore, remove from repo
Passwords Hardcoded in source files Use a secret manager (Vault, AWS SM)
Cloud credentials service-account*.json, credentials Use IAM roles, not key files

For a deeper guide, see Secrets Management for Developers.

5. Sandboxing: Restricting the Agent

Don't give the agent access to your entire computer. Restrict it to the project directory.

Terminal Agents

# Run from the project directory only
cd ~/my-project
claude # Agent starts in ~/my-project

# For stronger isolation, use Docker
docker run -v $(pwd):/workspace -w /workspace ubuntu bash

AI IDEs

# Open ONLY the project folder in your IDE
# Don't open ~ (home directory) or / (root)

code ~/my-project # ✅ Project only
code ~ # ❌ Too broad
code / # ❌ Dangerous

6. Test Baseline

Always run tests before the agent makes changes. This establishes what "working" looks like.

# Before agent session
pytest --tb=short 2>&1 | tail -5
# Output: 12 passed in 0.3s ← This is your baseline

# After agent session
pytest --tb=short 2>&1 | tail -5
# Output: 14 passed in 0.4s ← Agent added 2 tests, all pass ✅
# OR: 11 passed, 1 failed ← Agent broke something ❌
If you have no tests: Create at least a smoke test before running any agent. A test that imports your main module and verifies basic functionality is enough to catch catastrophic failures.

7. Dependency Review

Agents may suggest installing new packages. Always review before approving.

Green Flag Red Flag
Well-known package (flask, requests, pytest) Unknown package with few downloads
Active maintainer, recent updates Last updated years ago
Necessary for the task Seems unrelated to the request
Small, focused dependency tree Pulls in dozens of sub-dependencies
Rule of thumb: If you didn't ask for it, don't install it. If the agent suggests pip install something-suspicious, ask why — and verify the package on PyPI first.

8. Code Review: The Final Gate

After the agent finishes, review every change before merging.

# Review what the agent changed
git diff # All unstaged changes
git diff --stat # Files changed summary
git status # Modified, added, deleted

# Check for surprises
git diff | grep -E "^[+-]" | head -20 # First 20 changes

# Look for these red flags:
# - Files you didn't expect
# - Deleted files
# - New dependencies
# - Hardcoded credentials
# - Network calls to unexpected hosts
# - Changes to config files

9. Terminal Agent vs AI IDE: Safety Comparison

Safety Aspect Terminal Agent AI IDE
Visibility ✅ Every command visible in terminal ✅ Visual diff shows changes
Approval ⚠️ Must review each command ✅ Accept/reject UI per change
Scope control ✅ You choose the working directory ✅ Open only the project folder
Package installs ⚠️ Agent can pip install directly ⚠️ Agent may suggest installs
Network ⚠️ Can execute curl/wget ✅ Runs in browser sandbox
Rollback ✅ git checkout / git reset ✅ Undo / version history

10. The 10 Safety Rules

# Rule Why
1Always use a feature branchEasy rollback, clean main
2Commit before running the agentSnapshot to return to
3Scan for secrets firstAgent reads everything
4Ensure tests passBaseline for verification
5Restrict the working directoryLimit blast radius
6Review every package installSupply-chain risk
7Review the git diffSee exactly what changed
8Run tests after changesVerify nothing broke
9Never auto-merge to mainPR review required
10Log what the agent didAudit trail for debugging

11. FAQ

What if the agent breaks my code?
That's why you use a feature branch. Run git diff main..feature/branch to see all changes. If something is wrong, git checkout main and delete the branch. The main branch is untouched. Always commit before running the agent so you have a clean rollback point.
Can the agent access my SSH keys?
If you run the agent from your home directory, it potentially can. That's why sandboxing matters: run the agent from your project directory, not ~. For terminal agents, cd ~/my-project && claude restricts the initial scope. For AI IDEs, open only the project folder.
Should I trust the agent's test results?
Run tests yourself after the agent finishes. Don't rely solely on the agent's report that "all tests pass." Run pytest or npm test independently. Compare the before/after test counts. If the agent added tests, review them — they might be testing the wrong thing.
Is it safe to use agents on production code?
Only on a feature branch that you've tested. Never run an agent directly on production or the main branch. The workflow is: feature branch → agent → tests → review → PR → merge → deploy. For production deployment safety, see GitHub Actions CI/CD.

Continue Learning

💬 Discuss on BestWordz Community

Join the conversation about Python, Docker, AI Agents on the BestWordz Community forum.

Visit Forum →