Skip to content

Security: add CSRF protection to login and setup forms#87

Open
vdaluz wants to merge 1 commit intosam1am:mainfrom
vdaluz:fix/csrf-protection
Open

Security: add CSRF protection to login and setup forms#87
vdaluz wants to merge 1 commit intosam1am:mainfrom
vdaluz:fix/csrf-protection

Conversation

@vdaluz
Copy link
Copy Markdown

@vdaluz vdaluz commented Apr 16, 2026

Problem

The login and account setup forms have no CSRF tokens. A page on another origin could submit these forms on behalf of a visiting user (CSRF attack).

Fix

Uses itsdangerous.URLSafeTimedSerializer — already a project dependency — to generate signed, time-limited tokens embedded as hidden form fields. Tokens are scoped with a separate salt (backlogia-csrf) from the session signer and expire after 1 hour.

Invalid or missing tokens return 403 with a freshly generated token so the user can retry without reloading the page.

Changes

  • app_auth.py: _generate_csrf_token() and _validate_csrf_token() helpers; validation added to POST /auth/login and POST /auth/setup
  • login.html / setup.html: hidden csrf_token input added to both forms

Note: uses Starlette 1.0.0+ TemplateResponse API (see PR #84).

Login and account creation forms had no CSRF tokens. A malicious page
could submit these forms on behalf of an authenticated user.

Uses itsdangerous URLSafeTimedSerializer (already a dependency) to
generate signed, time-limited tokens embedded as hidden form fields.
Tokens expire after 1 hour. Invalid or missing tokens return 403 with
a fresh token so the user can retry immediately.

Uses Starlette 1.0.0+ TemplateResponse API (request as first arg).
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