Skip to content

feat(security): add CSRF token validation for state-changing requests#3248

Open
vishu-bh wants to merge 5 commits intomainfrom
feature/csrf-token-in-requests
Open

feat(security): add CSRF token validation for state-changing requests#3248
vishu-bh wants to merge 5 commits intomainfrom
feature/csrf-token-in-requests

Conversation

@vishu-bh
Copy link
Copy Markdown
Collaborator

@vishu-bh vishu-bh commented Feb 25, 2026

🔗 Related Issue

Closes #543


📝 Summary

What does this PR do and why?

Implements a unified CSRF protection system for ContextForge and binds CSRF tokens to authenticated JWT sessions (via verified jti). The change replaces the legacy admin‑only CSRF path with a single, consistent CSRF cookie/header across UI and API flows, backed by configurable settings. It adds middleware enforcement for unsafe methods, a /auth/csrf-token refresh endpoint, and Admin UI updates (meta tags + JS injection) so forms/fetch automatically carry the CSRF header. Default exemptions are expanded for admin login/reset flows and path exemptions now support prefix matching. Tests and templates are updated to align with the unified CSRF cookie name and behavior.

Key implementation points:

  • CSRF token generation/validation uses csrf_secret_key and JWT jti session binding.
  • Admin UI now relies on the unified CSRF token and metadata in admin.html + admin.js.
  • Auth and password‑reset templates read the unified CSRF cookie.
  • Exempt paths include /admin/login, /admin/forgot-password, /admin/reset-password with prefix support.
  • Updated unit tests for admin and CSRF middleware.

🏷️ Type of Change

  • Bug fix
  • Feature / Enhancement
  • Documentation
  • Refactor
  • Chore (deps, CI, tooling)
  • Other (describe below)

🧪 Verification

Check Command Status
Lint suite make lint
Unit tests make test
Coverage ≥ 80% make coverage

✅ Checklist

  • Code formatted (make black isort pre-commit)
  • Tests added/updated for changes
  • Documentation updated (if applicable)
  • No secrets or credentials committed

📓 Notes (optional)

Screenshots, design decisions, or additional context.

@vishu-bh vishu-bh changed the title Feature/csrf token in requests feat(security): add CSRF token validation for state-changing requests Feb 25, 2026
@crivetimihai crivetimihai added enhancement New feature or request security Improves security COULD P3: Nice-to-have features with minimal impact if left out; included if time permits labels Feb 26, 2026
@crivetimihai crivetimihai added this to the Release 1.2.0 milestone Feb 26, 2026
@crivetimihai
Copy link
Copy Markdown
Member

Thanks @vishu-bh for working on this. Heads up: PR #3134 by the maintainer also implements CSRF token validation and is significantly further along. It may make sense to coordinate to avoid duplicated effort — the maintainer's version may land first. Let me know if you'd like to focus on a different issue instead, or if there are aspects your approach covers that #3134 doesn't.

@vishu-bh vishu-bh force-pushed the feature/csrf-token-in-requests branch from 7082bfc to ebf674a Compare February 26, 2026 15:32
@vishu-bh vishu-bh marked this pull request as ready for review February 26, 2026 15:34
@vishu-bh vishu-bh force-pushed the feature/csrf-token-in-requests branch from ed1c2d5 to 377fe64 Compare March 1, 2026 19:26
vishu-bh added 5 commits March 2, 2026 10:28
…/PATCH requests

Signed-off-by: Vishu Bhatnagar <vishu.bhatnagar@ibm.com>
Signed-off-by: Vishu Bhatnagar <vishu.bhatnagar@ibm.com>
Signed-off-by: Vishu Bhatnagar <vishu.bhatnagar@ibm.com>
Signed-off-by: Vishu Bhatnagar <vishu.bhatnagar@ibm.com>
Signed-off-by: Vishu Bhatnagar <vishu.bhatnagar@ibm.com>
@vishu-bh vishu-bh force-pushed the feature/csrf-token-in-requests branch from 23f6b86 to 92d15d8 Compare March 2, 2026 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

COULD P3: Nice-to-have features with minimal impact if left out; included if time permits enhancement New feature or request security Improves security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE][SECURITY]: CSRF token protection system

2 participants