Security best practices for AI coding assistants including GitHub Copilot, Cursor, and Claude Code.
AI coding assistants—GitHub Copilot, Cursor, Claude Code, and similar tools—have become integral to development workflows. But they introduce new security risks: they can leak secrets, generate vulnerable code, exfiltrate data, and execute commands with your permissions. This guide covers practical security controls for AI coding agents.
The Threat Model
AI coding agents operate with your credentials and permissions. When you invoke them, they can:
- Read your codebase — including .env files, SSH keys, AWS credentials, database passwords
- Execute commands — shell commands, git operations, package installs, deployments
- Send context to cloud APIs — your proprietary code leaves your machine
- Modify files — including security-critical configs, CI/CD pipelines, infrastructure code
Control 1: Local-First / Air-Gapped Models
The single most effective control: run models locally so code never leaves your machine.
Ollama — Local LLM Runtime
# Install Ollama
curl -fsSL https://ollama.com/install.sh | sh
# Pull coding models
ollama pull codellama:13b # Meta's Code Llama
ollama pull deepseek-coder:6.7b # DeepSeek Coder
ollama pull starcoder2:7b # BigCode StarCoder2
ollama pull qwen2.5-coder:7b # Alibaba Qwen 2.5 Coder
# Run locally
ollama run codellama:13b "Write a secure Python JWT validator"
Local-First Editors & Extensions
| Tool | Local Model Support | Notes |
|---|---|---|
| Cursor | ✅ Via Ollama | Settings → Model → Ollama |
| Continue.dev | ✅ Native | Open source, supports Ollama, LM Studio |
| Claude Code | ❌ Cloud only | Use with –allowed-tools restrictions |
| GitHub Copilot | ❌ Cloud only | Enterprise: Copilot Enterprise with data residency |
| Codeium | ✅ Self-hosted option | Enterprise plan |
| Tabnine | ✅ Local/on-prem | Enterprise |
Control 2: Secret Detection & Prevention
Pre-Commit Hooks
# Install pre-commit
pip install pre-commit
# .pre-commit-config.yaml
repos:
- repo: https://github.com/Yelp/detect-secrets
rev: v1.5.0
hooks:
- id: detect-secrets
args: ['--baseline', '.secrets.baseline']
- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.0
hooks:
- id: gitleaks
# Create baseline
detect-secrets scan > .secrets.baseline
pre-commit install
IDE-Level Secret Scanning
- GitGuardian Shield — Real-time IDE scanning (VS Code, JetBrains)
- TruffleHog VS Code — Scans on save
- VS Code built-in —
"secret-detection.enabled": true
Runtime Secret Redaction
Configure AI agents to redact secrets before sending context:
// Cursor settings.json
{
"cursor.privacyMode": true,
"cursor.ignorePatterns": [
"*.env*",
"*.pem",
"*.key",
"secrets/**",
"**/aws/**",
"**/.aws/**"
]
}
Control 3: Command Execution Policies
Claude Code — Allowed Tools
# ~/.claude-code/config.json
{
"allowedTools": [
"Read",
"Write",
"Edit",
"Glob",
"Grep",
"Task",
"Bash"
],
"deniedTools": [
"Bash:rm -rf",
"Bash:sudo",
"Bash:docker",
"Bash:kubectl",
"Bash:aws",
"Bash:gcloud"
],
"bashCommands": {
"allow": ["ls", "cat", "grep", "find", "git status", "git diff"],
"deny": ["rm", "mv", "chmod", "chown", "systemctl", "docker", "kubectl"]
}
}
Cursor — Command Rules
Settings → AI Rules → Add custom rules:
# .cursorrules
- Never run destructive commands (rm, mv, sudo, docker, kubectl)
- Ask before running any shell command
- Prefer read-only operations (ls, cat, grep, find, git status)
- Never commit or push without explicit approval
- Never install packages without asking
Containerized Agent Execution
Run the agent itself in a restricted container:
# Claude Code in container
docker run --rm -it
-v $(pwd):/workspace:ro
-v ~/.claude-code:/home/user/.claude-code:ro
--cap-drop ALL
--security-opt no-new-privileges:true
--network none
--memory 2g --cpus 1
anthropic/claude-code:latest
Control 4: Code Review Gates
AI-Generated Code Must Pass:
- Static Analysis — Semgrep, CodeQL, SonarQube
- Dependency Scanning — npm audit, pip-audit, cargo audit, govulncheck
- Secret Scanning — gitleaks, trufflehog in CI
- Human Review — No auto-merge for AI-generated PRs
# .github/workflows/ai-code-review.yml
name: AI Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Semgrep
uses: returntocorp/semgrep-action@v1
- name: Gitleaks
uses: gitleaks/gitleaks-action@v2
- name: Dependency Scan
run: |
npm audit --audit-level=high || true
pip-audit || true
- name: Require Human Review
if: github.actor == 'dependabot[bot]' || github.actor == 'copilot[bot]'
run: |
echo "AI-generated PR requires human approval"
exit 1
Control 5: Data Residency & Compliance
| Regulation | Requirement | AI Agent Compliance |
|---|---|---|
| GDPR | Data processing in EU | Use EU-hosted models (Mistral, local Ollama) |
| SOC 2 | Access controls, audit logs | Local models + SIEM integration |
| HIPAA | PHI protection | Never send PHI to cloud models; use local only |
| ITAR | US persons only | Air-gapped local models required |
| Corporate IP | Code never leaves network | Self-hosted models (Tabnine Enterprise, Continue.dev) |
Control 6: Supply Chain — Model & Extension Verification
Verify Extensions Before Install
# Check VS Code extension publisher
code --list-extensions --show-versions
# Verify signature (if available)
# Only install from official marketplace
# Review permissions requested
# Pin versions in devcontainer.json
Model Supply Chain
- Use models from trusted sources (Hugging Face official, Ollama library)
- Verify model hashes:
ollama show --sha256 codellama:13b - Scan models for backdoors:
python -m safetensors.check model.safetensors - Prefer quantized models from original authors
Practical Checklist for Teams
| Control | Implementation | Priority |
|---|---|---|
| Local models | Ollama + Continue.dev / Cursor local mode | 🔴 Critical |
| Secret scanning | pre-commit + IDE plugins + CI | 🔴 Critical |
| Command restrictions | Agent config + containerization | 🔴 Critical |
| Code review gates | Semgrep + Gitleaks + Human review | 🟡 High |
| Data residency | Local models for sensitive code | 🟡 High |
| Extension verification | Marketplace only, pinned versions | 🟢 Medium |
| Model verification | Hash checking, safetensors validation | 🟢 Medium |
Related Tools on DevStackIO
- Secrets Management — Generate, rotate, and audit secrets
- Git Cheatsheet — Secure git workflows
- Regex Tester — Build secret detection patterns
- CSP Generator — Lock down browser execution
References
- OWASP LLM Top 10 — https://owasp.org/www-project-top-10-for-large-language-model-applications/
- GitGuardian Blog — https://blog.gitguardian.com/
- Semgrep Rules — https://semgrep.dev/r
- Ollama Models — https://ollama.com/library
- Continue.dev — https://continue.dev/
- Gitleaks — https://github.com/gitleaks/gitleaks