- CORS Configuration: Uses explicit origins from environment variables, not wildcards
- OAuth 2.0: Google OAuth for authentication
- Environment Variables: Sensitive data stored in
.env.local(not committed to git) - HTTPS Ready: FastAPI supports HTTPS in production
- Input Validation: Pydantic models validate all API inputs
- Token Tracking: Monitor and limit AI token usage per user
- Rate Limiting: Configurable limits on memories per day
The following are configured for local development:
- HTTP (not HTTPS) - acceptable for localhost
- CORS allows
http://localhost:3002andhttp://localhost:8000 - OAuth tokens stored unencrypted in SQLite database
Before deploying to production, implement these security measures:
# Use a reverse proxy like nginx or deploy to a platform that handles TLS
# Update CORS_ORIGINS in .env.local to use https://
CORS_ORIGINS=["https://yourdomain.com","https://api.yourdomain.com"]# Generate a strong secret key
SECRET_KEY=$(python -c "import secrets; print(secrets.token_urlsafe(32))")
# Use production database
DATABASE_URL=postgresql+asyncpg://user:password@host:5432/memagent
# Update origins
FRONTEND_URL=https://yourdomain.com
CORS_ORIGINS=["https://yourdomain.com"]- Use PostgreSQL instead of SQLite
- Enable SSL/TLS for database connections
- Encrypt OAuth tokens at rest (add encryption layer)
- Regular backups with encryption
- Rotate database credentials periodically
Already configured but can be adjusted:
# .env.local
MAX_MEMORIES_PER_DAY=10 # Adjust based on your needsfrom fastapi.middleware.trustedhost import TrustedHostMiddleware
# Only allow requests from your domain
app.add_middleware(
TrustedHostMiddleware,
allowed_hosts=["yourdomain.com", "www.yourdomain.com"]
)Add to nginx or use Starlette middleware:
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Strict-Transport-Security: max-age=31536000; includeSubDomains
Content-Security-Policy: default-src 'self'
- Use HTTPS for OAuth redirect URIs
- Implement token encryption at rest
- Rotate refresh tokens periodically
- Add token revocation on logout
- Monitor for suspicious OAuth activity
Already implemented with Pydantic, but verify:
- Max length limits on all text inputs
- Sanitize file uploads
- Validate image files before processing
- Check for SQL injection attempts (SQLAlchemy handles this)
- Use structured logging (already implemented with structlog)
- Set up log aggregation (ELK, Datadog, etc.)
- Monitor for suspicious activity patterns
- Alert on unusual token usage
- Track failed authentication attempts
# Regularly update dependencies
uv pip list --outdated
# Audit for known vulnerabilities
pip install safety
safety check
# Use dependabot or similar for automated updatesIn production, restrict API docs access:
# main.py
app = FastAPI(
title="MemAgent API",
version="0.1.0",
docs_url="/docs" if settings.debug else None, # Disable in production
redoc_url="/redoc" if settings.debug else None,
)Add to frontend response headers:
<meta http-equiv="Content-Security-Policy"
content="default-src 'self';
script-src 'self' 'unsafe-inline' 'unsafe-eval';
style-src 'self' 'unsafe-inline';
img-src 'self' data: https:;
connect-src 'self' https://yourdomain.com;"># frontend/.env.production
NEXT_PUBLIC_API_URL=https://api.yourdomain.comIf deploying to GCP (recommended for Google Photos integration):
- Enable HTTPS by default ✓
- Use Cloud SQL for PostgreSQL
- Use Secret Manager for sensitive data
- Enable Cloud Armor for DDoS protection
- Set up IAM roles correctly
- Add production redirect URIs in Google Cloud Console
- Restrict API keys to specific domains
- Enable Google Photos Library API
- Set up OAuth consent screen for production
- Revoke all user tokens immediately
- Force re-authentication for all users
- Investigate access logs
- Notify affected users
- Rotate OAuth client secrets
- Take database offline immediately
- Rotate all credentials
- Restore from clean backup
- Audit for data exfiltration
- Implement additional security measures
- Add privacy policy
- Implement data deletion on request
- Add consent management
- Data processing agreement with Google
- Regular security audits
- Define retention policies
- Implement automatic data cleanup
- Secure data deletion procedures
Before production deployment:
# Run security scan
bandit -r backend/
# Test for common vulnerabilities
pytest tests/security/
# Load testing
locust -f tests/load/locustfile.py
# Penetration testing (hire professional)
# OWASP ZAP or similar toolsThe CORS configuration is already secure for development:
# Uses environment variable from .env.local
CORS_ORIGINS=["http://localhost:3002","http://localhost:8000"]
# In main.py:
allow_origins=settings.cors_origins # NOT ["*"]
allow_credentials=True # Required for OAuth
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"] # Explicit list- ✅ Explicit Origins: Only listed domains can make requests
- ✅ No Wildcards: Never uses
["*"]with credentials - ✅ Configurable: Change origins via environment variable
- ✅ Credentials Enabled: Required for OAuth cookies/sessions
- ✅ Explicit Methods: Only allows specific HTTP methods
- ✅ Preflight Caching: Reduces OPTIONS requests
Simply update .env.local:
CORS_ORIGINS=["https://yourdomain.com"]
FRONTEND_URL=https://yourdomain.comFor security concerns or questions, consult: