diff options
| author | Dennis Fink | 2026-10-31 16:42:44 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-31 16:42:44 +0100 |
| commit | 3494eab1208dc18478485e02c61e292556502fa6 (patch) | |
| tree | 5d9e95d709b119a0245d0294837f9b6a7e38bff1 /webmentions_ssg/auth.py | |
| parent | fdb1762715658eae987dd104a0e6d3cb92a4a8d7 (diff) | |
| download | webmentions-ssg-3494eab1208dc18478485e02c61e292556502fa6.tar.gz webmentions-ssg-3494eab1208dc18478485e02c61e292556502fa6.zip | |
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.
Diffstat (limited to 'webmentions_ssg/auth.py')
| -rw-r--r-- | webmentions_ssg/auth.py | 27 |
1 files changed, 24 insertions, 3 deletions
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 |
