fix(media): ignore inherited prototype keys in the MIME allow-list#46
Open
zqxuii wants to merge 1 commit into
Open
fix(media): ignore inherited prototype keys in the MIME allow-list#46zqxuii wants to merge 1 commit into
zqxuii wants to merge 1 commit into
Conversation
mediaTypeForUpload gated on `ALLOWED_TYPES[mime]` (a truthy bracket lookup), so a Content-Type of "constructor" or "__proto__" resolved to an inherited Object.prototype member and returned truthy. The media upload route accepts the file whenever this returns non-null, so a crafted Content-Type slipped any file (e.g. evil.html) past the mp4/webm/mov allow-list. Gate on Object.hasOwn; add tests.
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.
What
mediaTypeForUploadgated the allow-list withALLOWED_TYPES[mime](a truthy bracket lookup). Because that resolves through the prototype chain, aContent-Typeofconstructoror__proto__returns an inheritedObject.prototypemember (truthy), so the function returned that string as an accepted type.Why it matters
app/api/media/route.tsaccepts the upload whenevermediaTypeForUpload(file.name, file.type)is non-null, andfile.typeis attacker-controlled. So:POST /api/mediawith a file part whoseContent-Type: constructorpasses theif (!type)guard and slips any file (e.g.evil.html) past the mp4/webm/mov allow-list.ALLOWED_TYPES[type](= ALLOWED_TYPES["constructor"]) returns theObjectfunction, which then coerces into a garbage stored filename.Fix
Gate on
Object.hasOwn(ALLOWED_TYPES, mime)instead of a truthy bracket lookup — inherited keys no longer match. Legit types and extension inference are unchanged.Tests
Added a case asserting
constructor/__proto__/hasOwnPropertycontent-types returnnull.node --test tests/media-upload-type.test.mjspasses; no other tests affected.