Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
SSO sign-in remembered only when asked; remember-me cookie set only with Remember me (#437)
Murtaza Nasiraac3af9de58f
diff --git a/config/env.asr.example b/config/env.asr.exampleindex da01bec5..25cddca4 100644--- a/config/env.asr.example+++ b/config/env.asr.example@@ -152,8 +152,7 @@ CHAT_MAX_TOKENS=5000# HTTPS deployment. Default: false so plain-HTTP LAN setups keep working.# SESSION_COOKIE_SECURE=false-# Days a "Remember me" or SSO sign-in lasts without a visit; each visit starts-# the period again. Default: 30.+# Days a sign-in with "Remember me" lasts (password or SSO). Default: 30.# REMEMBER_COOKIE_DAYS=30# --- Security headers ---diff --git a/config/env.transcription.example b/config/env.transcription.exampleindex 80c37c2a..a839484c 100644--- a/config/env.transcription.example+++ b/config/env.transcription.example@@ -366,8 +366,7 @@ REGISTRATION_ALLOWED_DOMAINS=# HTTPS deployment. Default: false so plain-HTTP LAN setups keep working.# SESSION_COOKIE_SECURE=false-# Days a "Remember me" or SSO sign-in lasts without a visit; each visit starts-# the period again. Default: 30.+# Days a sign-in with "Remember me" lasts (password or SSO). Default: 30.# REMEMBER_COOKIE_DAYS=30# --- Security headers ---diff --git a/config/env.whisper.example b/config/env.whisper.exampleindex dde2cbdf..8179569d 100644--- a/config/env.whisper.example+++ b/config/env.whisper.example@@ -135,8 +135,7 @@ CHAT_MAX_TOKENS=5000# HTTPS deployment. Default: false so plain-HTTP LAN setups keep working.# SESSION_COOKIE_SECURE=false-# Days a "Remember me" or SSO sign-in lasts without a visit; each visit starts-# the period again. Default: 30.+# Days a sign-in with "Remember me" lasts (password or SSO). Default: 30.# REMEMBER_COOKIE_DAYS=30# --- Security headers ---diff --git a/docs/admin-guide/system-settings.md b/docs/admin-guide/system-settings.mdindex 6b7b29ca..2e910521 100644--- a/docs/admin-guide/system-settings.md+++ b/docs/admin-guide/system-settings.md@@ -188,7 +188,7 @@ If the process that owns the background work is recycled or killed, the replacem**SESSION_COOKIE_SECURE**: When `true`, the session cookie is sent only over HTTPS. Set this to `true` for any production deployment behind TLS (Nginx Proxy Manager, Caddy, Traefik with a real certificate). Default: `false`, so http://localhost and bare-LAN deployments keep working out of the box. If you turn this on and reach the instance over plain HTTP, the browser will silently drop the cookie and you'll be unable to log in.-**REMEMBER_COOKIE_DAYS**: How long a sign-in with "Remember me" (and every SSO sign-in) lasts without a visit, in days. Each visit starts the period again, so a person who uses Speakr regularly stays signed in. The cookie follows `SESSION_COOKIE_SECURE` and is `SameSite=Lax`. Default: `30`.+**REMEMBER_COOKIE_DAYS**: How long a sign-in with "Remember me" lasts, in days from the sign-in. The option is on the sign-in page for password and SSO sign-ins; without it, the sign-in ends when the browser is closed. The cookie follows `SESSION_COOKIE_SECURE` and is `SameSite=Lax`. Default: `30`.When a password is changed, reset by email, or set by an administrator, the account is signed out of every browser. The browser in which the password was changed stays signed in. Sign-ins made before v0.10.15 remain valid until the account's password is next set.diff --git a/src/api/auth.py b/src/api/auth.pyindex d3481ab6..f63d669e 100644--- a/src/api/auth.py+++ b/src/api/auth.py@@ -304,6 +304,8 @@ def sso_login():session['sso_next'] = next_urlelse:session.pop('sso_next', None)+ # "Remember me" on the sign-in page, as for a password sign-in (#437)+ session['sso_remember'] = request.args.get('remember') == '1'return oauth.sso.authorize_redirect(redirect_uri=get_sso_config().get('redirect_uri'))@@ -335,6 +337,7 @@ def sso_callback():link_user_id = session.pop('sso_link_user_id', None)next_url = session.pop('sso_next', None)+ remember = bool(session.pop('sso_remember', False))cfg = get_sso_config()if link_user_id:@@ -368,7 +371,7 @@ def sso_callback():flash('Could not complete SSO login. Please try again.', 'danger')return redirect(url_for('auth.login'))- login_user(user, remember=True)+ login_user(user, remember=remember)if next_url and is_safe_url(next_url):return redirect(next_url)diff --git a/src/app.py b/src/app.pyindex a200338b..1a5e72d7 100644--- a/src/app.py+++ b/src/app.py@@ -435,8 +435,9 @@ app.config['SESSION_COOKIE_SAMESITE'] = 'Lax' # CSRF protection# Remember-me cookie (#437). Flask-Login's defaults are a 365-day cookie with# no Secure flag and no SameSite attribute. It now follows the session# cookie's Secure setting, is SameSite=Lax, and lasts REMEMBER_COOKIE_DAYS-# (default 30) from the last visit, so a person who keeps using Speakr stays-# signed in.+# (default 30) from the sign-in. It is set only when "Remember me" is ticked:+# REMEMBER_COOKIE_REFRESH_EACH_REQUEST stays off, because with it Flask-Login+# sets the cookie on every response for every signed-in user.app.config['REMEMBER_COOKIE_SECURE'] = app.config['SESSION_COOKIE_SECURE']app.config['REMEMBER_COOKIE_HTTPONLY'] = Trueapp.config['REMEMBER_COOKIE_SAMESITE'] = 'Lax'@@ -445,7 +446,6 @@ try:except ValueError:_remember_days = 30app.config['REMEMBER_COOKIE_DURATION'] = timedelta(days=_remember_days)-app.config['REMEMBER_COOKIE_REFRESH_EACH_REQUEST'] = True# --- Security response headers ---# Set at the app level so a deployment WITHOUT a hardening reverse proxy isdiff --git a/templates/login.html b/templates/login.htmlindex 857d981d..b9253bca 100644--- a/templates/login.html+++ b/templates/login.html@@ -80,9 +80,15 @@{% if sso_enabled %}<div class="flex flex-col space-y-3 {% if not password_login_disabled %}mb-6{% endif %}">- <a href="{{ url_for('auth.sso_login') }}" class="w-full inline-flex items-center justify-center px-4 py-2 bg-[var(--bg-button)] text-[var(--text-button)] rounded-md hover:bg-[var(--bg-button-hover)] focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-[var(--border-focus)] transition-colors duration-200">- <i class="fas fa-cloud mr-2"></i> Sign in with {{ sso_provider_name }}- </a>+ <form method="GET" action="{{ url_for('auth.sso_login') }}" class="flex flex-col space-y-3">+ <button type="submit" class="w-full inline-flex items-center justify-center px-4 py-2 bg-[var(--bg-button)] text-[var(--text-button)] rounded-md hover:bg-[var(--bg-button-hover)] focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-[var(--border-focus)] transition-colors duration-200">+ <i class="fas fa-cloud mr-2"></i> Sign in with {{ sso_provider_name }}+ </button>+ <label class="inline-flex items-center text-sm text-[var(--text-secondary)]">+ <input type="checkbox" name="remember" value="1" class="h-4 w-4 text-[var(--text-accent)] focus:ring-[var(--ring-focus)] border-[var(--border-secondary)] rounded">+ <span class="ml-2">Remember Me</span>+ </label>+ </form>{% if not password_login_disabled %}<div class="flex items-center text-xs text-[var(--text-muted)]"><span class="flex-grow border-t border-[var(--border-secondary)]"></span>diff --git a/tests/test_sign_in_revocation.py b/tests/test_sign_in_revocation.pyindex cd7602fb..e6969393 100644--- a/tests/test_sign_in_revocation.py+++ b/tests/test_sign_in_revocation.py@@ -55,7 +55,14 @@ def test_cookie_settings():assert app.config["REMEMBER_COOKIE_HTTPONLY"] is Trueassert app.config["REMEMBER_COOKIE_SECURE"] == app.config["SESSION_COOKIE_SECURE"]assert app.config["REMEMBER_COOKIE_DURATION"] == timedelta(days=30)- assert app.config["REMEMBER_COOKIE_REFRESH_EACH_REQUEST"] is True+ assert not app.config.get("REMEMBER_COOKIE_REFRESH_EACH_REQUEST")+++def test_no_remember_cookie_without_remember_me(user_id):+ """A signed-in browser that did not ask to be remembered gets no remember-me cookie."""+ resp = _client(str(user_id)).get("/account")+ name = app.config.get("REMEMBER_COOKIE_NAME", "remember_token")+ assert not [c for c in resp.headers.getlist("Set-Cookie") if c.startswith(name + "=")]def test_existing_plain_id_sign_in_stays_valid(user_id):diff --git a/tests/test_sso_remember.py b/tests/test_sso_remember.pynew file mode 100644index 00000000..4d16c591--- /dev/null+++ b/tests/test_sso_remember.py@@ -0,0 +1,64 @@+"""An SSO sign-in sets the remember-me cookie only when "Remember me" was+ticked on the sign-in page, as a password sign-in does (#437)."""+import uuid+from unittest.mock import MagicMock, patch++from flask import redirect++from src.app import app, db+from src.models import User+++def _user():+ s = uuid.uuid4().hex[:8]+ u = User(username=f"ssor_{s}", email=f"ssor_{s}@local.test", password="x",+ sso_provider="Test", sso_subject=f"sub-{s}")+ db.session.add(u)+ db.session.commit()+ return u+++def _sign_in(query):+ """Start the SSO sign-in with the given query string and complete it."""+ client = app.test_client()+ oauth = MagicMock()+ oauth.sso.authorize_redirect.side_effect = lambda **kw: redirect("https://idp.example/authorize")+ oauth.sso.authorize_access_token.return_value = {"access_token": "t"}+ with app.app_context():+ user = _user()+ uid = user.id+ try:+ with patch("src.api.auth.is_sso_enabled", return_value=True), \+ patch("src.api.auth.get_sso_client", return_value=oauth), \+ patch("src.api.auth.get_sso_config", return_value={"redirect_uri": "http://localhost/cb"}), \+ patch("src.api.auth.resolve_userinfo", return_value={"sub": "x"}), \+ patch("src.api.auth.create_or_update_sso_user",+ side_effect=lambda info: db.session.get(User, uid)):+ start = client.get("/auth/sso/login" + query)+ assert start.status_code == 302+ done = client.get("/auth/sso/callback")+ assert done.status_code == 302+ return done.headers.getlist("Set-Cookie")+ finally:+ with app.app_context():+ db.session.delete(db.session.get(User, uid))+ db.session.commit()+++def _remember_cookie(cookies):+ name = app.config.get("REMEMBER_COOKIE_NAME", "remember_token")+ return [c for c in cookies if c.startswith(name + "=") and "Expires=Thu, 01 Jan 1970" not in c]+++def test_sso_sign_in_does_not_remember_by_default():+ assert _remember_cookie(_sign_in("")) == []+++def test_sso_sign_in_remembers_when_ticked():+ assert len(_remember_cookie(_sign_in("?remember=1"))) == 1+++def test_sign_in_page_offers_remember_me_with_sso():+ with patch("src.api.auth.is_sso_enabled", return_value=True):+ html = app.test_client().get("/login").get_data(as_text=True)+ assert 'name="remember" value="1"' in html