Skip to content
Myr

Remove unnecessary escaping in res.jsonp

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent f684a64 · Browse files · 3 files+25 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+25 added−5 removed

  • tests:+13 added−0 removed
  • other:+12 added−5 removed

3 files+25 added−5 removed

modified:lib/response.js+11 added-5 removed
258 unmodified lines
259 // settings260 var app = this.app;261 var replacer = app.get('json replacer');262 var spaces = app.get('json spaces');263 var body = JSON.stringify(obj, replacer, spaces)264 .replace(/\u2028/g, '\\u2028')265 .replace(/\u2029/g, '\\u2029');263 var body = JSON.stringify(obj, replacer, spaces);264 var callback = this.req.query[app.get('jsonp callback name')];265266 // content-type267 if (!this.get('Content-Type')) {
11 unmodified lines
279 if (typeof callback === 'string' && callback.length !== 0) {280 this.charset = 'utf-8';281 this.set('X-Content-Type-Options', 'nosniff');282 this.set('Content-Type', 'text/javascript');285 var cb = callback.replace(/[^\[\]\w$.]/g, '');283284 // restrict callback charset285 callback = callback.replace(/[^\[\]\w$.]/g, '');286287 // replace chars not allowed in JavaScript that are in JSON288 body = body289 .replace(/\u2028/g, '\\u2028')290 .replace(/\u2029/g, '\\u2029');291292 // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"288 body = '/**/ typeof ' + cb + ' === \'function\' && ' + cb + '(' + body + ');';293 // the typeof check is just to reduce client error noise294 body = '/**/ typeof ' + callback + ' === \'function\' && ' + callback + '(' + body + ');';295 }296297 return this.send(body);298};
529 unmodified lines
modified:test/res.jsonp.js+13 added
96 unmodified lines
97 .expect('Content-Type', 'text/javascript; charset=utf-8')98 .expect(200, /foo\(\{"str":"\\u2028 \\u2029 woot"\}\);/, done);99 });100101 it('should not escape utf whitespace for json fallback', function(done){102 var app = express();103104 app.use(function(req, res){105 res.jsonp({ str: '\u2028 \u2029 woot' });106 });107108 request(app)109 .get('/')110 .expect('Content-Type', 'application/json; charset=utf-8')111 .expect(200, '{"str":"\u2028 \u2029 woot"}', done);112 });113114 it('should include security header and prologue', function (done) {115 var app = express();116117 app.use(function(req, res){
237 unmodified lines
modified:History.md+1 added
3 unmodified lines
4 * add explicit "Rosetta Flash JSONP abuse" protection5 - previous versions are not vulnerable; this is just explicit protection6 * deprecate `res.redirect(url, status)` -- use `res.redirect(status, url)` instead7 * fix `res.send(status, num)` to send `num` as json (not error)8 * remove unnecessary escaping when `res.jsonp` returns JSON response9 * deps: basic-auth@1.0.010 - support empty password11 - support empty username12 * deps: connect@2.23.0
1520 unmodified lines