|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const axios = require('axios') |
| 4 | +const getPort = require('get-port') |
| 5 | +const semver = require('semver') |
| 6 | +const agent = require('../../../../plugins/agent') |
| 7 | +const Config = require('../../../../../src/config') |
| 8 | +const { storage } = require('../../../../../../datadog-core') |
| 9 | +const iast = require('../../../../../src/appsec/iast') |
| 10 | +const iastContextFunctions = require('../../../../../src/appsec/iast/iast-context') |
| 11 | +const { isTainted, getRanges } = require('../../../../../src/appsec/iast/taint-tracking/operations') |
| 12 | +const { HTTP_REQUEST_PATH_PARAM } = require('../../../../../src/appsec/iast/taint-tracking/origin-types') |
| 13 | + |
| 14 | +describe('Path params sourcing with express', () => { |
| 15 | + let express |
| 16 | + let expressVersion |
| 17 | + let appListener |
| 18 | + |
| 19 | + withVersions('express', 'express', version => { |
| 20 | + const checkParamIsTaintedAndNext = (req, res, next, param) => { |
| 21 | + const store = storage.getStore() |
| 22 | + const iastContext = iastContextFunctions.getIastContext(store) |
| 23 | + |
| 24 | + const pathParamValue = param |
| 25 | + const isParameterTainted = isTainted(iastContext, pathParamValue) |
| 26 | + expect(isParameterTainted).to.be.true |
| 27 | + const taintedParameterValueRanges = getRanges(iastContext, pathParamValue) |
| 28 | + expect(taintedParameterValueRanges[0].iinfo.type).to.be.equal(HTTP_REQUEST_PATH_PARAM) |
| 29 | + |
| 30 | + next() |
| 31 | + } |
| 32 | + |
| 33 | + before(() => { |
| 34 | + return agent.load(['http', 'express'], { client: false }) |
| 35 | + }) |
| 36 | + |
| 37 | + after(() => { |
| 38 | + return agent.close({ ritmReset: false }) |
| 39 | + }) |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + iast.enable(new Config({ |
| 43 | + experimental: { |
| 44 | + iast: { |
| 45 | + enabled: true, |
| 46 | + requestSampling: 100 |
| 47 | + } |
| 48 | + } |
| 49 | + })) |
| 50 | + |
| 51 | + const expressRequire = require(`../../../../../../../versions/express@${version}`) |
| 52 | + express = expressRequire.get() |
| 53 | + expressVersion = expressRequire.version() |
| 54 | + }) |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + appListener && appListener.close() |
| 58 | + appListener = null |
| 59 | + |
| 60 | + iast.disable() |
| 61 | + }) |
| 62 | + |
| 63 | + it('should taint path params', function (done) { |
| 64 | + const app = express() |
| 65 | + app.get('/:parameter1/:parameter2', (req, res) => { |
| 66 | + const store = storage.getStore() |
| 67 | + const iastContext = iastContextFunctions.getIastContext(store) |
| 68 | + |
| 69 | + for (const pathParamName of ['parameter1', 'parameter2']) { |
| 70 | + const pathParamValue = req.params[pathParamName] |
| 71 | + const isParameterTainted = isTainted(iastContext, pathParamValue) |
| 72 | + expect(isParameterTainted).to.be.true |
| 73 | + const taintedParameterValueRanges = getRanges(iastContext, pathParamValue) |
| 74 | + expect(taintedParameterValueRanges[0].iinfo.type).to.be.equal(HTTP_REQUEST_PATH_PARAM) |
| 75 | + } |
| 76 | + |
| 77 | + res.status(200).send() |
| 78 | + }) |
| 79 | + |
| 80 | + getPort().then(port => { |
| 81 | + appListener = app.listen(port, 'localhost', () => { |
| 82 | + axios |
| 83 | + .get(`http://localhost:${port}/tainted1/tainted2`) |
| 84 | + .then(() => done()) |
| 85 | + .catch(done) |
| 86 | + }) |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should taint path params in nested routers with merged params', function (done) { |
| 91 | + if (!semver.satisfies(expressVersion, '>4.5.0')) { |
| 92 | + this.skip() |
| 93 | + } |
| 94 | + |
| 95 | + const app = express() |
| 96 | + const nestedRouter = express.Router({ mergeParams: true }) |
| 97 | + |
| 98 | + nestedRouter.get('/:parameterChild', (req, res) => { |
| 99 | + const store = storage.getStore() |
| 100 | + const iastContext = iastContextFunctions.getIastContext(store) |
| 101 | + |
| 102 | + for (const pathParamName of ['parameterParent', 'parameterChild']) { |
| 103 | + const pathParamValue = req.params[pathParamName] |
| 104 | + const isParameterTainted = isTainted(iastContext, pathParamValue) |
| 105 | + expect(isParameterTainted).to.be.true |
| 106 | + const taintedParameterValueRanges = getRanges(iastContext, pathParamValue) |
| 107 | + expect(taintedParameterValueRanges[0].iinfo.type).to.be.equal(HTTP_REQUEST_PATH_PARAM) |
| 108 | + } |
| 109 | + |
| 110 | + res.status(200).send() |
| 111 | + }) |
| 112 | + |
| 113 | + app.use('/:parameterParent', nestedRouter) |
| 114 | + |
| 115 | + getPort().then(port => { |
| 116 | + appListener = app.listen(port, 'localhost', () => { |
| 117 | + axios |
| 118 | + .get(`http://localhost:${port}/tainted1/tainted2`) |
| 119 | + .then(() => done()) |
| 120 | + .catch(done) |
| 121 | + }) |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
| 125 | + it('should taint path param on router.params callback', function (done) { |
| 126 | + const app = express() |
| 127 | + |
| 128 | + app.use('/:parameter1/:parameter2', (req, res) => { |
| 129 | + res.status(200).send() |
| 130 | + }) |
| 131 | + |
| 132 | + app.param('parameter1', checkParamIsTaintedAndNext) |
| 133 | + app.param('parameter2', checkParamIsTaintedAndNext) |
| 134 | + |
| 135 | + getPort().then(port => { |
| 136 | + appListener = app.listen(port, 'localhost', () => { |
| 137 | + axios |
| 138 | + .get(`http://localhost:${port}/tainted1/tainted2`) |
| 139 | + .then(() => done()) |
| 140 | + .catch(done) |
| 141 | + }) |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + it('should taint path param on router.params callback with custom implementation', function (done) { |
| 146 | + const app = express() |
| 147 | + |
| 148 | + app.use('/:parameter1/:parameter2', (req, res) => { |
| 149 | + res.status(200).send() |
| 150 | + }) |
| 151 | + |
| 152 | + app.param((param, option) => { |
| 153 | + return checkParamIsTaintedAndNext |
| 154 | + }) |
| 155 | + |
| 156 | + app.param('parameter1') |
| 157 | + app.param('parameter2') |
| 158 | + |
| 159 | + getPort().then(port => { |
| 160 | + appListener = app.listen(port, 'localhost', () => { |
| 161 | + axios |
| 162 | + .get(`http://localhost:${port}/tainted1/tainted2`) |
| 163 | + .then(() => done()) |
| 164 | + .catch(done) |
| 165 | + }) |
| 166 | + }) |
| 167 | + }) |
| 168 | + }) |
| 169 | +}) |
0 commit comments