Skip to content

Commit 11d16d7

Browse files
uuriennsavoire
authored andcommitted
Add sources tests (#3249)
* Add sources tests * styles
1 parent f773c06 commit 11d16d7

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict'
2+
3+
const { prepareTestServerForIastInExpress } = require('../utils')
4+
const axios = require('axios')
5+
6+
function noop () {}
7+
8+
describe('Taint tracking plugin sources express tests', () => {
9+
withVersions('express', 'express', '>=4.8.0', version => {
10+
prepareTestServerForIastInExpress('in express', version,
11+
(testThatRequestHasVulnerability, _, config) => {
12+
describe('tainted body', () => {
13+
function makePostRequest (done) {
14+
axios.post(`http://localhost:${config.port}/`, {
15+
command: 'echo 1'
16+
}).catch(done)
17+
}
18+
19+
testThatRequestHasVulnerability((req) => {
20+
const childProcess = require('child_process')
21+
childProcess.exec(req.body.command, noop)
22+
}, 'COMMAND_INJECTION', 1, noop, makePostRequest)
23+
})
24+
25+
describe('tainted query param', () => {
26+
function makeRequestWithQueryParam (done) {
27+
axios.get(`http://localhost:${config.port}/?command=echo`).catch(done)
28+
}
29+
30+
testThatRequestHasVulnerability((req) => {
31+
const childProcess = require('child_process')
32+
childProcess.exec(req.query.command, noop)
33+
}, 'COMMAND_INJECTION', 1, noop, makeRequestWithQueryParam)
34+
})
35+
36+
describe('tainted header', () => {
37+
function makeRequestWithHeader (done) {
38+
axios.get(`http://localhost:${config.port}/`, {
39+
headers: {
40+
'x-iast-test-command': 'echo 1'
41+
}
42+
}).catch(done)
43+
}
44+
45+
testThatRequestHasVulnerability((req) => {
46+
const childProcess = require('child_process')
47+
childProcess.exec(req.headers['x-iast-test-command'], noop)
48+
}, 'COMMAND_INJECTION', 1, noop, makeRequestWithHeader)
49+
})
50+
}
51+
)
52+
})
53+
})

packages/dd-trace/test/appsec/iast/utils.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function checkNoVulnerabilityInRequest (vulnerability, config, done) {
146146
.catch(done)
147147
axios.get(`http://localhost:${config.port}/`).catch(done)
148148
}
149-
function checkVulnerabilityInRequest (vulnerability, occurrencesAndLocation, cb, config, done) {
149+
function checkVulnerabilityInRequest (vulnerability, occurrencesAndLocation, cb, makeRequest, config, done) {
150150
let location
151151
let occurrences = occurrencesAndLocation
152152
if (typeof occurrencesAndLocation === 'object') {
@@ -195,7 +195,11 @@ function checkVulnerabilityInRequest (vulnerability, occurrencesAndLocation, cb,
195195
})
196196
.then(done)
197197
.catch(done)
198-
axios.get(`http://localhost:${config.port}/`).catch(done)
198+
if (makeRequest) {
199+
makeRequest(done)
200+
} else {
201+
axios.get(`http://localhost:${config.port}/`).catch(done)
202+
}
199203
}
200204

201205
function prepareTestServerForIast (description, tests, iastConfig) {
@@ -247,7 +251,7 @@ function prepareTestServerForIast (description, tests, iastConfig) {
247251
it(`should have ${vulnerability} vulnerability`, function (done) {
248252
this.timeout(5000)
249253
app = fn
250-
checkVulnerabilityInRequest(vulnerability, occurrences, cb, config, done)
254+
checkVulnerabilityInRequest(vulnerability, occurrences, cb, undefined, config, done)
251255
})
252256
}
253257

@@ -278,7 +282,10 @@ function prepareTestServerForIastInExpress (description, expressVersion, tests)
278282

279283
before((done) => {
280284
const express = require(`../../../../../versions/express@${expressVersion}`).get()
285+
const bodyParser = require(`../../../../../versions/body-parser`).get()
281286
const expressApp = express()
287+
expressApp.use(bodyParser.json())
288+
282289
expressApp.all('/', listener)
283290
getPort().then(newPort => {
284291
config.port = newPort
@@ -300,11 +307,11 @@ function prepareTestServerForIastInExpress (description, expressVersion, tests)
300307
return agent.close({ ritmReset: false })
301308
})
302309

303-
function testThatRequestHasVulnerability (fn, vulnerability, occurrences, cb) {
310+
function testThatRequestHasVulnerability (fn, vulnerability, occurrences, cb, makeRequest) {
304311
it(`should have ${vulnerability} vulnerability`, function (done) {
305312
this.timeout(5000)
306313
app = fn
307-
checkVulnerabilityInRequest(vulnerability, occurrences, cb, config, done)
314+
checkVulnerabilityInRequest(vulnerability, occurrences, cb, makeRequest, config, done)
308315
})
309316
}
310317

0 commit comments

Comments
 (0)