Skip to content

Fix res.jsonp() emitting invalid JavaScript when the callback sanitizes to empty - #7491

Closed
januththedev wants to merge 1 commit into
expressjs:masterfrom
januththedev:fix/jsonp-empty-callback
Closed

januththedev wants to merge 1 commit into
expressjs:masterfrom
januththedev:fix/jsonp-empty-callback

Conversation

@januththedev

Copy link
Copy Markdown

res.jsonp() can emit syntactically invalid JavaScript

Description

lib/response.js:285 guards the JSONP branch with callback.length !== 0 before sanitizing, and line 290 then reduces the callback to the charset [^\[\]\w$.]. A request like ?callback=!!! or ?callback=%20%20 passes the non-empty guard and is then sanitized to ''.

The wrapper is then built as:

'/**/ typeof ' + '' + " === 'function' && " + '' + '(' + body + ');'

which is /**/ typeof === 'function' && ({"count":1}); — a parse error, served as Content-Type: text/javascript; charset=utf-8 with X-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 !== 0 check, 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

$ node -e "...res.jsonp({count:1}); request /?callback=%21%21%21..."

HTTP status                   : 200
Content-Type                  : text/javascript; charset=utf-8
X-Content-Type-Options        : nosniff
body                          : "/**/ typeof  === 'function' && ({\"count\":1});"
parses as JS                  : NO -> SyntaxError: Unexpected token '==='

The fix

Sanitize first, then re-check, and only then take the JSONP branch:

if (typeof callback === 'string' && callback.length !== 0) {
    // restrict callback charset
    callback = callback.replace(/[^\[\]\w$.]/g, '');

    // sanitizing can empty the callback (eg. `?callback=!!!`), which would
    // produce a syntactically invalid response, so fall back to plain JSON
    if (callback.length !== 0) {
      this.set('X-Content-Type-Options', 'nosniff');
      this.set('Content-Type', 'text/javascript');
      ...
    }
}

Behaviour for every non-empty sanitized callback is unchanged. Full post-fix matrix:

request result
/ JSON
?callback=foo foo({...}), valid JS
?callback=%21%21%21 JSON
?callback=%20%20 JSON
?callback=%20foo%20 foo({...}), 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.

  • Before: the first fails with 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 a BigInt serialization error is #6756, res.set with an unknown content type storing the literal string false is #7034, and res.location() emitting Location: undefined is #6941.

@krzysdz

krzysdz commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Duplicate of #7161

@krzysdz krzysdz marked this as a duplicate of #7161 Sep 28, 2026
@krzysdz krzysdz closed this Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants