|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const Axios = require('axios') |
| 4 | +const { assert } = require('chai') |
| 5 | +const childProcess = require('child_process') |
| 6 | +const fs = require('fs') |
| 7 | +const path = require('path') |
| 8 | +const { promisify } = require('util') |
| 9 | +const msgpack = require('@msgpack/msgpack') |
| 10 | + |
| 11 | +const { createSandbox, FakeAgent, spawnProc } = require('../helpers') |
| 12 | + |
| 13 | +const exec = promisify(childProcess.exec) |
| 14 | + |
| 15 | +describe('esbuild support for IAST', () => { |
| 16 | + describe('cjs', () => { |
| 17 | + let proc, agent, sandbox, axios |
| 18 | + let applicationDir, bundledApplicationDir |
| 19 | + |
| 20 | + before(async () => { |
| 21 | + sandbox = await createSandbox([]) |
| 22 | + const cwd = sandbox.folder |
| 23 | + applicationDir = path.join(cwd, 'appsec/iast-esbuild') |
| 24 | + |
| 25 | + // Craft node_modules directory to ship native modules |
| 26 | + const craftedNodeModulesDir = path.join(applicationDir, 'tmp_node_modules') |
| 27 | + fs.mkdirSync(craftedNodeModulesDir) |
| 28 | + await exec('npm init -y', { cwd: craftedNodeModulesDir }) |
| 29 | + await exec('npm install @datadog/native-iast-rewriter @datadog/native-iast-taint-tracking', { |
| 30 | + cwd: craftedNodeModulesDir, |
| 31 | + timeout: 3e3 |
| 32 | + }) |
| 33 | + |
| 34 | + // Install app deps |
| 35 | + await exec('npm install || npm install', { |
| 36 | + cwd: applicationDir, |
| 37 | + timeout: 6e3 |
| 38 | + }) |
| 39 | + |
| 40 | + // Bundle the application |
| 41 | + await exec('npm run build', { |
| 42 | + cwd: applicationDir, |
| 43 | + timeout: 3e3 |
| 44 | + }) |
| 45 | + |
| 46 | + bundledApplicationDir = path.join(applicationDir, 'build') |
| 47 | + |
| 48 | + // Copy crafted node_modules with native modules |
| 49 | + fs.cpSync(path.join(craftedNodeModulesDir, 'node_modules'), bundledApplicationDir, { recursive: true }) |
| 50 | + }) |
| 51 | + |
| 52 | + after(async () => { |
| 53 | + await sandbox.remove() |
| 54 | + }) |
| 55 | + |
| 56 | + function startServer (appFile, iastEnabled) { |
| 57 | + beforeEach(async () => { |
| 58 | + agent = await new FakeAgent().start() |
| 59 | + proc = await spawnProc(path.join(bundledApplicationDir, appFile), { |
| 60 | + cwd: applicationDir, |
| 61 | + env: { |
| 62 | + DD_TRACE_AGENT_PORT: agent.port, |
| 63 | + DD_IAST_ENABLED: String(iastEnabled), |
| 64 | + DD_IAST_REQUEST_SAMPLING: '100', |
| 65 | + } |
| 66 | + }) |
| 67 | + axios = Axios.create({ baseURL: proc.url }) |
| 68 | + }) |
| 69 | + |
| 70 | + afterEach(async () => { |
| 71 | + proc.kill() |
| 72 | + await agent.stop() |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + describe('with IAST enabled', () => { |
| 77 | + describe('with sourcemap esbuild option enabled', () => { |
| 78 | + startServer('iast-enabled-with-sm.js', true) |
| 79 | + |
| 80 | + it('should detect vulnerability with correct location', async () => { |
| 81 | + await axios.get('/iast/cmdi-vulnerable?args=-la') |
| 82 | + |
| 83 | + const expectedVulnerabilityType = 'COMMAND_INJECTION' |
| 84 | + const expectedVulnerabilityLocationPath = path.join('iast', 'index.js') |
| 85 | + const expectedVulnerabilityLocationLine = 9 |
| 86 | + |
| 87 | + await agent.assertMessageReceived(({ payload }) => { |
| 88 | + const spans = payload.flatMap(p => p.filter(span => span.name === 'express.request')) |
| 89 | + spans.forEach(span => { |
| 90 | + assert.property(span.meta, '_dd.iast.json') |
| 91 | + const spanIastData = JSON.parse(span.meta['_dd.iast.json']) |
| 92 | + assert.strictEqual(spanIastData.vulnerabilities[0].type, expectedVulnerabilityType) |
| 93 | + assert.strictEqual(spanIastData.vulnerabilities[0].location.path, expectedVulnerabilityLocationPath) |
| 94 | + assert.strictEqual(spanIastData.vulnerabilities[0].location.line, expectedVulnerabilityLocationLine) |
| 95 | + |
| 96 | + const ddStack = msgpack.decode(span.meta_struct['_dd.stack']) |
| 97 | + assert.property(ddStack.vulnerability[0], 'frames') |
| 98 | + assert.isNotEmpty(ddStack.vulnerability[0].frames) |
| 99 | + }) |
| 100 | + }, null, 1, true) |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + describe('with sourcemap esbuild option disabled', () => { |
| 105 | + startServer('iast-enabled-with-no-sm.js', true) |
| 106 | + |
| 107 | + it('should detect vulnerability with first callsite location', async () => { |
| 108 | + await axios.get('/iast/cmdi-vulnerable?args=-la') |
| 109 | + |
| 110 | + const expectedVulnerabilityType = 'COMMAND_INJECTION' |
| 111 | + const expectedVulnerabilityLocationPath = path.join('build', 'iast-enabled-with-no-sm.js') |
| 112 | + |
| 113 | + await agent.assertMessageReceived(({ payload }) => { |
| 114 | + const spans = payload.flatMap(p => p.filter(span => span.name === 'express.request')) |
| 115 | + spans.forEach(span => { |
| 116 | + assert.property(span.meta, '_dd.iast.json') |
| 117 | + const spanIastData = JSON.parse(span.meta['_dd.iast.json']) |
| 118 | + assert.strictEqual(spanIastData.vulnerabilities[0].type, expectedVulnerabilityType) |
| 119 | + assert.strictEqual(spanIastData.vulnerabilities[0].location.path, expectedVulnerabilityLocationPath) |
| 120 | + |
| 121 | + const ddStack = msgpack.decode(span.meta_struct['_dd.stack']) |
| 122 | + assert.property(ddStack.vulnerability[0], 'frames') |
| 123 | + assert.isNotEmpty(ddStack.vulnerability[0].frames) |
| 124 | + }) |
| 125 | + }, null, 1, true) |
| 126 | + }) |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + describe('with IAST disabled', () => { |
| 131 | + startServer('iast-disabled.js', false) |
| 132 | + |
| 133 | + it('should not detect any vulnerability', async () => { |
| 134 | + await axios.get('/iast/cmdi-vulnerable?args=-la') |
| 135 | + await agent.assertMessageReceived(({ payload }) => { |
| 136 | + const spans = payload.flatMap(p => p.filter(span => span.name === 'express.request')) |
| 137 | + spans.forEach(span => { |
| 138 | + assert.notProperty(span.meta, '_dd.iast.json') |
| 139 | + }) |
| 140 | + }, null, 1, true) |
| 141 | + }) |
| 142 | + }) |
| 143 | + }) |
| 144 | +}) |
0 commit comments