Skip to content

Commit

Permalink
Refactor report derivatives
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlesDD committed Oct 8, 2024
1 parent 36fc6cb commit 767e186
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 37 deletions.
30 changes: 7 additions & 23 deletions packages/dd-trace/src/appsec/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function reportAttack (attackData) {
rootSpan.addTags(newTags)
}

function reportSchemas (derivatives) {
function reportDerivatives (derivatives) {
if (!derivatives) return

const req = storage.getStore()?.req
Expand All @@ -162,26 +162,11 @@ function reportSchemas (derivatives) {
if (!rootSpan) return

const tags = {}
for (const [tag, value] of Object.entries(derivatives)) {
if (isFingerprintDerivative(tag)) continue
const gzippedValue = zlib.gzipSync(JSON.stringify(value))
tags[tag] = gzippedValue.toString('base64')
}

rootSpan.addTags(tags)
}

function reportFingerprints (derivatives) {
if (!derivatives) return

const req = storage.getStore()?.req
const rootSpan = web.root(req)

if (!rootSpan) return

const tags = {}
for (const [tag, value] of Object.entries(derivatives)) {
if (!isFingerprintDerivative(tag)) continue
for (let [tag, value] of Object.entries(derivatives)) {
if (!isFingerprintDerivative(tag)) {
const gzippedValue = zlib.gzipSync(JSON.stringify(value))
value = gzippedValue.toString('base64')
}
tags[tag] = value
}

Expand Down Expand Up @@ -270,8 +255,7 @@ module.exports = {
reportMetrics,
reportAttack,
reportWafUpdate: incrementWafUpdatesMetric,
reportSchemas,
reportFingerprints,
reportDerivatives,
finishRequest,
setRateLimit,
mapHeaderAndTags
Expand Down
3 changes: 1 addition & 2 deletions packages/dd-trace/src/appsec/waf/waf_context_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ class WAFContextWrapper {
Reporter.reportAttack(JSON.stringify(result.events))
}

Reporter.reportSchemas(result.derivatives)
Reporter.reportFingerprints(result.derivatives)
Reporter.reportDerivatives(result.derivatives)

if (wafRunFinished.hasSubscribers) {
wafRunFinished.publish({ payload })
Expand Down
17 changes: 9 additions & 8 deletions packages/dd-trace/test/appsec/reporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ describe('reporter', () => {

describe('reportSchemas', () => {
it('should not call addTags if parameter is undefined', () => {
Reporter.reportSchemas(undefined)
Reporter.reportDerivatives(undefined)
expect(span.addTags).not.to.be.called
})

it('should call addTags with an empty array', () => {
Reporter.reportSchemas([])
Reporter.reportDerivatives([])
expect(span.addTags).to.be.calledOnceWithExactly({})
})

Expand All @@ -340,10 +340,11 @@ describe('reporter', () => {
'custom.processor.output': schemaValue
}

Reporter.reportSchemas(derivatives)
Reporter.reportDerivatives(derivatives)

const schemaEncoded = zlib.gzipSync(JSON.stringify(schemaValue)).toString('base64')
expect(span.addTags).to.be.calledOnceWithExactly({
expect(span.addTags).to.be.calledOnce
expect(span.addTags).to.be.calledWithMatch({
'_dd.appsec.s.req.headers': schemaEncoded,
'_dd.appsec.s.req.query': schemaEncoded,
'_dd.appsec.s.req.params': schemaEncoded,
Expand All @@ -356,12 +357,12 @@ describe('reporter', () => {

describe('reportFingerprints', () => {
it('should not call addTags if parameter is undefined', () => {
Reporter.reportFingerprints(undefined)
Reporter.reportDerivatives(undefined)
sinon.assert.notCalled(span.addTags)
})

it('should call addTags with an empty array', () => {
Reporter.reportFingerprints([])
Reporter.reportDerivatives([])
sinon.assert.calledOnceWithExactly(span.addTags, {})
})

Expand All @@ -380,9 +381,9 @@ describe('reporter', () => {
'custom.processor.output': schemaValue
}

Reporter.reportFingerprints(derivatives)
Reporter.reportDerivatives(derivatives)

sinon.assert.calledOnceWithExactly(span.addTags, {
sinon.assert.calledOnceWithMatch(span.addTags, {
'_dd.appsec.fp.http.endpoint': 'endpoint_fingerprint',
'_dd.appsec.fp.http.header': 'header_fingerprint',
'_dd.appsec.fp.http.network': 'network_fingerprint',
Expand Down
7 changes: 3 additions & 4 deletions packages/dd-trace/test/appsec/waf/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ describe('WAF Manager', () => {
sinon.stub(Reporter, 'reportMetrics')
sinon.stub(Reporter, 'reportAttack')
sinon.stub(Reporter, 'reportWafUpdate')
sinon.stub(Reporter, 'reportSchemas')
sinon.stub(Reporter, 'reportFingerprints')
sinon.stub(Reporter, 'reportDerivatives')

webContext = {}
sinon.stub(web, 'getContext').returns(webContext)
Expand Down Expand Up @@ -405,7 +404,7 @@ describe('WAF Manager', () => {
ddwafContext.run.returns(result)

wafContextWrapper.run(params)
expect(Reporter.reportSchemas).to.be.calledOnceWithExactly(result.derivatives)
expect(Reporter.reportDerivatives).to.be.calledOnceWithExactly(result.derivatives)
})

it('should report fingerprints when ddwafContext returns fingerprints in results derivatives', () => {
Expand All @@ -427,7 +426,7 @@ describe('WAF Manager', () => {
'server.request.body': 'foo'
}
})
sinon.assert.calledOnceWithExactly(Reporter.reportFingerprints, result.derivatives)
sinon.assert.calledOnceWithExactly(Reporter.reportDerivatives, result.derivatives)
})
})
})
Expand Down

0 comments on commit 767e186

Please sign in to comment.