Skip to content

The agent that grows with you

fix(gateway): treat absent adapter media lists as no media at native admission

unsupportedpastels012ef5e00f1f
diff --git a/gateway/session_envelope.py b/gateway/session_envelope.py
index 60ed56f9f78..22a761e5a0c 100644
--- a/gateway/session_envelope.py
+++ b/gateway/session_envelope.py
@@ -6,6 +6,7 @@ Relay snapshots bind the authenticated connector and exact source; replay rechec
"""
import asyncio
from copy import copy, deepcopy
+from dataclasses import replace
from datetime import datetime
import json
@@ -27,6 +28,7 @@ _WHATSAPP_METADATA = frozenset({'whatsapp_native_type', 'whatsapp_native', 'what
_WHATSAPP_NATIVE_KEYS = frozenset({'video', 'audio', 'sticker', 'location', 'contact', 'contacts',
'reaction', 'poll', 'pollUpdate'})
_WHATSAPP_NATIVE_MAX_BYTES = 16 * 1024
+_MEDIA_FIELDS = ('media_urls', 'media_types', 'media_text_inlined')
def _native_metadata(event):
@@ -54,6 +56,13 @@ def _native_metadata(event):
return projected
+def _adapter_event(event, **changes):
+ """An adapter's event at the envelope boundary: an absent media list (``None``) is no media.
+ Any other non-list still reaches ``_validate_native`` and refuses."""
+ changes.update({name: [] for name in _MEDIA_FIELDS if getattr(event, name) is None})
+ return replace(event, **changes) if changes else event
+
+
def _validate_native(runner, event, provenance=None, fresh_roles=False):
source = event.source
if (source is None or not isinstance(event.text, str)
@@ -103,17 +112,16 @@ def _validate_native(runner, event, provenance=None, fresh_roles=False):
def snapshot_native(runner, event):
from gateway.session_ingress_context import capture_provenance
provenance = capture_provenance(runner, event)
- return _snapshot_native(runner, event, provenance)
+ return _snapshot_native(runner, _adapter_event(event), provenance)
async def prepare_native(runner, event):
- from dataclasses import replace
from gateway.session_ingress_context import capture_provenance, reauthorize_roles
provenance = capture_provenance(runner, event)
# Freeze caller-owned identity and content before the first network yield.
# Preserve dynamic trust fields too: validation must reject forged ones,
# not silently discard them while freezing the caller's source identity.
- frozen = replace(event, source=copy(event.source))
+ frozen = _adapter_event(event, source=copy(event.source))
fresh_roles = await reauthorize_roles(runner, frozen.source, provenance)
encoded_source = _validate_native(runner, frozen, provenance, fresh_roles)
# Copy + SHA-256 + fsyncs of up to max_inbound_media_bytes stay off the owner loop; the
diff --git a/tests/gateway/test_native_admission_metadata.py b/tests/gateway/test_native_admission_metadata.py
index ce3c1a1f124..a9dc9ee0967 100644
--- a/tests/gateway/test_native_admission_metadata.py
+++ b/tests/gateway/test_native_admission_metadata.py
@@ -109,6 +109,13 @@ async def test_native_codec_closed_metadata_contract_and_reply_expected(tmp_path
for bad in (replace(event, reply_expected='no'), replace(event, text='/new now')):
with pytest.raises(RuntimeStoreError, match='invalid_params'):
snapshot_native(runner, bad)
+ # MSG-2: an adapter's absent media list (None) is no media at the envelope boundary, with the
+ # same fingerprint as []; any other non-list still refuses.
+ for name in ('media_urls', 'media_types', 'media_text_inlined'):
+ assert snapshot_native(runner, replace(event, **{name: None})) == snapshot_native(runner, event)
+ for bad in ('x', ('x',)):
+ with pytest.raises(RuntimeStoreError, match='invalid_params'):
+ snapshot_native(runner, replace(event, **{name: bad}))
finally:
authority.db.close()
# Closed producer matrix: no other adapter stamps MessageEvent.metadata on ordinary inbound,