1
1
'use strict'
2
2
3
- const { createSandbox, FakeAgent, spawnProc } = require ( '../../../../../integration-tests/helpers' )
4
3
const getPort = require ( 'get-port' )
5
4
const path = require ( 'path' )
6
5
const Axios = require ( 'axios' )
6
+ const { assert } = require ( 'chai' )
7
+ const { createSandbox, FakeAgent, spawnProc } = require ( '../../../../../integration-tests/helpers' )
7
8
8
9
describe ( 'IAST - code_injection - integration' , ( ) => {
9
- let axios , sandbox , cwd , appPort , appFile , agent , proc
10
+ let axios , sandbox , cwd , appPort , agent , proc
10
11
11
12
before ( async function ( ) {
12
13
this . timeout ( process . platform === 'win32' ? 90000 : 30000 )
@@ -19,8 +20,6 @@ describe('IAST - code_injection - integration', () => {
19
20
20
21
appPort = await getPort ( )
21
22
cwd = sandbox . folder
22
- appFile = path . join ( cwd , 'resources' , 'vm.js' )
23
-
24
23
axios = Axios . create ( {
25
24
baseURL : `http://localhost:${ appPort } `
26
25
} )
@@ -33,16 +32,6 @@ describe('IAST - code_injection - integration', () => {
33
32
34
33
beforeEach ( async ( ) => {
35
34
agent = await new FakeAgent ( ) . start ( )
36
- proc = await spawnProc ( appFile , {
37
- cwd,
38
- env : {
39
- DD_TRACE_AGENT_PORT : agent . port ,
40
- APP_PORT : appPort ,
41
- DD_IAST_ENABLED : 'true' ,
42
- DD_IAST_REQUEST_SAMPLING : '100'
43
- } ,
44
- execArgv : [ '--experimental-vm-modules' ]
45
- } )
46
35
} )
47
36
48
37
afterEach ( async ( ) => {
@@ -53,24 +42,79 @@ describe('IAST - code_injection - integration', () => {
53
42
async function testVulnerabilityRepoting ( url ) {
54
43
await axios . get ( url )
55
44
56
- return agent . assertMessageReceived ( ( { headers, payload } ) => {
57
- expect ( payload [ 0 ] [ 0 ] . metrics [ '_dd.iast.enabled' ] ) . to . be . equal ( 1 )
58
- expect ( payload [ 0 ] [ 0 ] . meta ) . to . have . property ( '_dd.iast.json' )
45
+ let iastTelemetryReceived = false
46
+ const checkTelemetry = agent . assertTelemetryReceived ( ( { headers, payload } ) => {
47
+ const { namespace, series } = payload . payload
48
+
49
+ if ( namespace === 'iast' ) {
50
+ iastTelemetryReceived = true
51
+
52
+ const instrumentedSink = series . find ( ( { metric, tags, type } ) => {
53
+ return type === 'count' &&
54
+ metric === 'instrumented.sink' &&
55
+ tags [ 0 ] === 'vulnerability_type:code_injection'
56
+ } )
57
+ assert . isNotNull ( instrumentedSink )
58
+ }
59
+ } , 30_000 , 'generate-metrics' , 2 )
60
+
61
+ const checkMessages = agent . assertMessageReceived ( ( { headers, payload } ) => {
62
+ assert . strictEqual ( payload [ 0 ] [ 0 ] . metrics [ '_dd.iast.enabled' ] , 1 )
63
+ assert . property ( payload [ 0 ] [ 0 ] . meta , '_dd.iast.json' )
59
64
const vulnerabilitiesTrace = JSON . parse ( payload [ 0 ] [ 0 ] . meta [ '_dd.iast.json' ] )
60
- expect ( vulnerabilitiesTrace ) . to . not . be . null
65
+ assert . isNotNull ( vulnerabilitiesTrace )
61
66
const vulnerabilities = new Set ( )
62
67
63
68
vulnerabilitiesTrace . vulnerabilities . forEach ( v => {
64
69
vulnerabilities . add ( v . type )
65
70
} )
66
71
67
- expect ( vulnerabilities . has ( 'CODE_INJECTION' ) ) . to . be . true
72
+ assert . isTrue ( vulnerabilities . has ( 'CODE_INJECTION' ) )
73
+ } )
74
+
75
+ return Promise . all ( [ checkMessages , checkTelemetry ] ) . then ( ( ) => {
76
+ assert . equal ( iastTelemetryReceived , true )
77
+
78
+ return true
68
79
} )
69
80
}
70
81
71
82
describe ( 'SourceTextModule' , ( ) => {
83
+ beforeEach ( async ( ) => {
84
+ proc = await spawnProc ( path . join ( cwd , 'resources' , 'vm.js' ) , {
85
+ cwd,
86
+ env : {
87
+ DD_TRACE_AGENT_PORT : agent . port ,
88
+ APP_PORT : appPort ,
89
+ DD_IAST_ENABLED : 'true' ,
90
+ DD_IAST_REQUEST_SAMPLING : '100' ,
91
+ DD_TELEMETRY_HEARTBEAT_INTERVAL : 1
92
+ } ,
93
+ execArgv : [ '--experimental-vm-modules' ]
94
+ } )
95
+ } )
96
+
72
97
it ( 'should report Code injection vulnerability' , async ( ) => {
73
98
await testVulnerabilityRepoting ( '/vm/SourceTextModule?script=export%20const%20result%20%3D%203%3B' )
74
99
} )
75
100
} )
101
+
102
+ describe ( 'eval' , ( ) => {
103
+ beforeEach ( async ( ) => {
104
+ proc = await spawnProc ( path . join ( cwd , 'resources' , 'eval.js' ) , {
105
+ cwd,
106
+ env : {
107
+ DD_TRACE_AGENT_PORT : agent . port ,
108
+ APP_PORT : appPort ,
109
+ DD_IAST_ENABLED : 'true' ,
110
+ DD_IAST_REQUEST_SAMPLING : '100' ,
111
+ DD_TELEMETRY_HEARTBEAT_INTERVAL : 1
112
+ }
113
+ } )
114
+ } )
115
+
116
+ it ( 'should report Code injection vulnerability' , async ( ) => {
117
+ await testVulnerabilityRepoting ( '/eval?code=2%2B2' )
118
+ } )
119
+ } )
76
120
} )
0 commit comments