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

Path_params_blocking #3666

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/datadog-instrumentations/src/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,21 @@ addHook({
})

const processParamsStartCh = channel('datadog:express:process_params:start')
const wrapProcessParamsMethod = (requestPositionInArguments) => {
function wrapProcessParamsMethod (requestPositionInArguments) {
return (original) => {
return function () {
if (processParamsStartCh.hasSubscribers) {
processParamsStartCh.publish({ req: arguments[requestPositionInArguments] })
const req = arguments[requestPositionInArguments]
const abortController = new AbortController()

processParamsStartCh.publish({
req,
res: req?.res,
abortController,
params: req?.params
})

if (abortController.signal.aborted) return
}

return original.apply(this, arguments)
Expand Down
1 change: 1 addition & 0 deletions packages/dd-trace/src/appsec/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
graphqlFinishExecute: dc.channel('apm:graphql:execute:finish'),
incomingHttpRequestStart: dc.channel('dd-trace:incomingHttpRequestStart'),
incomingHttpRequestEnd: dc.channel('dd-trace:incomingHttpRequestEnd'),
paramsParser: dc.channel('datadog:express:process_params:start'),
passportVerify: dc.channel('datadog:passport:verify:finish'),
queryParser: dc.channel('datadog:query:read:finish'),
setCookieChannel: dc.channel('datadog:iast:set-cookie'),
Expand Down
16 changes: 16 additions & 0 deletions packages/dd-trace/src/appsec/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
graphqlFinishExecute,
incomingHttpRequestStart,
incomingHttpRequestEnd,
paramsParser,
passportVerify,
queryParser,
nextBodyParsed,
Expand Down Expand Up @@ -49,6 +50,7 @@ function enable (_config) {
nextQueryParsed.subscribe(onRequestQueryParsed)
queryParser.subscribe(onRequestQueryParsed)
cookieParser.subscribe(onRequestCookieParser)
paramsParser.subscribe(onRequestParamsParsed)
graphqlFinishExecute.subscribe(onGraphqlFinishExecute)

if (_config.appsec.eventTracking.enabled) {
Expand Down Expand Up @@ -182,6 +184,19 @@ function onRequestCookieParser ({ req, res, abortController, cookies }) {
handleResults(results, req, res, rootSpan, abortController)
}

function onRequestParamsParsed ({ req, res, abortController, params }) {
const rootSpan = web.root(req)
if (!rootSpan) return

if (!params || typeof params !== 'object' || !Object.keys(params).length) return

const results = waf.run({
[addresses.HTTP_INCOMING_PARAMS]: params
}, req)

handleResults(results, req, res, rootSpan, abortController)
}

function onPassportVerify ({ credentials, user }) {
const store = storage.getStore()
const rootSpan = store && store.req && web.root(store.req)
Expand Down Expand Up @@ -233,6 +248,7 @@ function disable () {
if (incomingHttpRequestEnd.hasSubscribers) incomingHttpRequestEnd.unsubscribe(incomingHttpEndTranslator)
if (queryParser.hasSubscribers) queryParser.unsubscribe(onRequestQueryParsed)
if (cookieParser.hasSubscribers) cookieParser.unsubscribe(onRequestCookieParser)
if (paramsParser.hasSubscribers) paramsParser.unsubscribe(onRequestParamsParsed)
if (passportVerify.hasSubscribers) passportVerify.unsubscribe(onPassportVerify)
}

Expand Down
8 changes: 6 additions & 2 deletions packages/dd-trace/test/appsec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
graphqlFinishExecute,
incomingHttpRequestStart,
incomingHttpRequestEnd,
paramsParser,
queryParser,
passportVerify
} = require('../../src/appsec/channels')
Expand Down Expand Up @@ -125,6 +126,7 @@ describe('AppSec Index', () => {
it('should subscribe to blockable channels', () => {
expect(bodyParser.hasSubscribers).to.be.false
expect(cookieParser.hasSubscribers).to.be.false
expect(paramsParser.hasSubscribers).to.be.false
expect(queryParser.hasSubscribers).to.be.false
expect(passportVerify.hasSubscribers).to.be.false
expect(graphqlFinishExecute.hasSubscribers).to.be.false
Expand All @@ -134,6 +136,7 @@ describe('AppSec Index', () => {
expect(bodyParser.hasSubscribers).to.be.true
expect(cookieParser.hasSubscribers).to.be.true
expect(graphqlFinishExecute.hasSubscribers).to.be.true
expect(paramsParser.hasSubscribers).to.be.true
expect(queryParser.hasSubscribers).to.be.true
expect(passportVerify.hasSubscribers).to.be.true
})
Expand Down Expand Up @@ -196,6 +199,7 @@ describe('AppSec Index', () => {
expect(bodyParser.hasSubscribers).to.be.false
expect(cookieParser.hasSubscribers).to.be.false
expect(graphqlFinishExecute.hasSubscribers).to.be.false
expect(paramsParser.hasSubscribers).to.be.false
expect(queryParser.hasSubscribers).to.be.false
expect(passportVerify.hasSubscribers).to.be.false
})
Expand Down Expand Up @@ -614,7 +618,7 @@ describe('AppSec Index', () => {
})

it('Should not call waf if req is unavailable', () => {
const resolvers = { user: [ { id: '1234' } ] }
const resolvers = { user: [{ id: '1234' }] }
sinon.stub(waf, 'run')
sinon.stub(storage, 'getStore').returns({})

Expand All @@ -626,7 +630,7 @@ describe('AppSec Index', () => {
it('Should call waf if resolvers is well formatted', () => {
const context = {
resolvers: {
user: [ { id: '1234' } ]
user: [{ id: '1234' }]
}
}
const rootSpan = {}
Expand Down
Loading