Skip to content

Commit

Permalink
Protect req.socket.remoteAddress in appsec reporter (#4954)
Browse files Browse the repository at this point in the history
  • Loading branch information
uurien authored Dec 2, 2024
1 parent ccc13e2 commit 865654c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/dd-trace/src/appsec/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ function reportAttack (attackData) {
newTags['_dd.appsec.json'] = '{"triggers":' + attackData + '}'
}

newTags['network.client.ip'] = req.socket.remoteAddress
if (req.socket) {
newTags['network.client.ip'] = req.socket.remoteAddress
}

rootSpan.addTags(newTags)
}
Expand Down
16 changes: 16 additions & 0 deletions packages/dd-trace/test/appsec/reporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,22 @@ describe('reporter', () => {
storage.disable()
})

it('should add tags to request span when socket is not there', () => {
delete req.socket

const result = Reporter.reportAttack('[{"rule":{},"rule_matches":[{}]}]')

expect(result).to.not.be.false
expect(web.root).to.have.been.calledOnceWith(req)

expect(span.addTags).to.have.been.calledOnceWithExactly({
'appsec.event': 'true',
'_dd.origin': 'appsec',
'_dd.appsec.json': '{"triggers":[{"rule":{},"rule_matches":[{}]}]}'
})
expect(prioritySampler.setPriority).to.have.been.calledOnceWithExactly(span, USER_KEEP, SAMPLING_MECHANISM_APPSEC)
})

it('should add tags to request span', () => {
const result = Reporter.reportAttack('[{"rule":{},"rule_matches":[{}]}]')
expect(result).to.not.be.false
Expand Down

0 comments on commit 865654c

Please sign in to comment.