diff options
| author | Dennis Fink | 2026-11-13 19:13:48 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-13 19:13:48 +0100 |
| commit | c421d269313d6623926954935a680fe4acb1dc86 (patch) | |
| tree | ee68db5d127808c544c218027cd117e7062e97d1 /webmentions_ssg/auth.py | |
| parent | fd61299d8a4d54836f0aa082aa47d02a786c7d93 (diff) | |
| download | webmentions-ssg-c421d269313d6623926954935a680fe4acb1dc86.tar.gz webmentions-ssg-c421d269313d6623926954935a680fe4acb1dc86.zip | |
fix(core): harden session and redirect validation
Reject non-string session identifiers explicitly and simplify
fingerprint parsing so malformed authentication state fails closed.
Tighten local redirect checks against control-character and backslash
host tricks without relying on an unnecessary scheme check.
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/auth.py | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/webmentions_ssg/auth.py b/webmentions_ssg/auth.py index 1707917..6d4fe95 100644 --- a/webmentions_ssg/auth.py +++ b/webmentions_ssg/auth.py @@ -44,26 +44,27 @@ def validate_password(password: str) -> None: @AUTH.user_loader -def load_user(session_id: str) -> User | None: +def load_user(session_id: object) -> User | None: """Load a user from the database by identifier. :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. """ - 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: + 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) + ): return None + user = db.session.get(User, int(user_id)) if user is None: return None @@ -90,8 +91,7 @@ def is_safe_next_url(next_url: str) -> bool: except ValueError: return False return ( - not target.scheme - and not target.netloc + not target.netloc and target.path.startswith("/") and not target.path.startswith("//") ) |
