Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
Fix CSRF bypass via unauthenticated API token parameter and SSO-only account takeover, preserving the safe add-password flow (CWE-287)
Murtaza Nasir71d6941322c9
diff --git a/src/api/auth.py b/src/api/auth.pyindex a432ea15..294f5ca3 100644--- a/src/api/auth.py+++ b/src/api/auth.py@@ -352,7 +352,12 @@ def sso_unlink():return redirect(url_for('auth.account'))if not current_user.password:- flash('Cannot unlink SSO - you have no password set. Please set a password first.', 'danger')+ flash(+ 'Cannot unlink SSO: you have no password set. Use the Forgot Password '+ 'link on the login page with your email address to receive a reset link '+ 'that lets you set a password.',+ 'danger',+ )return redirect(url_for('auth.account'))current_user.sso_provider = None@@ -473,14 +478,18 @@ def forgot_password():# Always show the same message to prevent email enumerationif user:- # Check if user has a password (not SSO-only)- if user.password:- # Check cooldown- 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:- send_password_reset_email(user)+ # Issue a reset link for both regular users and SSO-only users+ # (whose password is None). For SSO-only users this doubles as+ # the "add a password to my account" flow, gated by access to+ # the user's email. The reset_password route handles either+ # 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:+ send_password_reset_email(user)flash('If an account exists with this email, a password reset link has been sent.', 'info')return render_template('auth/check_email.html',@@ -779,6 +788,28 @@ def change_password():# Check if user has an existing passwordhas_existing_password = bool(current_user.password)+ # GHSA-x4q4-3ww4-h329 (Irina Iarlykanova) — SSO-only account takeover.+ # When an SSO-provisioned user has no local password, the old current-+ # password check was bypassed unconditionally, so anyone with a valid+ # session (including via the chained CSRF bypass that was the primary+ # finding) could silently set a password on an SSO-only account and+ # then log in directly with the new credentials, bypassing SSO.+ #+ # The safe way for an SSO-only user to add a password is the+ # password-reset flow: their email account is the proof-of-ownership+ # gate, the same trust boundary every password-reset flow relies on.+ # We redirect them there instead of letting this endpoint set the+ # password without any second factor.+ if not has_existing_password:+ flash(+ 'This account has no local password set. To add one, use the '+ 'Forgot Password link on the login page with your email '+ 'address; the reset email will let you set a password '+ 'securely.',+ 'warning',+ )+ return redirect(url_for('auth.account'))+# Validate form data - current password only required if user has oneif has_existing_password and not current_password:flash('Current password is required.', 'danger')diff --git a/src/app.py b/src/app.pyindex a5b22aff..1fe902f5 100644--- a/src/app.py+++ b/src/app.py@@ -463,26 +463,74 @@ def exempt_status_endpoints():csrf = CSRFProtect(app)+# Disable Flask-WTF's automatic CSRF check so we can run our own+# token-aware version below. The replacement still calls csrf.protect()+# for browser/session requests; the bypass only fires when a valid+# API token is presented via header (never query string).+#+# GHSA-x4q4-3ww4-h329 (Irina Iarlykanova): the previous+# ``csrf_exempt_for_api_tokens`` hook called ``csrf.exempt(view_func)``+# from a request handler. That mutates Flask-WTF's process-global+# ``_exempt_views`` set permanently, so any cross-origin request with+# ``?token=anything`` would silently disable CSRF on the targeted+# endpoint for the rest of the worker's lifetime, allowing follow-up+# CSRF attacks. Replaced with a per-request decision below.+app.config['WTF_CSRF_CHECK_DEFAULT'] = False-# Exempt token-authenticated requests from CSRF protection[email protected]_request-def csrf_exempt_for_api_tokens():- """- Exempt API token-authenticated requests from CSRF validation.- This allows automation tools (n8n, Zapier, curl, etc.) to make- authenticated requests without needing CSRF tokens.[email protected]_request+def csrf_token_aware_check():+ """Per-request CSRF check that allows valid header-only API tokens+ to bypass CSRF, without ever mutating Flask-WTF's exemption set.++ The check honours all the registration-time exemptions Flask-WTF+ already knows about (``csrf.exempt(blueprint)`` and+ ``csrf.exempt(view_func)`` called at module load time), and only+ skips CSRF for the specific request currently being handled.++ Tokens are accepted only from the Authorization, X-API-Token, and+ API-Token headers. The query-string token is intentionally NOT+ honoured here because a Simple Cross-Origin Request can carry one+ without triggering CORS preflight, which is exactly the attack+ vector in GHSA-x4q4-3ww4-h329."""- from src.utils.token_auth import is_token_authenticated-- # If request has a valid token, skip CSRF check- if is_token_authenticated():- # Mark this view as CSRF exempt- if hasattr(request, 'endpoint') and request.endpoint:- view_func = app.view_functions.get(request.endpoint)- if view_func:- csrf.exempt(view_func)+ if not app.config.get('WTF_CSRF_ENABLED', True):+ return+ if request.method not in app.config.get(+ 'WTF_CSRF_METHODS', {'POST', 'PUT', 'PATCH', 'DELETE'}+ ):+ return+ if not request.endpoint:+ return++ # Blueprint-level registration-time exemptions (e.g. api_v1_bp).+ # Note that csrf._exempt_blueprints holds Blueprint OBJECTS, so we+ # resolve the request's blueprint name through app.blueprints, the+ # same way Flask-WTF's own protect() hook does.+ if app.blueprints.get(request.blueprint) in csrf._exempt_blueprints:+ return++ # View-level registration-time exemptions (e.g. share_target).+ # Flask-WTF identifies exempt views by f"{__module__}.{__name__}";+ # match that exactly so any view registered via csrf.exempt() at+ # import time is honoured here.+ view = app.view_functions.get(request.endpoint)+ if view is not None:+ dest = f"{view.__module__}.{view.__name__}"+ if dest in csrf._exempt_views:+ return++ # If a valid header API token is presented, this request is from a+ # programmatic client and CSRF does not apply. The token is validated+ # against the DB by load_user_from_token_headers_only().+ from src.utils.token_auth import load_user_from_token_headers_only+ if load_user_from_token_headers_only() is not None:+ return++ # Otherwise, fall through to the standard CSRF check. If validation+ # fails, csrf.protect() raises a CSRFError which Flask-WTF's error+ # handler turns into a 400 response.+ csrf.protect()# Add context processor to make 'now' available to all templatesdiff --git a/src/utils/token_auth.py b/src/utils/token_auth.pyindex 9fd0086f..1464fa7d 100644--- a/src/utils/token_auth.py+++ b/src/utils/token_auth.py@@ -11,7 +11,7 @@ from flask import requestfrom src.models import APIToken, User-def extract_token_from_request():+def extract_token_from_request(headers_only=False):"""Extract API token from various possible locations in the request.@@ -19,7 +19,14 @@ def extract_token_from_request():1. Authorization header with Bearer scheme2. X-API-Token header3. API-Token header- 4. 'token' query parameter+ 4. 'token' query parameter (only when ``headers_only=False``)++ The ``headers_only`` flag exists because the query-string token can+ be triggered by a Simple Cross-Origin Request without CORS preflight+ (see GHSA-x4q4-3ww4-h329). Code paths that make security decisions+ based on whether a request is API-token-authenticated MUST pass+ ``headers_only=True`` so an attacker cannot place ``?token=...`` on+ a victim-browser URL to fake API authentication.Returns:str: The extracted token, or None if not found@@ -39,6 +46,9 @@ def extract_token_from_request():if token:return token+ if headers_only:+ return None+# Check query parametertoken = request.args.get('token')if token:@@ -97,12 +107,40 @@ def load_user_from_token():return api_token.user-def is_token_authenticated():+def load_user_from_token_headers_only():+ """Validate a header-only API token against the database.++ Same DB lookup as :func:`load_user_from_token` but ignores the+ ``?token=`` query parameter. Used by the CSRF protection hook+ because Simple Cross-Origin Requests can carry an attacker-supplied+ query string without triggering CORS preflight, while the three+ accepted headers (Authorization, X-API-Token, API-Token) all do+ trigger preflight and so cannot be set by a CSRF attack page.++ Returns the authenticated User, or None."""- Check if the current request is authenticated via API token.+ token = extract_token_from_request(headers_only=True)+ if not token:+ return None- Returns:- bool: True if a valid token was provided, False otherwise+ token_hash = hash_token(token)+ api_token = APIToken.query.filter_by(token_hash=token_hash).first()+ if not api_token or not api_token.is_valid():+ return None++ api_token.last_used_at = datetime.utcnow()+ from src.database import db+ db.session.commit()+ return api_token.user+++def is_token_authenticated():+ """Deprecated stub kept only for inbound import compatibility.++ The original implementation returned True on the mere PRESENCE of a+ token-looking parameter, which was the root cause of GHSA-x4q4-3ww4-h329.+ It is no longer used by the CSRF protection hook; the replacement is+ :func:`load_user_from_token_headers_only` plus the+ :func:`src.app.csrf_token_aware_check` before_request handler."""- token = extract_token_from_request()- return token is not None+ return load_user_from_token_headers_only() is not Nonediff --git a/tests/test_csrf_token_bypass_fix.py b/tests/test_csrf_token_bypass_fix.pynew file mode 100644index 00000000..b8df47de--- /dev/null+++ b/tests/test_csrf_token_bypass_fix.py@@ -0,0 +1,612 @@+"""Regression tests for GHSA-x4q4-3ww4-h329 (Irina Iarlykanova).++The advisory documented two coupled bugs:++A. ``csrf_exempt_for_api_tokens`` called ``csrf.exempt(view_func)`` from+ a before_request hook, mutating Flask-WTF's process-global+ ``_exempt_views`` set on every request that looked like it carried a+ token. That mutation persisted for the lifetime of the worker.++B. ``is_token_authenticated()`` returned True for the mere presence of a+ token value (header or ``?token=``), never validating the value+ against the DB. So any cross-origin GET with ``?token=anything``+ would silently disable CSRF protection on the targeted endpoint for+ the rest of the worker's lifetime.++The fix introduced:++1. ``load_user_from_token_headers_only()`` — DB-validated, header-only.+2. A per-request ``csrf_token_aware_check`` before_request hook that+ calls ``csrf.protect()`` unless a valid header token is present.+3. ``WTF_CSRF_CHECK_DEFAULT = False`` so Flask-WTF's own auto-check+ does not double-run.++These tests assert that:++- ``?token=anything`` no longer bypasses CSRF.+- ``X-API-Token: anything`` no longer bypasses CSRF.+- A valid header token DOES bypass CSRF (preserving the legitimate+ automation use case).+- Crucially, an unsuccessful bypass attempt does NOT add the target+ view to ``csrf._exempt_views``. The exempt set is registration-time+ immutable from a request handler's perspective.+- ``/account/change_password`` refuses to act on SSO-only accounts.+"""++import os+import sys+import uuid++sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))++from src.app import app, db, csrf+from src.models import User, APIToken+from src.utils.token_auth import hash_token+++def _make_user(prefix, **overrides):+ suffix = uuid.uuid4().hex[:8]+ user = User(+ username=f"{prefix}_{suffix}",+ email=f"{prefix}_{suffix}@local.test",+ password=overrides.pop('password', 'placeholder-bcrypt-hash'),+ **overrides,+ )+ db.session.add(user)+ db.session.commit()+ return user+++def _make_token(user, value=None):+ plaintext = value or uuid.uuid4().hex+ api_token = APIToken(+ user_id=user.id,+ token_hash=hash_token(plaintext),+ name='test',+ )+ db.session.add(api_token)+ db.session.commit()+ return plaintext, api_token+++def _login_session(client, user):+ """Plant a session cookie matching `user` and clear Flask-Login's+ per-app-context user cache."""+ from flask import g+ with client.session_transaction() as sess:+ sess.clear()+ sess['_user_id'] = str(user.id)+ sess['_fresh'] = True+ try:+ g.pop('_login_user', None)+ except RuntimeError:+ pass+++def _exempt_views_snapshot():+ return set(csrf._exempt_views)+++def test_query_string_fake_token_does_not_bypass_csrf():+ """Attack 2 from the advisory: an attacker's cross-origin GET sends+ ?token=any-value to silently disable CSRF for the endpoint. Must+ fail under the fix."""+ # Don't disable CSRF; we want to exercise the production path+ saved = app.config.get('WTF_CSRF_ENABLED', True)+ app.config['WTF_CSRF_ENABLED'] = True+ try:+ with app.app_context():+ user = _make_user('csrf_qstr')+ client = app.test_client()+ _login_session(client, user)++ # 1. The "disable CSRF" GET request the attacker would send.+ # This used to mutate csrf._exempt_views permanently.+ before = _exempt_views_snapshot()+ client.get('/account?token=any-bogus-value')+ after = _exempt_views_snapshot()++ # The exempt set must be unchanged.+ assert before == after, (+ 'csrf._exempt_views was mutated by a request with a bogus '+ 'query-string token; the fix did not stick.'+ )++ # 2. A follow-up POST without CSRF token must STILL be rejected.+ resp = client.post(+ '/account',+ data={'summary_prompt': 'EXPLOIT_PROOF'},+ follow_redirects=False,+ )+ assert resp.status_code == 400, (+ f'Expected 400 (CSRF rejected), got {resp.status_code}'+ )++ db.session.delete(user)+ db.session.commit()+ finally:+ app.config['WTF_CSRF_ENABLED'] = saved+++def test_header_with_fake_token_does_not_bypass_csrf():+ """Attack 1 variant: a bogus value in X-API-Token must not exempt."""+ saved = app.config.get('WTF_CSRF_ENABLED', True)+ app.config['WTF_CSRF_ENABLED'] = True+ try:+ with app.app_context():+ user = _make_user('csrf_hdr')+ client = app.test_client()+ _login_session(client, user)++ before = _exempt_views_snapshot()+ resp = client.post(+ '/account',+ data={'summary_prompt': 'EXPLOIT_PROOF'},+ headers={'X-API-Token': 'totally-bogus'},+ follow_redirects=False,+ )+ after = _exempt_views_snapshot()+ assert before == after, 'exempt set mutated by bogus header token'+ assert resp.status_code == 400++ db.session.delete(user)+ db.session.commit()+ finally:+ app.config['WTF_CSRF_ENABLED'] = saved+++def test_valid_header_token_bypasses_csrf_for_this_request_only():+ """The legitimate use case: a real API token in a header lets+ automation tools POST without a CSRF token. The bypass is per-request+ and does not mutate csrf._exempt_views."""+ saved = app.config.get('WTF_CSRF_ENABLED', True)+ app.config['WTF_CSRF_ENABLED'] = True+ try:+ with app.app_context():+ user = _make_user('csrf_valid')+ plaintext, _ = _make_token(user)+ client = app.test_client()+ _login_session(client, user)++ before = _exempt_views_snapshot()+ resp = client.post(+ '/account',+ data={'summary_prompt': 'API-DRIVEN-CHANGE'},+ headers={'X-API-Token': plaintext},+ follow_redirects=False,+ )+ after = _exempt_views_snapshot()++ # The set must still be unchanged: no mutation from a+ # request handler is the whole point of the fix.+ assert before == after+ # 302 redirect on success (account POST redirects).+ assert resp.status_code in (200, 302), resp.data++ # And the very next request without a token must again be+ # subject to CSRF.+ _login_session(client, user)+ resp2 = client.post(+ '/account',+ data={'summary_prompt': 'NO_TOKEN_NO_CSRF'},+ follow_redirects=False,+ )+ assert resp2.status_code == 400, (+ 'Endpoint stayed exempt after a token-authenticated '+ 'request — the bypass was not properly per-request.'+ )++ # Clean up+ for t in APIToken.query.filter_by(user_id=user.id).all():+ db.session.delete(t)+ db.session.delete(user)+ db.session.commit()+ finally:+ app.config['WTF_CSRF_ENABLED'] = saved+++def test_valid_query_string_token_does_not_bypass_csrf_post():+ """The query-string token is fine for read-only data access (the+ historical use case) but must never be honoured as a CSRF-bypass+ signal because Simple Cross-Origin Requests can carry one without+ preflight. Even when the token is genuinely valid."""+ saved = app.config.get('WTF_CSRF_ENABLED', True)+ app.config['WTF_CSRF_ENABLED'] = True+ try:+ with app.app_context():+ user = _make_user('csrf_qvalid')+ plaintext, _ = _make_token(user)+ client = app.test_client()+ _login_session(client, user)++ resp = client.post(+ f'/account?token={plaintext}',+ data={'summary_prompt': 'EXPLOIT_PROOF'},+ follow_redirects=False,+ )+ assert resp.status_code == 400, (+ 'A valid query-string token must NOT be honoured for '+ 'CSRF bypass; that was the exact attack vector in '+ 'GHSA-x4q4-3ww4-h329.'+ )++ for t in APIToken.query.filter_by(user_id=user.id).all():+ db.session.delete(t)+ db.session.delete(user)+ db.session.commit()+ finally:+ app.config['WTF_CSRF_ENABLED'] = saved+++def test_change_password_refuses_sso_only_account():+ """The chained takeover finding: SSO-only users had no local+ password, so the change_password route used to skip the+ current-password check and silently set a new one. After the fix+ the endpoint must refuse outright and point users to the safe+ email-gated path instead."""+ saved = app.config.get('WTF_CSRF_ENABLED', True)+ app.config['WTF_CSRF_ENABLED'] = False # focus on logic, not CSRF+ try:+ with app.app_context():+ user = _make_user('sso_only', password=None, sso_provider='Drift', sso_subject=f'subj-{uuid.uuid4().hex[:6]}')+ assert user.password is None+ client = app.test_client()+ _login_session(client, user)++ resp = client.post(+ '/change_password',+ data={+ 'new_password': 'A_Strong_New_Password!1',+ 'confirm_password': 'A_Strong_New_Password!1',+ },+ follow_redirects=False,+ )+ # The route redirects on the refusal path (with a flash).+ assert resp.status_code == 302++ # The user must still have no password.+ db.session.refresh(user)+ assert user.password is None, (+ 'change_password set a password on an SSO-only account; '+ 'the chained-takeover finding from GHSA-x4q4-3ww4-h329 '+ 'is not fixed.'+ )++ db.session.delete(user)+ db.session.commit()+ finally:+ app.config['WTF_CSRF_ENABLED'] = saved+++def test_sso_only_user_can_add_password_via_password_reset_flow():+ """Regression test for the legitimate SSO-to-password workflow that+ would have caught it if the GHSA-x4q4-3ww4-h329 fix were over-broad+ (i.e. blocking change_password without offering a replacement).++ The shape of the legitimate flow:++ 1. SSO-only user (no local password) wants to unlink SSO later.+ 2. ``sso_unlink`` requires a password, so they need to add one.+ 3. They go to ``forgot_password`` with their email.+ 4. The system emails a reset link (must work for SSO-only users).+ 5. They open the link and ``reset_password`` sets their first+ password.+ 6. The account now has both an SSO link and a password, so+ ``sso_unlink`` will work next time.++ Steps 3-5 are covered here. Step 6 is exercised separately because+ sso_unlink hits CSRF; the unit-level coverage is in the route's own+ happy-path tests.+ """+ from unittest.mock import patch++ saved_csrf = app.config.get('WTF_CSRF_ENABLED', True)+ app.config['WTF_CSRF_ENABLED'] = False+ try:+ with app.app_context():+ user = _make_user(+ 'sso_reset',+ password=None,+ sso_provider='Drift',+ sso_subject=f'subj-{uuid.uuid4().hex[:6]}',+ email_verified=True,+ )+ assert user.password is None++ client = app.test_client()++ # Step 3-4: forgot_password must send a reset email even+ # though the user has no existing password.+ with patch('src.api.auth.send_password_reset_email', return_value=True) as send_mail, \+ patch('src.api.auth.can_resend_password_reset', return_value=(True, 0)), \+ patch('src.api.auth.is_smtp_configured', return_value=True):+ resp = client.post('/forgot-password', data={'email': user.email}, follow_redirects=False)+ # forgot_password renders a "check your email" template on success (200)+ assert resp.status_code == 200, resp.data+ send_mail.assert_called_once()+ # Confirm the call targeted the SSO-only user we just created+ sent_to = send_mail.call_args.args[0]+ assert sent_to.id == user.id, (+ 'forgot_password did not call send_password_reset_email '+ 'for an SSO-only user; this is the regression that '+ 'would have surfaced any over-broad fix to '+ 'change_password.'+ )++ # Step 5: reset_password sets the first password.+ # Patch the token verifier so we do not need the real+ # serializer round-trip for the test.+ new_password = 'A_Strong_New_Password!1'+ with patch('src.api.auth.verify_reset_token', return_value=user.id):+ resp = client.post(+ '/reset-password/fake-token-value',+ data={'password': new_password, 'confirm_password': new_password},+ follow_redirects=False,+ )+ # Successful reset redirects to login+ assert resp.status_code == 302, resp.data++ db.session.refresh(user)+ assert user.password is not None, (+ 'reset_password did not set an initial password on an '+ 'SSO-only user; the safe add-password flow is broken.'+ )+ # And the SSO link is intact, so sso_unlink can run later.+ assert user.sso_subject is not None+ assert user.sso_provider is not None++ db.session.delete(user)+ db.session.commit()+ finally:+ app.config['WTF_CSRF_ENABLED'] = saved_csrf+++def test_api_v1_blueprint_remains_csrf_exempt_for_all_token_methods():+ """The documented programmatic surface (every example in+ docs/user-guide/api-tokens.md points at /api/v1/*) must continue+ to work for all four documented token methods even after the+ csrf_exempt_for_api_tokens hook was removed. The v1 blueprint is+ CSRF-exempt at registration time, so neither the hook nor its+ replacement is in the picture; this test pins that.++ We exercise the /api/v1/stats endpoint because it is a GET that+ Flask-WTF would normally not check anyway, plus it does not need+ any setup. The real assertion is that authentication completes+ via the request_loader and the route returns the user's stats."""+ saved = app.config.get('WTF_CSRF_ENABLED', True)+ app.config['WTF_CSRF_ENABLED'] = True+ try:+ with app.app_context():+ user = _make_user('api_v1_methods')+ plaintext, _ = _make_token(user)+ client = app.test_client()+ # No session login -- token must do all the work+ for method_name, kwargs in [+ ('Authorization Bearer', {'headers': {'Authorization': f'Bearer {plaintext}'}}),+ ('X-API-Token', {'headers': {'X-API-Token': plaintext}}),+ ('API-Token', {'headers': {'API-Token': plaintext}}),+ ('query string', {'query_string': {'token': plaintext}}),+ ]:+ resp = client.get('/api/v1/stats', **kwargs)+ assert resp.status_code == 200, (+ f'/api/v1/stats with {method_name} returned '+ f'{resp.status_code}; documented token method broke'+ )+ for t in APIToken.query.filter_by(user_id=user.id).all():+ db.session.delete(t)+ db.session.delete(user)+ db.session.commit()+ finally:+ app.config['WTF_CSRF_ENABLED'] = saved+++def test_api_v1_post_with_query_string_token_still_works():+ """Critical for backwards compatibility: a POST to /api/v1/* with+ ``?token=...`` still succeeds, because the v1 blueprint is+ blueprint-level CSRF-exempt. The fix only changed how non-v1+ endpoints react to a query-string token, not the v1 surface."""+ saved = app.config.get('WTF_CSRF_ENABLED', True)+ app.config['WTF_CSRF_ENABLED'] = True+ try:+ with app.app_context():+ user = _make_user('api_v1_query_post')+ plaintext, _ = _make_token(user)+ client = app.test_client()+ # Need a recording owned by the user to call regenerate_title+ from src.models import Recording+ rec = Recording(+ user_id=user.id,+ title='for-test',+ audio_path='/tmp/nonexistent.mp3',+ status='COMPLETED',+ transcription='hello world',+ )+ db.session.add(rec)+ db.session.commit()++ # Pure-query-string authentication on a v1 POST endpoint+ from unittest.mock import patch+ with patch('src.api.api_v1._enqueue_title_regeneration', create=True), \+ patch('src.services.job_queue.job_queue.enqueue', return_value=999):+ resp = client.post(+ f'/api/v1/recordings/{rec.id}/regenerate_title?token={plaintext}',+ )+ # Acceptable success codes (the route may 202 or 200)+ assert resp.status_code in (200, 202, 404), (+ f'/api/v1/* POST with ?token= regressed; got {resp.status_code}: {resp.data!r}'+ )+ # 404 is acceptable here if the endpoint requires more+ # setup than we have (e.g. completed transcript); what we+ # really care about is that it is NOT a 400 CSRF rejection.+ assert resp.status_code != 400++ db.session.delete(rec)+ for t in APIToken.query.filter_by(user_id=user.id).all():+ db.session.delete(t)+ db.session.delete(user)+ db.session.commit()+ finally:+ app.config['WTF_CSRF_ENABLED'] = saved+++def test_browser_session_post_with_valid_csrf_token_succeeds():+ """Backwards-compat positive: the regular web-UI flow (logged-in+ user POSTs with a CSRF token from the form) must still succeed+ after the CSRF refactor. The Attack 2 test already covers the+ NEGATIVE path (no token → 400); this pins the POSITIVE path."""+ saved = app.config.get('WTF_CSRF_ENABLED', True)+ app.config['WTF_CSRF_ENABLED'] = True+ try:+ with app.app_context():+ user = _make_user('csrf_browser_ok')+ client = app.test_client()+ _login_session(client, user)++ # Fetch the page to obtain a fresh CSRF token from the form.+ page = client.get('/account')+ import re+ m = re.search(rb'name="csrf_token" value="([^"]+)"', page.data)+ assert m, 'Could not find csrf_token in /account page'+ csrf_token = m.group(1).decode()++ resp = client.post(+ '/account',+ data={'user_name': 'Renamed User', 'csrf_token': csrf_token},+ follow_redirects=False,+ )+ # 302 redirect on success (account form pattern)+ assert resp.status_code in (200, 302), (+ f'Browser POST with valid CSRF token regressed: {resp.status_code}'+ )++ db.session.refresh(user)+ assert user.name == 'Renamed User', (+ 'Field did not persist; CSRF accepted but view did not run.'+ )++ db.session.delete(user)+ db.session.commit()+ finally:+ app.config['WTF_CSRF_ENABLED'] = saved+++def test_change_password_happy_path_for_user_with_existing_password():+ """Backwards-compat positive: the change_password fix only refuses+ accounts where ``current_user.password`` is None. Regular users+ with an existing password must still be able to change theirs.+ The fix did not touch that flow, but the test pins it so we know+ if anyone breaks it later."""+ from flask_bcrypt import Bcrypt+ bcrypt = Bcrypt(app)+ saved = app.config.get('WTF_CSRF_ENABLED', True)+ app.config['WTF_CSRF_ENABLED'] = False+ try:+ with app.app_context():+ current_password = 'Existing_Password!1'+ hashed = bcrypt.generate_password_hash(current_password).decode()+ user = _make_user('csrf_chpw_happy', password=hashed)+ client = app.test_client()+ _login_session(client, user)++ new_password = 'A_New_Password!1'+ resp = client.post(+ '/change_password',+ data={+ 'current_password': current_password,+ 'new_password': new_password,+ 'confirm_password': new_password,+ },+ follow_redirects=False,+ )+ assert resp.status_code == 302++ db.session.refresh(user)+ assert bcrypt.check_password_hash(user.password, new_password), (+ 'change_password did not update the password on a happy-path '+ 'user; the SSO refusal fix was over-broad.'+ )+ db.session.delete(user)+ db.session.commit()+ finally:+ app.config['WTF_CSRF_ENABLED'] = saved+++def test_api_v1_post_with_authorization_header_token_succeeds():+ """Backwards-compat positive for the recommended automation path.+ docs/user-guide/api-tokens.md lists ``Authorization: Bearer ...`` as+ the recommended method; this pins that POSTing to a v1 endpoint+ that way returns a non-CSRF response code (i.e. authentication and+ routing both worked; the actual route may 404 or 202 depending on+ record state, but it must not be CSRF-rejected)."""+ saved = app.config.get('WTF_CSRF_ENABLED', True)+ app.config['WTF_CSRF_ENABLED'] = True+ try:+ with app.app_context():+ user = _make_user('api_v1_auth_hdr_post')+ plaintext, _ = _make_token(user)+ client = app.test_client()+ from src.models import Recording+ rec = Recording(+ user_id=user.id,+ title='for-test',+ audio_path='/tmp/nonexistent.mp3',+ status='COMPLETED',+ transcription='hello world',+ )+ db.session.add(rec)+ db.session.commit()++ from unittest.mock import patch+ with patch('src.services.job_queue.job_queue.enqueue', return_value=999):+ resp = client.post(+ f'/api/v1/recordings/{rec.id}/regenerate_title',+ headers={'Authorization': f'Bearer {plaintext}'},+ )+ # Anything except 400/401/403 is fine here; the route's own+ # internal semantics may 404 if it cannot find an LLM, etc.+ assert resp.status_code not in (400, 401, 403), (+ f'Bearer token POST on /api/v1/* returned {resp.status_code} '+ f'(expected success or non-auth error); the documented '+ f'automation path is broken.'+ )+ db.session.delete(rec)+ for t in APIToken.query.filter_by(user_id=user.id).all():+ db.session.delete(t)+ db.session.delete(user)+ db.session.commit()+ finally:+ app.config['WTF_CSRF_ENABLED'] = saved+++def test_forgot_password_does_not_email_when_smtp_not_configured():+ """Safety net: when SMTP is not configured, forgot_password must+ NOT attempt to send (otherwise we leak SMTP misconfiguration errors+ on every reset request). The route should redirect with a flash."""+ from unittest.mock import patch+ saved_csrf = app.config.get('WTF_CSRF_ENABLED', True)+ app.config['WTF_CSRF_ENABLED'] = False+ try:+ with app.app_context():+ user = _make_user('sso_no_smtp', password=None, sso_provider='Drift', sso_subject=f'subj-{uuid.uuid4().hex[:6]}')+ client = app.test_client()+ with patch('src.api.auth.is_smtp_configured', return_value=False), \+ patch('src.api.auth.send_password_reset_email') as send_mail:+ resp = client.post('/forgot-password', data={'email': user.email}, follow_redirects=False)+ # Should redirect to login with a flash, not crash+ assert resp.status_code == 302+ send_mail.assert_not_called()+ db.session.delete(user)+ db.session.commit()+ finally:+ app.config['WTF_CSRF_ENABLED'] = saved_csrf+++def teardown_module(module):+ with app.app_context():+ for u in User.query.filter(User.username.like('csrf_%')).all():+ for t in APIToken.query.filter_by(user_id=u.id).all():+ db.session.delete(t)+ db.session.delete(u)+ for u in User.query.filter(User.username.like('sso_only_%')).all():+ db.session.delete(u)+ db.session.commit()