Skip to content

The agent that grows with you

fix(gateway): validate every surviving image on a partial staging retry

David Dudok de Wit4cd61ebf6ccb
diff --git a/gateway/session_ingress_media.py b/gateway/session_ingress_media.py
index 70cdb1c0e2e..87e0457bdba 100644
--- a/gateway/session_ingress_media.py
+++ b/gateway/session_ingress_media.py
@@ -63,21 +63,22 @@ def admit_attachments(attachments, *, admitted=None):
mimes = [item['mime'] for item in attachments]
row = admitted() if admitted is not None else None
committed = row['payload'].get('attachments_v1') if row is not None else None
- # A hardlink placed in staging is a second name for a file outside it (~/.ssh/id_rsa).
try:
- if any(path.lstat().st_nlink != 1 for path in paths):
- raise RuntimeStoreError('invalid_params')
- # Retry identity is the committed bytes, not the disposable staging name: a client that
- # re-staged the same image under a new name is retrying, not sending different work.
- retry = (committed is not None and committed['media_types'] == mimes
- and [r['sha256'] for r in committed['media']] == [_sha256(path) for path in paths])
- except FileNotFoundError as exc:
+ hashes = _attachment_hashes(paths)
+ except (OSError, ValueError) as exc:
+ raise RuntimeStoreError('invalid_params') from exc
+ if None in hashes:
if (committed is None or [Path(r['path']).name for r in committed['media']] != [p.name for p in paths]
or committed['media_types'] != mimes):
- raise RuntimeStoreError('invalid_params') from exc
+ raise RuntimeStoreError('invalid_params')
+ if any(digest is not None and digest != reference['sha256']
+ for digest, reference in zip(hashes, committed['media'])):
+ raise RuntimeStoreError('admission_conflict')
retry = True
- except (OSError, ValueError) as exc:
- raise RuntimeStoreError('invalid_params') from exc
+ else:
+ # Retry identity is the committed bytes, not the disposable staging name.
+ retry = (committed is not None and committed['media_types'] == mimes
+ and [r['sha256'] for r in committed['media']] == hashes)
if retry:
# A live row still executes these bytes, so they must verify; a terminal row is
# exact-retry evidence by digest only. ``admit_session_input`` still checks the digest.
@@ -87,6 +88,20 @@ def admit_attachments(attachments, *, admitted=None):
return {'attachments_v1': {'media': capture_native_media(paths), 'media_types': mimes}}
+def _attachment_hashes(paths):
+ hashes = []
+ for path in paths:
+ try:
+ # Staging must not provide a second name for a file outside it.
+ if path.lstat().st_nlink != 1:
+ raise RuntimeStoreError('invalid_params')
+ hashes.append(_sha256(path))
+ except FileNotFoundError:
+ # One pruned file cannot skip validation of the remaining batch.
+ hashes.append(None)
+ return hashes
+
+
def _sha256(path):
with _open_regular(path) as source:
return hashlib.file_digest(source, 'sha256').hexdigest()
diff --git a/tests/gateway/test_attachment_retry_mixed_staging.py b/tests/gateway/test_attachment_retry_mixed_staging.py
new file mode 100644
index 00000000000..775d6494cb9
--- /dev/null
+++ b/tests/gateway/test_attachment_retry_mixed_staging.py
@@ -0,0 +1,26 @@
+"""A missing staging file must not hide a changed sibling on an exact retry."""
+import pytest
+
+from gateway.session_ingress_media import admit_attachments
+from hermes_state_runtime import RuntimeStoreError
+
+
[email protected]('missing_index', [0, 1])
+def test_missing_staging_sibling_does_not_skip_remaining_image_hashes(missing_index):
+ from gateway.platforms.base import get_image_cache_dir
+ staging = get_image_cache_dir()
+ paths = [staging / 'first.png', staging / 'second.png']
+ for index, path in enumerate(paths):
+ path.write_bytes(b'\x89PNG\r\n\x1a\n' + str(index).encode())
+ attachments = [{'path': str(path), 'mime': 'image/png'} for path in paths]
+ committed = admit_attachments(attachments)
+ row = {'status': 'queued', 'payload': committed}
+ paths[missing_index].unlink()
+ remaining = paths[1 - missing_index]
+ remaining.write_bytes(b'\x89PNG\r\n\x1a\nchanged')
+ with pytest.raises(RuntimeStoreError, match='admission_conflict'):
+ admit_attachments(attachments, admitted=lambda: row)
+
+ # Restore the surviving file: a partially pruned exact retry remains valid.
+ remaining.write_bytes(b'\x89PNG\r\n\x1a\n' + str(1 - missing_index).encode())
+ assert admit_attachments(attachments, admitted=lambda: row) == committed