Skip to content

Security: Godstaf/Devsters_25

Security

SECURITY.md

SECURITY.md

Security Best Practices for Devsters SIH25 (FastAPI + React)

Authentication and Authorization

  • Prefer OAuth2.1 with JWT bearer tokens for API auth. Scope endpoints under /api and require auth where applicable.
  • Current implementation exposes a form login at POST /loginit that uses SHA-256 password hashing and sets httponly cookies for userEmail and userName. This is NOT a session and is insufficient for auth. Replace with:
    • Proper user table with salted password hashes using bcrypt or argon2.
    • 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).
  • Enforce authorization checks on protected routes; least-privilege role-based access.

Data Protection

  • 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.

Dependency Management

  • Keep backend dependencies in requirements.txt and update regularly. Run pip-audit in CI.
  • For the frontend, run npm audit, use npm audit fix --force cautiously, and consider Snyk or Dependabot for both ecosystems.

Secrets and Configuration

  • 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.

Logging and Monitoring

  • 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.

Regular Security Audits

  • 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.

Network, Containers, and Infrastructure

  • Database is exposed on host port 5433 mapped to container 5432. Limit exposure to local network in production; prefer private networks and security groups.
  • The runtime container in Dockerfile runs 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.

Application Hardening Checklist

  • 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., slowapi or 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.

References

  • 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.

There aren't any published security advisories