Skip to content

fix(gateway): MEDIA: directive silently drops .html and other extensions due to regex/whitelist drift #31137

@Razultull

Description

@Razultull

Summary

gateway/platforms/base.py has two independent file-extension whitelists used for media routing that have drifted out of sync:

  • extract_media() parses explicit MEDIA:<path> directives produced by the model.
  • extract_local_files() finds bare local paths in response text.

extract_local_files's _LOCAL_MEDIA_EXTS accepts .html, .htm, .md, .svg, .bmp, .tiff, .json, .xml, .yaml, .yml, .tsv, .ods, .odp, .odt, .rtf, .key, .tar, .gz, .tgz, .bz2, .xz — but the regex inside extract_media does not.

When the model emits MEDIA:/abs/path/file.html (or any other missing extension), the extract_media regex doesn't match, the file is never attached, and the parent send_message tool still reports success: true because the text portion ships fine. Silent data loss, no log line at any level.

The (?<![/:\w.]) anti-URL guard inside extract_local_files also disqualifies the same path (the : after MEDIA defeats it), so the path falls between both detectors.

Reproduction

Telegram, gateway running:

  1. Put any file with one of the missing extensions on a media-allowed root, e.g. /root/.hermes/media_cache/foo.html.
  2. Have the agent return a reply containing literally MEDIA:/root/.hermes/media_cache/foo.html.
  3. Observe: text arrives, file does not. send_message tool result is success: true.

Direct POST https://api.telegram.org/bot$TOKEN/sendDocument against the same file works first try — confirming the bug is in extraction, not transport.

Affected platforms

All platforms that use BasePlatformAdapter.extract_media — i.e. every messaging platform in gateway/platforms/. The bug only surfaces for extensions in the gap between the two lists, but those gaps cover common artifact types (HTML reports, Markdown notes, JSON exports, SVG charts, archives, etc.).

Root cause

gateway/platforms/base.py, around line 2270:

media_pattern = re.compile(
    r'''[`"']?MEDIA:\s*(?P<path>`[^`\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`"',;:)\]}]|$))[`"']?'''
)

vs.

_LOCAL_MEDIA_EXTS = (
    '.png', '.jpg', '.jpeg', '.gif', '.webp', '.bmp', '.tiff', '.svg',
    '.mp4', '.mov', '.avi', '.mkv', '.webm',
    '.mp3', '.wav', '.ogg', '.m4a', '.flac',
    '.pdf', '.docx', '.doc', '.odt', '.rtf', '.txt', '.md',
    '.xlsx', '.xls', '.ods', '.csv', '.tsv', '.json', '.xml', '.yaml', '.yml',
    '.pptx', '.ppt', '.odp', '.key',
    '.zip', '.tar', '.gz', '.tgz', '.bz2', '.xz', '.7z', '.rar',
    '.html', '.htm',
)

The two lists need to stay in sync, but the structure (one is a regex alternation, one is a tuple) makes it easy to forget.

Expected behavior

Any extension accepted by extract_local_files should also be accepted by extract_media. A model emitting MEDIA:/path/to/report.html should result in the file being attached as a document (the same routing the bare-path detection would do).

Suggested fix

Extend the extract_media regex extension list to be a superset of _LOCAL_MEDIA_EXTS, and add a binding comment so future drift gets caught in review.

I have a PR ready: #<PR_NUMBER_PLACEHOLDER> (43-case parametrized regression test + the original user-reported repro path).

Environment

  • Hermes Agent: git rev origin/mainf3fb7899d (May 2026)
  • OS: Linux (Ubuntu, kernel 6.8.0-111-generic)
  • Python 3.11, gateway running under user systemd unit
  • Transport: Telegram bot

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existscomp/gatewayGateway runner, session dispatch, deliverytype/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions