Fix handling very large stacks of sync middleware
Before adoption: this commit came in with the history the project adopted, so no landing records it.
closes #4891
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 92c5ce5 · Browse files · 5 files·+56 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+56 added−0 removed
- tests:+38 added−0 removed
- other:+18 added−0 removed
Provenance
- Commit
708ac4c- Tree
3944d6e- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
92c5ce5
5 files changed+56 added−0 removed
modified:lib/router/index.js+8 added
140 unmodified lines141 var idx = 0;142 var protohost = getProtohost(req.url) || ''143 var removed = '';144 var slashAdded = false;145+ var sync = 0146 var paramcalled = {};147 148 // store options for OPTIONS request149 // only used if OPTIONS request53 unmodified lines203 setImmediate(done, layerError);204 return;205 }206 207+ // max sync stack208+ if (++sync > 100) {209+ return setImmediate(next, err)210+ }211+212 // get pathname of request213 var path = getPathname(req);214 215 if (path == null) {110 unmodified lines326 layer.handle_error(layerError, req, res, next);327 } else {328 layer.handle_request(req, res, next);329 }330+331+ sync = 0332 }333 };334 335 /**340 unmodified linesmodified:lib/router/route.js+9 added
96 unmodified lines97 98 Route.prototype.dispatch = function dispatch(req, res, done) {99 var idx = 0;100 var stack = this.stack;101+ var sync = 0102+103 if (stack.length === 0) {104 return done();105 }106 21 unmodified lines128 if (!layer) {129 return done(err);130 }131 132+ // max sync stack133+ if (++sync > 100) {134+ return setImmediate(next, err)135+ }136+137 if (layer.method && layer.method !== method) {138 return next(err);139 }140 1 unmodified line142 layer.handle_error(err, req, res, next);143 } else {144 layer.handle_request(req, res, next);145 }146+147+ sync = 0148 }149 };150 151 /**74 unmodified linesmodified:test/Route.js+22 added
11 unmodified lines12 var route = new Route('/foo')13 route.dispatch(req, {}, done)14 })15 16+ it('should not stack overflow with a large sync stack', function (done) {17+ this.timeout(5000) // long-running test18+19+ var req = { method: 'GET', url: '/' }20+ var route = new Route('/foo')21+22+ for (var i = 0; i < 6000; i++) {23+ route.all(function (req, res, next) { next() })24+ }25+26+ route.get(function (req, res, next) {27+ req.called = true28+ next()29+ })30+31+ route.dispatch(req, {}, function (err) {32+ if (err) return done(err)33+ assert.ok(req.called)34+ done()35+ })36+ })37+38 describe('.all', function(){39 it('should add handler', function(done){40 var req = { method: 'GET', url: '/' };41 var route = new Route('/foo');224 unmodified linesmodified:test/Router.js+16 added
74 unmodified lines75 76 router.handle({ url: '/', method: 'GET' }, { end: done });77 });78 79+ it('should not stack overflow with a large sync stack', function (done) {80+ this.timeout(5000) // long-running test81+82+ var router = new Router()83+84+ for (var i = 0; i < 6000; i++) {85+ router.use(function (req, res, next) { next() })86+ }87+88+ router.use(function (req, res) {89+ res.end()90+ })91+92+ router.handle({ url: '/', method: 'GET' }, { end: done })93+ })94+95 describe('.handle', function(){96 it('should dispatch', function(done){97 var router = new Router();98 479 unmodified linesmodified:History.md+1 added
3 unmodified lines4 * Add "root" option to `res.download`5 * Allow `options` without `filename` in `res.download`6 * Deprecate string and non-integer arguments to `res.status`7 * Fix behavior of `null`/`undefined` as `maxAge` in `res.cookie`8+ * Fix handling very large stacks of sync middleware9 * Ignore `Object.prototype` values in settings through `app.set`/`app.get`10 * Invoke `default` with same arguments as types in `res.format`11 * Support proper 205 responses using `res.send`12 * Use `http-errors` for `res.format` error3562 unmodified lines