Spaces:
Sleeping
Security Setup Instructions
Summary
All security improvements have been successfully committed and pushed to GitHub. Follow the instructions below to complete the deployment to HuggingFace and set up your environment variables.
What Was Fixed
Security Improvements β
- Removed hardcoded ADMIN123 token (now environment-based with auto-generation)
- Rotated Flask secret key with fail-fast validation
- Added rate limiting to authentication endpoints
- Removed exposed HuggingFace token from git config
- Created comprehensive SECURITY.md documentation
HuggingFace Deployment Fixes β
- Fixed TRANSFORMERS_CACHE deprecation warning
- Resolved matplotlib permission errors
- Improved SQLite database locking issues
- Fixed sentence text display in PDF exports
Step 1: Push to HuggingFace
Since you revoked the old HuggingFace token, you need to authenticate with your new token:
cd /home/thadillo/MyProjects/participatory_planner
# Push to HuggingFace (you'll be prompted for credentials)
git push hf feature/sentence-level-categorization:main
When prompted:
- Username:
thadillo - Password: Enter your NEW HuggingFace write token (starts with
hf_...)
The credentials will be stored in git credential helper for future pushes.
Step 2: Configure HuggingFace Spaces Secrets
Go to your HuggingFace Space settings and add these Repository Secrets:
https://huggingface.co/spaces/thadillo/participatory-planner/settings
Required Secrets:
FLASK_SECRET_KEY (Critical - for session security)
Generate a new one with: python -c "import secrets; print(secrets.token_hex(32))" Example output (DO NOT USE THIS - generate your own): c5eafdcbd348d3176bf1fcf52d90357575f7bc1986b7baeb67bb9d140f401881ADMIN_TOKEN (Critical - for admin access)
Generate a new one with: python -c "import secrets; print(secrets.token_urlsafe(16))" Example output (DO NOT USE THIS - generate your own): xulmKoajFS07akF9Eos9TgSAVE THIS TOKEN SECURELY - You'll need it to log in to the admin panel!
How to Add Secrets in HuggingFace:
- Go to your Space: https://huggingface.co/spaces/thadillo/participatory-planner
- Click Settings tab
- Scroll to Repository Secrets section
- Click New Secret
- Add each secret:
- Name:
FLASK_SECRET_KEY - Value: (paste the generated secret key)
- Click Add
- Name:
- Repeat for
ADMIN_TOKEN
Step 3: Configure Local Development Environment
Update your local .env file with the SAME values you set in HuggingFace:
cd /home/thadillo/MyProjects/participatory_planner
# Edit your .env file
nano .env
Your .env file should contain:
# Flask Configuration
FLASK_SECRET_KEY=<PASTE THE SAME VALUE YOU USED IN HUGGINGFACE>
FLASK_ENV=development
# Model Configuration
MODELS_DIR=models/finetuned
CUDA_VISIBLE_DEVICES=-1
# Admin Token (must match HuggingFace secret)
ADMIN_TOKEN=<PASTE THE SAME VALUE YOU USED IN HUGGINGFACE>
Important: Use the EXACT SAME values for FLASK_SECRET_KEY and ADMIN_TOKEN that you set in HuggingFace. This ensures your local environment matches production.
Step 4: Verify Local Setup
Test that your local environment works correctly:
cd /home/thadillo/MyProjects/participatory_planner
# Start the application
./start.sh
You should see output like:
π Admin token: Check startup logs or set ADMIN_TOKEN in .env
π Starting application...
If you didn't set ADMIN_TOKEN in .env, a random token will be generated and displayed ONCE in the logs. Save it immediately!
Step 5: Test Admin Access
Local Testing:
- Start the application:
./start.sh - Go to: http://localhost:5000/generate
- Generate a new token using your ADMIN_TOKEN
- Use the generated token to test the application
HuggingFace Testing:
- Wait for HuggingFace to rebuild (after pushing changes)
- Go to: https://thadillo-participatory-planner.hf.space/generate
- Generate a new token using your ADMIN_TOKEN
- Verify the application works without the previous errors
Step 6: Verify HuggingFace Deployment
After pushing to HuggingFace, monitor the build logs:
https://huggingface.co/spaces/thadillo/participatory-planner/logs
Check for these confirmations:
- β No TRANSFORMERS_CACHE deprecation warning
- β No matplotlib permission errors
- β No SQLite database locking errors
- β Application starts successfully
Security Best Practices Going Forward
DO:
β
Keep .env file local and never commit it to git
β
Use different secrets for development and production
β
Rotate secrets periodically (every 3-6 months)
β
Store production secrets only in HuggingFace Spaces secrets
β
Use strong, randomly generated tokens (via secrets module)
DON'T:
β Never commit .env to git
β Never hardcode secrets in code
β Never share your ADMIN_TOKEN publicly
β Never use simple passwords like "ADMIN123"
β Never include tokens in git remote URLs
Quick Reference Commands
Generate Flask Secret Key:
python -c "import secrets; print(secrets.token_hex(32))"
Generate Admin Token:
python -c "import secrets; print(secrets.token_urlsafe(16))"
Push to HuggingFace:
git push hf feature/sentence-level-categorization:main
Start Local Server:
./start.sh
View HuggingFace Logs:
https://huggingface.co/spaces/thadillo/participatory-planner/logs
View HuggingFace Settings:
https://huggingface.co/spaces/thadillo/participatory-planner/settings
Troubleshooting
If HuggingFace deployment fails:
- Check the build logs for error messages
- Verify all secrets are set correctly in HuggingFace settings
- Ensure
FLASK_SECRET_KEYandADMIN_TOKENare set
If you get "database is locked" errors:
- The new retry logic should handle this automatically
- If it persists, check HuggingFace logs for concurrent request issues
If you forget your ADMIN_TOKEN:
- Generate a new one:
python -c "import secrets; print(secrets.token_urlsafe(16))" - Update it in HuggingFace Spaces secrets
- Update it in your local
.envfile - Restart the application
If authentication fails:
- Ensure you're using the correct ADMIN_TOKEN
- Check that rate limiting hasn't blocked you (wait 1 hour)
- Verify the token in HuggingFace secrets matches your local
.env
Next Steps
- Immediate: Push to HuggingFace using the command above
- Immediate: Set up HuggingFace Spaces secrets (FLASK_SECRET_KEY, ADMIN_TOKEN)
- Immediate: Update your local
.envfile - Soon: Test the application on HuggingFace to verify all errors are resolved
- Optional: Consider merging
feature/sentence-level-categorizationtomainbranch
Files Modified
All changes have been committed to git. Here's what was changed:
- app/init.py - Secure token generation, secret key validation, rate limiter
- app/routes/auth.py - Rate limiting on login/generate endpoints
- app/routes/admin.py - Removed hardcoded ADMIN123 references
- requirements.txt - Added Flask-Limiter
- .env.example - Updated with new structure
- Dockerfile - Fixed HuggingFace deployment issues
- SECURITY.md - New comprehensive security documentation
- Multiple markdown files - Documentation cleanup
Support
For questions or issues:
- Review SECURITY.md for detailed security information
- Check HuggingFace build logs for deployment errors
- Verify environment variables are set correctly
Remember: Save your ADMIN_TOKEN securely - it's shown only once when auto-generated!