Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Make logout a CSRF-protected POST (blocks forced-logout via cross-site GET) and stop the forgot-password cooldown message from leaking whether an email is registered

Murtaza Nasirfb82a927fa26
diff --git a/src/api/auth.py b/src/api/auth.py
index 9e60329a..11491f0e 100644
--- a/src/api/auth.py
+++ b/src/api/auth.py
@@ -372,13 +372,11 @@ def sso_unlink():
return redirect(url_for('auth.account'))
-@auth_bp.route('/logout')
+@auth_bp.route('/logout', methods=['POST'])
def logout():
- # /logout is GET-only, so Flask-WTF's CSRF check (which runs only on
- # state-changing methods) does not apply. No explicit exemption is
- # needed. A future hardening pass should move logout to POST so a
- # CSRF-redirected GET can't log a victim out, but that is a behaviour
- # change deferred from this security release.
+ # POST-only so the global CSRF check applies: a cross-site GET (e.g.
+ # <img src="/logout">) can no longer force-log-out a victim. The menu
+ # "Sign out" controls are CSRF-token-carrying POST forms.
logout_user()
return redirect(url_for('auth.login'))
@@ -494,10 +492,13 @@ def forgot_password():
# case by setting user.password from the form input; if the
# SSO link is also present, the user ends up with both, which
# is the prerequisite for sso_unlink later.
- can_resend, remaining = can_resend_password_reset(user)
- if not can_resend:
- flash(f'Please wait {remaining} seconds before requesting another reset email.', 'warning')
- else:
+ # Only send if outside the resend cooldown, but never surface the
+ # cooldown to the caller: a "please wait N seconds" message would
+ # only ever appear for a real account, confirming the address
+ # exists. Silently skip within the window; the generic message
+ # below is returned identically whether or not the account exists.
+ can_resend, _ = can_resend_password_reset(user)
+ if can_resend:
send_password_reset_email(user)
flash('If an account exists with this email, a password reset link has been sent.', 'info')
diff --git a/templates/components/header.html b/templates/components/header.html
index 4493d264..a710bd2a 100644
--- a/templates/components/header.html
+++ b/templates/components/header.html
@@ -243,9 +243,12 @@
<span v-text="t('modal.colorScheme')"></span>
</button>
<div class="border-t border-[var(--border-primary)] my-1"></div>
- <a href="/logout" class="block px-4 py-2 hover:bg-[var(--bg-tertiary)] transition-colors">
- <i class="fas fa-sign-out-alt mr-2 w-4 text-center"></i><span v-text="t('nav.signOut')"></span>
- </a>
+ <form method="POST" action="/logout" class="m-0">
+ <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
+ <button type="submit" class="w-full text-left block px-4 py-2 hover:bg-[var(--bg-tertiary)] transition-colors">
+ <i class="fas fa-sign-out-alt mr-2 w-4 text-center"></i><span v-text="t('nav.signOut')"></span>
+ </button>
+ </form>
</div>
</div>
{% endif %}
diff --git a/templates/components/page-header.html b/templates/components/page-header.html
index eb4e5f25..cd5183bf 100644
--- a/templates/components/page-header.html
+++ b/templates/components/page-header.html
@@ -50,9 +50,12 @@
</a>
{% endif %}
<div class="border-t border-[var(--border-primary)] my-1"></div>
- <a href="{{ url_for('auth.logout') }}" class="block px-4 py-2 hover:bg-[var(--bg-tertiary)] transition-colors">
- <i class="fas fa-sign-out-alt mr-2 w-4 text-center"></i><span data-i18n="nav.signOut">Sign out</span>
- </a>
+ <form method="POST" action="{{ url_for('auth.logout') }}" class="m-0">
+ <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
+ <button type="submit" class="w-full text-left block px-4 py-2 hover:bg-[var(--bg-tertiary)] transition-colors">
+ <i class="fas fa-sign-out-alt mr-2 w-4 text-center"></i><span data-i18n="nav.signOut">Sign out</span>
+ </button>
+ </form>
</div>
</div>
{% endif %}
diff --git a/templates/group-admin.html b/templates/group-admin.html
index c8a4cb73..e12d3c50 100644
--- a/templates/group-admin.html
+++ b/templates/group-admin.html
@@ -146,9 +146,12 @@
<i class="fas fa-users-cog mr-2"></i>
Group Management
</a>
- <a href="{{ url_for('auth.logout') }}" class="block px-4 py-2 text-[var(--text-secondary)] hover:bg-[var(--bg-tertiary)] hover:text-[var(--text-danger)]">
- <i class="fas fa-sign-out-alt mr-2"></i> Sign Out
- </a>
+ <form method="POST" action="{{ url_for('auth.logout') }}" class="m-0">
+ <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
+ <button type="submit" class="w-full text-left block px-4 py-2 text-[var(--text-secondary)] hover:bg-[var(--bg-tertiary)] hover:text-[var(--text-danger)]">
+ <i class="fas fa-sign-out-alt mr-2"></i> Sign Out
+ </button>
+ </form>
</div>
</div>
</div>
diff --git a/templates/inquire.html b/templates/inquire.html
index 6d9c6a25..2035db73 100644
--- a/templates/inquire.html
+++ b/templates/inquire.html
@@ -229,9 +229,12 @@
<span v-text="t('modal.colorScheme')"></span>
</button>
<div class="border-t border-[var(--border-primary)] my-1"></div>
- <a href="/logout" class="block px-4 py-2 hover:bg-[var(--bg-tertiary)] transition-colors">
- <i class="fas fa-sign-out-alt mr-2 w-4 text-center"></i><span v-text="t('nav.signOut')"></span>
- </a>
+ <form method="POST" action="/logout" class="m-0">
+ <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
+ <button type="submit" class="w-full text-left block px-4 py-2 hover:bg-[var(--bg-tertiary)] transition-colors">
+ <i class="fas fa-sign-out-alt mr-2 w-4 text-center"></i><span v-text="t('nav.signOut')"></span>
+ </button>
+ </form>
</div>
</div>
{% endif %}
diff --git a/tests/test_cov_auth.py b/tests/test_cov_auth.py
index ddc15de9..e2ad10d0 100644
--- a/tests/test_cov_auth.py
+++ b/tests/test_cov_auth.py
@@ -176,12 +176,23 @@ def test_login_when_already_authenticated_redirects(cleanup_users):
def test_logout_clears_session(cleanup_users):
user = _mk_user(cleanup_users)
with _client_logged_in_as(user) as client:
- resp = client.get('/logout')
+ # Logout is POST-only (CSRF-protected) so a cross-site GET can't force it.
+ resp = client.post('/logout')
assert resp.status_code == 302
with client.session_transaction() as s:
assert '_user_id' not in s
+def test_logout_rejects_get(cleanup_users):
+ # A cross-site <img src="/logout"> style GET must not log the user out.
+ user = _mk_user(cleanup_users)
+ with _client_logged_in_as(user) as client:
+ resp = client.get('/logout')
+ assert resp.status_code == 405
+ with client.session_transaction() as s:
+ assert s.get('_user_id') == str(user.id) # still logged in
+
+
def test_login_blocked_when_email_unverified(cleanup_users):
# auth.py:205 — verification required AND user not verified ⇒ login
# is gated: the check-email page renders and no session is established.