From 9b0d49200c1a13aeb335f1c7f148823870179647 Mon Sep 17 00:00:00 2001 From: chraulley Date: Thu, 21 May 2026 10:49:40 +0800 Subject: [PATCH] fix(gateway): sync MEDIA regex extension allowlist with SUPPORTED_DOCUMENT_TYPES After commit ea49b3862 tightened the MEDIA extraction regex by removing the greedy \S+ fallback, the hardcoded extension allowlist fell out of sync with SUPPORTED_DOCUMENT_TYPES. This caused valid file types (.md, .json, .xml, .yaml, .py, .ts, .sh, .log, .ini, .cfg, .html, etc.) to be silently ignored as MEDIA attachments. Fix: build the regex extension list dynamically from SUPPORTED_DOCUMENT_TYPES plus known media types, so they stay in sync. Future additions to SUPPORTED_DOCUMENT_TYPES propagate automatically. Closes #29582 --- gateway/platforms/base.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/gateway/platforms/base.py b/gateway/platforms/base.py index 5157593ac579a..f00cd8b17941b 100644 --- a/gateway/platforms/base.py +++ b/gateway/platforms/base.py @@ -2158,9 +2158,24 @@ def extract_media(content: str) -> Tuple[List[Tuple[str, bool]], str]: # Extract MEDIA: tags, allowing optional whitespace after the colon # and quoted/backticked paths for LLM-formatted outputs. - media_pattern = re.compile( - r'''[`"']?MEDIA:\s*(?P`[^`\n]+`|"[^"\n]+"|'[^'\n]+'|(?:~/|/)\S+(?:[^\S\n]+\S+)*?\.(?:png|jpe?g|gif|webp|mp4|mov|avi|mkv|webm|ogg|opus|mp3|wav|m4a|flac|epub|pdf|zip|rar|7z|docx?|xlsx?|pptx?|txt|csv|apk|ipa)(?=[\s`"',;:)\]}]|$))[`"']?''' + # Build extension list from SUPPORTED_DOCUMENT_TYPES + known media types + # so the two stay in sync automatically. + _media_exts = set() + for ext in SUPPORTED_DOCUMENT_TYPES: + _media_exts.add(ext.lstrip(".")) + _media_exts.update({"png", "jpg", "jpeg", "gif", "webp", + "mp4", "mov", "avi", "mkv", "webm", + "ogg", "opus", "mp3", "wav", "m4a", "flac", + "epub", "rar", "7z", "apk", "ipa", "markdown"}) + _ext_list = sorted(_media_exts, key=lambda x: (-len(x), x)) + _ext_pattern = "|".join(_ext_list) + _MEDIA_RE_STR = ( + r'''[`"']?MEDIA:\s*(?P''' + r'''`[^`\n]+`|"[^"\n]+"|'[^'\n]+'|''' + r'''(?:~/|/)\S+(?:[^\S\n]+\S+)*?\.(?:''' + + _ext_pattern + r''')(?=[\s`"',;:)\]}]|$))[`"']?''' ) + media_pattern = re.compile(_MEDIA_RE_STR) for match in media_pattern.finditer(content): path = match.group("path").strip() if len(path) >= 2 and path[0] == path[-1] and path[0] in "`\"'":