Skip to content

The agent that grows with you

fix(gateway): image.attach_bytes accepts its declared profile selector

Teknium2080d246d1c6
diff --git a/gateway/session_images.py b/gateway/session_images.py
index b768007e52a..72eee3d9e7a 100644
--- a/gateway/session_images.py
+++ b/gateway/session_images.py
@@ -7,12 +7,14 @@ from hermes_state_runtime import RuntimeStoreError
async def attach_bytes(connection, ref, params):
if (not isinstance(ref.session_id, str) or not ref.session_id
- or set(params) - {'session_id', 'content_base64', 'data', 'filename', 'ext'}
+ or set(params) - {'session_id', 'profile', 'content_base64', 'data', 'filename', 'ext'}
or any(not isinstance(value, str) for value in params.values())):
raise RuntimeStoreError('invalid_params')
if ref.session_id not in connection.subscriptions:
raise RuntimeStoreError('permission_denied')
- connection.authority.authorize(connection.actor, ref, 'session:submit')
+ # The declared ``profile`` selector names this owner's own home or is refused (dokterdok N73).
+ from gateway.session_busy_controls import authorize
+ authorize(connection, ref, params, 'session:submit')
# The owner, never the caller or ambient process profile, selects staging.
directory = Path(connection.authority.profile_id) / 'cache' / 'images'
return await asyncio.to_thread(_stage, directory, params)
diff --git a/tests/gateway/test_session_images_profile.py b/tests/gateway/test_session_images_profile.py
new file mode 100644
index 00000000000..e31c7b423d4
--- /dev/null
+++ b/tests/gateway/test_session_images_profile.py
@@ -0,0 +1,40 @@
+"""image.attach_bytes accepts its declared ``profile`` selector for the owner's own home (dokterdok N73)."""
+import base64
+from types import SimpleNamespace
+
+import pytest
+
+from gateway.config import Platform
+from gateway.session_authority import LiveSession, SessionAuthority
+from gateway.session_controls import AuthorityConnection
+from hermes_state import SessionDB
+from hermes_state_runtime import begin_runtime_epoch
+
+PNG = base64.b64decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Wl2nL8AAAAASUVORK5CYII=')
+
+
+async def test_upload_profile_selector_names_own_home_or_is_refused(tmp_path, monkeypatch):
+ # The owner is the `ops` profile; `profile: "ops"` is what Bot Mode's group turns send.
+ root = tmp_path / 'root'
+ home = root / 'profiles' / 'ops'
+ home.mkdir(parents=True)
+ monkeypatch.setenv('HERMES_HOME', str(root))
+ monkeypatch.setattr('hermes_constants.get_default_hermes_root', lambda: root)
+ with SessionDB(db_path=home / 'state.db') as db:
+ db.create_session('s', source='test')
+ authority = SessionAuthority(SimpleNamespace(_draining=False), profile_id=str(home),
+ instance_id='current', db=db, epoch=begin_runtime_epoch(db, instance_id='current'))
+ owner = AuthorityConnection(authority, SimpleNamespace(write=lambda frame: None), {'user_id': 'owner'})
+ live = authority.sessions['s'] = LiveSession(SimpleNamespace(platform=Platform.LOCAL,
+ user_id=owner.actor.subject), 'route')
+ owner.subscriptions['s'] = 'test-viewer'
+ live.subscribers['test-viewer'] = owner.actor
+ params = {'session_id': 's', 'content_base64': base64.b64encode(PNG).decode(), 'filename': 'shot.png'}
+
+ own = await owner.dispatch({'id': 1, 'method': 'image.attach_bytes', 'params': {**params, 'profile': 'ops'}})
+ assert own.get('result', {}).get('mime') == 'image/png', own
+ foreign = await owner.dispatch({'id': 2, 'method': 'image.attach_bytes', 'params': {**params, 'profile': 'other'}})
+ assert foreign['error']['message'] == 'profile_mismatch', foreign
+ assert len(list((home / 'cache/images').iterdir())) == 1
+ await owner.close()