fix: verify audio magic bytes before extension-based short-circuit in convert_audio_format - #9651
Open
xiaoyuyu6420 wants to merge 2 commits into
Open
Conversation
… convert_audio_format When a platform (e.g. NapCat) saves AMR-encoded audio with a .wav extension, the extension-only short-circuit returned the raw AMR file unchanged. Downstream STT providers then received malformed WAV data and returned HTTP 400. The fix reuses the existing _get_audio_magic_type() helper to verify the file content actually matches the target format before skipping ffmpeg conversion. When the detected format differs from the output format, conversion proceeds normally. Unrecognised content still falls back to extension matching to preserve existing behaviour. Closes AstrBotDevs#9594.
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The ffmpeg subprocess mocking logic (including
_FakeFFmpegProcessand thefake_execsetup) is duplicated across several tests; consider extracting a reusable helper or fixture to keep the test suite DRY and easier to maintain. - In
_FakeFFmpegProcess, thestderrparameter is accepted but never used; either wire it intocommunicate()or remove the parameter to avoid confusion about its purpose.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The ffmpeg subprocess mocking logic (including `_FakeFFmpegProcess` and the `fake_exec` setup) is duplicated across several tests; consider extracting a reusable helper or fixture to keep the test suite DRY and easier to maintain.
- In `_FakeFFmpegProcess`, the `stderr` parameter is accepted but never used; either wire it into `communicate()` or remove the parameter to avoid confusion about its purpose.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Address Sourcery review feedback on AstrBotDevs#9651: - Extract _patch_ffmpeg() helper to deduplicate the fake_exec setup across the three conversion-expected tests. - Remove the unused stderr parameter from _FakeFFmpegProcess.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a platform adapter (e.g. NapCat via aiocqhttp / OneBot v11) saves QQ voice messages, the actual audio encoding is AMR (magic bytes
#!AMR) but the local file is given a.wavextension. Theconvert_audio_format()function short-circuits purely on file extension:When
output_formatiswav, the extension matches and the raw AMR byte stream is returned unchanged. Downstream STT providers (e.g. Whisper-compatible APIs) receive malformed WAV data and reject it with HTTP 400.Reported in #9594.
Fix
Reuse the existing
_get_audio_magic_type()helper (already used byensure_wav()) to verify the file content actually matches the target format before short-circuiting:Three cases:
Tests
5 new tests added to
tests/test_media_utils.py, all passing:amr_with_wav_extension_does_not_short_circuit.wavext, targetwavreal_wav_with_wav_extension_short_circuits.wavext, targetwavunknown_content_with_matching_ext_short_circuits.wavext, targetwavreal_amr_with_amr_extension_short_circuits.amrext, targetamrwav_with_ogg_extension_does_not_short_circuit.oggext, targetoggFull suite: 55 passed.
ruff format/ruff checkclean.Closes #9594.
Summary by Sourcery
Validate audio file magic bytes before short-circuiting format conversion based on file extension to prevent malformed audio from being returned to downstream consumers.
Enhancements:
Tests: