From 2fbe1d383650cb31f3ef8d58c04e4d4095935833 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Fri, 13 Nov 2026 19:26:30 +0100 Subject: refactor(core): simplify session fingerprint validation Validate only the numeric user ID before lookup, then compare the complete stored session identifier against the current user ID with a constant-time byte check. --- webmentions_ssg/auth.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) (limited to 'webmentions_ssg/auth.py') diff --git a/webmentions_ssg/auth.py b/webmentions_ssg/auth.py index edfc858..a5f3089 100644 --- a/webmentions_ssg/auth.py +++ b/webmentions_ssg/auth.py @@ -58,22 +58,12 @@ def load_user(session_id: object) -> User | None: if not isinstance(session_id, str): return None - user_id, separator, fingerprint = session_id.partition(":") - if ( - not separator - or 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) - ): + user_id, _, _ = session_id.partition(":") + if not (user_id.isascii() and user_id.isdecimal() and len(user_id) <= 19): return None user = db.session.get(User, int(user_id)) - if user is None: - return None - - if not compare_digest(session_id, user.get_id()): + if user is None or not compare_digest(session_id.encode(), user.get_id().encode()): return None return user -- cgit v1.3.1