Skip to content

Commit

Permalink
fix jsonp callback char restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Aug 29, 2012
1 parent 2bba69f commit 89c5aff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/response.js
Expand Up @@ -196,7 +196,7 @@ res.json = function(obj){
// jsonp
if (callback && jsonp) {
this.set('Content-Type', 'text/javascript');
body = callback.replace(/[^[]\w$.]/g, '') + '(' + body + ');';
body = callback.replace(/[^\[\]\w$.]/g, '') + '(' + body + ');';
}

return this.send(body);
Expand Down
17 changes: 17 additions & 0 deletions test/res.json.js
Expand Up @@ -59,6 +59,23 @@ describe('res', function(){
done();
})
})

it('should disallow arbitrary js', function(done){
var app = express();

app.enable('jsonp callback');
app.use(function(req, res){
res.json({});
});

request(app)
.get('/?callback=foo;bar()')
.end(function(err, res){
res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');
res.text.should.equal('foobar({});');
done();
})
})
})

describe('when given primitives', function(){
Expand Down

0 comments on commit 89c5aff

Please sign in to comment.