From 3494eab1208dc18478485e02c61e292556502fa6 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sat, 31 Oct 2026 16:42:44 +0100 Subject: fix(core): harden session and credential security Require persistent non-placeholder signing keys outside local development and bind login sessions to the current password hash so password changes revoke existing cookies. Enforce a minimal password policy for user administration while preserving long Unicode passphrases without arbitrary composition limits. --- tests/test_auth.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) (limited to 'tests/test_auth.py') diff --git a/tests/test_auth.py b/tests/test_auth.py index e0416b0..a6117b2 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -20,9 +20,72 @@ def test_load_user_returns_matching_user(app: Flask) -> None: user = User(username="account", password="secret") db.session.add(user) db.session.commit() - assert load_user(user.id) is user + assert load_user(user.get_id()) is user def test_load_user_returns_none_for_unknown_id(app: Flask) -> None: with app.app_context(): - assert load_user(99_999) is None + assert load_user("99999:" + "a" * 64) is None + + +def test_load_user_rejects_legacy_and_malformed_sessions(app: Flask) -> None: + with app.app_context(): + user = User(username="account", password="secret") + db.session.add(user) + db.session.commit() + + for identifier in ( + str(user.id), # Old ID-only sessions must not bypass revocation. + "", + "not-an-id:" + "a" * 64, + f"{user.id}:invalid", + f"{user.id}:" + "é" * 64, + f"{user.id}:" + "z" * 64, + f"{user.id}:" + "0" * 64, + f"{user.id}:" + "0" * 64 + ":trailing", + "-1:" + "0" * 64, + ): + assert load_user(identifier) is None + + +def test_password_change_invalidates_existing_session(app: Flask) -> None: + old_password = "a long original passphrase" + new_password = "a different long passphrase" + + with app.app_context(): + user = User(username="account", password=old_password) + db.session.add(user) + db.session.commit() + old_identifier = user.get_id() + + client = app.test_client() + response = client.post( + "/login", data={"username": "account", "password": old_password} + ) + assert response.status_code == 302 + assert client.get("/received").status_code == 200 + + with client.session_transaction() as session: + assert session["_user_id"] == old_identifier + + result = app.test_cli_runner().invoke( + args=["users", "set-password", "account", "--password", new_password] + ) + assert result.exit_code == 0, result.output + + with app.app_context(): + user = db.session.get(User, int(old_identifier.split(":", 1)[0])) + assert user is not None + assert user.get_id() != old_identifier + assert load_user(old_identifier) is None + assert load_user(user.get_id()) is user + + # A pre-existing, validly signed cookie no longer authenticates. + assert client.get("/received").status_code == 302 + + # Logging in with the newly chosen password still works. + response = client.post( + "/login", data={"username": "account", "password": new_password} + ) + assert response.status_code == 302 + assert client.get("/received").status_code == 200 -- cgit v1.3.1