Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added typeof check to the jsonp callback #729

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/formatters/jsonp.js
Expand Up @@ -25,7 +25,7 @@ function formatJSONP(req, res, body) {
var cb = req.query.callback || req.query.jsonp;
var data;
if (cb) {
data = cb + '(' + JSON.stringify(body) + ');';
data = 'typeof ' + cb + ' === \'function\' && ' + cb + '(' + JSON.stringify(body) + ');';
} else {
data = JSON.stringify(body);
}
Expand Down
12 changes: 12 additions & 0 deletions test/client.test.js
Expand Up @@ -100,6 +100,7 @@ before(function (callback) {
});

SERVER.use(restify.acceptParser(['json', 'text/plain']));
SERVER.use(restify.jsonp()); // Added for GH-729
SERVER.use(restify.dateParser());
SERVER.use(restify.authorizationParser());
SERVER.use(restify.queryParser());
Expand Down Expand Up @@ -207,6 +208,17 @@ test('GH-388 GET json, but really HTML', function (t) {
});


test('GH-729 GET jsonp', function (t) {
JSON_CLIENT.get('/json/jsonp?callback=testCallback', function (err, req, res) {
t.ifError(err);
t.ok(req);
t.ok(res);
t.equal(res.body, 'typeof testCallback === \'function\' && testCallback({"hello":"jsonp"});');
t.end();
});
});


test('GH-115 GET path with spaces', function (t) {
// As of node v0.11, this throws, since it's never valid HTTP
try {
Expand Down