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

JSONP capability seems to return invalid JS as injected script to HTML #32

Open
dlcurry opened this issue Sep 28, 2011 · 1 comment
Open

Comments

@dlcurry
Copy link

dlcurry commented Sep 28, 2011

Current code in createRouter.js:
var JSONPWRAP = exports.JSONPWRAP = function(namespace, data) {
return 'function ' + namespace + '() {
return "' + data + '"
}';
};

Given a wrapper function name of wrap, returns:
function wrap() {"wrap(){ return "})
where is a well-formed JSON string.

This is unsuitable for HTML script injection, necessary for some solutions that need to work around SOP constraints. While the script is inserted, it cannot be executed because of the quotes around the function body and, possibly, because while the function is created, it is not necessarily called(???).

Recommend the following:
var JSONPWRAP = exports.JSONPWRAP = function(namespace, data) {
return data;
};

with the nominal call of:
callback(null, options.callback + '(' + data + ');');

This returns:
wrap(data);
which IS injectable and verified as a valid, well-formed and executed injection script.

NB: It is possible that the JSONP capability was created to satisfy non-HTML requirements. Perhaps the suggested code can be included as one of 2+ options for serving JSNOP.

@aaaasmile
Copy link

I am using jQuery to call the service with jsonp. To make it working, I have modified the createRouter.js like:
var JSONPWRAP = exports.JSONPWRAP = function(namespace, data) {
var str_p = "" + namespace + '(' + data + ')';
return str_p;
};
the caller with this:
result_p = JSONPWRAP(requestHandler.request.params[0].callback, result);
...
res.send(response.statusCode, {'Content-Type': contentType}, result_p);

In my demoModule I use following:
data = {[1,'b', 'hello']};
callback(null, data);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants