From fd2d054d8b7488b07d9192ff7cfff334c14c6928 Mon Sep 17 00:00:00 2001 From: teknium1 <127238744+teknium1@users.noreply.github.com> Date: Tue, 30 Jun 2026 13:47:30 -0700 Subject: [PATCH] fix(gateway): strip [[as_document]] even without a MEDIA: tag The extension-less MEDIA delivery guards short-circuited on "MEDIA: not in text and [[audio_as_voice]] not in text", so a response carrying only [[as_document]] (an image-only reply requesting unmodified document delivery) leaked the directive as visible text. Add [[as_document]] to both guard conditions (_strip_media_tag_directives and strip_media_directives_for_display) and cover it with a regression test. --- gateway/platforms/base.py | 12 ++++++++++-- tests/gateway/test_platform_base.py | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/gateway/platforms/base.py b/gateway/platforms/base.py index c5ed7c7baba..f7ce60477f8 100644 --- a/gateway/platforms/base.py +++ b/gateway/platforms/base.py @@ -1471,7 +1471,11 @@ def _path_lacks_deliverable_extension(path: str) -> bool: def _strip_media_tag_directives(text: str) -> str: """Remove MEDIA: tags and [[audio_as_voice]] / [[as_document]] markers.""" - if "MEDIA:" not in text and "[[audio_as_voice]]" not in text: + if ( + "MEDIA:" not in text + and "[[audio_as_voice]]" not in text + and "[[as_document]]" not in text + ): return text cleaned = text.replace("[[audio_as_voice]]", "").replace("[[as_document]]", "") @@ -3544,7 +3548,11 @@ class BasePlatformAdapter(ABC): ``validate_media_delivery_path`` accepts the path so undeliverable paths stay visible for debugging. """ - if "MEDIA:" not in text and "[[audio_as_voice]]" not in text: + if ( + "MEDIA:" not in text + and "[[audio_as_voice]]" not in text + and "[[as_document]]" not in text + ): return text cleaned = _strip_media_tag_directives(text) cleaned = re.sub(r'\n{3,}', '\n\n', cleaned) diff --git a/tests/gateway/test_platform_base.py b/tests/gateway/test_platform_base.py index 21e0f6a3675..47d1286ad82 100644 --- a/tests/gateway/test_platform_base.py +++ b/tests/gateway/test_platform_base.py @@ -696,6 +696,21 @@ class TestExtensionlessMediaDelivery: stripped = BasePlatformAdapter.strip_media_directives_for_display(text) assert "MEDIA:" not in stripped + def test_as_document_directive_stripped_without_media_tag(self): + """[[as_document]] must be stripped even when no MEDIA: tag is present. + + The display/strip guards short-circuit on text containing none of the + delivery directives; [[as_document]] must be in that guard or it leaks + to the user as visible text on any image-only response. + """ + from gateway.platforms.base import _strip_media_directives + + text = "Here is your image. [[as_document]]" + assert "[[as_document]]" not in _strip_media_directives(text) + assert "[[as_document]]" not in ( + BasePlatformAdapter.strip_media_directives_for_display(text) + ) + class TestMediaDeliveryPathValidation: def _patch_roots(self, monkeypatch, *roots):