Skip to content

feat: add CSRF protection for state-mutating API routes - #1127

Closed
smartalee wants to merge 7 commits into
rinafcode:mainfrom
smartalee:security/csrf-protection-fix
Closed

feat: add CSRF protection for state-mutating API routes#1127
smartalee wants to merge 7 commits into
rinafcode:mainfrom
smartalee:security/csrf-protection-fix

Conversation

@smartalee

Copy link
Copy Markdown
Contributor

Summary

Adds CSRF protection to all state-mutating API routes using the double-submit cookie pattern.

Changes

  • Added src/lib/csrfMiddleware.ts with CSRF token generation and validation
  • Updated src/middleware.ts to validate CSRF tokens on state-mutating requests
  • Exempted auth endpoints and safe methods (GET/HEAD/OPTIONS)
  • Uses timing-safe token comparison to prevent timing attacks

Security

  • Double-submit cookie pattern prevents CSRF attacks
  • SameSite=Lax cookies for additional protection
  • No server-side storage required (stateless)

API Endpoints Protected

  • POST/PATCH/DELETE /api/notes
  • POST/PATCH/DELETE /api/bookmarks
  • POST/PATCH/DELETE /api/approvals
  • POST/PATCH/DELETE /api/tips

Closes #723

smartalee and others added 7 commits June 30, 2026 08:22
- Add double-submit cookie CSRF pattern with x-csrf-token header
- Implement CSRF middleware with validation for POST/PATCH/DELETE
- Exempt auth endpoints and GET/HEAD/OPTIONS from validation
- Apply CSRF validation to notes, bookmarks, approvals, and tips routes

Closes rinafcode#723
@smartalee smartalee closed this Jul 31, 2026
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.

[Security] No CSRF protection on state-mutating API routes

1 participant