Repository navigation
Fix res.jsonp() emitting invalid JavaScript when the callback sanitizes to empty - #7491
Closed
januththedev wants to merge 1 commit into
Closed
januththedev wants to merge 1 commit into
januththedev wants to merge 1 commit into
Conversation
Contributor
|
Duplicate of #7161 |
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.
res.jsonp()can emit syntactically invalid JavaScriptDescription
lib/response.js:285guards the JSONP branch withcallback.length !== 0before sanitizing, and line 290 then reduces the callback to the charset[^\[\]\w$.]. A request like?callback=!!!or?callback=%20%20passes the non-empty guard and is then sanitized to''.The wrapper is then built as:
which is
/**/ typeof === 'function' && ({"count":1});— a parse error, served asContent-Type: text/javascript; charset=utf-8withX-Content-Type-Options: nosniff, so browsers refuse to run it.Why it's wrong
Express has already decided "empty callback means not JSONP" — that is the
callback.length !== 0check, and the no-callback path (?callback=,?callback[]=!!!) depends on it. Sanitizing silently defeats that decision, so the endpoint is broken for any client that lets a user pick the callback name.Reproduction
The fix
Sanitize first, then re-check, and only then take the JSONP branch:
Behaviour for every non-empty sanitized callback is unchanged. Full post-fix matrix:
/?callback=foofoo({...}), valid JS?callback=%21%21%21?callback=%20%20?callback=%20foo%20foo({...}), valid JS?callback=foo;bar()foobar({...})(unchanged)Tests
Two cases in
test/res.jsonp.js: the sanitized-to-empty fallback, and a control asserting a partially sanitized callback (?callback=%20foo%20) still produces valid JSONP — so the fix cannot regress the legitimate sanitization path.expected "Content-Type" of "application/json; charset=utf-8", got "text/javascript; charset=utf-8". After: passes.npx mocha test/res.jsonp.js→ 25 passing.npx mocha test/ test/acceptance/→ 1263 passing, 0 failing (baseline 1261, so +2 new).npm run lint→ exit 0.I searched all open and closed issues for "jsonp" (30 hits) — nothing about a callback that sanitizes to empty — and none of the 30 open PRs touch
res.jsonp.For the record, the other divergences I found in
res.jsonp's neighbourhood are already filed:res.status(1n)throwing aBigIntserialization error is #6756,res.setwith an unknown content type storing the literal stringfalseis #7034, andres.location()emittingLocation: undefinedis #6941.