Skip to content

The agent that grows with you

fix(state): let a dashboard login act on its Desktop-stamped label rows

liuhao102400cebe8057f1
diff --git a/hermes_state_mutation_binding.py b/hermes_state_mutation_binding.py
index a2329bfc575..c7c7048b2a4 100644
--- a/hermes_state_mutation_binding.py
+++ b/hermes_state_mutation_binding.py
@@ -19,6 +19,26 @@ def _owner_user(row):
return None if legacy_account_label(dict(row)) else row['user_id']
+def _login_label(subject):
+ """The ``<provider>:<user>`` label Desktop stamps on its rows (tui_gateway/session_transports.py) when it
+ names exactly the dashboard subject ``auth:v1:[provider, "", user]`` (gateway/session_identity.py).
+ Only an issuer-less login maps: the label drops the issuer, so an issuer-bearing subject must not claim it."""
+ if not isinstance(subject, str) or not subject.startswith('auth:v1:'):
+ return None
+ try:
+ parts = json.loads(subject[len('auth:v1:'):])
+ except ValueError:
+ return None
+ if not (isinstance(parts, list) and len(parts) == 3 and all(isinstance(p, str) for p in parts)):
+ return None
+ provider, issuer, user = parts
+ if issuer or ':' in provider or not provider.strip() or not user.strip():
+ return None
+ label = f'{provider.strip()}:{user.strip()}'
+ # Never yield something that reads as a subject itself (a provider named ``auth`` + user ``v1:[...]``).
+ return None if label.startswith('auth:') else label
+
+
def import_history_control(conn, actor, session_ids):
binding = json.dumps([actor.profile_id, actor.subject], separators=(',', ':'))
for sid in session_ids:
@@ -43,7 +63,7 @@ def authorize_history(conn, actor, session_id, *, claim=False):
return True
if row['source'] not in _history_sources():
return False
- if _owner_user(row) and row['user_id'] != actor.subject:
+ if _owner_user(row) and row['user_id'] not in (actor.subject, _login_label(actor.subject)):
raise RuntimeStoreError('permission_denied')
if 'session:create' not in actor.capabilities:
raise RuntimeStoreError('permission_denied')
diff --git a/tests/hermes_state/test_history_login_label.py b/tests/hermes_state/test_history_login_label.py
new file mode 100644
index 00000000000..4bdd3880e5a
--- /dev/null
+++ b/tests/hermes_state/test_history_login_label.py
@@ -0,0 +1,80 @@
+"""A dashboard login may act on the ``<provider>:<user>`` rows Desktop stamps for it.
+
+Desktop stamps its rows as ``<provider>:<user>`` (tui_gateway/session_transports.py), while a
+forwarded dashboard actor carries ``authenticated_subject``'s ``auth:v1:[provider, "", user]``
+form (gateway/session_identity.py). ``authorize_history`` compared the two byte for byte, so the
+right logged-in user got ``permission_denied`` on rows it owned (#138950 review). The label maps
+only for an issuer-less login — the label drops the issuer, so an issuer-bearing subject must not
+claim one — and never to anything that itself reads as a subject.
+"""
+
+import pytest
+
+from gateway.session_contract import Principal
+from hermes_state import SessionDB
+from hermes_state_mutation_binding import _login_label, authorize_history
+
+LOGIN = 'auth:v1:["basic","","e2e"]'
+
+
+def _actor(subject, tmp_path):
+ return Principal(subject, str(tmp_path), frozenset({"session:read", "session:create"}), "native-http:x")
+
+
+def _auth(db, subject, row_id):
+ with db._read_ctx() as conn:
+ return authorize_history(conn, _actor(subject, db.db_path.parent), row_id)
+
+
+def db(tmp_path):
+ db = SessionDB(db_path=tmp_path / "state.db")
+ yield db
+ db.close()
+
+
+def test_the_login_label_maps_for_an_issuer_less_login():
+ assert _login_label(LOGIN) == "basic:e2e"
+
+
+def test_a_desktop_labelled_row_takes_its_login(db):
+ db.create_session("desktop-row", source="desktop", user_id="basic:e2e")
+ assert _auth(db, LOGIN, "desktop-row") is True
+
+
+def test_a_foreign_account_still_refused(db):
+ db.create_session("mallory-row", source="desktop", user_id="basic:mallory")
+ with pytest.raises(Exception) as raised:
+ _auth(db, LOGIN, "mallory-row")
+ assert getattr(raised.value, "reason", None) == "permission_denied"
+
+
+def test_the_exact_subject_still_matches(db):
+ db.create_session("subject-row", source="desktop", user_id=LOGIN)
+ assert _auth(db, LOGIN, "subject-row") is True
+
+
[email protected]("subject", [
+ 'auth:v1:["basic","https://idp.example","e2e"]', # issuer-bearing: the label drops the issuer
+ 'auth:v1:["basic","",""]', # empty user
+ 'auth:v1:["basic",""," "]', # blank user
+ 'auth:v1:["ba:sic","","e2e"]', # a ':' in the provider would forge a third form
+ 'auth:v1:["","","e2e"]', # empty provider
+ 'auth:v1:["auth","","v1:[\\"x\\"]"]', # label would read as a subject itself
+ 'auth:v1:"basic"', # non-list payload
+ 'uid:501', # not a login subject at all
+])
+def test_labels_that_must_not_map(subject):
+ assert _login_label(subject) is None
+
+
+def test_an_issuer_bearing_login_cannot_touch_the_labelled_row(db):
+ db.create_session("desktop-row", source="desktop", user_id="basic:e2e")
+ with pytest.raises(Exception) as raised:
+ _auth(db, 'auth:v1:["basic","https://idp.example","e2e"]', "desktop-row")
+ assert getattr(raised.value, "reason", None) == "permission_denied"
+
+
+def test_an_unowned_row_still_admits_any_local_actor(db):
+ db.create_session("unowned-row", source="desktop")
+ assert _auth(db, LOGIN, "unowned-row") is True