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
Provenance
- Commit
7154014- Tree
0ec4e28- Author
Greg Guthe <gguthe@mozilla.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
628438d
4 files changed+76 added−5 removed
modified:lib/response.js+31 added-5 removed
252 unmodified lines253 }254 255 // 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)261 262 // content-type263 if (!this.get('Content-Type')) {264 this.set('Content-Type', 'application/json');31 unmodified lines296 }297 298 // 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')];305 306 // content-type307 if (!this.get('Content-Type')) {791 unmodified lines1099 file.pipe(res);1100 }1101 1102 /**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 */1113 1105-function stringify(value, replacer, spaces) {1114+function 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);1120+1121+ 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+ }1135+1136+ return json1137 }modified:test/res.json.js+22 added
100 unmodified lines101 .expect(200, '{"name":"tobi"}', done)102 })103 })104 105+ 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+ })110+111+ it('should unicode escape HTML-sniffing characters', function (done) {112+ var app = express()113+114+ app.enable('json escape')115+116+ app.use(function (req, res) {117+ res.json({ '&': '<script>' })118+ })119+120+ request(app)121+ .get('/')122+ .expect('Content-Type', 'application/json; charset=utf-8')123+ .expect(200, '{"\\u0026":"\\u003cscript\\u003e"}', done)124+ })125+ })126+127 describe('"json replacer" setting', function(){128 it('should be passed to JSON.stringify()', function(done){129 var app = express();130 83 unmodified linesmodified:test/res.jsonp.js+22 added
240 unmodified lines241 .expect(200, '"str"', done)242 })243 })244 245+ 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+ })250+251+ it('should unicode escape HTML-sniffing characters', function (done) {252+ var app = express()253+254+ app.enable('json escape')255+256+ app.use(function (req, res) {257+ res.jsonp({ '&': '\u2028<script>\u2029' })258+ })259+260+ 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+ })266+267 describe('"json replacer" setting', function(){268 it('should be passed to JSON.stringify()', function(done){269 var app = express();270 97 unmodified linesmodified:History.md+1 added
1 unreleased2 ==========3 4+ * 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 API3337 unmodified lines