Skip to content
Myr

Add explicit "Rosetta Flash JSONP abuse" protection

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 5d03d0e · Browse files · 3 files+46 added−36 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+46 added−36 removed

  • tests:+33 added−32 removed
  • other:+13 added−4 removed

3 files+46 added−36 removed

modified:lib/response.js+11 added-4 removed
264 unmodified lines
265 .replace(/\u2029/g, '\\u2029');266 var callback = this.req.query[app.get('jsonp callback name')];267268 // content-type269 this.charset = this.charset || 'utf-8';270 this.get('Content-Type') || this.set('Content-Type', 'application/json');269 if (!this.get('Content-Type')) {270 this.charset = 'utf-8';271 this.set('X-Content-Type-Options', 'nosniff');272 this.set('Content-Type', 'application/json');273 }274275 // fixup callback276 if (Array.isArray(callback)) {277 callback = callback[0];278 }279280 // jsonp278 if (callback && 'string' === typeof callback) {281 if (typeof callback === 'string' && callback.length !== 0) {282 this.charset = 'utf-8';283 this.set('X-Content-Type-Options', 'nosniff');284 this.set('Content-Type', 'text/javascript');285 var cb = callback.replace(/[^\[\]\w$.]/g, '');286287 // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"281 body = 'typeof ' + cb + ' === \'function\' && ' + cb + '(' + body + ');';288 body = '/**/ typeof ' + cb + ' === \'function\' && ' + cb + '(' + body + ');';289 }290291 return this.send(body);292};
529 unmodified lines
modified:test/res.jsonp.js+33 added-32 removed
12 unmodified lines
13 });1415 request(app)16 .get('/?callback=something')17 .end(function(err, res){18 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');19 res.text.should.equal('typeof something === \'function\' && something({"count":1});');20 done();21 })17 .expect('Content-Type', 'text/javascript; charset=utf-8')18 .expect(200, /something\(\{"count":1\}\);/, done);19 })2021 it('should use first callback parameter with jsonp', function(done){22 var app = express();
2 unmodified lines
25 res.jsonp({ count: 1 });26 });2728 request(app)32 .get('/?callback=something&callback=somethingelse')33 .end(function(err, res){34 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');35 res.text.should.equal('typeof something === \'function\' && something({"count":1});');36 done();37 })29 .get('/?callback=something&callback=somethingelse')30 .expect('Content-Type', 'text/javascript; charset=utf-8')31 .expect(200, /something\(\{"count":1\}\);/, done);32 })3334 it('should ignore object callback parameter with jsonp', function(done){35 var app = express();
18 unmodified lines
54 });5556 request(app)57 .get('/?clb=something')64 .end(function(err, res){65 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');66 res.text.should.equal('typeof something === \'function\' && something({"count":1});');67 done();68 })58 .expect('Content-Type', 'text/javascript; charset=utf-8')59 .expect(200, /something\(\{"count":1\}\);/, done);60 })6162 it('should allow []', function(done){63 var app = express();
3 unmodified lines
67 });6869 request(app)70 .get('/?callback=callbacks[123]')80 .end(function(err, res){81 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');82 res.text.should.equal('typeof callbacks[123] === \'function\' && callbacks[123]({"count":1});');83 done();84 })71 .expect('Content-Type', 'text/javascript; charset=utf-8')72 .expect(200, /callbacks\[123\]\(\{"count":1\}\);/, done);73 })7475 it('should disallow arbitrary js', function(done){76 var app = express();
3 unmodified lines
80 });8182 request(app)83 .get('/?callback=foo;bar()')96 .end(function(err, res){97 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');98 res.text.should.equal('typeof foobar === \'function\' && foobar({});');99 done();100 })84 .expect('Content-Type', 'text/javascript; charset=utf-8')85 .expect(200, /foobar\(\{\}\);/, done);86 })8788 it('should escape utf whitespace', function(done){89 var app = express();
3 unmodified lines
93 });9495 request(app)96 .get('/?callback=foo')112 .end(function(err, res){113 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');114 res.text.should.equal('typeof foo === \'function\' && foo({"str":"\\u2028 \\u2029 woot"});');115 done();116 });97 .expect('Content-Type', 'text/javascript; charset=utf-8')98 .expect(200, /foo\(\{"str":"\\u2028 \\u2029 woot"\}\);/, done);99 });100101 it('should include security header and prologue', function (done) {102 var app = express();103104 app.use(function(req, res){105 res.jsonp({ count: 1 });106 });107108 request(app)109 .get('/?callback=something')110 .expect('Content-Type', 'text/javascript; charset=utf-8')111 .expect('X-Content-Type-Options', 'nosniff')112 .expect(200, /^\/\*\*\//, done);113 })114115 it('should not override previous Content-Types with no callback', function(done){116 var app = express();117118 app.get('/', function(req, res){
3 unmodified lines
122123 request(app)124 .get('/')125 .expect('Content-Type', 'application/vnd.example+json; charset=utf-8')130 .expect(200, '{"hello":"world"}', done);126 .expect(200, '{"hello":"world"}', function (err, res) {127 if (err) return done(err);128 res.headers.should.not.have.property('x-content-type-options');129 done();130 });131 })132133 it('should override previous Content-Types with callback', function(done){134 var app = express();
5 unmodified lines
140141 request(app)142 .get('/?callback=cb')143 .expect('Content-Type', 'text/javascript; charset=utf-8')144 .expect('X-Content-Type-Options', 'nosniff')145 .expect(200, /cb\(\{"hello":"world"\}\);$/, done);146 })147148 describe('when given primitives', function(){
193 unmodified lines
modified:History.md+2 added
13.x2===34 * 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 * deps: basic-auth@1.0.09 - support empty password
1522 unmodified lines