Skip to content

Commit de09037

Browse files
committed
call site frames
1 parent 0a9cc8d commit de09037

File tree

6 files changed

+180
-148
lines changed

6 files changed

+180
-148
lines changed

packages/dd-trace/src/appsec/iast/analyzers/vulnerability-analyzer.js

+36-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { storage } = require('../../../../../datadog-core')
4-
const { getFirstNonDDPathAndLine } = require('../path-line')
4+
const { getNonDDPathAndLineFromCallsites } = require('../path-line')
55
const { addVulnerability, getVulnerabilityCallSiteList } = require('../vulnerability-reporter')
66
const { getIastContext, getIastStackTraceId } = require('../iast-context')
77
const overheadController = require('../overhead-controller')
@@ -29,14 +29,19 @@ class Analyzer extends SinkIastPlugin {
2929

3030
_reportEvidence (value, context, evidence) {
3131
const callSiteList = getVulnerabilityCallSiteList()
32-
const location = this._getLocation(value, callSiteList)
32+
const nonDDCallSiteList = getNonDDPathAndLineFromCallsites(callSiteList, this._getExcludedPaths())
33+
34+
const location = this._getLocation(value, nonDDCallSiteList)
3335

3436
if (!this._isExcluded(location)) {
35-
const locationSourceMap = this._replaceLocationFromSourceMap(location)
37+
const originalCallSiteList = nonDDCallSiteList.map(callSite => this._replaceCallsiteFromSourceMap(callSite))
38+
39+
const originalLocation = this._getOriginalLocation(originalCallSiteList)
3640
const spanId = context && context.rootSpan && context.rootSpan.context().toSpanId()
3741
const stackId = getIastStackTraceId(context)
38-
const vulnerability = this._createVulnerability(this._type, evidence, spanId, locationSourceMap, stackId)
39-
addVulnerability(context, vulnerability, callSiteList, stackId)
42+
const vulnerability = this._createVulnerability(this._type, evidence, spanId, originalLocation, stackId)
43+
44+
addVulnerability(context, vulnerability, originalCallSiteList, stackId)
4045
}
4146
}
4247

@@ -53,23 +58,41 @@ class Analyzer extends SinkIastPlugin {
5358
}
5459

5560
_getLocation (value, callSiteList) {
56-
return getFirstNonDDPathAndLine(callSiteList, this._getExcludedPaths())
61+
return callSiteList[0]
5762
}
5863

59-
_replaceLocationFromSourceMap (location) {
60-
if (location) {
61-
const { path, line, column } = getOriginalPathAndLineFromSourceMap(location)
64+
_getOriginalLocation (originalCallSiteList) {
65+
const [location] = originalCallSiteList
66+
const originalLocation = {}
67+
68+
if (location.path) {
69+
originalLocation.path = location.path
70+
}
71+
if (location.line) {
72+
originalLocation.line = location.line
73+
}
74+
if (location.column) {
75+
originalLocation.column = location.column
76+
}
77+
78+
return originalLocation
79+
}
80+
81+
_replaceCallsiteFromSourceMap (callsite) {
82+
if (callsite) {
83+
const { path, line, column } = getOriginalPathAndLineFromSourceMap(callsite)
6284
if (path) {
63-
location.path = path
85+
callsite.path = path
6486
}
6587
if (line) {
66-
location.line = line
88+
callsite.line = line
6789
}
6890
if (column) {
69-
location.column = column
91+
callsite.column = column
7092
}
7193
}
72-
return location
94+
95+
return callsite
7396
}
7497

7598
_getExcludedPaths () {}

packages/dd-trace/src/appsec/iast/path-line.js

+19-20
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ const path = require('path')
44
const process = require('process')
55
const { calculateDDBasePath } = require('../../util')
66
const pathLine = {
7-
getFirstNonDDPathAndLine,
87
getNodeModulesPaths,
98
getRelativePath,
10-
getFirstNonDDPathAndLineFromCallsites, // Exported only for test purposes
9+
getNonDDPathAndLineFromCallsites,
1110
calculateDDBasePath, // Exported only for test purposes
1211
ddBasePath: calculateDDBasePath(__dirname) // Only for test purposes
1312
}
@@ -24,22 +23,26 @@ const EXCLUDED_PATH_PREFIXES = [
2423
'async_hooks'
2524
]
2625

27-
function getFirstNonDDPathAndLineFromCallsites (callsites, externallyExcludedPaths) {
28-
if (callsites) {
29-
for (let i = 0; i < callsites.length; i++) {
30-
const callsite = callsites[i]
31-
const filepath = callsite.getFileName()
32-
if (!isExcluded(callsite, externallyExcludedPaths) && filepath.indexOf(pathLine.ddBasePath) === -1) {
33-
return {
34-
path: getRelativePath(filepath),
35-
line: callsite.getLineNumber(),
36-
column: callsite.getColumnNumber(),
37-
isInternal: !path.isAbsolute(filepath)
38-
}
39-
}
26+
function getNonDDPathAndLineFromCallsites (callsites, externallyExcludedPaths) {
27+
if (!callsites) {
28+
return []
29+
}
30+
31+
const result = []
32+
33+
for (const callsite of callsites) {
34+
const filepath = callsite.getFileName()
35+
if (!isExcluded(callsite, externallyExcludedPaths) && filepath.indexOf(pathLine.ddBasePath) === -1) {
36+
callsite.column = callsite.getLineNumber()
37+
callsite.line = callsite.getColumnNumber()
38+
callsite.path = getRelativePath(filepath)
39+
callsite.isInternal = !path.isAbsolute(filepath)
40+
41+
result.push(callsite)
4042
}
4143
}
42-
return null
44+
45+
return result
4346
}
4447

4548
function getRelativePath (filepath) {
@@ -72,10 +75,6 @@ function isExcluded (callsite, externallyExcludedPaths) {
7275
return false
7376
}
7477

75-
function getFirstNonDDPathAndLine (callSiteList, externallyExcludedPaths) {
76-
return getFirstNonDDPathAndLineFromCallsites(callSiteList, externallyExcludedPaths)
77-
}
78-
7978
function getNodeModulesPaths (...paths) {
8079
const nodeModulesPaths = []
8180

packages/dd-trace/src/appsec/stack_trace.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ function getFramesForMetaStruct (callSiteList, maxDepth = 32) {
5050
const callSite = filteredFrames[index]
5151
indexedFrames.push({
5252
id: index,
53-
file: callSite.getFileName(),
54-
line: callSite.getLineNumber(),
55-
column: callSite.getColumnNumber(),
53+
file: callSite.file || callSite.getFileName(),
54+
line: callSite.line || callSite.getLineNumber(),
55+
column: callSite.column || callSite.getColumnNumber(),
5656
function: callSite.getFunctionName(),
57-
class_name: callSite.getTypeName()
57+
class_name: callSite.getTypeName(),
58+
isNative: callSite.isNative()
5859
})
5960
}
6061

packages/dd-trace/test/appsec/iast/analyzers/vulnerability-analyzer.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('vulnerability-analyzer', () => {
2525
getVulnerabilityCallSiteList: sinon.stub().returns([])
2626
}
2727
pathLine = {
28-
getFirstNonDDPathAndLine: sinon.stub().returns(VULNERABILITY_LOCATION)
28+
getNonDDPathAndLineFromCallsites: sinon.stub().returns([VULNERABILITY_LOCATION])
2929
}
3030
overheadController = {
3131
hasQuota: sinon.stub()
@@ -132,7 +132,7 @@ describe('vulnerability-analyzer', () => {
132132
},
133133
hash: 5975567724
134134
},
135-
[],
135+
sinon.match.array,
136136
1
137137
)
138138
})
@@ -289,7 +289,7 @@ describe('vulnerability-analyzer', () => {
289289
ANALYZER_TYPE,
290290
{ value: 'test' },
291291
SPAN_ID,
292-
VULNERABILITY_LOCATION
292+
VULNERABILITY_LOCATION_FROM_SOURCEMAP
293293
)
294294
})
295295
})

0 commit comments

Comments
 (0)