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

improve express regex middleware path parsing #3203

Merged
merged 4 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 74 additions & 0 deletions packages/datadog-plugin-express/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,80 @@ describe('Plugin', () => {
})
})

it('long regex should not steal path', done => {
const app = express()

try {
app.use(/\/foo\/(bar|baz|bez)/, (req, res, next) => {
next()
})
} catch (err) {
// eslint-disable-next-line no-console
console.log('This version of Express (>4.0 <4.6) has broken support for regex routing. Skipping this test.')
this.skip && this.skip() // mocha allows dynamic skipping, tap does not
return done()
}

app.get('/foo/bar', (req, res) => {
res.status(200).send('')
})

getPort().then(port => {
agent
.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('resource', 'GET /foo/bar')
})
.then(done)
.catch(done)

appListener = app.listen(port, 'localhost', () => {
axios
.get(`http://localhost:${port}/foo/bar`)
.catch(done)
})
})
})

it('long regex child of string router should not steal path', done => {
const app = express()
const router = express.Router()

try {
router.use(/\/(bar|baz|bez)/, (req, res, next) => {
next()
})
app.use('/foo', router)
} catch (err) {
// eslint-disable-next-line no-console
console.log('This version of Express (>4.0 <4.6) has broken support for regex routing. Skipping this test.')
this.skip && this.skip() // mocha allows dynamic skipping, tap does not
return done()
}

app.get('/foo/bar', (req, res) => {
res.status(200).send('')
})

getPort().then(port => {
agent
.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('resource', 'GET /foo/bar')
})
.then(done)
.catch(done)

appListener = app.listen(port, 'localhost', () => {
axios
.get(`http://localhost:${port}/foo/bar`)
.catch(done)
})
})
})

it('should not lose the current path on next', done => {
const app = express()
const Router = express.Router
Expand Down
13 changes: 12 additions & 1 deletion packages/datadog-plugin-router/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class RouterPlugin extends WebPlugin {
route = context.stack.join('')

// Longer route is more likely to be the actual route handler route.
if (route.length > context.route.length) {
if (isMoreSpecificThan(route, context.route)) {
context.route = route
}
} else {
Expand All @@ -148,4 +148,15 @@ class RouterPlugin extends WebPlugin {
}
}

function isMoreSpecificThan (routeA, routeB) {
if (!routeIsRegex(routeA) && routeIsRegex(routeB)) {
return true
}
return routeA.length > routeB.length
}

function routeIsRegex (route) {
return route.includes('(/') && route.includes('/)')
Copy link
Member Author

Choose a reason for hiding this comment

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

@rochdev I replaced the .startsWith and .endsWith with .includes. You were right that having a parent router with a path and a child handler being regex was sneaking through.

Copy link
Member

Choose a reason for hiding this comment

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

I think for now this could be sufficient, although I wonder if maybe checking just endsWith would be enough? Then it would only match if the last nested route was a regex. Not sure whether that's actually better though 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting... What if a parent route is a regex and a child is a path?

Copy link
Member Author

Choose a reason for hiding this comment

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

I do agree that a single .endsWith would be a lot faster than two or even one .includes.

}

module.exports = RouterPlugin