Skip to content

The agent that grows with you

fix(gateway): let a served login control its own stamped chats over HTTP

unsupportedpastelsf90a8e00e40b
diff --git a/gateway/session_local_migration.py b/gateway/session_local_migration.py
index 484be549e64..ca3ce1b0b3d 100644
--- a/gateway/session_local_migration.py
+++ b/gateway/session_local_migration.py
@@ -33,12 +33,14 @@ def unbind_native_transport(authority, actor):
def require_history_claim(authority, conn, actor, sid):
"""Import receipts confer ownership; merely knowing an old ID does not."""
- from hermes_state_mutation_binding import BINDING_PREFIX
- row = conn.execute('SELECT session_key,chat_id,origin_json FROM sessions WHERE id=?', (sid,)).fetchone()
- if row is None or any(row):
+ from hermes_state_mutation_binding import BINDING_PREFIX, _login_label, _owner_user
+ row = conn.execute('SELECT * FROM sessions WHERE id=?', (sid,)).fetchone()
+ if row is None or row['session_key'] or row['chat_id'] or row['origin_json']:
return
if conn.execute('SELECT 1 FROM state_meta WHERE key=?', (BINDING_PREFIX + sid,)).fetchone():
return # authorize_history checks the already committed importer identity.
+ if _owner_user(row) and row['user_id'] in (actor.subject, _login_label(actor.subject)):
+ return # The login that created it (a served remote Desktop); authorize_history re-checks it.
if getattr(authority, '_native_legacy_transports', {}).get(actor.transport_id) != actor:
raise RuntimeStoreError('permission_denied')
diff --git a/tests/gateway/test_legacy_desktop_source.py b/tests/gateway/test_legacy_desktop_source.py
index f256e6a1cfe..10ec901ae2f 100644
--- a/tests/gateway/test_legacy_desktop_source.py
+++ b/tests/gateway/test_legacy_desktop_source.py
@@ -83,3 +83,32 @@ def test_a_login_labelled_desktop_row_belongs_to_that_login_only(tmp_path):
assert same_history_owner(conn, 'plain', 'alice-row') is False
finally:
db.close()
+
+
+def test_a_served_login_controls_its_own_stamped_chat_over_http(tmp_path):
+ """#143547 DB-15: a remote Desktop signed in as ``basic:alice`` archives, pins and renames the
+ chats it created over plain HTTP (transport ``http``, no native ticket). The claim gate refused
+ every non-native transport before ``authorize_history`` could read the login stamp."""
+ from types import SimpleNamespace
+ from gateway.session_contract import SessionRef
+ from gateway.session_mutations import _authorize_control
+
+ db = SessionDB(db_path=tmp_path / 'state.db')
+ try:
+ db.create_session('alice-row', source='desktop', user_id='basic:alice')
+ db.create_session('unstamped', source='desktop')
+ authority = SimpleNamespace(profile_id=str(tmp_path), db=db, sessions={})
+ caps = frozenset({'session:read', 'session:create', 'session:control'})
+ alice, bob, host = (Principal(subject, str(tmp_path), caps, 'http')
+ for subject in (_login('alice'), _login('bob'), 'uid:1'))
+
+ def control(actor, sid):
+ params = {'session_id': sid, 'request_id': 'r-' + sid, 'operation': 'archive'}
+ return _authorize_control(authority, actor, SessionRef(str(tmp_path), sid), params)
+
+ assert control(alice, 'alice-row') is True
+ for stranger, sid in ((bob, 'alice-row'), (host, 'alice-row'), (alice, 'unstamped')):
+ with pytest.raises(RuntimeStoreError, match='permission_denied'):
+ control(stranger, sid)
+ finally:
+ db.close()