Skip to content

Commit 015a722

Browse files
authored
fix AppSec SDK not triggering twice in a row (#5115)
1 parent 3b8a6b9 commit 015a722

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

packages/dd-trace/src/appsec/waf/waf_context_wrapper.js

+37
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class WAFContextWrapper {
1919
this.rulesVersion = rulesVersion
2020
this.addressesToSkip = new Set()
2121
this.knownAddresses = knownAddresses
22+
this.cachedUserIdActions = new Map()
2223
}
2324

2425
run ({ persistent, ephemeral }, raspRule) {
@@ -27,6 +28,16 @@ class WAFContextWrapper {
2728
return
2829
}
2930

31+
// SPECIAL CASE FOR USER_ID
32+
// TODO: make this universal
33+
const userId = persistent?.[addresses.USER_ID] || ephemeral?.[addresses.USER_ID]
34+
if (userId) {
35+
const cachedAction = this.cachedUserIdActions.get(userId)
36+
if (cachedAction) {
37+
return cachedAction
38+
}
39+
}
40+
3041
const payload = {}
3142
let payloadHasData = false
3243
const newAddressesToSkip = new Set(this.addressesToSkip)
@@ -79,6 +90,12 @@ class WAFContextWrapper {
7990

8091
const blockTriggered = !!getBlockingAction(result.actions)
8192

93+
// SPECIAL CASE FOR USER_ID
94+
// TODO: make this universal
95+
if (userId && ruleTriggered && blockTriggered) {
96+
this.setUserIdCache(userId, result)
97+
}
98+
8299
Reporter.reportMetrics({
83100
duration: result.totalRuntime / 1e3,
84101
durationExt: parseInt(end - start) / 1e3,
@@ -105,6 +122,26 @@ class WAFContextWrapper {
105122
}
106123
}
107124

125+
setUserIdCache (userId, result) {
126+
// using old loops for speed
127+
for (let i = 0; i < result.events.length; i++) {
128+
const event = result.events[i]
129+
130+
for (let j = 0; j < event?.rule_matches?.length; j++) {
131+
const match = event.rule_matches[j]
132+
133+
for (let k = 0; k < match?.parameters?.length; k++) {
134+
const parameter = match.parameters[k]
135+
136+
if (parameter?.address === addresses.USER_ID) {
137+
this.cachedUserIdActions.set(userId, result.actions)
138+
return
139+
}
140+
}
141+
}
142+
}
143+
}
144+
108145
dispose () {
109146
this.ddwafContext.dispose()
110147
}

packages/dd-trace/test/appsec/sdk/user_blocking.spec.js

+13
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,19 @@ describe('user_blocking', () => {
227227
}).then(done).catch(done)
228228
axios.get(`http://localhost:${port}/`)
229229
})
230+
231+
it('should return true action if userID was matched before with trackUserLoginSuccessEvent()', (done) => {
232+
controller = (req, res) => {
233+
tracer.appsec.trackUserLoginSuccessEvent({ id: 'blockedUser' })
234+
const ret = tracer.appsec.isUserBlocked({ id: 'blockedUser' })
235+
expect(ret).to.be.true
236+
res.end()
237+
}
238+
agent.use(traces => {
239+
expect(traces[0][0].meta).to.have.property('usr.id', 'blockedUser')
240+
}).then(done).catch(done)
241+
axios.get(`http://localhost:${port}/`)
242+
})
230243
})
231244

232245
describe('blockRequest', () => {

0 commit comments

Comments
 (0)