Skip to content

Add CSRF protection and rate limiting to maintenance requests - #1

Open
minoosama wants to merge 2 commits into
mainfrom
fix/csrf-dos-protection
Open

minoosama wants to merge 2 commits into
mainfrom
fix/csrf-dos-protection

Conversation

@minoosama

Copy link
Copy Markdown
Owner

Summary

Fixes two vulnerabilities in the maintenance requests feature.

CSRF. Write routes previously trusted the auth cookie alone, so any site the user visited could forge requests in their name.

  • The server now uses double-submit CSRF tokens (csrf-csrf) bound to the user's session. Every POST/PUT/PATCH/DELETE must send a valid token in the x-csrf-token header, except login and register. Invalid or missing tokens get a 403.
  • New endpoint GET /api/csrf-token hands the client its token.
  • The client's axios instance fetches the token and attaches it to every mutating request. It clears the token when the session changes (login/register/logout) and retries once with a fresh token after a 403.

DoS. There was no rate limiting, so the public list endpoint could be flooded.

  • express-rate-limit caps /api/maintenance at 30 requests per minute per client, and writes at 10 per minute. Excess requests get a 429 before any auth check or database query runs.

Tests

  • Added Jest suites for server CSRF, server DoS and client CSRF behavior (6 tests). They use an in-memory MongoDB, so no database setup is needed. Run them with npm test.
  • Added a GitHub Actions workflow (.github/workflows/test.yml) that runs npm test on every push and pull request.

All 6 tests pass locally and on GitHub Actions.

Configuration

Set CSRF_SECRET in server/.env. Without it, the server generates a random secret at startup, so tokens stop working after a restart.

🤖 Generated with Claude Code

minoosama and others added 2 commits November 12, 2026 16:04
- Server: double-submit CSRF tokens (csrf-csrf) bound to the session,
  required on all state-changing requests; new GET /api/csrf-token.
- Server: express-rate-limit on /api/maintenance (30/min) with a tighter
  limit on writes (10/min).
- Client: axios interceptor fetches and attaches x-csrf-token on mutating
  requests, refreshing on session change or a rejected token.
- Tests: jest suites for server CSRF, server DoS, and client CSRF behavior.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant