Code and Environment Security
When developing with Vibe Coding, apart from waiting for AI to generate code, I also pay special attention to the behavior of the Cursor. If any potential risks are identified, I immediately create rules in cursorrule.md to ensure every step is securely managed.
Cursor Permissions Management and Security Considerations
1. Key Points for Configuring Cursor Permissions
File System Permissions
- Principle of Least Privilege: Grant Cursor access only to necessary project directories.
- Avoid System-Level Permissions: Do not grant Cursor administrator privileges.
- Sensitive File Protection: Ensure that sensitive files such as
.env,config.json, andsecretsare inaccessible to Cursor.
Gitignore Best Practices
- Exclude Sensitive Files: All files containing sensitive information must be excluded from version control.
- Environment Variable Files: Exclude all
.env*,.env.local,.env.production, etc. - Configuration and Credential Files: Exclude files containing passwords, keys, or certificates.
- Database and Log Files: Exclude local databases, backups, and logs that might contain sensitive information.
- Cache and Temporary Files: Exclude files that could leak information.
Network Permissions
- API Key Protection: Ensure Cursor cannot access files containing API keys.
- Database Connection Info: Avoid hardcoding database passwords in files accessible by Cursor.
- Third-Party Service Credentials: Protect all external service authentication details.
2. Security Checks for Code Output
Risks of Sensitive Information Leakage
// Unsafe Example:
const API_KEY = "dy-1234567890abcdef";
const DB_PASSWORD = "dy1203";
const JWT_SECRET = "secretkey";
// Safe Example:
const API_KEY = process.env.API_KEY;
const DB_PASSWORD = process.env.DB_PASSWORD;
const JWT_SECRET = process.env.JWT_SECRET;
Common Security Vulnerability Checks
- Hardcoded Passwords: Check for plaintext passwords or API keys.
- SQL Injection Risks: Always use parameterized queries.
- XSS Vulnerabilities: Strengthen input validation and output encoding.
- CSRF Protection: Implement CSRF tokens.
3. Pre-Deployment Security Checklist
Key Points for Code Review
- Use of Environment Variables: Ensure all sensitive information uses environment variables.
- Error Handling: Check for error messages that might expose sensitive information.
- Log Records: Ensure logs do not contain passwords or personal data.
- Dependency Checks: Scan third-party packages for known vulnerabilities.
Deployment Environment Configuration
- Enforce HTTPS: Ensure all communications use HTTPS.
- CORS Configuration: Properly configure cross-origin resource sharing.
- Security Headers: Set appropriate HTTP security headers.
- File Upload Restrictions: Limit the type and size of uploaded files.
4. Recommended Security Practices
Development Phase
- Use
.gitignoreto exclude sensitive files. - Create
.env.exampletemplate files. - Implement a code review process.
- Use static code analysis tools.
Deployment Phase
- Use separate environment variables for production.
- Regularly update dependencies.
- Implement automated security scans.
- Establish monitoring and alert mechanisms.
5. Suggested Cursor Project Settings
Project Structure
project/
├── .env.local # Local development environment variables
├── .env.example # Environment variable template
├── .gitignore # Exclude sensitive files
├── .cursorignore # Cursor ignore files
└── src/
├── config/ # Configuration files
└── utils/ # Utility functions
Example .cursorignore
.env*
*.key
*.pem
secrets/
config/production.json
Example .gitignore
# Environment Variable Files
.env
.env.local
.env.development
.env.test
.env.production
.env.staging
.env.*
# Sensitive Configuration Files
config/secrets.json
config/database.json
config/production.json
config/*.secret.*
# Certificates and Keys
*.pem
*.key
*.crt
*.p12
*.pfx
*.jks
*.keystore
*.truststore
# Database Files
*.db
*.sqlite
*.sqlite3
*.mdb
*.accdb
*.frm
*.ibd
# Log Files
*.log
logs/
log/
# Cache and Temporary Files
.cache/
.tmp/
temp/
tmp/
*.tmp
*.temp
# IDE and Editor Settings
.vscode/settings.json
.idea/
*.swp
*.swo
*~
# OS Files
.DS_Store
Thumbs.db
desktop.ini
# Backup Files
*.bak
*.backup
*.old
backup/
backups/
Core Security Principles
1. Environment Isolation and Protection
2. Code Security Checks
Vibe Coding emphasizes continuous security checks during development:
- Static Code Analysis: Detect potential vulnerabilities in real-time.
- Dependency Scanning: Ensure third-party libraries are free of known vulnerabilities.
- Sensitive Data Protection: Prohibit hardcoding of passwords or API keys.
- Input Validation: Strictly validate and sanitize all external inputs.
3. Team Collaboration Security
Creating a secure collaboration environment is a key part of Vibe Coding:
- Permission Management: Ensure each developer only has access to necessary resources.
- Code Reviews: All code changes must undergo peer reviews.
- Security Training: Conduct regular security awareness training.
- Incident Response: Establish processes for quickly responding to security incidents.
Practical Use Cases
Development Phase
- Use secure development tools and IDEs.
- Implement code signing and verification.
- Establish secure CI/CD pipelines.
Deployment Phase
- Secure configuration for containerized applications.
- Network security settings.
- Database access control.
Operations Phase
- Continuous monitoring and log analysis.
- Regular security updates and patches.
- Disaster recovery planning.
Why Choose Vibe Coding?
- Prevention Over Cure: Build security awareness during development.
- Team Collaboration: Security is everyone's responsibility.
- Continuous Improvement: Continuously optimize security processes and tools.
- User Experience: Ensure the product is secure and user-friendly.
Conclusion
Vibe Coding is not just a programming methodology but also a security culture. By fostering a good development atmosphere and security awareness, teams can create secure, high-quality software products.
Special Reminder: When using AI programming tools like Cursor, always establish security awareness and proper permission controls:
- Principle of Least Privilege: Grant only necessary access.
- Sensitive Information Protection: Never hardcode sensitive information.
- Pre-Deployment Checks: Establish a comprehensive security review process.
- Continuous Monitoring: Regularly inspect and update security configurations.
Remember: The best security measure is to make security a natural habit during development, not a remedial action afterward.