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. --- webmentions_ssg/auth.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'webmentions_ssg/auth.py') diff --git a/webmentions_ssg/auth.py b/webmentions_ssg/auth.py index 48a95b7..147220e 100644 --- a/webmentions_ssg/auth.py +++ b/webmentions_ssg/auth.py @@ -4,6 +4,8 @@ """Configure user authentication and session loading.""" +from hmac import compare_digest + from flask_login import LoginManager from . import DATABASE as db @@ -15,11 +17,30 @@ AUTH.login_view = "root.login" # pyright: ignore[reportAttributeAccessIssue] @AUTH.user_loader -def load_user(id: int) -> User | None: +def load_user(session_id: str) -> User | None: """ Load a user from the database by identifier. - :param id: User identifier stored in the login session. + :param session_id: Database ID and password-hash fingerprint stored in the session. :return: Matching user, or ``None`` if no user exists with the identifier. """ - return db.session.get(User, int(id)) + try: + user_id, fingerprint = session_id.split(":", 1) + if ( + not user_id.isascii() + or not user_id.isdecimal() + or len(user_id) > 19 + or len(fingerprint) != 64 + or any(char not in "0123456789abcdef" for char in fingerprint) + ): + return None + user = db.session.get(User, int(user_id)) + except AttributeError, TypeError, ValueError: + return None + + if user is None: + return None + + if not compare_digest(session_id, user.get_id()): + return None + return user -- cgit v1.3.1