fix: decode WHATWG-escaped characters in file originalname (#836)#1421
Open
webdevelopersrinu wants to merge 1 commit into
Open
fix: decode WHATWG-escaped characters in file originalname (#836)#1421webdevelopersrinu wants to merge 1 commit into
webdevelopersrinu wants to merge 1 commit into
Conversation
Browsers escape the bytes 0x0A (LF), 0x0D (CR) and 0x22 (") as %0A, %0D
and %22 when serialising filenames in a multipart/form-data body, per the
WHATWG HTML spec. Busboy leaves them escaped, so a file named `file".ext`
was exposed as `file%22.ext` on `req.file.originalname`.
Reverse exactly those three sequences when building the file object. Only
these are decoded on purpose: a bare `%` is never escaped by the spec, so
a full decodeURIComponent would corrupt legitimate names like `50%.pdf`.
Adds regression tests covering the escaped quote, CR/LF, an unescaped
name, and preservation of a literal percent sign.
Fixes expressjs#836
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 & why
Fixes #836.
Browsers escape the bytes
0x0A(LF),0x0D(CR) and0x22(") as%0A,%0Dand%22when serialising a filename in amultipart/form-databody, per the WHATWG HTML spec. Busboy leavesthem escaped, so a file uploaded as
file".extwas exposed onreq.file.originalnameasfile%22.ext.Change
lib/make-middleware.js.%is never escapedby the spec, so a full
decodeURIComponentwould corrupt legitimate nameslike
50%off.pdf. A dedicated test guards this.Reproduction (before)
Uploading a file named
file".ext→req.file.originalname === 'file%22.ext' // ❌ before
req.file.originalname === 'file".ext' // ✅ after
Tests
Adds
test/filename-decoding.jscovering:%22)%0D,%0A)50%off.ext)All pass;
standardlint is clean. Targeting the 2.x line as discussed onthe issue.