Skip to content

Commit 99410fb

Browse files
committed
fix hardcoded-analyzers tests
1 parent a4f7df6 commit 99410fb

11 files changed

+88
-77
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ class CookieAnalyzer extends Analyzer {
5454
return super._checkOCE(context, value)
5555
}
5656

57-
_getLocation (value, callSiteList) {
57+
_getLocation (value, callSiteFrames) {
5858
if (!value) {
59-
return super._getLocation(value, callSiteList)
59+
return super._getLocation(value, callSiteFrames)
6060
}
6161

6262
if (value.location) {
6363
return value.location
6464
}
65-
const location = super._getLocation(value, callSiteList)
65+
const location = super._getLocation(value, callSiteFrames)
6666
value.location = location
6767
return location
6868
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class HardcodedBaseAnalyzer extends Analyzer {
5858
return { value: `${value.data}` }
5959
}
6060

61-
_getLocation (value) {
61+
_getLocation (value, callSiteFrames) {
6262
return {
6363
path: value.file,
6464
line: value.line,

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

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

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

3030
_reportEvidence (value, context, evidence) {
3131
const callSiteFrames = getVulnerabilityCallSiteFrames()
32-
const nonDDCallSiteFrames = getNonDDFrames(callSiteFrames, this._getExcludedPaths())
32+
const nonDDCallSiteFrames = getNonDDCallSiteFrames(callSiteFrames, this._getExcludedPaths())
3333

3434
const location = this._getLocation(value, nonDDCallSiteFrames)
3535

3636
if (!this._isExcluded(location)) {
37-
const originalCallSiteList = nonDDCallSiteFrames.map(callSite => this._replaceCallsiteFromSourceMap(callSite))
37+
const originalCallSiteList = nonDDCallSiteFrames.map(callsite => this._replaceCallsiteFromSourceMap(callsite))
3838

39-
const originalLocation = this._getOriginalLocation(originalCallSiteList)
39+
const originalLocation = this._getOriginalLocation(location)
4040
const spanId = context && context.rootSpan && context.rootSpan.context().toSpanId()
4141
const stackId = getIastStackTraceId(context)
42-
const vulnerability = this._createVulnerability(this._type, evidence, spanId, originalLocation, stackId)
42+
const vulnerability = this._createVulnerability(
43+
this._type,
44+
evidence,
45+
spanId,
46+
originalLocation,
47+
stackId
48+
)
4349

4450
addVulnerability(context, vulnerability, originalCallSiteList, stackId)
4551
}
@@ -57,22 +63,22 @@ class Analyzer extends SinkIastPlugin {
5763
return { value }
5864
}
5965

60-
_getLocation (value, callSiteList) {
61-
return callSiteList[0]
66+
_getLocation (value, callSiteFrames) {
67+
return callSiteFrames[0]
6268
}
6369

64-
_getOriginalLocation (originalCallSiteList) {
65-
const [location] = originalCallSiteList
70+
_getOriginalLocation (location) {
71+
const locationFromSourceMap = this._replaceCallsiteFromSourceMap(location)
6672
const originalLocation = {}
6773

68-
if (location?.path) {
69-
originalLocation.path = location.path
74+
if (locationFromSourceMap?.path) {
75+
originalLocation.path = locationFromSourceMap.path
7076
}
71-
if (location?.line) {
72-
originalLocation.line = location.line
77+
if (locationFromSourceMap?.line) {
78+
originalLocation.line = locationFromSourceMap.line
7379
}
74-
if (location?.column) {
75-
originalLocation.column = location.column
80+
if (locationFromSourceMap?.column) {
81+
originalLocation.column = locationFromSourceMap.column
7682
}
7783

7884
return originalLocation

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { calculateDDBasePath } = require('../../util')
66
const pathLine = {
77
getNodeModulesPaths,
88
getRelativePath,
9-
getNonDDFrames,
9+
getNonDDCallSiteFrames,
1010
calculateDDBasePath, // Exported only for test purposes
1111
ddBasePath: calculateDDBasePath(__dirname) // Only for test purposes
1212
}
@@ -23,7 +23,7 @@ const EXCLUDED_PATH_PREFIXES = [
2323
'async_hooks'
2424
]
2525

26-
function getNonDDFrames (callSiteFrames, externallyExcludedPaths) {
26+
function getNonDDCallSiteFrames (callSiteFrames, externallyExcludedPaths) {
2727
if (!callSiteFrames) {
2828
return []
2929
}

packages/dd-trace/src/appsec/iast/vulnerability-reporter.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { IAST_ENABLED_TAG_KEY, IAST_JSON_TAG_KEY } = require('./tags')
66
const standalone = require('../standalone')
77
const { SAMPLING_MECHANISM_APPSEC } = require('../../constants')
88
const { keepTrace } = require('../../priority_sampler')
9-
const { reportStackTrace, getFramesForMetaStruct, STACK_TRACE_NAMESPACES } = require('../stack_trace')
9+
const { reportStackTrace, getCallsiteFrames, STACK_TRACE_NAMESPACES } = require('../stack_trace')
1010

1111
const VULNERABILITIES_KEY = 'vulnerabilities'
1212
const VULNERABILITY_HASHES_MAX_SIZE = 1000
@@ -106,7 +106,7 @@ function isDuplicatedVulnerability (vulnerability) {
106106
}
107107

108108
function getVulnerabilityCallSiteFrames () {
109-
return getFramesForMetaStruct(stackTraceMaxDepth)
109+
return getCallsiteFrames(stackTraceMaxDepth)
110110
}
111111

112112
function start (config, _tracer) {

packages/dd-trace/src/appsec/rasp/utils.js

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

33
const web = require('../../plugins/util/web')
4-
const { getFramesForMetaStruct, reportStackTrace } = require('../stack_trace')
4+
const { getCallsiteFrames, reportStackTrace } = require('../stack_trace')
55
const { getBlockingAction } = require('../blocking')
66
const log = require('../../log')
77

@@ -34,7 +34,7 @@ function handleResult (actions, req, res, abortController, config) {
3434
const { enabled, maxDepth, maxStackTraces } = config.appsec.stackTrace
3535

3636
if (generateStackTraceAction && enabled) {
37-
const frames = getFramesForMetaStruct(maxDepth)
37+
const frames = getCallsiteFrames(maxDepth)
3838

3939
const rootSpan = web.root(req)
4040
reportStackTrace(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function filterOutFramesFromLibrary (callSiteList) {
3737
return callSiteList.filter(callSite => !callSite.getFileName()?.startsWith(ddBasePath))
3838
}
3939

40-
function getFramesForMetaStruct (maxDepth = 32, callSiteListGetter = getCallSiteList) {
40+
function getCallsiteFrames (maxDepth = 32, callSiteListGetter = getCallSiteList) {
4141
if (maxDepth < 1) maxDepth = Infinity
4242

4343
const callSiteList = callSiteListGetter(maxDepth)
@@ -91,7 +91,7 @@ function reportStackTrace (
9191
}
9292

9393
module.exports = {
94-
getFramesForMetaStruct,
94+
getCallsiteFrames,
9595
reportStackTrace,
9696
STACK_TRACE_NAMESPACES
9797
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const Config = require('../../../../src/config')
1010

1111
const hardcodedPasswordAnalyzer = require('../../../../src/appsec/iast/analyzers/hardcoded-password-analyzer')
1212
const iast = require('../../../../src/appsec/iast')
13+
const vulnerabilityReporter = require('../../../../src/appsec/iast/vulnerability-reporter')
1314

1415
const ruleId = 'hardcoded-password'
1516
const samples = [
@@ -131,6 +132,7 @@ describe('Hardcoded Password Analyzer', () => {
131132

132133
afterEach(() => {
133134
iast.disable()
135+
vulnerabilityReporter.clearCache()
134136
})
135137

136138
afterEach(() => {

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

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const { NameAndValue, ValueOnly } = require('../../../../src/appsec/iast/analyze
1111
const hardcodedSecretAnalyzer = require('../../../../src/appsec/iast/analyzers/hardcoded-secret-analyzer')
1212
const { suite } = require('./resources/hardcoded-secrets-suite.json')
1313
const iast = require('../../../../src/appsec/iast')
14+
const vulnerabilityReporter = require('../../../../src/appsec/iast/vulnerability-reporter')
1415

1516
describe('Hardcoded Secret Analyzer', () => {
1617
describe('unit test', () => {
@@ -101,6 +102,7 @@ describe('Hardcoded Secret Analyzer', () => {
101102

102103
afterEach(() => {
103104
iast.disable()
105+
vulnerabilityReporter.clearCache()
104106
})
105107

106108
afterEach(() => {

packages/dd-trace/test/appsec/iast/path-line.spec.js

+39-38
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ describe('path-line', function () {
5959
})
6060
})
6161

62-
describe('getNonDDFrames', () => {
62+
describe('getNonDDCallSiteFrames', () => {
6363
describe('does not fail', () => {
6464
it('with null parameter', () => {
65-
const result = pathLine.getNonDDFrames(null)
65+
const result = pathLine.getNonDDCallSiteFrames(null)
6666
expect(result).to.be.an('array').that.is.empty
6767
})
6868

6969
it('with empty list parameter', () => {
70-
const result = pathLine.getNonDDFrames([])
70+
const result = pathLine.getNonDDCallSiteFrames([])
7171
expect(result).to.be.an('array').that.is.empty
7272
})
7373

7474
it('without parameter', () => {
75-
const result = pathLine.getNonDDFrames()
75+
const result = pathLine.getNonDDCallSiteFrames()
7676
expect(result).to.be.an('array').that.is.empty
7777
})
7878
})
@@ -108,7 +108,7 @@ describe('path-line', function () {
108108
callsites.push(new CallSiteMock(firstFileOutOfDD, 13, 42))
109109
callsites.push(new CallSiteMock(secondFileOutOfDD, 20, 15))
110110

111-
const results = pathLine.getNonDDFrames(callsites)
111+
const results = pathLine.getNonDDCallSiteFrames(callsites)
112112

113113
expect(results).to.have.lengthOf(2)
114114

@@ -127,7 +127,7 @@ describe('path-line', function () {
127127
callsites.push(new CallSiteMock(path.join(DD_BASE_PATH, 'other', 'file', 'in', 'dd.js'), 89))
128128
callsites.push(new CallSiteMock(path.join(DD_BASE_PATH, 'another', 'file', 'in', 'dd.js'), 5))
129129

130-
const results = pathLine.getNonDDFrames(callsites)
130+
const results = pathLine.getNonDDCallSiteFrames(callsites)
131131
expect(results).to.be.an('array').that.is.empty
132132
})
133133

@@ -142,7 +142,7 @@ describe('path-line', function () {
142142
callsites.push(new CallSiteMock(dcPath, 25))
143143
callsites.push(new CallSiteMock(firstFileOutOfDD, 13, 42))
144144

145-
const results = pathLine.getNonDDFrames(callsites)
145+
const results = pathLine.getNonDDCallSiteFrames(callsites)
146146
expect(results).to.have.lengthOf(1)
147147

148148
expect(results[0].path).to.be.equals(expectedFilePath)
@@ -184,7 +184,7 @@ describe('path-line', function () {
184184
callsites.push(new CallSiteMock(firstFileOutOfDD, 13, 42))
185185
callsites.push(new CallSiteMock(secondFileOutOfDD, 20, 15))
186186

187-
const results = pathLine.getNonDDFrames(callsites)
187+
const results = pathLine.getNonDDCallSiteFrames(callsites)
188188
expect(results).to.have.lengthOf(2)
189189

190190
expect(results[0].path).to.be.equals(expectedFilePaths[0])
@@ -199,36 +199,37 @@ describe('path-line', function () {
199199
})
200200

201201
describe('getNodeModulesPaths', () => {
202-
// function getCallSiteInfo () {
203-
// const previousPrepareStackTrace = Error.prepareStackTrace
204-
// const previousStackTraceLimit = Error.stackTraceLimit
205-
// let callsiteList
206-
// Error.stackTraceLimit = 100
207-
// Error.prepareStackTrace = function (_, callsites) {
208-
// callsiteList = callsites
209-
// }
210-
// const e = new Error()
211-
// e.stack
212-
// Error.prepareStackTrace = previousPrepareStackTrace
213-
// Error.stackTraceLimit = previousStackTraceLimit
214-
215-
// return callsiteList
216-
// }
217-
// TODO: propose another test similar to this
218-
// it('should handle windows paths correctly', () => {
219-
// const basePath = pathLine.ddBasePath
220-
// pathLine.ddBasePath = path.join('test', 'base', 'path')
221-
// const { getFramesForMetaStruct } = require('../../../src/appsec/stack_trace')
222-
223-
// const list = getFramesForMetaStruct(32, getCallSiteInfo)
224-
// const firstNonDDPath = pathLine.getNonDDFrames(list)[0]
225-
226-
// const nodeModulesPaths = pathLine.getNodeModulesPaths(__filename)
227-
228-
// expect(nodeModulesPaths[0]).to.eq(path.join('node_modules', process.cwd(), firstNonDDPath.path))
229-
230-
// pathLine.ddBasePath = basePath
231-
// })
202+
function getCallSiteInfo () {
203+
const previousPrepareStackTrace = Error.prepareStackTrace
204+
const previousStackTraceLimit = Error.stackTraceLimit
205+
let callsiteList
206+
Error.stackTraceLimit = 100
207+
Error.prepareStackTrace = function (_, callsites) {
208+
callsiteList = callsites
209+
}
210+
const e = new Error()
211+
e.stack
212+
Error.prepareStackTrace = previousPrepareStackTrace
213+
Error.stackTraceLimit = previousStackTraceLimit
214+
215+
return callsiteList
216+
}
217+
218+
it('should handle windows paths correctly', () => {
219+
const basePath = pathLine.ddBasePath
220+
pathLine.ddBasePath = path.join('test', 'base', 'path')
221+
const { getCallsiteFrames } = require('../../../src/appsec/stack_trace')
222+
223+
const list = getCallsiteFrames(32, getCallSiteInfo)
224+
const firstNonDDPath = pathLine.getNonDDCallSiteFrames(list)[0]
225+
226+
const expectedPath = path.join('node_modules', firstNonDDPath.path)
227+
const nodeModulesPaths = pathLine.getNodeModulesPaths(firstNonDDPath.path)
228+
229+
expect(nodeModulesPaths[0]).to.equal(expectedPath)
230+
231+
pathLine.ddBasePath = basePath
232+
})
232233

233234
it('should convert / to \\ in windows platforms', () => {
234235
const dirname = __dirname

0 commit comments

Comments
 (0)