Skip to content
Myr

Add "escape json" setting for res.json and res.jsonp

Before adoption: this commit came in with the history the project adopted, so no landing records it.

closes #3268 closes #3269

authored by Greg Guthe · committed by Douglas Christopher Wilson · parent 628438d · Browse files · 4 files+76 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+76 added−5 removed

  • tests:+44 added−0 removed
  • other:+32 added−5 removed

4 files+76 added−5 removed

modified:lib/response.js+31 added-5 removed
252 unmodified lines
253 }254255 // settings256 var app = this.app;257 var escape = app.get('json escape')258 var replacer = app.get('json replacer');259 var spaces = app.get('json spaces');259 var body = stringify(val, replacer, spaces);260 var body = stringify(val, replacer, spaces, escape)261262 // content-type263 if (!this.get('Content-Type')) {264 this.set('Content-Type', 'application/json');
31 unmodified lines
296 }297298 // settings299 var app = this.app;300 var escape = app.get('json escape')301 var replacer = app.get('json replacer');302 var spaces = app.get('json spaces');301 var body = stringify(val, replacer, spaces);303 var body = stringify(val, replacer, spaces, escape)304 var callback = this.req.query[app.get('jsonp callback name')];305306 // content-type307 if (!this.get('Content-Type')) {
791 unmodified lines
1099 file.pipe(res);1100}11011102/**1101 * Stringify JSON, like JSON.stringify, but v8 optimized.1103 * Stringify JSON, like JSON.stringify, but v8 optimized, with the1104 * ability to escape characters that can trigger HTML sniffing.1105 *1106 * @param {*} value1107 * @param {function} replaces1108 * @param {number} spaces1109 * @param {boolean} escape1110 * @returns {string}1111 * @private1112 */11131105function stringify(value, replacer, spaces) {1114function stringify (value, replacer, spaces, escape) {1115 // v8 checks arguments.length for optimizing simple call1116 // https://bugs.chromium.org/p/v8/issues/detail?id=47301108 return replacer || spaces1117 var json = replacer || spaces1118 ? JSON.stringify(value, replacer, spaces)1119 : JSON.stringify(value);11201121 if (escape) {1122 json = json.replace(/[<>&]/g, function (c) {1123 switch (c.charCodeAt(0)) {1124 case 0x3c:1125 return '\\u003c'1126 case 0x3e:1127 return '\\u003e'1128 case 0x26:1129 return '\\u0026'1130 default:1131 return c1132 }1133 })1134 }11351136 return json1137}
modified:test/res.json.js+22 added
100 unmodified lines
101 .expect(200, '{"name":"tobi"}', done)102 })103 })104105 describe('"json escape" setting', function () {106 it('should be undefined by default', function () {107 var app = express()108 assert.strictEqual(app.get('json escape'), undefined)109 })110111 it('should unicode escape HTML-sniffing characters', function (done) {112 var app = express()113114 app.enable('json escape')115116 app.use(function (req, res) {117 res.json({ '&': '<script>' })118 })119120 request(app)121 .get('/')122 .expect('Content-Type', 'application/json; charset=utf-8')123 .expect(200, '{"\\u0026":"\\u003cscript\\u003e"}', done)124 })125 })126127 describe('"json replacer" setting', function(){128 it('should be passed to JSON.stringify()', function(done){129 var app = express();130
83 unmodified lines
modified:test/res.jsonp.js+22 added
240 unmodified lines
241 .expect(200, '"str"', done)242 })243 })244245 describe('"json escape" setting', function () {246 it('should be undefined by default', function () {247 var app = express()248 assert.strictEqual(app.get('json escape'), undefined)249 })250251 it('should unicode escape HTML-sniffing characters', function (done) {252 var app = express()253254 app.enable('json escape')255256 app.use(function (req, res) {257 res.jsonp({ '&': '\u2028<script>\u2029' })258 })259260 request(app)261 .get('/?callback=foo')262 .expect('Content-Type', 'text/javascript; charset=utf-8')263 .expect(200, /foo\({"\\u0026":"\\u2028\\u003cscript\\u003e\\u2029"}\)/, done)264 })265 })266267 describe('"json replacer" setting', function(){268 it('should be passed to JSON.stringify()', function(done){269 var app = express();270
97 unmodified lines
modified:History.md+1 added
1unreleased2==========34 * Add `"json escape"` setting for `res.json` and `res.jsonp`5 * Improve error message when autoloading invalid view engine6 * Improve error messages when non-function provided as middleware7 * Skip `Buffer` encoding when not generating ETag for small response8 * Use `safe-buffer` for improved Buffer API
3337 unmodified lines