Skip to content

Commit a24f8ca

Browse files
iunanuahoolioh
authored andcommitted
Add some multiple concat tests (#40)
* Add some multiple concat tests * Include not string elements
1 parent 7aa7ca3 commit a24f8ca

File tree

2 files changed

+105
-16
lines changed

2 files changed

+105
-16
lines changed

test/js/concat.spec.js

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
33
* This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc.
44
**/
5-
const { TaintedUtils } = require('./util')
5+
const { TaintedUtils, taintFormattedString, formatTaintedValue } = require('./util')
66
const assert = require('assert')
77

88
describe('Plus operator', function () {
@@ -137,3 +137,90 @@ describe('Plus operator', function () {
137137
assert.deepEqual(expected, TaintedUtils.getRanges(id, ret))
138138
})
139139
})
140+
141+
describe('concat method', () => {
142+
const id = '1'
143+
144+
afterEach(function () {
145+
TaintedUtils.removeTransaction(id)
146+
})
147+
148+
const rangesTestCases = [
149+
{
150+
testStrings: [':+-A-+:', 'B', 'C'],
151+
result: ':+-A-+:BC'
152+
},
153+
{
154+
testStrings: ['A', ':+-B-+:', 'C'],
155+
result: 'A:+-B-+:C'
156+
},
157+
{
158+
testStrings: ['A', 'B', ':+-C-+:'],
159+
result: 'AB:+-C-+:'
160+
},
161+
{
162+
testStrings: ['A', '�', ':+-C-+:'],
163+
result: 'A�:+-C-+:'
164+
},
165+
{
166+
testStrings: ['A', null, ':+-C-+:'],
167+
result: 'Anull:+-C-+:'
168+
},
169+
{
170+
testStrings: ['A', undefined, ':+-C-+:'],
171+
result: 'Aundefined:+-C-+:'
172+
},
173+
{
174+
testStrings: ['A', 'B', 'C'],
175+
result: 'ABC',
176+
tainted: false
177+
},
178+
{
179+
testStrings: [':+-A-+:', 'B', ':+-C-+:'],
180+
result: ':+-A-+:B:+-C-+:'
181+
},
182+
{
183+
testStrings: [':+-A-+:', ':+-B-+:', 'C'],
184+
result: ':+-A-+::+-B-+:C'
185+
},
186+
{
187+
testStrings: [':+-A-+: BC :+-D-+:', ':+-E-+:', 'F'],
188+
result: ':+-A-+: BC :+-D-+::+-E-+:F'
189+
},
190+
{
191+
testStrings: [':+-A-+: ���', ':+-B-+:', 'C'],
192+
result: ':+-A-+: ���:+-B-+:C'
193+
},
194+
{
195+
testStrings: [':+-A-+:', 1, 'C'],
196+
result: ':+-A-+:1C'
197+
},
198+
{
199+
testStrings: ['A', {}, ':+-C-+:'],
200+
result: 'A[object Object]:+-C-+:'
201+
}
202+
]
203+
204+
function testConcatCheckRanges (formattedTestStrings, expectedResult, resultTainted) {
205+
const [testString, ...rest] = formattedTestStrings
206+
.map(formattedTestString => taintFormattedString(id, formattedTestString))
207+
const res = String.prototype.concat.call(testString, ...rest)
208+
209+
const ret = TaintedUtils.concat(id, res, testString, ...rest)
210+
assert.equal(res, ret, 'Unexpected vale')
211+
assert.equal(TaintedUtils.isTainted(id, ret), resultTainted,
212+
`Concat returned value ${resultTainted ? 'not tainted' : 'tainted'}`)
213+
214+
const formattedResult = resultTainted ? formatTaintedValue(id, ret) : ret
215+
assert.equal(formattedResult, expectedResult, 'Unexpected ranges')
216+
}
217+
218+
describe('Check ranges', function () {
219+
rangesTestCases.forEach(({ testStrings, result, tainted }) => {
220+
it(`Test ${testStrings}`, () => {
221+
const resultTainted = tainted === undefined ? true : tainted
222+
testConcatCheckRanges(testStrings, result, resultTainted)
223+
})
224+
})
225+
})
226+
})

test/js/util.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@ const PARAM_NAME = 'param'
1212
const PARAM_TYPE = 'REQUEST'
1313

1414
function taintFormattedString (transactionId, formattedString) {
15-
return formattedString.split(RANGE_OPEN_MARK).reduce((previousValue, currentValue) => {
16-
if (currentValue.length === 0) {
17-
return previousValue
18-
}
19-
if (currentValue.indexOf(RANGE_CLOSING_MARK) > -1) {
20-
const splitParts = currentValue.split(RANGE_CLOSING_MARK)
21-
const tainted = TaintedUtils.newTaintedString(transactionId, splitParts[0], PARAM_NAME, PARAM_TYPE)
22-
const previousPlusTainted = TaintedUtils.concat(transactionId, previousValue + tainted, previousValue, tainted)
23-
if (splitParts.length === 1) return previousPlusTainted
24-
const literal = splitParts[1]
25-
return TaintedUtils.concat(transactionId, previousPlusTainted + literal, previousPlusTainted, literal)
26-
} else {
27-
return TaintedUtils.concat(transactionId, previousValue + currentValue, previousValue, currentValue)
28-
}
29-
}, '')
15+
return formattedString && typeof formattedString === 'string'
16+
? formattedString.split(RANGE_OPEN_MARK).reduce((previousValue, currentValue) => {
17+
if (currentValue.length === 0) {
18+
return previousValue
19+
}
20+
if (currentValue.indexOf(RANGE_CLOSING_MARK) > -1) {
21+
const splitParts = currentValue.split(RANGE_CLOSING_MARK)
22+
const tainted = TaintedUtils.newTaintedString(transactionId, splitParts[0], PARAM_NAME, PARAM_TYPE)
23+
const previousPlusTainted = TaintedUtils.concat(transactionId, previousValue + tainted, previousValue, tainted)
24+
if (splitParts.length === 1) return previousPlusTainted
25+
const literal = splitParts[1]
26+
return TaintedUtils.concat(transactionId, previousPlusTainted + literal, previousPlusTainted, literal)
27+
} else {
28+
return TaintedUtils.concat(transactionId, previousValue + currentValue, previousValue, currentValue)
29+
}
30+
}, '')
31+
: formattedString
3032
}
3133

3234
function checkRangesOrder (ranges) {

0 commit comments

Comments
 (0)