|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { createSandbox, FakeAgent, spawnProc } = require('../../../../integration-tests/helpers') |
| 4 | +const getPort = require('get-port') |
| 5 | +const path = require('path') |
| 6 | +const Axios = require('axios') |
| 7 | +const { assert } = require('chai') |
| 8 | + |
| 9 | +describe('WAF truncation metrics', () => { |
| 10 | + let axios, sandbox, cwd, appPort, appFile, agent, proc |
| 11 | + |
| 12 | + before(async function () { |
| 13 | + this.timeout(process.platform === 'win32' ? 90000 : 30000) |
| 14 | + |
| 15 | + sandbox = await createSandbox( |
| 16 | + ['express'], |
| 17 | + false, |
| 18 | + [path.join(__dirname, 'resources')] |
| 19 | + ) |
| 20 | + |
| 21 | + appPort = await getPort() |
| 22 | + cwd = sandbox.folder |
| 23 | + appFile = path.join(cwd, 'resources', 'index.js') |
| 24 | + |
| 25 | + axios = Axios.create({ |
| 26 | + baseURL: `http://localhost:${appPort}` |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + after(async function () { |
| 31 | + this.timeout(60000) |
| 32 | + await sandbox.remove() |
| 33 | + }) |
| 34 | + |
| 35 | + beforeEach(async () => { |
| 36 | + agent = await new FakeAgent().start() |
| 37 | + proc = await spawnProc(appFile, { |
| 38 | + cwd, |
| 39 | + env: { |
| 40 | + DD_TRACE_AGENT_PORT: agent.port, |
| 41 | + APP_PORT: appPort, |
| 42 | + DD_APPSEC_ENABLED: 'true', |
| 43 | + DD_TELEMETRY_HEARTBEAT_INTERVAL: 1 |
| 44 | + } |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + afterEach(async () => { |
| 49 | + proc.kill() |
| 50 | + await agent.stop() |
| 51 | + }) |
| 52 | + |
| 53 | + it('should report tuncation metrics', async () => { |
| 54 | + let appsecTelemetryMetricsReceived = false |
| 55 | + let appsecTelemetryDistributionsReceived = false |
| 56 | + |
| 57 | + const longValue = 'testattack'.repeat(500) |
| 58 | + const largeObject = {} |
| 59 | + for (let i = 0; i < 300; ++i) { |
| 60 | + largeObject[`key${i}`] = `value${i}` |
| 61 | + } |
| 62 | + const deepObject = createNestedObject(25, { value: 'a' }) |
| 63 | + const complexPayload = { |
| 64 | + deepObject, |
| 65 | + longValue, |
| 66 | + largeObject |
| 67 | + } |
| 68 | + |
| 69 | + await axios.post('/', { complexPayload }) |
| 70 | + |
| 71 | + const checkMessages = agent.assertMessageReceived(({ payload }) => { |
| 72 | + assert.strictEqual(payload[0][0].metrics['_dd.appsec.enabled'], 1) |
| 73 | + assert.strictEqual(payload[0][0].metrics['_dd.appsec.truncated.container_depth'], 20) |
| 74 | + assert.strictEqual(payload[0][0].metrics['_dd.appsec.truncated.container_size'], 300) |
| 75 | + assert.strictEqual(payload[0][0].metrics['_dd.appsec.truncated.string_length'], 5000) |
| 76 | + }) |
| 77 | + |
| 78 | + const checkTelemetryMetrics = agent.assertTelemetryReceived(({ payload }) => { |
| 79 | + const namespace = payload.payload.namespace |
| 80 | + |
| 81 | + if (namespace === 'appsec') { |
| 82 | + appsecTelemetryMetricsReceived = true |
| 83 | + const series = payload.payload.series |
| 84 | + const inputTruncated = series.find(s => s.metric === 'waf.input_truncated') |
| 85 | + |
| 86 | + assert.exists(inputTruncated, 'input truncated serie should exist') |
| 87 | + assert.strictEqual(inputTruncated.type, 'count') |
| 88 | + assert.include(inputTruncated.tags, 'truncation_reason:7') |
| 89 | + } |
| 90 | + }, 30_000, 'generate-metrics', 2) |
| 91 | + |
| 92 | + const checkTelemetryDistributions = agent.assertTelemetryReceived(({ payload }) => { |
| 93 | + const namespace = payload.payload.namespace |
| 94 | + |
| 95 | + if (namespace === 'appsec') { |
| 96 | + appsecTelemetryDistributionsReceived = true |
| 97 | + const series = payload.payload.series |
| 98 | + const wafDuration = series.find(s => s.metric === 'waf.duration') |
| 99 | + const wafDurationExt = series.find(s => s.metric === 'waf.duration_ext') |
| 100 | + const wafTuncated = series.filter(s => s.metric === 'waf.truncated_value_size') |
| 101 | + |
| 102 | + assert.exists(wafDuration, 'waf duration serie should exist') |
| 103 | + assert.exists(wafDurationExt, 'waf duration ext serie should exist') |
| 104 | + |
| 105 | + assert.equal(wafTuncated.length, 3) |
| 106 | + assert.include(wafTuncated[0].tags, 'truncation_reason:1') |
| 107 | + assert.include(wafTuncated[1].tags, 'truncation_reason:2') |
| 108 | + assert.include(wafTuncated[2].tags, 'truncation_reason:4') |
| 109 | + } |
| 110 | + }, 30_000, 'distributions', 1) |
| 111 | + |
| 112 | + return Promise.all([checkMessages, checkTelemetryMetrics, checkTelemetryDistributions]).then(() => { |
| 113 | + assert.equal(appsecTelemetryMetricsReceived, true) |
| 114 | + assert.equal(appsecTelemetryDistributionsReceived, true) |
| 115 | + |
| 116 | + return true |
| 117 | + }) |
| 118 | + }) |
| 119 | +}) |
| 120 | + |
| 121 | +const createNestedObject = (n, obj) => { |
| 122 | + if (n > 0) { |
| 123 | + return { a: createNestedObject(n - 1, obj) } |
| 124 | + } |
| 125 | + return obj |
| 126 | +} |
0 commit comments