Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

6.8 KB
Raw Blame
"""Per-user account lockout after repeated failed login attempts.

Complements the per-IP rate limiting in ``security/rate_limiter.py`` by
tracking failures at the *username* level. A user who exceeds the
configured threshold is temporarily locked out regardless of the source IP.

Note: lockout state is in-memory and per-process. In multi-worker
deployments (e.g. gunicorn), each worker maintains separate state.
"""

import threading
from collections import OrderedDict
from datetime import datetime, timedelta, timezone

from loguru import logger

from .security_settings import get_security_default


class AccountLockoutManager:
    """Track failed login attempts and lock accounts after a threshold.

    A failure counter lasts one lockout window from its last failure, the
    same as a lockout. Within its window an entry is kept, with one
    exception that bounds memory: counters holding a single failure, the
    only kind a spray of throwaway usernames creates for one request each,
    are capped at ``_MAX_SINGLE_FAILURES``, and a new one past that evicts
    the oldest. A counter that has reached two failures, and an active
    lockout, stay until their window ends or the user logs in, so flooding
    the table cannot reset them.
    """

    _MAX_SINGLE_FAILURES = 10_000

    def __init__(
        self,
        threshold: int | None = None,
        lockout_minutes: int | None = None,
    ) -> None:
        if threshold is None:
            threshold = get_security_default(
                "security.account_lockout_threshold", 10
            )
        if lockout_minutes is None:
            lockout_minutes = get_security_default(
                "security.account_lockout_duration_minutes", 15
            )

        self.threshold: int = threshold
        self.lockout_minutes: int = lockout_minutes

        # {username: {"count": int, "locked_until": datetime | None,
        #             "last_failure": datetime}}, ordered by last failure
        self._state: OrderedDict[str, dict] = OrderedDict()
        # The usernames in _state whose counter holds a single failure and
        # no lockout, in the order of that failure.
        self._singles: OrderedDict[str, None] = OrderedDict()
        self._lock = threading.Lock()

    # ------------------------------------------------------------------
    # Public API
    # ------------------------------------------------------------------

    def is_locked(self, username: str) -> bool:
        """Return ``True`` if *username* is currently locked out."""
        with self._lock:
            entry = self._state.get(username)
            if entry is None:
                return False
            locked_until = entry.get("locked_until")
            if locked_until is None:
                return False
            if datetime.now(timezone.utc) >= locked_until:
                # Lockout expired — reset automatically
                self._state.pop(username, None)
                logger.info("Account lockout expired")
                return False
            return True

    def _drop_expired(self, now: datetime) -> None:
        """Drop the entries whose last failure is a full window old.

        The table is ordered by last failure, so they are all at its front.
        A lockout ends one window after the failure that set it, so this
        covers expired lockouts as well as counters whose window has passed.

        Must be called while ``self._lock`` is held.
        """
        cutoff = now - timedelta(minutes=self.lockout_minutes)
        while self._state:
            username, entry = next(iter(self._state.items()))
            if entry["last_failure"] > cutoff:
                break
            del self._state[username]
            self._singles.pop(username, None)

    def record_failure(self, username: str) -> None:
        """Record a failed login attempt for *username*."""
        with self._lock:
            # Read under the lock, so the table's order by last failure is
            # the order in which failures take the lock — assuming the
            # wall clock doesn't step backwards (#6943). A backward step
            # of d (NTP correction, manual clock change) can leave an older
            # stamp after a newer one. _drop_expired stops at the first
            # unexpired entry, so an expired counter behind it can linger
            # up to d past its window; repeated steps add up, and nothing
            # caps the linger at the lockout window. While it lingers, its
            # count carries into that user's next failure. The direction
            # is conservative: a backward step drops nothing early, and
            # is_locked compares locked_until with the clock directly, so
            # no lockout ends early (one set before the step lasts up to d
            # longer). If that ever needs hardening, clamp stamps
            # (max(latest_seen, now)) or order on a monotonic clock instead.
            now = datetime.now(timezone.utc)
            self._drop_expired(now)
            entry = self._state.get(username)
            if entry is None:
                entry = {"count": 0, "locked_until": None}
                self._state[username] = entry
            else:
                self._state.move_to_end(username)
            entry["last_failure"] = now
            entry["count"] += 1
            if entry["count"] >= self.threshold:
                entry["locked_until"] = now + timedelta(
                    minutes=self.lockout_minutes
                )
                logger.warning(
                    "Account locked after {} failed attempts",
                    self.threshold,
                )

            if entry["count"] == 1 and entry["locked_until"] is None:
                self._singles[username] = None
                if len(self._singles) > self._MAX_SINGLE_FAILURES:
                    oldest, _ = self._singles.popitem(last=False)
                    del self._state[oldest]
            else:
                self._singles.pop(username, None)

    def record_success(self, username: str) -> None:
        """Clear the failure counter for *username* after a successful login."""
        with self._lock:
            removed = self._state.pop(username, None)
            self._singles.pop(username, None)
            if removed is not None:
                logger.info("Account lockout cleared after successful login")


# ------------------------------------------------------------------
# Module-level singleton
# ------------------------------------------------------------------

_manager: AccountLockoutManager | None = None
_singleton_lock = threading.Lock()


def get_account_lockout_manager() -> AccountLockoutManager:
    """Return the module-level singleton ``AccountLockoutManager``."""
    global _manager
    if _manager is None:
        with _singleton_lock:
            if _manager is None:
                _manager = AccountLockoutManager()
    return _manager