Skip to content

Security: korkmazdev/LagerFlow

Security

SECURITY.md

Security notes

What the API does to protect itself and its data, mapped roughly to the OWASP Top 10. This is a portfolio project, but I tried to make the defaults the ones I'd want in production.

Authentication

  • Short-lived JWT access tokens (HS256), 15 min lifetime, 30 s clock skew. Issuer, audience, signature, lifetime and algorithm are all validated (alg: none is rejected).
  • Passwords are hashed with ASP.NET Core Identity's PasswordHasher (PBKDF2, random salt). Old hashes are upgraded automatically on login.
  • Password policy: minimum 12 characters (length over complexity rules, per NIST 800-63B).
  • Login returns the same message for "unknown user" and "wrong password", and does a dummy hash for unknown users so the response time doesn't give it away either.
  • Account lockout: 5 failed attempts → locked for 15 minutes.
  • Rate limit: 10 requests/minute per IP on login/register/change-password, 60/minute on /refresh, plus a global per-user limit. Behind nginx the real client IP comes from X-Forwarded-For, which is only trusted from the configured proxy network (otherwise everyone would share nginx's IP bucket, and anyone could spoof the header when calling the API directly).
  • Disabled users can't sign in or refresh.

Two-factor authentication (TOTP, RFC 6238)

  • Opt-in per user. Setup only activates after a valid code, so a half-done setup can't lock anyone out.
  • The TOTP secret is encrypted with AES-256-GCM using Mfa:EncryptionKey from the environment, not stored in the DB - a database dump alone can't generate codes. (Chosen over ASP.NET Data Protection, whose key ring would sit in the container and be lost on redeploy.)
  • After the password step the API returns a 5 min challenge token (JWT with its own audience, so it is rejected as an access token). No refresh cookie is set until the code is verified.
  • ±1 time step for clock drift; an accepted step is stored so the same code can't be replayed.
  • Wrong codes count towards the account lockout, and the password step doesn't reset the counter.
  • 10 recovery codes (~50 bits each), SHA-256 hashed, single use, shown once. Admins can reset 2FA for a user, which also revokes their sessions. All of it shows up in the security log.

Sessions (refresh tokens)

  • Login returns the access token in the body and sets a 256-bit random refresh token as a cookie: HttpOnly, Secure, SameSite=Strict, Path=/api/auth, 7 days.
  • Only a SHA-256 hash of the refresh token is stored, so a database leak doesn't hand out sessions.
  • Rotation: every refresh issues a new token and revokes the old one. Tokens from one login form a family; presenting an already-rotated token (= it was stolen and replayed) revokes the whole family.
  • CSRF: the cookie endpoints also require an X-LagerFlow-Client header. Cross-site forms can't send custom headers, and cross-origin fetches with one fail the CORS preflight.
  • Logout, "log out everywhere", password change, role change and disabling a user all revoke sessions.
  • CORS uses an explicit origin list together with AllowCredentials (never AllowAnyOrigin).
  • Self-registration always creates a User. There is no way to make yourself admin through the API.

Authorization (Broken Access Control)

  • Roles: Admin, Warehouse, User. Endpoints check permissions (stock.manage, orders.process, users.manage, ...); the role → permission map lives in one place (Domain/Users/Permissions.cs) and is shared by the policies and the UI.
  • An admin can't change their own role or disable themselves (no locking yourself out).
  • Security-relevant events (logins, failed logins, lockouts, token reuse, role changes...) are written to a SecurityEvents table with IP and actor, viewable by admins.
  • A fallback policy requires an authenticated user on every endpoint. Public endpoints have to opt out explicitly with [AllowAnonymous] (only auth and /health).
  • Admin-only actions use a policy (AdminOnly).
  • Users can only see and cancel their own orders. The customer email always comes from the token, never from the request body. Other users' orders return 404 rather than 403, so you can't probe which order ids exist.

Input handling

  • All request DTOs are validated with FluentValidation (lengths, ranges, formats).
  • EF Core parameterises all queries. No raw SQL.
  • OData is read-only and limited: $top max 100, page size 50, no $expand.
  • Request body size is capped at 1 MB. List endpoints clamp page size to 100.

Data integrity

  • Optimistic concurrency (rowversion) on products prevents lost updates and overselling.
  • DB check constraint on stock, and unique indexes on SKU, user email and ERP order id.

Error handling and logging

  • A global exception handler returns RFC 7807 ProblemDetails with a trace id. Unknown errors become a generic 500: no stack traces, SQL or exception messages leak out.
  • Structured logging with Serilog. Passwords and tokens are never logged.

Transport and headers

  • HTTPS redirection + HSTS outside Development.
  • X-Content-Type-Options, X-Frame-Options, Referrer-Policy and a strict CSP on API responses.
  • The Server header is removed.
  • CORS only allows the explicitly configured origins.

Frontend

  • Served by nginx with a strict CSP (no inline scripts, connect-src 'self'), X-Frame-Options: DENY and no server version. nginx proxies /api to the backend, so the browser talks to one origin only.
  • The access token is kept in memory only (not in local/sessionStorage) and only attached to requests for our own API. On reload the app gets a new one from the httpOnly refresh cookie. On a 401 the interceptor refreshes once (shared between parallel requests, so rotation doesn't trip reuse detection) and retries.
  • Route guards are only for UX. Every permission check happens in the API.

Secrets and supply chain

  • No real secrets in the repo. docker-compose reads them from .env (git-ignored); the app refuses to start if the JWT key is missing or shorter than 256 bits. The values in appsettings.Development.json are local dev only.
  • The ERP integration uses an API key compared in constant time.
  • Containers run as a non-root user. The database port is bound to 127.0.0.1 only.
  • CI fails on known vulnerable NuGet and npm packages (dotnet list package --vulnerable, npm audit), and Dependabot keeps dependencies up to date.

Known gaps

  • Access tokens can't be revoked before they expire (max 15 min) - the usual JWT trade-off.
  • 2FA is opt-in, not enforced for staff roles.
  • Swagger is only enabled in Development.
  • Seeding and migrations on startup are dev-only; in production they'd run as a pipeline step.

There aren't any published security advisories