- Prefer OAuth2.1 with JWT bearer tokens for API auth. Scope endpoints under
/apiand require auth where applicable. - Current implementation exposes a form login at
POST /loginitthat uses SHA-256 password hashing and setshttponlycookies foruserEmailanduserName. This is NOT a session and is insufficient for auth. Replace with:- Proper user table with salted password hashes using
bcryptorargon2. - Token-based auth (JWT) issued on login; store token in memory or secure cookie with
HttpOnly,Secure,SameSite=Strict. - Implement logout by invalidating tokens (short TTL + rotation/blacklist if needed).
- Proper user table with salted password hashes using
- Enforce authorization checks on protected routes; least-privilege role-based access.
- Validate and constrain all inputs with Pydantic schemas; reject unexpected fields.
- Database access uses parameterized queries via
psycopg2— continue to avoid string interpolation to prevent SQL injection. - Encode/escape user-provided content rendered in templates or returned to the SPA to mitigate XSS.
- Always deploy behind HTTPS in production; terminate TLS at a reverse proxy (e.g., Nginx or Caddy) and forward to Uvicorn.
- Do not log sensitive data (passwords, tokens, full PII). Redact on logs/metrics.
- Configure CORS to only allow trusted origins for the SPA.
- If using cookies for auth, add CSRF protection on state-changing endpoints (double-submit or token per form) and set cookie flags:
HttpOnly,Secure,SameSite=Strict.
- Keep backend dependencies in
requirements.txtand update regularly. Runpip-auditin CI. - For the frontend, run
npm audit, usenpm audit fix --forcecautiously, and consider Snyk or Dependabot for both ecosystems.
- Docker Compose sets DB env vars. For production, use Docker secrets or a secrets manager (e.g., AWS Secrets Manager) instead of plain env vars.
- Never commit secrets. Keep a
.env(local only) out of VCS via.gitignore. - Rotate credentials periodically. Use distinct credentials per environment.
- Centralize structured logs; include request IDs and minimal PII.
- Enable access/error logs at the reverse proxy. Consider rate and anomaly detection on login endpoints.
- Add metrics (latency, error rates) and alerts. Consider tools like Prometheus/Grafana, ELK, or OpenTelemetry.
- Run automated security scans (dependency, container image, IaC). Suggested tools:
pip-audit,npm audit, Trivy, Snyk. - Conduct periodic penetration tests and threat modeling. Review code for security issues pre-release.
- Database is exposed on host port
5433mapped to container5432. Limit exposure to local network in production; prefer private networks and security groups. - The runtime container in
Dockerfileruns as non-root (appuser). Keep it that way; avoid mounting unnecessary volumes in production. - Keep images minimal and updated. Scan images (Trivy) and pin base images.
- Expose only required ports (backend
8000). Place a reverse proxy in front with HTTPS, HSTS, and request size limits.
- Enforce CORS for known origins only.
- Add security headers via Starlette middleware: CSP, HSTS, X-Content-Type-Options, X-Frame-Options, Referrer-Policy.
- Implement rate limiting on login and sensitive endpoints (e.g.,
slowapior proxy-level limits). - Strong password policy and breach-check (k-Anonymity/HaveIBeenPwned).
- Account lockout/backoff on repeated failed logins.
- Short-lived tokens with refresh; rotate and revoke on logout.
- Comprehensive input validation and consistent error handling without leaking internals.
- FastAPI Security: OAuth2 with JWT
- OWASP ASVS and OWASP Top 10
- Docker Security and CIS Benchmarks
Following these practices will help secure the application and protect user data across the FastAPI backend, React SPA, and PostgreSQL services.