Fix incorrect middleware execution with unanchored RegExps

fixes #4204
closes #4205
This commit is contained in:
Ivan Derevianko 2020-03-05 12:00:08 +01:00 committed by Douglas Christopher Wilson
parent bdc2c973a4
commit abb7793ae0
3 changed files with 36 additions and 0 deletions

View File

@ -1,6 +1,7 @@
unreleased
==========
* Fix incorrect middleware execution with unanchored `RegExp`s
* Fix `res.jsonp(obj, status)` deprecation message
* Fix typo in `res.is` JSDoc

View File

@ -287,6 +287,12 @@ proto.handle = function handle(req, res, out) {
function trim_prefix(layer, layerError, layerPath, path) {
if (layerPath.length !== 0) {
// Validate path is a prefix match
if (layerPath !== path.substr(0, layerPath.length)) {
next(layerError)
return
}
// Validate path breaks on a path separator
var c = path[layerPath.length]
if (c && c !== '/' && c !== '.') return next(layerError)

View File

@ -186,6 +186,35 @@ describe('app.router', function(){
.get('/user/10/edit')
.expect('editing user 10', done);
})
it('should ensure regexp matches path prefix', function (done) {
var app = express()
var p = []
app.use(/\/api.*/, function (req, res, next) {
p.push('a')
next()
})
app.use(/api/, function (req, res, next) {
p.push('b')
next()
})
app.use(/\/test/, function (req, res, next) {
p.push('c')
next()
})
app.use(function (req, res) {
res.end()
})
request(app)
.get('/test/api/1234')
.expect(200, function (err) {
if (err) return done(err)
assert.deepEqual(p, ['c'])
done()
})
})
})
describe('case sensitivity', function(){