Skip to content

Fix CSRF middleware planned before introducing any cookie-based auth - #405

Merged
kilodesodiq-arch merged 1 commit into
ChainForgee:mainfrom
ZionApprove:fix/issue-217-csrf-middleware
Jul 22, 2026
Merged

Fix CSRF middleware planned before introducing any cookie-based auth#405
kilodesodiq-arch merged 1 commit into
ChainForgee:mainfrom
ZionApprove:fix/issue-217-csrf-middleware

Conversation

@ZionApprove

Copy link
Copy Markdown
Contributor

Summary

Implemented a production-ready double-submit-cookie CSRF middleware guarded behind the CSRF_PROTECTION_ENABLED feature flag, while maintaining the existing security posture where CSRF is mitigated by design (API-key header auth).

Changes

  • Created src/common/security/csrf.middleware.ts — double-submit-cookie CSRF middleware with:
    • Cookie parsing from Cookie header (no cookie-parser dependency)
    • Cryptographically random 32-byte tokens via crypto.randomBytes
    • Constant-time token comparison via crypto.timingSafeEqual
    • SameSite=strict, secure in production, 1-hour expiry
    • Only active when CSRF_PROTECTION_ENABLED=true
  • Created src/common/security/csrf.middleware.spec.ts — 13 unit tests covering:
    • Safe methods receive CSRF cookie
    • Existing valid cookies not overwritten
    • Unsafe methods reject missing tokens (cookie/header)
    • Unsafe methods reject mismatched tokens
    • Unsafe methods accept matching tokens (POST, PUT, PATCH, DELETE)
    • Token length mismatch handling
  • Created docs/security/csrf-posture.md — documentation covering:
    • Current CSRF posture and when it would break
    • Middleware contract and enabling instructions
    • Client integration guide with code examples
  • Updated main.ts to conditionally register CSRF middleware
  • Updated security.module.ts posture comment to reference the new middleware

Closes #217

…e flag

- Implement CsrfMiddleware using the double-submit-cookie pattern
- Guard behind CSRF_PROTECTION_ENABLED=true environment variable
- Parse cookies from Cookie header (no cookie-parser dependency)
- Constant-time token comparison to prevent timing attacks
- Set SameSite=strict, secure in production, 1-hour expiry
- Add comprehensive unit tests (13 tests covering all scenarios)
- Add docs/security/csrf-posture.md with contract and client guide
- Update security.module.ts posture comment to reference new middleware

Closes ChainForgee#217

@kilodesodiq-arch kilodesodiq-arch left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kilodesodiq-arch
kilodesodiq-arch merged commit 23015cc into ChainForgee:main Jul 22, 2026
5 checks passed
ifygreg01-best pushed a commit to ifygreg01-best/ChainForge that referenced this pull request Jul 23, 2026
…e flag (ChainForgee#405)

- Implement CsrfMiddleware using the double-submit-cookie pattern
- Guard behind CSRF_PROTECTION_ENABLED=true environment variable
- Parse cookies from Cookie header (no cookie-parser dependency)
- Constant-time token comparison to prevent timing attacks
- Set SameSite=strict, secure in production, 1-hour expiry
- Add comprehensive unit tests (13 tests covering all scenarios)
- Add docs/security/csrf-posture.md with contract and client guide
- Update security.module.ts posture comment to reference new middleware

Closes ChainForgee#217

Co-authored-by: ZionApprove <zionapprove@users.noreply.github.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.

CSRF middleware planned before introducing any cookie-based auth

2 participants