Security & Best Practices
Overview
DSPy Code is designed with security in mind to protect your files and personal data. This guide explains our security measures and best practices.
đ Core Security Principles
1. CWD-Only Operation
ALL data stays in your current working directory.
my-dspy-project/ # Everything in here!
âââ .venv/ # Packages
âââ .dspy_cache/ # DSPy cache
âââ .dspy_code/ # dspy-code data
âââ generated/ # Your code
âââ dspy_config.yaml # Config
Never accesses: - â Your home directory (~/) - â User directories (Desktop, Documents, Downloads, Pictures) - â System directories (/System, /Library, /usr) - â Files outside your project
2. Path Boundary Validation
Every file operation is validated to ensure it stays within your project directory:
- â Depth limiting: Maximum 10 levels of recursion
- â Symlink protection: Verifies files are actually within project
- â Permission checks: Verifies read access before accessing files
- â Explicit boundaries: Operations confined to CWD
3. Startup Safety Checks
DSPy Code validates your working directory before starting:
# Running from home directory - shows warning
cd ~/
dspy-code
# đ¨ SECURITY WARNING: You are running from your home directory!
# Running from project directory - safe
cd ~/projects/my-project
dspy-code
# â
Safe to proceed
đĻ Recommended Setup
Always Use Virtual Environment IN Project
Critical for security and isolation:
# 1. Create project directory
mkdir my-dspy-project
cd my-dspy-project
# 2. Create venv IN this directory (not elsewhere!)
python -m venv .venv
# 3. Activate it
source .venv/bin/activate # For fish: source .venv/bin/activate.fish
# 4. Install dspy-code (goes into .venv/ here)
pip install dspy-code
# 5. Run dspy-code (everything stays here!)
dspy-code
Why This Matters
When you create the virtual environment inside your project:
- Packages are local: Installed to
./venv/lib/ - Cache is local: Stored in
./.dspy_code/ - RAG index is local: Scans only
./.venv/packages - Everything contained: One directory = one project
Result: Can't access files outside your project! đ
đ¨ What Gets Scanned
Safe Scanning Scope
DSPy Code only scans these locations (all in CWD):
-
Virtual environment packages:
-
Specific project directories:
-
Python files in root (non-recursive):
Never Scanned
- â Home directory
- â Parent directories
- â System directories
- â User directories
- â Hidden files (starting with
.) - â Test directories
- â Cache directories
- â
node_modules/
đĄī¸ Security Features
Directory Blocking
Automatic protection against dangerous directories:
# Blocked directories
- Home directory: ~/
- System dirs: /System, /Library, /usr, /private
- User dirs: ~/Desktop, ~/Documents, ~/Downloads
- Cloud dirs: ~/iCloud, ~/Dropbox, ~/Google Drive
Symlink Attack Prevention
DSPy Code verifies files are actually within your project:
# Even if you create a symlink pointing outside:
ln -s ~/Documents/secret.txt ./link.txt
# DSPy Code will detect and skip it:
â ī¸ Skipping file outside base path: ./link.txt
Depth Limiting
Prevents infinite recursion and excessive scanning:
# Maximum depth: 10 levels
./level1/level2/.../level10/ # â
Scanned
./level1/level2/.../level11/ # â Skipped (too deep)
Permission Checks
All file operations check permissions:
â ī¸ Security Warnings
Home Directory Warning
If you run from home directory:
đ¨ SECURITY WARNING
You are running dspy-code from your home directory!
This is dangerous as it may attempt to scan ALL files
in your home directory, including personal documents,
photos, and sensitive data.
Recommended actions:
1. Create a dedicated project directory
2. Navigate to it: cd ~/my-dspy-project
3. Create virtual environment: python -m venv .venv
4. Activate it: source .venv/bin/activate # For fish: source .venv/bin/activate.fish
5. Install dspy-code: pip install dspy-code
6. Then run: dspy-code
Press Ctrl+C to exit, or Enter to continue at your own risk...
User Directory Warning
If you run from Desktop, Documents, etc.:
â ī¸ WARNING
You are running dspy-code from: ~/Desktop
This directory may contain personal files. For safety,
dspy-code will not scan files here.
Recommendation:
Create a dedicated project directory for your DSPy work:
mkdir ~/projects/my-dspy-project && cd ~/projects/my-dspy-project
System Directory Error
If you run from system directory:
đ¨ CRITICAL ERROR
You cannot run dspy-code from system directory: /usr
This could damage your system. Please run from a user
project directory.
â Best Practices
1. Dedicated Project Directories
Always create a dedicated directory for each project:
# Good
~/projects/sentiment-analysis/
~/projects/question-answering/
~/projects/rag-system/
# Bad
~/Desktop/ # Personal files here
~/ # Home directory
/tmp/ # Temp directory
2. Virtual Environment in Project
Never create virtual environment outside your project:
# â Bad - venv outside project
cd ~/
python -m venv my_global_venv
source my_global_venv/bin/activate
cd ~/Desktop/project
dspy-code
# â
Good - venv inside project
cd ~/projects/my-project
python -m venv .venv
source .venv/bin/activate # For fish: source .venv/bin/activate.fish
dspy-code
3. Add to .gitignore
Protect sensitive data:
# Virtual environment
.venv/
# dspy-code internal data
.dspy_code/
.dspy_cache/
# Environment variables (contains API keys!)
.env
.env.*
!.env.example
# Secrets
*.key
*.pem
secrets.json
4. Environment Variables for API Keys
Never commit API keys:
# â Bad - API key in config
models:
openai:
api_key: sk-1234567890abcdef # DON'T DO THIS!
# â
Good - use environment variable
models:
openai:
api_key: ${OPENAI_API_KEY} # From environment
# Set in environment
export OPENAI_API_KEY=sk-1234567890abcdef
# Or use .env file (add to .gitignore!)
echo "OPENAI_API_KEY=sk-1234567890abcdef" > .env
5. Regular Updates
Keep dspy-code updated for security patches:
đ Data Privacy
What Gets Cached
In .dspy_code/cache/: - Code signatures and metadata - Function/class names and docstrings - Project structure information
NOT cached: - API keys or secrets - Personal files or data - Anything outside your project
What Gets Sent to LLMs
When using model connections: - Your prompts and code generation requests - Generated code for validation - Optimization metrics
NOT sent: - Your entire codebase - API keys or secrets - Personal files
đ If Something Goes Wrong
Suspected Security Issue
If you believe dspy-code is accessing files it shouldn't:
- Stop immediately: Press Ctrl+C
- Check location: Run
pwdto verify directory - Report: Open issue on GitHub with details
- Check logs: Look in
.dspy_code/dspy-code.log
Clean Up
If you want to remove all dspy-code data:
# Remove internal data
rm -rf .dspy_code/
rm -rf .dspy_cache/
# Remove virtual environment
rm -rf .venv/
# Or delete entire project
cd ..
rm -rf my-dspy-project/
đŦ Reporting Security Vulnerabilities
If you discover a security vulnerability:
- Do NOT open a public issue
- Email security concerns to maintainers
- Include steps to reproduce
- Allow time for fix before disclosure
⨠Summary
DSPy Code is designed to be secure by default:
- â All data in current working directory
- â No home directory access
- â No system directory access
- â Path boundary validation
- â Symlink protection
- â Startup safety checks
- â Comprehensive testing
Remember: Always run from a dedicated project directory with a virtual environment inside it!
Questions about security? Open an issue or check our FAQ.