|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { createSandbox, spawnProc, FakeAgent } = require('../helpers') |
| 4 | +const path = require('path') |
| 5 | +const getPort = require('get-port') |
| 6 | +const Axios = require('axios') |
| 7 | +const { assert } = require('chai') |
| 8 | + |
| 9 | +describe('ESM', () => { |
| 10 | + let axios, sandbox, cwd, appPort, appFile, agent, proc |
| 11 | + |
| 12 | + before(async function () { |
| 13 | + this.timeout(process.platform === 'win32' ? 90000 : 30000) |
| 14 | + sandbox = await createSandbox(['express']) |
| 15 | + appPort = await getPort() |
| 16 | + cwd = sandbox.folder |
| 17 | + appFile = path.join(cwd, 'appsec', 'esm-app', 'index.mjs') |
| 18 | + |
| 19 | + axios = Axios.create({ |
| 20 | + baseURL: `http://localhost:${appPort}` |
| 21 | + }) |
| 22 | + }) |
| 23 | + |
| 24 | + after(async function () { |
| 25 | + await sandbox.remove() |
| 26 | + }) |
| 27 | + |
| 28 | + const nodeOptionsList = [ |
| 29 | + '--import dd-trace/initialize.mjs', |
| 30 | + '--require dd-trace/init.js --loader dd-trace/loader-hook.mjs' |
| 31 | + ] |
| 32 | + |
| 33 | + nodeOptionsList.forEach(nodeOptions => { |
| 34 | + describe(`with NODE_OPTIONS=${nodeOptions}`, () => { |
| 35 | + beforeEach(async () => { |
| 36 | + agent = await new FakeAgent().start() |
| 37 | + |
| 38 | + proc = await spawnProc(appFile, { |
| 39 | + cwd, |
| 40 | + env: { |
| 41 | + DD_TRACE_AGENT_PORT: agent.port, |
| 42 | + APP_PORT: appPort, |
| 43 | + DD_IAST_ENABLED: 'true', |
| 44 | + DD_IAST_REQUEST_SAMPLING: '100', |
| 45 | + NODE_OPTIONS: nodeOptions |
| 46 | + } |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + afterEach(async () => { |
| 51 | + proc.kill() |
| 52 | + await agent.stop() |
| 53 | + }) |
| 54 | + |
| 55 | + function verifySpan (payload, verify) { |
| 56 | + let err |
| 57 | + for (let i = 0; i < payload.length; i++) { |
| 58 | + const trace = payload[i] |
| 59 | + for (let j = 0; j < trace.length; j++) { |
| 60 | + try { |
| 61 | + verify(trace[j]) |
| 62 | + return |
| 63 | + } catch (e) { |
| 64 | + err = err || e |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + throw err |
| 69 | + } |
| 70 | + |
| 71 | + it('should detect COMMAND_INJECTION vulnerability', async function () { |
| 72 | + await axios.get('/cmdi-vulnerable?args=-la') |
| 73 | + |
| 74 | + await agent.assertMessageReceived(({ payload }) => { |
| 75 | + verifySpan(payload, span => { |
| 76 | + assert.property(span.meta, '_dd.iast.json') |
| 77 | + assert.include(span.meta['_dd.iast.json'], '"COMMAND_INJECTION"') |
| 78 | + }) |
| 79 | + }, null, 1, true) |
| 80 | + }) |
| 81 | + |
| 82 | + it('should detect COMMAND_INJECTION vulnerability in imported file', async () => { |
| 83 | + await axios.get('/more/cmdi-vulnerable?args=-la') |
| 84 | + |
| 85 | + await agent.assertMessageReceived(({ payload }) => { |
| 86 | + verifySpan(payload, span => { |
| 87 | + assert.property(span.meta, '_dd.iast.json') |
| 88 | + assert.include(span.meta['_dd.iast.json'], '"COMMAND_INJECTION"') |
| 89 | + }) |
| 90 | + }, null, 1, true) |
| 91 | + }) |
| 92 | + }) |
| 93 | + }) |
| 94 | +}) |
0 commit comments