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

WIP api security sampling #4766

Draft
wants to merge 7 commits into
base: api-security-sampling
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/datadog-instrumentations/src/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function wrapResponseJson (json) {
obj = arguments[1]
}

responseJsonChannel.publish({ req: this.req, res: this.res, body: obj })
responseJsonChannel.publish({ req: this.req, res: this, body: obj })
}

return json.apply(this, arguments)
Expand Down
23 changes: 9 additions & 14 deletions packages/dd-trace/src/appsec/api_security_sampler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@

const crypto = require('node:crypto')
const LRUCache = require('lru-cache')
const PrioritySampler = require('../priority_sampler')
const web = require('../plugins/util/web')
const log = require('../log')
const { USER_KEEP, AUTO_KEEP, AUTO_REJECT, USER_REJECT } = require('../../../../ext/priority')
const { AUTO_REJECT, USER_REJECT } = require('../../../../ext/priority')

const MAX_SIZE = 4096
const DEFAULT_DELAY = 30 // 30s

let enabled
let sampledRequests
let prioritySampler

function configure ({ apiSecurity }) {
enabled = apiSecurity.enabled
const ttl = parseSampleDelay(apiSecurity.sampleDelay) * 1000
sampledRequests = new LRUCache({ max: MAX_SIZE, ttl })
prioritySampler = new PrioritySampler()
}

function disable () {
enabled = false
sampledRequests?.clear()
}

function sampleRequest (req, res) {
if (!enabled) return false
function sampleRequest (req, res, forceSample) {
if (!enabled || this.isSampled(req, res)) return false

const rootSpan = web.root(req)
if (!rootSpan) return false
Expand All @@ -38,17 +35,15 @@ function sampleRequest (req, res) {
return false
}

if (priority === AUTO_KEEP || priority === USER_KEEP) {
return sample(req, res)
if (!priority && !rootSpan._prioritySampler?.isSampled(rootSpan)) {
return false
}

const isSampled = prioritySampler.isSampled(rootSpan)

if (!isSampled) {
return false
if (forceSample) {
sample(req, res)
}

return sample(req, res)
return true
}

function sample (req, res) {
Expand All @@ -70,7 +65,7 @@ function isSampled (req, res) {
function computeKey (req, res) {
const route = req.route?.path || req.url
const method = req.method.toLowerCase()
const statusCode = res.statusCode
const statusCode = res.statusCode === 304 ? 200 : res.statusCode
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is funny because sometimes, for the same request, status code is 200 in onResponseBody but 304 in incomingHttpEndTranslator. It depends on when you are trying to get it.

const str = route + statusCode + method
return crypto.createHash('md5').update(str).digest('hex')
}
Expand Down
4 changes: 2 additions & 2 deletions packages/dd-trace/src/appsec/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function incomingHttpEndTranslator ({ req, res }) {
persistent[addresses.HTTP_INCOMING_QUERY] = req.query
}

if (apiSecuritySampler.sampleRequest(req, res)) {
if (apiSecuritySampler.sampleRequest(req, res, true)) {
persistent[addresses.WAF_CONTEXT_PROCESSOR] = { 'extract-schema': true }
}

Expand Down Expand Up @@ -202,7 +202,7 @@ function onRequestCookieParser ({ req, res, abortController, cookies }) {

function onResponseBody ({ req, res, body }) {
if (!body || typeof body !== 'object') return
if (!apiSecuritySampler.isSampled(req, res)) return
if (!apiSecuritySampler.sampleRequest(req, res)) return

// we don't support blocking at this point, so no results needed
waf.run({
Expand Down