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
Provenance
- Commit
f684a64- Tree
09aa20a- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
5d03d0e
3 files changed+46 added−36 removed
modified:lib/response.js+11 added-4 removed
264 unmodified lines265 .replace(/\u2029/g, '\\u2029');266 var callback = this.req.query[app.get('jsonp callback name')];267 268 // 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+ }274 275 // fixup callback276 if (Array.isArray(callback)) {277 callback = callback[0];278 }279 280 // 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, '');286+287+ // 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 }290 291 return this.send(body);292 };529 unmodified linesmodified:test/res.jsonp.js+33 added-32 removed
12 unmodified lines13 });14 15 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 })20 21 it('should use first callback parameter with jsonp', function(done){22 var app = express();2 unmodified lines25 res.jsonp({ count: 1 });26 });27 28 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 })33 34 it('should ignore object callback parameter with jsonp', function(done){35 var app = express();18 unmodified lines54 });55 56 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 })61 62 it('should allow []', function(done){63 var app = express();3 unmodified lines67 });68 69 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 })74 75 it('should disallow arbitrary js', function(done){76 var app = express();3 unmodified lines80 });81 82 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 })87 88 it('should escape utf whitespace', function(done){89 var app = express();3 unmodified lines93 });94 95 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 });100 101+ it('should include security header and prologue', function (done) {102+ var app = express();103+104+ app.use(function(req, res){105+ res.jsonp({ count: 1 });106+ });107+108+ 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+ })114+115 it('should not override previous Content-Types with no callback', function(done){116 var app = express();117 118 app.get('/', function(req, res){3 unmodified lines122 123 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 })132 133 it('should override previous Content-Types with callback', function(done){134 var app = express();5 unmodified lines140 141 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 })147 148 describe('when given primitives', function(){193 unmodified linesmodified:History.md+2 added
1 3.x2 ===3 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 * deps: basic-auth@1.0.09 - support empty password1522 unmodified lines