Skip to content

The agent that grows with you

fix(dashboard-auth): give every password-provider session its own tokens

unsupportedpastels9ed6c206515e
diff --git a/plugins/dashboard_auth/basic/__init__.py b/plugins/dashboard_auth/basic/__init__.py
index 6abf5e7f18f..4d65cbf62b3 100644
--- a/plugins/dashboard_auth/basic/__init__.py
+++ b/plugins/dashboard_auth/basic/__init__.py
@@ -169,12 +169,15 @@ class BasicAuthProvider(NonInteractiveMixin, DashboardAuthProvider):
# ---- internals ---------------------------------------------------------
def _mint_session(self, user_id: str) -> Session:
+ # ``jti`` gives every session its own tokens, even two minted in one second: a listener that
+ # drops a session's tokens (the Webapp's sign-out) must not get them back from a later login.
now = int(time.time())
exp = now + self._ttl
+ jti = secrets.token_urlsafe(16)
return self._session(
user_id, exp,
- _sign({"sub": user_id, "kind": "access", "exp": exp}, self._secret),
- _sign({"sub": user_id, "kind": "refresh", "exp": now + _REFRESH_TTL_SECONDS}, self._secret))
+ _sign({"sub": user_id, "kind": "access", "exp": exp, "jti": jti}, self._secret),
+ _sign({"sub": user_id, "kind": "refresh", "exp": now + _REFRESH_TTL_SECONDS, "jti": jti}, self._secret))
def _session(self, user_id: str, exp: int, access_token: str, refresh_token: str) -> Session:
return Session(
diff --git a/tests/hermes_cli/test_webapp_gated_sign_out.py b/tests/hermes_cli/test_webapp_gated_sign_out.py
index a1a71736d7c..391c5e01372 100644
--- a/tests/hermes_cli/test_webapp_gated_sign_out.py
+++ b/tests/hermes_cli/test_webapp_gated_sign_out.py
@@ -279,8 +279,7 @@ def test_the_webapp_leaves_cookies_named_for_no_gateway_to_the_dashboard(gated,
@pytest.fixture
def clock(monkeypatch):
- """Moves the password provider's clock forward. It signs each token's expiry in whole seconds, so
- without this a renewal or a second login in the same second gets byte-identical tokens."""
+ """Moves the password provider's clock forward, as time passes between a login and its renewal."""
import time
from plugins.dashboard_auth import basic
@@ -451,6 +450,52 @@ def test_only_the_spelling_given_to_a_native_app_works(gated):
_refused_alone(gated, f'"{access}"', f'"{refresh}"')
+def _native_app_password_sign_in(gated):
+ """A whole RFC 8252 sign-in with the password provider on the Webapp's listener: the system
+ browser lands on the sign-in form and the user signs in there. Returns the app's token pair."""
+ import base64
+ import hashlib
+ from urllib.parse import parse_qs, urlsplit
+
+ verifier = "native-app-verifier-" + "v" * 40
+ challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).rstrip(b"=").decode()
+ system_browser = gated.browser()
+ started = system_browser.get("/auth/native/authorize", follow_redirects=False, params={
+ "provider": "basic", "code_challenge": challenge, "code_challenge_method": "S256",
+ "redirect_uri": "http://127.0.0.1:53999/cb", "state": "desk"})
+ assert started.status_code == 302, started.text
+ signed_in = system_browser.post("/auth/password-login",
+ json={"provider": "basic", "username": USER, "password": PASSWORD})
+ assert signed_in.status_code == 200, signed_in.text
+ code = parse_qs(urlsplit(signed_in.json()["next"]).query)["code"][0]
+ tokens = gated.browser().post("/auth/native/token", json={"code": code, "code_verifier": verifier})
+ assert tokens.status_code == 200, tokens.text
+ return tokens.json()["access_token"], tokens.json()["refresh_token"]
+
+
+def test_a_login_in_the_second_of_a_native_sign_in_keeps_its_tokens_to_its_cookies(gated, sign_out, monkeypatch):
+ """A browser login and a native app's sign-in with the password provider in one second of its
+ clock. The login's tokens still work only as its cookies, before and after sign-out and across a
+ restart, and the native app stays signed in."""
+ import time
+
+ from plugins.dashboard_auth import basic
+
+ frozen = time.time()
+ monkeypatch.setattr(basic, "time", SimpleNamespace(time=lambda: frozen))
+ browser = gated.browser()
+ _sign_in_with_password(browser)
+ access, refresh = _tokens(browser)
+ native_access, native_refresh = _native_app_password_sign_in(gated)
+ _refused_alone(gated, access, refresh)
+ sign_out()
+ _refused_alone(gated, access, refresh)
+ _restart(gated, monkeypatch)
+ _refused_alone(gated, access, refresh)
+ assert gated.browser().get("/api/auth/me", headers=_bearer(native_access)).status_code == 200
+ assert _renew(gated, native_refresh).status_code == 200
+
+
def test_off_the_webapp_any_provider_token_works_on_its_own_as_on_main(gated, monkeypatch):
monkeypatch.setattr(gated.server.app.state, "ui_surface", "dashboard")
browser = gated.browser()
diff --git a/tests/plugins/dashboard_auth/test_basic_provider.py b/tests/plugins/dashboard_auth/test_basic_provider.py
index 6162d141236..d38f6889967 100644
--- a/tests/plugins/dashboard_auth/test_basic_provider.py
+++ b/tests/plugins/dashboard_auth/test_basic_provider.py
@@ -9,6 +9,7 @@ import path as a package) and exercises the provider behaviour + the
from __future__ import annotations
import secrets
+from types import SimpleNamespace
from unittest.mock import MagicMock
import pytest
@@ -117,6 +118,18 @@ class TestProvider:
assert p.verify_session(access_token=r.access_token) is not None
+ def test_sessions_minted_in_one_second_get_their_own_tokens(self, basic, monkeypatch):
+ # A listener that drops a session's tokens (the Webapp's sign-out) must not see them come
+ # back from a later login or refresh, even one in the same second.
+ monkeypatch.setattr(basic, "time", SimpleNamespace(time=lambda: 1_700_000_000))
+ p = self._make(basic)
+ first = p.complete_password_login(username="admin", password="hunter2")
+ second = p.complete_password_login(username="admin", password="hunter2")
+ renewed = p.refresh_session(refresh_token=first.refresh_token)
+ sessions = (first, second, renewed)
+ assert len({s.access_token for s in sessions}) == len({s.refresh_token for s in sessions}) == 3
+ assert all(p.verify_session(access_token=s.access_token) is not None for s in sessions)
+
def test_cross_secret_token_does_not_verify(self, basic):
p1 = self._make(basic)
p2 = self._make(basic) # different random secret