Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

Rotate handoff tickets with a controlled invalid-token error

Bingxi Zhao (Frank)3d58ba5253b3
diff --git a/deeptutor/multi_user/session_handoff.py b/deeptutor/multi_user/session_handoff.py
index 06e36c4a7..8d2698c39 100644
--- a/deeptutor/multi_user/session_handoff.py
+++ b/deeptutor/multi_user/session_handoff.py
@@ -21,7 +21,7 @@ from typing import Any
from urllib.parse import urlsplit, urlunsplit
from jose import jwe
-from jose.exceptions import JWSError
+from jose.exceptions import JWEError, JWSError
from .identity import AUTH_DIR, load_or_create_auth_secret
@@ -175,7 +175,7 @@ def decrypt_ticket_payload(token: str, *, secret: str | None = None) -> dict[str
try:
plaintext = jwe.decrypt(token, _jwe_key(secret))
payload = json.loads(plaintext)
- except (JWSError, ValueError, TypeError, json.JSONDecodeError):
+ except (JWEError, JWSError, ValueError, TypeError, json.JSONDecodeError):
raise HandoffRejected("Invalid handoff ticket") from None
if not isinstance(payload, dict):
raise HandoffRejected("Invalid handoff ticket")
diff --git a/tests/multi_user/test_session_handoff.py b/tests/multi_user/test_session_handoff.py
index aed67a9bf..3eb265504 100644
--- a/tests/multi_user/test_session_handoff.py
+++ b/tests/multi_user/test_session_handoff.py
@@ -25,7 +25,8 @@ def test_public_origin_is_https_and_host_bound() -> None:
session_handoff.public_origin(raw)
-def test_handoff_code_and_ticket_are_single_use(tmp_path) -> None:
+def test_handoff_code_and_ticket_are_single_use(tmp_path, monkeypatch) -> None:
+ monkeypatch.setattr(session_handoff, "load_or_create_auth_secret", lambda: "test-secret")
store = session_handoff.SessionHandoffStore(tmp_path / "handoff.sqlite3")
ticket = session_handoff.encrypt_ticket_payload(
{"mode": "builtin", "host": "app.example", "exp": 220},
@@ -40,15 +41,17 @@ def test_handoff_code_and_ticket_are_single_use(tmp_path) -> None:
with pytest.raises(session_handoff.HandoffRejected):
store.exchange(code=record.code, public_host="other.example", now=101)
- assert store.exchange(code=record.code, public_host="app.example", now=102) == ticket
+ exchanged = store.exchange(code=record.code, public_host="app.example", now=102)
+ assert exchanged != ticket
+ assert session_handoff.decrypt_ticket_payload(exchanged)["exp"] == 222
with pytest.raises(session_handoff.HandoffRejected):
store.exchange(code=record.code, public_host="app.example", now=103)
with pytest.raises(session_handoff.HandoffRejected):
- store.consume_ticket(ticket=ticket, public_host="other.example", now=104)
- assert store.consume_ticket(ticket=ticket, public_host="app.example", now=105) == ticket
+ store.consume_ticket(ticket=exchanged, public_host="other.example", now=104)
+ assert store.consume_ticket(ticket=exchanged, public_host="app.example", now=105) == exchanged
with pytest.raises(session_handoff.HandoffRejected):
- store.consume_ticket(ticket=ticket, public_host="app.example", now=106)
+ store.consume_ticket(ticket=exchanged, public_host="app.example", now=106)
def test_expired_code_is_rejected_and_rate_limited(tmp_path, monkeypatch) -> None:
@@ -109,3 +112,8 @@ def test_database_never_stores_plaintext_code_ticket_or_bearer(tmp_path) -> None
assert record.code not in persisted
assert bearer not in persisted
+
+
+def test_invalid_encrypted_ticket_is_a_controlled_rejection() -> None:
+ with pytest.raises(session_handoff.HandoffRejected):
+ session_handoff.decrypt_ticket_payload("not-a-valid-jwe", secret="test-secret")