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

Add 'json escape' option to unicode escape <, >, and & #3269

Closed
wants to merge 8 commits into from

Conversation

g-k
Copy link
Contributor

@g-k g-k commented Apr 5, 2017

refs: #3268

Copy link
Contributor

@dougwilson dougwilson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I am reviewing this with the assumption that #3268 will result in the desire to add this feature, but you can wait on that discussion to fix the issue if you like :)

I would expect that res.jsonp would respect this option, if you can fix that (i.e. I would expect anywhere in Express that respects json replacer, json spaces, etc. to also honor this new json escape).

@g-k
Copy link
Contributor Author

g-k commented Apr 12, 2017

Added the setting to jsonp and git grep doesn't show the json options used elsewhere.

Updated to use a single String.replace call that a similar change to hapijs/hoek showed as ~3x faster on the twitter.json test data.

@dougwilson
Copy link
Contributor

Unless there are any objections, I'm suggesting that this land in the 4.16.0 release.

Copy link
Contributor

@dougwilson dougwilson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final comments added. Ideally all changed code adheres to the StandardJS style, but I can alter your changes on landing if you don't want to change that up.

lib/response.js Outdated
@@ -248,6 +248,18 @@ res.json = function json(obj) {
var spaces = app.get('json spaces');
var body = stringify(val, replacer, spaces);

// escape characters that can cause old browsers to mistake json for html
if (app.get('json escape')) {
body = body.replace(/[<>&]/g, function (match) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please hoist regular expressions to the top of the file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also since the function close not close over anything, please hoist that as well.

lib/response.js Outdated

// escape characters that can cause old browsers to mistake json for html
if (app.get('json escape')) {
body = body.replace(/[<>&\u2028\u2029]/g, function (match) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as the other replace: hoist regular expression and function.

test/res.json.js Outdated
describe('"json escape" setting', function(){
it('should be undefined by default', function(){
var app = express();
assert(undefined === app.get('json escape'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would really just say it's off by default and run through a response to validate it doesn't get escaped. That would help in case a change accidentally unconditionally escapes it.

@@ -112,6 +112,21 @@ describe('res', function(){
.expect(200, '{"str":"\u2028 \u2029 woot"}', done);
});

it('should escape <, >, and & when "json escape" is true', function(done){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is missing a default off test, like the res.json one :)

@g-k
Copy link
Contributor Author

g-k commented Jul 20, 2017

Thanks @dougwilson! I think I got all of the requested changes, but please modify the PR as necessary to get it in shape too.

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

Successfully merging this pull request may close these issues.

None yet

2 participants