Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Check the session version on the user the caller route already loads

Petr Bauch560af2c7ad9a
diff --git a/core/switch_core/gateway/auth.py b/core/switch_core/gateway/auth.py
index efe65022d..95e9cc4c9 100644
--- a/core/switch_core/gateway/auth.py
+++ b/core/switch_core/gateway/auth.py
@@ -430,11 +430,12 @@ async def get_authenticated_caller(
claims, which are a snapshot from login: an address can change, and a
revoked operator bit must not stay valid for the life of a cookie.
"""
- payload = await _authenticate(request, session_factory, user_store, config)
+ payload = _session_claims(request, config)
async with session_factory() as system_session:
user = await user_store.get(system_session, payload["sub"])
if user is None:
raise HTTPException(status_code=401, detail="User not found")
+ _refuse_ended_session(user.session_version, payload)
return AuthenticatedCaller(
id=user.id, email=user.email, is_operator=user.role == "admin"
)
diff --git a/core/tests/switch_core/gateway/test_session_version.py b/core/tests/switch_core/gateway/test_session_version.py
index 581a2098a..afa503990 100644
--- a/core/tests/switch_core/gateway/test_session_version.py
+++ b/core/tests/switch_core/gateway/test_session_version.py
@@ -10,17 +10,24 @@ from __future__ import annotations
from datetime import UTC, datetime
from types import SimpleNamespace
+from typing import Annotated
import httpx
import jwt
-from fastapi import FastAPI
+from fastapi import Depends, FastAPI
from sqlalchemy import select, update
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from switch_core.db.models import ServerAuditEvent, User
from switch_core.db.stores.user_store import UserStore
from switch_core.gateway import dependencies as gw_deps
-from switch_core.gateway.auth import JWT_ALGORITHM, create_jwt, hash_password
+from switch_core.gateway.auth import (
+ JWT_ALGORITHM,
+ AuthenticatedCaller,
+ create_jwt,
+ get_authenticated_caller,
+ hash_password,
+)
from switch_core.gateway.auth_routes import router
from switch_core.keys import Keyring, Purpose
@@ -132,6 +139,31 @@ class TestTheVersionIsChecked:
assert refused.status_code == 401
assert await _version(session_factory, user_id) == 1
+ async def test_a_route_reading_the_caller_without_a_workspace_checks_it(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ user_id = await _seed_user(session_factory, "[email protected]")
+ app = _app(session_factory)
+
+ @app.get("/caller")
+ async def caller(
+ who: Annotated[AuthenticatedCaller, Depends(get_authenticated_caller)],
+ ) -> str:
+ return who.id
+
+ stale = _token(user_id, "[email protected]", "user", 0)
+ async with _client(app, stale) as client:
+ before = await client.get("/caller")
+ async with session_factory() as session:
+ await _USER_STORE.end_sessions(session, user_id)
+ await session.commit()
+ async with _client(app, stale) as client:
+ after = await client.get("/caller")
+
+ assert before.json() == user_id
+ assert after.status_code == 401
+ assert after.json() == {"detail": "Session ended"}
+
async def test_a_cookie_minted_before_the_claim_existed_counts_as_version_0(
self, session_factory: async_sessionmaker[AsyncSession]
) -> None: