Security Features Documentation¶
This document provides comprehensive information about the security features implemented in the FastAPI RBAC project.
🔒 Security Overview¶
The FastAPI RBAC system implements enterprise-grade security with multiple layers of protection against common web vulnerabilities and attacks.
Security Rating: ⭐⭐⭐⭐⭐ (Enterprise-Grade)
🛡️ Implemented Security Features¶
1. CSRF Protection¶
Implementation: fastapi-csrf-protect==1.0.3
- Endpoint:
GET /api/v1/auth/csrf-token - Protection: All state-changing operations (POST, PUT, DELETE)
- Token Management: Secure cookie handling with signed/unsigned token support
- Validation: 403 responses for invalid or missing CSRF tokens
Usage Example:
// Frontend CSRF token handling
const csrfToken = await csrfService.getCsrfToken();
// Token automatically included in subsequent requests
2. Input Sanitization¶
Implementation: Custom InputSanitizer class with bleach==6.2.0
- XSS Prevention: HTML tag removal and content cleaning
- Field-Type Sanitization: Text, email, HTML, URL, and search field sanitization
- SQL Injection Protection: Parameter sanitization and validation
- Path Traversal Protection: File path validation and cleaning
- DoS Protection: Input length validation and rate limiting
Sanitization Types:
sanitize_text(): Basic text cleaningsanitize_email(): Email format validation and cleaningsanitize_html(): HTML content sanitization with allowed tagssanitize_url(): URL validation and cleaningsanitize_search(): Search query cleaning
3. HTTP Rate Limiting¶
Implementation: slowapi (sole HTTP rate limit library; see ADR 0008)
Protected Endpoints (HTTP rate limits, all currently anonymous and therefore address-keyed):
- Login: 5 attempts per minute
- Registration: 3 attempts per hour
- Password Reset request: 3 attempts per hour
- Access token: 5 attempts per minute
Configuration: shared Limiter in app/core/rate_limit.py (rate_limit_key; Redis storage_uri outside testing).
The key is user:{id} when get_current_user has already established the caller, and ip:{client address} otherwise. Prefixes keep those two from colliding. The key function does not decode tokens. The four endpoints above never call get_current_user, so they stay address-keyed at the thresholds listed. A later authenticated route that takes @limiter.limit gets a per-user bucket instead of sharing one address quota.
The address used for the IP key is the real client address: ProxyHeadersMiddleware corrects request.client from X-Forwarded-For / X-Real-IP when the peer is a TRUSTED_PROXIES member, and ignores those headers otherwise (ADR 0011 decision 8). Behind a proxy that is not configured as trusted, every anonymous client shares one bucket.
Registration / resend-verification also use separate Redis abuse counters (not slowapi).
4. Enhanced Security Headers¶
Implementation: Custom middleware and Nginx configuration
Headers Applied:
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: geolocation=(), microphone=(), camera=()
Strict-Transport-Security: max-age=31536000; includeSubDomains
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; ...
Protection Against:
- Clickjacking (X-Frame-Options)
- MIME sniffing (X-Content-Type-Options)
- XSS attacks (CSP and X-XSS-Protection)
- Information leakage (Referrer-Policy)
5. JWT Token Security¶
Implementation: PyJWT signing/verification plus Redis allowlist session invalidation. app/utils/sessions.py owns the session lifecycle; its storage is app/utils/allowlist.py. See System Architecture and ADR 0001.
Features:
- Access Tokens: Short-lived, stored in memory (Redux state)
- Refresh Tokens: Long-lived, HttpOnly Secure cookies (see ADR 0006)
- Token Allowlisting: Only tokens recorded at login/refresh are accepted
- Automatic Refresh: Transparent token renewal via cookie + CSRF
- Secure Logout:
POST /logoutrevokes the calling session only;POST /logout/allrevokes every session. The refresh cookie is cleared on both. See ADR 0011 decision 9 - Origin-Network Anomaly Detection: when
VALIDATE_TOKEN_IPis on, the address a session was established from is recorded with its refresh allowlist entry. A refresh presented from a different IPv4 /24 or IPv6 /64 revokes that one session and answers with the ordinary refresh failure. Access tokens are never checked this way, the user's other sessions survive, and a session with no recorded origin refreshes normally. This is detection, not IP binding -- see ADR 0011 decision 5
Security Measures:
# Open a session at login (simplified): mints both tokens, records them on
# the allowlist, and evicts the oldest session at CONCURRENT_SESSION_LIMIT
issued = await sessions.open(user, client_address, redis_client=redis_client)
write_session_cookie(response, issued)
# Reject tokens missing from the live allowlist
try:
user_id = await sessions.accepts(access_token, redis_client=redis_client)
except sessions.SessionRejected:
raise HTTPException(status_code=403, detail="Could not validate credentials, token invalid or revoked.")
6. Password Security¶
Implementation: Advanced password validation and history
Features:
- Password Strength: Integration with
zxcvbnfor strength validation - Password History: Prevents reuse of last 5 passwords
- Account Locking:
MAX_LOGIN_ATTEMPTS(5) failed attempts lock the account forACCOUNT_LOCKOUT_MINUTES(24 hours); a viewer withuser.update(administrators and managers by default) can lift a lock early withPOST /users/{user_id}/unlock; login failures get one uniform response (ADR 0015) - Secure Hashing: bcrypt with salt for password storage
Password Policy:
- Minimum 12 characters (
PASSWORD_MIN_LENGTH), maximum 128 - Upper case, lower case, digit and special character required
- Rejects common passwords, sequential runs (
abc,123) and repeated runs - Strength score validation
- History tracking for compliance
- Automatic lockout protection
The thresholds above are settings, not constants in code. Every path that sets
a password goes through app/utils/password_policy.py, so none of them can
drift into a looser rule of its own. Registration, admin create and database
seeding call accept_initial_password (rules only), which returns an
AcceptedPassword holding the hash. crud.user.create and create_with_role
accept only that, never a plaintext, so a new way of creating users cannot
skip the policy: it is a type error, and refused at runtime. Seeding refuses a
FIRST_SUPERUSER_PASSWORD that fails the policy. Change password, both reset-confirm
endpoints and admin update of a single user call change_password, which
applies the rules, then the reuse policy, then ends every session, then
commits, then writes the success audit event. A refusal is answered as 400
with detail = {"message", "errors"} on every path. Bulk user update refuses
a password key rather than applying one password to many accounts.
7. Audit Logging¶
Implementation: Comprehensive security event logging
Logged Events:
- Authentication attempts (success/failure)
- Account lockouts and unlocks: sign-in attempts refused on a locked account, and an administrator or manager unlocking one (
user_unlocked, with them as the actor) - Password changes
- Permission changes
- Administrative actions
- Security violations
Log Format:
audit_log = AuditLog(
actor_id=user_id, # null when the event has no known user
action="successful_login",
resource_type="security_event",
resource_id=str(user_id) if user_id else "",
details={"ip_address": client_ip, "email": email},
timestamp=datetime.utcnow(),
)
log_security_event writes this row in-process and is awaited, including on paths that then raise HTTPException. A failed audit write is logged and does not change the endpoint's status code.
🔍 Security Testing¶
Backend Security Tests¶
Files:
backend/test/test_csrf_implementation.py: CSRF protection validationbackend/test/test_sanitization.py: Input sanitization testing- Backend test suite includes security-focused test cases
Frontend Security Tests¶
Coverage: 17 CSRF-related tests in the frontend test suite
Test Areas:
- CSRF token retrieval and storage
- Token inclusion in requests
- Error handling for invalid tokens
- Token refresh mechanisms
🚨 Security Monitoring¶
Rate Limiting Monitoring¶
Security Event Monitoring¶
Failed Authentication Monitoring¶
# Failed login attempt tracking
user.failed_attempts += 1
if user.failed_attempts >= 5:
user.locked_until = datetime.utcnow() + timedelta(minutes=15)
🔧 Security Configuration¶
Environment Variables¶
# Rate Limiting
RATE_LIMIT_STORAGE_URL=redis://localhost:6379
# Signing and encryption keys: five distinct generated values (see below)
SECRET_KEY=
JWT_REFRESH_SECRET_KEY=
JWT_RESET_SECRET_KEY=
JWT_VERIFICATION_SECRET_KEY=
ENCRYPT_KEY=
# Token lifetimes
ACCESS_TOKEN_EXPIRE_MINUTES=30
REFRESH_TOKEN_EXPIRE_MINUTES=144000 # 100 days
# Password Security
PASSWORD_MIN_LENGTH=8
PASSWORD_HISTORY_COUNT=5
MAX_LOGIN_ATTEMPTS=5
ACCOUNT_LOCKOUT_MINUTES=1440 # how long a lock lasts
# Mode and token exposure
MODE=production # defaults to development; set it for every deployment
EXPOSE_TOKENS_IN_RESPONSES=false # default; refused when MODE=production
EXPOSE_TOKENS_IN_RESPONSES puts password-reset tokens and email verification
codes in the responses of POST /auth/password-reset/request and
POST /auth/register, so automated test stacks can finish those flows without
reading mail. It is off by default in every mode, whatever MODE is. The
backend refuses to start with it on in production, and refuses to start when
FASTAPI_ENV=production (set by the production images) and MODE is not
production. At startup the API logs a warning when MODE is not
production and another when the opt-in is on.
Signing and encryption keys¶
| Setting | What it protects |
|---|---|
SECRET_KEY |
Signs access tokens; also the CSRF secret and Celery's message-signing key |
JWT_REFRESH_SECRET_KEY |
Signs refresh tokens |
JWT_RESET_SECRET_KEY |
Signs password-reset tokens |
JWT_VERIFICATION_SECRET_KEY |
Signs email verification tokens |
ENCRYPT_KEY |
The Fernet key for get_data_encrypt and get_content (hashed into one when it is not a valid Fernet key) |
With MODE=production the backend refuses to start unless each of the five
is set, at least 32 characters, and different from the other four. It also
refuses any of them that is a value published in this repository's example or
template files, or that contains an obvious placeholder word (change,
your-, your_, replace, example, placeholder or generate, in any
case). The error names the keys at fault, never their values. Development and
testing modes skip these checks, so the placeholders in backend/.env.example
keep working there.
Generate a separate key for each, running the command once per key:
JWT_SECRET_KEY is removed: nothing signed with it. A deployment that still
sets it starts normally and the value is ignored. CSRF uses SECRET_KEY;
there is no separate CSRF_SECRET_KEY.
Docker Security¶
# Non-root user containers
USER 1000:1000
# Security options
security_opt:
- no-new-privileges:true
# Read-only root filesystem
read_only: true
📋 Security Checklist¶
Pre-Deployment Security Validation¶
- [ ] CSRF Protection: Verify all state-changing endpoints are protected
- [ ] Input Sanitization: Test XSS prevention on all form inputs
- [ ] Rate Limiting: Validate rate limits are working on auth endpoints
- [ ] Security Headers: Confirm all security headers are present
- [ ] JWT Security: Test token generation, validation, and allowlist invalidation
- [ ] Password Security: Verify password policies and account locking
- [ ] Audit Logging: Confirm security events are being logged
- [ ] HTTPS: Ensure all communications are encrypted in production
- [ ] Mode:
MODE=productionis set in the container environment, andEXPOSE_TOKENS_IN_RESPONSESis unset or false - [ ] Keys: the five signing and encryption keys are separately generated values
Security Testing Commands¶
# Test CSRF protection
python backend/test/test_csrf_implementation.py
# Test input sanitization
python backend/test/test_sanitization.py
# Run security-focused backend tests
pytest test/ -k "security or auth or csrf" -v
# Run frontend security tests
cd react-frontend
npm test -- --run csrfService.test.ts
🔒 Production Security Recommendations¶
1. Infrastructure Security¶
- Use HTTPS/TLS certificates
- Configure firewall rules
- Implement network segmentation
- Regular security updates
2. Database Security¶
- Use encrypted connections
- Implement database user permissions
- Regular backup encryption
- Access logging
3. Monitoring & Alerting¶
- Security event monitoring
- Failed authentication alerting
- Rate limiting breach notifications
- Unusual activity detection
4. Regular Security Reviews¶
- Monthly security audits
- Dependency vulnerability scanning
- Code security reviews
- Penetration testing
📚 Additional Resources¶
- OWASP Security Guidelines
- FastAPI Security Documentation
- JWT Security Best Practices
- CSRF Protection Guide
Last Updated: June 11, 2025 Security Review: All features verified and operational Compliance: Enterprise-grade security standards met