fix(weixin): check full 4-byte PNG magic signature#2970
Open
chinesepowered wants to merge 1 commit intoQwenLM:mainfrom
Open
fix(weixin): check full 4-byte PNG magic signature#2970chinesepowered wants to merge 1 commit intoQwenLM:mainfrom
chinesepowered wants to merge 1 commit intoQwenLM:mainfrom
Conversation
PNG's magic bytes are 89 50 4E 47, but detectImageMime only checked
the first three. The WebP branch in the same function correctly checks
all four bytes of its signature — the PNG path was clearly an oversight.
Extend the PNG check to include 0x47 ('G') for consistency and to
eliminate the (admittedly rare) false-positive window.
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.
Check full 4-byte PNG magic signature in Weixin image MIME detection.
TLDR
WeixinAdapter.detectImageMimechecked only 3 of the 4 bytes of PNG's magic signature (89 50 4E 47), while the WebP branch in the same function correctly checked all 4 bytes. This is a clear oversight, not a deliberate relaxation. Extend the PNG check to include the fourth byte (0x47, 'G') for consistency.Screenshots / Video Demo
N/A — byte-level MIME detection has no user-visible surface.
Dive Deeper
Before:
PNG's magic bytes are
89 50 4E 47. The check only validated the first three. In practice, false positives are nearly impossible — any file starting with\x89PNis essentially always PNG — but the inconsistency with the WebP branch suggests the omission was unintentional rather than a deliberate relaxation.After:
Scope note: The GIF check has a similar issue (the full signature is
GIF87aorGIF89a, 6 bytes) but a 3-byteGIFprefix is uniquely identifying in practice and is arguably acceptable. Left untouched to keep this PR focused.Modified file:
packages/channels/weixin/src/WeixinAdapter.ts— addeddata[3] === 0x47checkReviewer Test Plan
\x89\x50\x4E\x47→ should still return'image/png'\x89\x50\x4E+ any other fourth byte (e.g.,\x00) → now returns'image/jpeg'(fallback); previously returned'image/png'Testing Matrix