The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: stage canonical remote images in the authorized owner cache
Tekniumbaaa4464a468
diff --git a/gateway/session_images.py b/gateway/session_images.pynew file mode 100644index 00000000000..b768007e52a--- /dev/null+++ b/gateway/session_images.py@@ -0,0 +1,39 @@+"""Staging-only image uploads on the authenticated session authority."""+import asyncio+from pathlib import Path++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 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 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)+++def _stage(directory, params):+ from tui_gateway.prompt_attachments import (+ _ATTACH_BYTES_MAX_BYTES, _decode_attach_base64, _sniff_image_ext, stage_image_bytes,+ )+ data = _decode_attach_base64(params.get('content_base64') or params.get('data') or '', mime_prefix='image/')+ if not data or len(data) > _ATTACH_BYTES_MAX_BYTES:+ raise RuntimeStoreError('invalid_params')+ hint = params.get('ext', '').strip().lower().lstrip('.')+ ext = _sniff_image_ext(data, params.get('filename') or (f'x.{hint}' if hint else ''))+ mime = {'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg',+ '.gif': 'image/gif', '.webp': 'image/webp'}.get(ext)+ if mime is None:+ raise RuntimeStoreError('invalid_params')+ try:+ path = stage_image_bytes(directory, data, ext, prefix='upload')+ except OSError as exc:+ raise RuntimeStoreError('storage_unavailable') from exc+ return {'attached': True, 'path': str(path), 'mime': mime, 'name': path.name,+ 'count': 1, 'remainder': '', 'text': f'[User attached image: {path.name}]', 'bytes': len(data)}diff --git a/tests/gateway/test_session_images.py b/tests/gateway/test_session_images.pynew file mode 100644index 00000000000..fb7b5f04150--- /dev/null+++ b/tests/gateway/test_session_images.py@@ -0,0 +1,131 @@+"""Canonical uploads stage bytes only after viewer and owner authorization."""+import base64+from pathlib import Path+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_is_authorized_private_staging_not_legacy_execution(tmp_path, monkeypatch):+ monkeypatch.setenv('HERMES_HOME', str(tmp_path / 'wrong-home'))+ home = tmp_path / 'owner'+ home.mkdir()+ 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'}+ async def upload(connection=owner, **changes):+ return await connection.dispatch({'id': 1, 'method': 'image.attach_bytes', 'params': {**params, **changes}})+ first = await upload()+ assert 'result' in first, first+ result = first['result']+ path = Path(result['path'])+ assert path.parent == home / 'cache/images' and path.read_bytes() == PNG+ assert path.stat().st_mode & 0o777 == 0o600+ assert result['mime'] == 'image/png' and result['bytes'] == len(PNG)+ second = await upload()+ assert second['result']['path'] != str(path)+ assert live.task is None and not db.get_messages('s')+ with db._read_ctx() as conn:+ assert conn.execute('SELECT count(*) FROM session_admissions').fetchone()[0] == 0+ before = sorted((home / 'cache/images').iterdir())+ for identity, subscribed, reason in [+ ({'user_id': 'owner'}, False, 'permission_denied'),+ ({'user_id': 'owner', 'capabilities': ['session:read']}, True, 'permission_denied'),+ ({'user_id': 'other'}, True, 'permission_denied'),+ ({'user_id': 'owner', 'profile_id': 'foreign'}, True, 'profile_mismatch'),+ ]:+ peer = AuthorityConnection(authority, object(), identity)+ if subscribed:+ peer.subscriptions['s'] = 'test-authority-boundary'+ denied = await upload(peer)+ assert denied['error']['message'] == reason, denied+ for changes in [{'session_id': ''}, {'session_id': ['s']}, {'profile_home': str(tmp_path)},+ {'content_base64': 'not base64'}, {'content_base64': ''},+ {'content_base64': base64.b64encode(b'x' * (25 * 1024 * 1024 + 1)).decode()},+ {'filename': 'bad.bmp'}, {'filename': 'bad.svg'},+ {'content_base64': base64.b64encode(b'BMbad').decode(), 'filename': ''}]:+ denied = await upload(**changes)+ assert denied['error']['message'] == 'invalid_params', denied+ assert sorted((home / 'cache/images').iterdir()) == before+ assert not (tmp_path / 'wrong-home/cache/images').exists()+ for method in ('image.attach', 'clipboard.paste'):+ denied = await owner.dispatch({'id': 2, 'method': method, 'params': params})+ assert denied['error']['message'] == 'invalid_params'+ await owner.close()++++def test_authenticated_daemon_upload_retains_exact_admitted_bytes(tmp_path):+ import asyncio+ from contextlib import closing+ import json+ import sqlite3+ from websockets.asyncio.client import connect+ from websockets.exceptions import InvalidStatus+ from tests.gateway.fixtures.local_recovery_probe import child_env, daemon, rpc, websocket++ root = Path(__file__).resolve().parents[2]+ home, user = tmp_path / 'state', tmp_path / 'user'+ home.mkdir(mode=0o700)+ user.mkdir()+ config = {'gateway': {'multiplex_profiles': False},+ 'model': {'provider': 'custom', 'default': 'fixture', 'base_url': 'http://127.0.0.1:1/v1'},+ 'auxiliary': {'title_generation': {'enabled': False}}, 'platform_toolsets': {'cli': []}}+ (home / 'config.yaml').write_text(json.dumps(config))+ env = child_env()+ env.update(HOME=str(user), USERPROFILE=str(user), HERMES_HOME=str(home), PYTHONPATH=str(root),+ OPENAI_API_KEY='loopback-only', PYTHONUNBUFFERED='1')++ async def exercise(desc):+ url = desc['api_origin'].replace('http:', 'ws:') + '/api/ws'+ with pytest.raises(InvalidStatus):+ async with connect(url):+ pass+ async with websocket(home, desc) as ws:+ created = await rpc(ws, 'session.create', request_id='image-owner', source='tui', cwd=str(user), toolsets=[])+ assert 'result' in created, created+ sid = created['result']['session_id']+ params = dict(session_id=sid, filename='client.png', content_base64=base64.b64encode(PNG).decode())+ uploaded = await rpc(ws, 'image.attach_bytes', **params)+ assert 'result' in uploaded, uploaded+ image = uploaded['result']+ path = Path(image['path'])+ assert path.parent == home / 'cache/images' and path.read_bytes() == PNG+ async with websocket(home, desc) as detached:+ denied = await rpc(detached, 'image.attach_bytes', **params)+ assert denied['error']['message'] == 'permission_denied', denied+ for changes in ({'profile_home': str(user)}, {'filename': 'bad.bmp'}, {'content_base64': 'bad!'}):+ denied = await rpc(ws, 'image.attach_bytes', **{**params, **changes})+ assert denied['error']['message'] == 'invalid_params', denied+ assert list((home / 'cache/images').iterdir()) == [path]+ with closing(sqlite3.connect(f'file:{home / "state.db"}?mode=ro', uri=True)) as db:+ assert db.execute('SELECT count(*) FROM session_admissions').fetchone()[0] == 0+ submitted = await rpc(ws, 'prompt.submit', session_id=sid, input_id='image-input',+ text='Retain upload', attachments=[{'path': str(path), 'mime': image['mime']}])+ assert 'result' in submitted, submitted+ with closing(sqlite3.connect(f'file:{home / "state.db"}?mode=ro', uri=True)) as db:+ payload = json.loads(db.execute('SELECT payload_json FROM session_admissions WHERE request_id=?', ('image-input',)).fetchone()[0])+ retained = payload['attachments_v1']['media'][0]+ path.write_bytes(b'client staging changed after ACK')+ assert Path(retained['path']).read_bytes() == PNG+ assert payload['attachments_v1']['media_types'] == ['image/png']+ print(json.dumps({'authenticated_public_ws': True, 'session_id': sid,+ 'owner_path': str(path), 'retained': retained, 'no_admission_on_upload': True}))+ with daemon(root, home, env, barrier=False) as (_, desc):+ asyncio.run(exercise(desc))diff --git a/tui_gateway/prompt_attachments.py b/tui_gateway/prompt_attachments.pyindex bdb749e4ef7..a6a89dfb719 100644--- a/tui_gateway/prompt_attachments.py+++ b/tui_gateway/prompt_attachments.py@@ -63,6 +63,7 @@ def _decode_attach_payload(def _sniff_image_ext(img_bytes: bytes, filename: str = "") -> str:"""Extension from the filename hint, else magic bytes (WebP: RIFF container), else ``.png``."""+ from pathlib import Pathif filename and (suffix := Path(filename).suffix.lower()):return suffixhead = img_bytes[:16]@@ -92,15 +93,32 @@ def _session_images_dir(session: dict) -> Path:return _session_home_dir(session, "images")+def stage_image_bytes(directory, img_bytes: bytes, ext: str, *, prefix: str):+ """Private, collision-free staging shared by local and canonical byte uploads."""+ import os+ from pathlib import Path+ import tempfile+ directory = Path(directory)+ directory.mkdir(mode=0o700, parents=True, exist_ok=True)+ fd, name = tempfile.mkstemp(dir=directory, prefix=prefix + "_", suffix=ext)+ path = Path(name)+ try:+ with os.fdopen(fd, "wb") as output:+ output.write(img_bytes)+ output.flush()+ os.fsync(output.fileno())+ except BaseException:+ path.unlink(missing_ok=True)+ raise+ return path++def _queue_attached_image(session: dict, img_bytes: bytes, ext: str, *, prefix: str) -> Path:"""Write image bytes into the session images dir and queue them for the next submit."""session["image_counter"] = session.get("image_counter", 0) + 1img_dir = _session_images_dir(session)- img_dir.mkdir(parents=True, exist_ok=True)- ts = datetime.now().strftime("%Y%m%d_%H%M%S")- img_path = img_dir / f"{prefix}_{ts}_{session['image_counter']}{ext}"try:- img_path.write_bytes(img_bytes)+ img_path = stage_image_bytes(img_dir, img_bytes, ext, prefix=prefix)except Exception:session["image_counter"] = max(0, session["image_counter"] - 1)raise