Security Guidelines - Crossword App
π Environment Variables & API Keys
β Secure Practices Implemented
1. Environment Files
- β
.env
files are gitignored - β
.env.example
template provided - β No real secrets in source code
- β Automatic setup script provided
2. API Key Management
Local Development:
# 1. Set up environment
./setup-env.sh
# 2. Edit .env with your real key
HUGGINGFACE_API_KEY=hf_your_real_key_here
# 3. .env is automatically gitignored
Production Deployment:
- HuggingFace Spaces: Use Settings β Environment Variables
- Railway/Heroku: Use config vars
- Docker: Pass as runtime environment variables
3. Default Security
- π‘οΈ Graceful fallback when API keys missing
- π‘οΈ No crashes on missing configuration
- π‘οΈ Warning messages instead of errors
- π‘οΈ Safe defaults for all settings
π¨ What NOT to Do
β Never commit real API keys:
// β NEVER DO THIS
const apiKey = 'hf_real_key_here';
β Never hardcode secrets:
// β NEVER DO THIS
const config = {
huggingfaceKey: 'hf_abcd1234...'
};
β Never share .env files:
# β NEVER DO THIS
git add .env
git commit -m "added config"
β Safe Patterns
β Always use environment variables:
// β
SAFE
const apiKey = process.env.HUGGINGFACE_API_KEY;
β Always check for existence:
// β
SAFE WITH FALLBACK
if (!apiKey || apiKey === 'hf_xxxxxxxxxx') {
console.warn('API key not configured, using fallback');
return this.fallbackMethod();
}
β Always use templates:
# β
SAFE
cp .env.example .env
# Edit .env with real values
π File Security
Gitignore Coverage
# Environment files
.env
.env.local
.env.*.local
# Security files
*.key
*.pem
.secret
secrets/
File Structure
backend/
βββ .env.example # β
Safe template (committed)
βββ .env # π Real values (gitignored)
βββ .env.backup # π Backup (gitignored)
βββ setup-env.sh # β
Setup script (committed)
π Deployment Security
HuggingFace Spaces
- Go to Space Settings
- Add Environment Variable:
HUGGINGFACE_API_KEY
- Set value to your real API key
- Restart space
Docker Deployment
# Runtime environment variable
docker run -e HUGGINGFACE_API_KEY=hf_your_key app
CI/CD Pipelines
# GitHub Actions example
env:
HUGGINGFACE_API_KEY: ${{ secrets.HUGGINGFACE_API_KEY }}
π Security Verification
Pre-commit Checklist
- No real API keys in code
- .env in .gitignore
- Only .env.example committed
- All secrets use environment variables
- Fallback mechanisms working
Testing Security
# Test without API key
unset HUGGINGFACE_API_KEY
npm run dev
# Should work with fallback
# Test with invalid key
export HUGGINGFACE_API_KEY="invalid"
npm run dev
# Should gracefully fallback
π Resources
π If API Key Gets Exposed
- Immediately revoke the key at https://huggingface.co/settings/tokens
- Generate new key with appropriate permissions
- Update all deployment environments
- Check git history for any committed secrets
- Consider repository security scan
Remember: Security is a process, not a destination. Always be vigilant! π‘οΈ