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.
This commit is contained in:
teknium1 2026-06-30 13:47:30 -07:00 committed by Teknium
parent 6b89439ef1
commit fd2d054d8b
2 changed files with 25 additions and 2 deletions

View file

@ -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)

View file

@ -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):