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

fix AppSec SDK not triggering twice in a row #5115

Merged
merged 5 commits into from
Jan 17, 2025
Merged
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
37 changes: 37 additions & 0 deletions packages/dd-trace/src/appsec/waf/waf_context_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class WAFContextWrapper {
this.rulesVersion = rulesVersion
this.addressesToSkip = new Set()
this.knownAddresses = knownAddresses
this.cachedUserIdActions = new Map()
}

run ({ persistent, ephemeral }, raspRule) {
Expand All @@ -27,6 +28,16 @@ class WAFContextWrapper {
return
}

// SPECIAL CASE FOR USER_ID
// TODO: make this universal
const userId = persistent?.[addresses.USER_ID] || ephemeral?.[addresses.USER_ID]
if (userId) {
const cachedAction = this.cachedUserIdActions.get(userId)
if (cachedAction) {
return cachedAction
}
}

const payload = {}
let payloadHasData = false
const newAddressesToSkip = new Set(this.addressesToSkip)
Expand Down Expand Up @@ -79,6 +90,12 @@ class WAFContextWrapper {

const blockTriggered = !!getBlockingAction(result.actions)

// SPECIAL CASE FOR USER_ID
// TODO: make this universal
if (userId && ruleTriggered && blockTriggered) {
this.setUserIdCache(userId, result)
}

Reporter.reportMetrics({
duration: result.totalRuntime / 1e3,
durationExt: parseInt(end - start) / 1e3,
Expand All @@ -105,6 +122,26 @@ class WAFContextWrapper {
}
}

setUserIdCache (userId, result) {
// using old loops for speed
for (let i = 0; i < result.events.length; i++) {
const event = result.events[i]

for (let j = 0; j < event?.rule_matches?.length; j++) {
const match = event.rule_matches[j]

for (let k = 0; k < match?.parameters?.length; k++) {
const parameter = match.parameters[k]

if (parameter?.address === addresses.USER_ID) {
this.cachedUserIdActions.set(userId, result.actions)
return
}
}
}
}
}

dispose () {
this.ddwafContext.dispose()
}
Expand Down
13 changes: 13 additions & 0 deletions packages/dd-trace/test/appsec/sdk/user_blocking.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,19 @@ describe('user_blocking', () => {
}).then(done).catch(done)
axios.get(`http://localhost:${port}/`)
})

it('should return true action if userID was matched before with trackUserLoginSuccessEvent()', (done) => {
controller = (req, res) => {
tracer.appsec.trackUserLoginSuccessEvent({ id: 'blockedUser' })
const ret = tracer.appsec.isUserBlocked({ id: 'blockedUser' })
expect(ret).to.be.true
res.end()
}
agent.use(traces => {
expect(traces[0][0].meta).to.have.property('usr.id', 'blockedUser')
}).then(done).catch(done)
axios.get(`http://localhost:${port}/`)
})
})

describe('blockRequest', () => {
Expand Down
Loading