Skip to content

Commit 865654c

Browse files
authored
Protect req.socket.remoteAddress in appsec reporter (#4954)
1 parent ccc13e2 commit 865654c

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

packages/dd-trace/src/appsec/reporter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ function reportAttack (attackData) {
148148
newTags['_dd.appsec.json'] = '{"triggers":' + attackData + '}'
149149
}
150150

151-
newTags['network.client.ip'] = req.socket.remoteAddress
151+
if (req.socket) {
152+
newTags['network.client.ip'] = req.socket.remoteAddress
153+
}
152154

153155
rootSpan.addTags(newTags)
154156
}

packages/dd-trace/test/appsec/reporter.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ describe('reporter', () => {
223223
storage.disable()
224224
})
225225

226+
it('should add tags to request span when socket is not there', () => {
227+
delete req.socket
228+
229+
const result = Reporter.reportAttack('[{"rule":{},"rule_matches":[{}]}]')
230+
231+
expect(result).to.not.be.false
232+
expect(web.root).to.have.been.calledOnceWith(req)
233+
234+
expect(span.addTags).to.have.been.calledOnceWithExactly({
235+
'appsec.event': 'true',
236+
'_dd.origin': 'appsec',
237+
'_dd.appsec.json': '{"triggers":[{"rule":{},"rule_matches":[{}]}]}'
238+
})
239+
expect(prioritySampler.setPriority).to.have.been.calledOnceWithExactly(span, USER_KEEP, SAMPLING_MECHANISM_APPSEC)
240+
})
241+
226242
it('should add tags to request span', () => {
227243
const result = Reporter.reportAttack('[{"rule":{},"rule_matches":[{}]}]')
228244
expect(result).to.not.be.false

0 commit comments

Comments
 (0)