Skip to content

Commit

Permalink
clean router handle instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyasShabi committed Nov 27, 2024
1 parent 84c05bf commit c44d539
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 21 deletions.
16 changes: 8 additions & 8 deletions packages/datadog-instrumentations/src/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ function wrapResponseRender (render) {
}

addHook({ name: 'express', versions: ['>=4'] }, express => {
shimmer.wrap(express.application, 'handle', wrapHandle)

shimmer.wrap(express.response, 'json', wrapResponseJson)
shimmer.wrap(express.response, 'jsonp', wrapResponseJson)
shimmer.wrap(express.response, 'render', wrapResponseRender)
Expand All @@ -66,30 +68,28 @@ addHook({ name: 'express', versions: ['>=4'] }, express => {
})

addHook({ name: 'express', versions: ['>=4.0.0 <5.0.0'] }, express => {
shimmer.wrap(express.application, 'handle', wrapHandle)
shimmer.wrap(express.Router, 'use', wrapRouterMethod)
shimmer.wrap(express.Router, 'route', wrapRouterMethod)

return express
})

addHook({ name: 'express', versions: ['>=5.0.0'] }, express => {
shimmer.wrap(express.application, 'handle', wrapHandle)
shimmer.wrap(express.Router.prototype, 'use', wrapRouterMethod)
shimmer.wrap(express.Router.prototype, 'route', wrapRouterMethod)

return express
})

const queryReaderReadCh = channel('datadog:query:read:finish')
const queryParserReadCh = channel('datadog:query:read:finish')

function publishQueryParsedAndNext (req, res, next) {
return shimmer.wrapFunction(next, next => function () {
if (queryReaderReadCh.hasSubscribers && req) {
if (queryParserReadCh.hasSubscribers && req) {
const abortController = new AbortController()
const query = req.query

queryReaderReadCh.publish({ req, res, query, abortController })
queryParserReadCh.publish({ req, res, query, abortController })

if (abortController.signal.aborted) return
}
Expand Down Expand Up @@ -146,7 +146,7 @@ addHook({ name: 'express', versions: ['>=4.3.0 <5.0.0'] }, express => {
return express
})

const queryParserReadCh = channel('datadog:query:parse:finish')
const queryReadCh = channel('datadog:express:query:finish')

addHook({ name: 'express', file: ['lib/request.js'], versions: ['>=5.0.0'] }, request => {
const requestDescriptor = Object.getOwnPropertyDescriptor(request, 'query')
Expand All @@ -155,8 +155,8 @@ addHook({ name: 'express', file: ['lib/request.js'], versions: ['>=5.0.0'] }, re
return function wrappedGet () {
const query = originalGet.apply(this, arguments)

if (queryParserReadCh.hasSubscribers && query) {
queryParserReadCh.publish({ query })
if (queryReadCh.hasSubscribers && query) {
queryReadCh.publish({ query })
}

return query
Expand Down
9 changes: 3 additions & 6 deletions packages/datadog-instrumentations/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,8 @@ addHook({ name: 'router', versions: ['>=2'] }, Router => {
return function wrappedMethod (options) {
const router = originalRouter.call(this, options)

if (!router._queryParsingWrapped) {
router._queryParsingWrapped = true
const originalHandle = router.handle

router.handle = function (req, res, next) {
shimmer.wrap(router, 'handle', function wrapHandle (originalHandle) {
return function wrappedHandle (req, res, next) {
const abortController = new AbortController()

if (queryParserReadCh.hasSubscribers && req) {
Expand All @@ -198,7 +195,7 @@ addHook({ name: 'router', versions: ['>=2'] }, Router => {

return originalHandle.apply(this, arguments)
}
}
})

return router
}
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/src/appsec/iast/taint-tracking/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class TaintTrackingPlugin extends SourceIastPlugin {
)

this.addSub(
{ channelName: 'datadog:query:parse:finish', tag: HTTP_REQUEST_PARAMETER },
{ channelName: 'datadog:express:query:finish', tag: HTTP_REQUEST_PARAMETER },
({ query }) => this._taintTrackingHandler(HTTP_REQUEST_PARAMETER, query)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('IAST Taint tracking plugin', () => {
expect(taintTrackingPlugin._subscriptions[0]._channel.name).to.equals('datadog:body-parser:read:finish')
expect(taintTrackingPlugin._subscriptions[1]._channel.name).to.equals('datadog:multer:read:finish')
expect(taintTrackingPlugin._subscriptions[2]._channel.name).to.equals('datadog:query:read:finish')
expect(taintTrackingPlugin._subscriptions[3]._channel.name).to.equals('datadog:query:parse:finish')
expect(taintTrackingPlugin._subscriptions[3]._channel.name).to.equals('datadog:express:query:finish')
expect(taintTrackingPlugin._subscriptions[4]._channel.name).to.equals('apm:express:middleware:next')
expect(taintTrackingPlugin._subscriptions[5]._channel.name).to.equals('datadog:cookie:parse:finish')
expect(taintTrackingPlugin._subscriptions[6]._channel.name).to.equals('datadog:express:process_params:start')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ describe('Path params sourcing with express', () => {
res.status(200).send()
})

app.param('parameter1', checkParamIsTaintedAndNext)
app.param('parameter2', checkParamIsTaintedAndNext)
app.param(['parameter1', 'parameter2'], checkParamIsTaintedAndNext)

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port
Expand All @@ -209,8 +208,7 @@ describe('Path params sourcing with express', () => {
res.status(200).send()
})

app.param('parameter1', checkParamIsTaintedAndNext)
app.param('parameter2', checkParamIsTaintedAndNext)
app.param(['parameter1', 'parameter2'], checkParamIsTaintedAndNext)

appListener = app.listen(0, 'localhost', () => {
const port = appListener.address().port
Expand Down
2 changes: 1 addition & 1 deletion packages/dd-trace/test/plugins/externals.json
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@
},
{
"name": "express",
"versions": [">=4", ">=4.0.0 <4.3.0", ">=4.0.0 <5.0.0", ">=4.3.0 <5.0.0", ">=5.0.0"]
"versions": [">=4", ">=4.0.0 <4.3.0", ">=4.0.0 <5.0.0", ">=4.3.0 <5.0.0", ">=5.0.0"]
},
{
"name": "body-parser",
Expand Down

0 comments on commit c44d539

Please sign in to comment.