fix(x): declare media_type matching the uploaded bytes#1670
Open
bradykirk wants to merge 1 commit into
Open
Conversation
X's v2 /2/media/upload validates the declared `media_type` against the actual
bytes and rejects any mismatch. uploadMedia re-encoded every non-mp4 image to
GIF (`sharp(...).gif()`) while still declaring the original type via
`lookup(m.path)` (image/png, image/jpeg, ...). The mismatch made X reject all
static-image posts with a generic error; only GIFs (whose bytes happened to
match) succeeded. It became fatal once uploads moved from the lenient v1.1
endpoint to v2, which honors the declared media_type.
Extract prepareMedia() which returns { buffer, media_type } together, keyed to
what the bytes actually are: mp4 -> video/mp4; gif -> image/gif (passthrough);
png/jpeg keep their original format (lossless, alpha preserved); anything else
is converted to jpeg. Images are downscaled to fit a 6000x6000 box instead of a
fixed width. This mirrors the approach already used in linkedin.provider.ts.
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
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
X's v2
/2/media/uploadvalidates the declaredmedia_typeagainst the actual bytes and rejects any mismatch.XProvider.uploadMediare-encoded every non-mp4 image to GIF (sharp(...).gif()) while still declaring the original type vialookup(m.path)(image/png,image/jpeg, ...). X rejected that mismatch, so all static-image posts failed with a genericbad_bodyerror — only GIFs (whose bytes happened to matchimage/gif) succeeded.This became fatal once uploads moved from the lenient v1.1 endpoint to v2 (which honors the declared
media_type) in8bb1fc82.Fix
Extract
prepareMedia(), which returns{ buffer, media_type }together, keyed to what the bytes actually are:mp4→ raw bytes,video/mp4gif→ passthrough,image/gif(preserves animation)png/jpeg→ keep original format (lossless, alpha preserved), matchingmedia_typejpegImages are downscaled to fit a 6000×6000 box (downscale-only) instead of a fixed width. This mirrors the approach already used in
linkedin.provider.ts(prepareMediaBuffer).Because
media_typeis now derived from the actual output format, it can never drift from the bytes.Testing
Verified the invariant (declared
media_type== actual buffer format) against realsharpoutput for JPEG, PNG (with transparency preserved), GIF, and WEBP inputs. The previous logic fails the JPEG/PNG/WEBP cases (bytes aregif, type declares otherwise); the new logic passes all.