-
Notifications
You must be signed in to change notification settings - Fork 341
API Security -Add support for trace tagging rules #6075
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
Open
CarlesDD
wants to merge
5
commits into
master
Choose a base branch
from
ccapell/waf-trace-tagging-rules
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+292
−70
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9fda8b5
Move rate limiter to waf from reporter to match new sampling priority…
CarlesDD 5c4c762
Trace attributes
CarlesDD 7b7c2f2
Add RC capability
CarlesDD a30cdb3
Integration tests
CarlesDD 192de57
Update native-appsec package
CarlesDD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict' | ||
|
||
const { assert } = require('chai') | ||
const path = require('path') | ||
const Axios = require('axios') | ||
|
||
const { | ||
createSandbox, | ||
FakeAgent, | ||
spawnProc | ||
} = require('../helpers') | ||
|
||
describe('ASM Trace Tagging rules', () => { | ||
let axios, sandbox, cwd, appFile, agent, proc | ||
|
||
function startServer () { | ||
beforeEach(async () => { | ||
agent = await new FakeAgent().start() | ||
|
||
const env = { | ||
DD_TRACE_AGENT_PORT: agent.port, | ||
DD_APPSEC_ENABLED: true, | ||
DD_APPSEC_RULES: path.join(cwd, 'appsec', 'data-collection', 'data-collection-rules.json') | ||
} | ||
|
||
proc = await spawnProc(appFile, { cwd, env, execArgv: [] }) | ||
axios = Axios.create({ baseURL: proc.url }) | ||
}) | ||
|
||
afterEach(async () => { | ||
proc.kill() | ||
await agent.stop() | ||
}) | ||
} | ||
|
||
describe('express', () => { | ||
before(async () => { | ||
sandbox = await createSandbox(['express']) | ||
cwd = sandbox.folder | ||
appFile = path.join(cwd, 'appsec/data-collection/index.js') | ||
}) | ||
|
||
after(async () => { | ||
await sandbox.remove() | ||
}) | ||
|
||
startServer() | ||
|
||
it('should report waf attributes', async () => { | ||
await axios.get('/', { headers: { 'User-Agent': 'TraceTaggingTest/v1' } }) | ||
|
||
await agent.assertMessageReceived(({ _, payload }) => { | ||
assert.property(payload[0][0].meta, '_dd.appsec.trace.agent') | ||
assert.strictEqual(payload[0][0].meta['_dd.appsec.trace.agent'], 'TraceTaggingTest/v1') | ||
assert.property(payload[0][0].metrics, '_dd.appsec.trace.integer') | ||
assert.strictEqual(payload[0][0].metrics['_dd.appsec.trace.integer'], 1234) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('fastify', () => { | ||
before(async () => { | ||
sandbox = await createSandbox(['fastify']) | ||
cwd = sandbox.folder | ||
appFile = path.join(cwd, 'appsec/data-collection/fastify.js') | ||
}) | ||
|
||
after(async () => { | ||
await sandbox.remove() | ||
}) | ||
|
||
startServer() | ||
|
||
it('should report waf attributes', async () => { | ||
let fastifyRequestReceived = false | ||
|
||
await axios.get('/', { headers: { 'User-Agent': 'TraceTaggingTest/v1' } }) | ||
|
||
await agent.assertMessageReceived(({ _, payload }) => { | ||
if (payload[0][0].name !== 'fastify.request') { | ||
throw new Error('Not the span we are looking for') | ||
} | ||
|
||
fastifyRequestReceived = true | ||
|
||
assert.property(payload[0][0].meta, '_dd.appsec.trace.agent') | ||
assert.strictEqual(payload[0][0].meta['_dd.appsec.trace.agent'], 'TraceTaggingTest/v1') | ||
assert.property(payload[0][0].metrics, '_dd.appsec.trace.integer') | ||
assert.strictEqual(payload[0][0].metrics['_dd.appsec.trace.integer'], 1234) | ||
}, 30000, 10, true) | ||
|
||
assert.isTrue(fastifyRequestReceived) | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
const dc = require('dc-polyfill') | ||
const zlib = require('zlib') | ||
|
||
const Limiter = require('../rate_limiter') | ||
const { storage } = require('../../../datadog-core') | ||
const web = require('../plugins/util/web') | ||
const { ipHeaderList } = require('../plugins/util/ip_extractor') | ||
|
@@ -15,7 +14,6 @@ const { | |
updateWafRequestsMetricTags, | ||
updateRaspRequestsMetricTags, | ||
updateRaspRuleSkippedMetricTags, | ||
updateRateLimitedMetric, | ||
getRequestMetrics | ||
} = require('./telemetry') | ||
const { keepTrace } = require('../priority_sampler') | ||
|
@@ -31,9 +29,6 @@ const COLLECTED_REQUEST_BODY_MAX_ELEMENTS_PER_NODE = 256 | |
|
||
const telemetryLogCh = dc.channel('datadog:telemetry:log') | ||
|
||
// default limiter, configurable with setRateLimit() | ||
let limiter = new Limiter(100) | ||
|
||
const config = { | ||
headersExtendedCollectionEnabled: false, | ||
maxHeadersCollected: 0, | ||
|
@@ -91,7 +86,6 @@ const NON_EXTENDED_REQUEST_HEADERS = new Set([...requestHeadersList, ...eventHea | |
const NON_EXTENDED_RESPONSE_HEADERS = new Set(contentHeaderList) | ||
|
||
function init (_config) { | ||
limiter = new Limiter(_config.rateLimit) | ||
config.headersExtendedCollectionEnabled = _config.extendedHeadersCollection.enabled | ||
config.maxHeadersCollected = _config.extendedHeadersCollection.maxHeaders | ||
config.headersRedaction = _config.extendedHeadersCollection.redaction | ||
|
@@ -325,12 +319,6 @@ function reportAttack (attackData) { | |
'appsec.event': 'true' | ||
} | ||
|
||
if (limiter.isAllowed()) { | ||
keepTrace(rootSpan, ASM) | ||
} else { | ||
updateRateLimitedMetric(req) | ||
} | ||
|
||
// TODO: maybe add this to format.js later (to take decision as late as possible) | ||
if (!currentTags['_dd.origin']) { | ||
newTags['_dd.origin'] = 'appsec' | ||
|
@@ -430,8 +418,8 @@ function isRaspAttack (events) { | |
return events.some(e => e.rule?.tags?.module === 'rasp') | ||
} | ||
|
||
function isFingerprintAttribute (attribute) { | ||
return attribute.startsWith('_dd.appsec.fp') | ||
function isSchemaAttribute (attribute) { | ||
return attribute.startsWith('_dd.appsec.s.') | ||
} | ||
|
||
function reportAttributes (attributes) { | ||
|
@@ -444,7 +432,7 @@ function reportAttributes (attributes) { | |
|
||
const tags = {} | ||
for (let [tag, value] of Object.entries(attributes)) { | ||
if (!isFingerprintAttribute(tag)) { | ||
if (isSchemaAttribute(tag)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Conditional has been flipped in order to only apply the special format (gizp + base64 encoding) to schema attributes (aka derivatives) |
||
const gzippedValue = zlib.gzipSync(JSON.stringify(value)) | ||
value = gzippedValue.toString('base64') | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sampling priority relies now in the keep field returned by the waf and not in the event presence. Due to this, appsec rate limiter has been moved to waf.