| name | security-environment-standards |
| description | Security and environment configuration standards for web applications, including environment variable management, secure coding practices, and production deployment security. Use when setting up environments, configuring security, or deploying applications. Use when this capability is needed. |
| metadata | {"author":"findinfinitelabs"} |
Security & Environment Standards
Authentication Model
The app uses passwordless magic-link authentication — no passwords, no flask-login, no WTForms. A time-limited token is emailed; clicking it creates an authenticated session.
magic_links = {}
active_sessions = {}
MAGIC_LINK_EXPIRY_MINUTES = 15
def send_magic_link_email(email, magic_token, base_url) -> bool:
...
The @login_required decorator (defined in app.py) checks session['authenticated'] and active_sessions.
Environment Variables
FLASK_SECRET_KEY=<64-hex-chars>
FLASK_ENV=production
COSMOS_DB_CONNECTION_STRING=mongodb://...
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your@gmail.com
SMTP_PASSWORD=app-specific-password
SMTP_FROM=your@gmail.com
GOOGLE_VISION_API_KEY=...
MAX_CONTENT_LENGTH=16777216
UPLOAD_FOLDER=uploads
Session Security (actual app.py config)
app.config['SESSION_COOKIE_SECURE'] = os.getenv('FLASK_ENV') == 'production'
app.config['SESSION_COOKIE_HTTPONLY'] = True
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
app.config['PERMANENT_SESSION_LIFETIME'] = 86400 * 7
File Upload Security
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif', 'pdf', 'docx', 'txt', 'csv', 'epub'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
filename = secure_filename(file.filename)
Regex Safety
User input used in MongoDB $regex must be escaped:
import re
pattern = re.escape(user_input)
collection.find({'field': {'$regex': f'^{pattern}$'}})
Secrets Management
- Never hardcode secrets in source files
- Use Azure Key Vault references for production environment variables
FLASK_SECRET_KEY must be set explicitly in production; app raises ValueError otherwise
users.json (authorised email list) is gitignored in production deployments
Production Checklist
Source Files
app.py — auth routes, login_required decorator, send_magic_link_email()
config/users.json — authorised user email list
deploy-chuuk.sh — production Azure deployment script
Converted and distributed by TomeVault — claim your Tome and manage your conversions.