Skip to content

Commit e10e83c

Browse files
uurienCarlesDD
andcommitted
Fix ranges in operations (#61)
--------- Co-authored-by: Carles Capell <[email protected]>
1 parent 24f46a4 commit e10e83c

File tree

6 files changed

+73
-13
lines changed

6 files changed

+73
-13
lines changed

src/api/substring.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void substring(const FunctionCallbackInfo<Value>& args) {
6060
int subjectLen = TO_V8STRING(subject)->Length();
6161
int start = TO_INTEGER_VALUE(args[3], context);
6262
int end = subjectLen;
63-
if (argc > 4) {
63+
if (argc > 4 && !args[4]->IsUndefined()) {
6464
end = TO_INTEGER_VALUE(args[4], context);
6565
}
6666

@@ -130,8 +130,8 @@ void substr(const FunctionCallbackInfo<Value>& args) {
130130
auto subject = args[2];
131131
int subjectLen = TO_V8STRING(subject)->Length();
132132
int start = TO_INTEGER_VALUE(args[3], context);
133-
int length = subjectLen;
134-
if (argc > 4) {
133+
int length = subjectLen - start;
134+
if (argc > 4 && !args[4]->IsUndefined()) {
135135
length = TO_INTEGER_VALUE(args[4], context);
136136
}
137137

src/api/trim.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "../tainted/transaction.h"
1414
#include "../iast.h"
1515

16+
#define TO_V8STRING(arg) (v8::Local<v8::String>::Cast(arg))
17+
1618
using v8::FunctionCallbackInfo;
1719
using v8::Value;
1820
using v8::Local;
@@ -65,9 +67,7 @@ void TaintTrimOperator(const FunctionCallbackInfo<Value>& args) {
6567
left++;
6668
} while (*selfCh++);
6769

68-
v8::String::Utf8Value resultStr(isolate, args[1]);
69-
std::string cResultStr(*resultStr);
70-
int resultLength = cResultStr.length();
70+
int resultLength = TO_V8STRING(args[1])->Length();
7171

7272
auto resultRanges = transaction->GetSharedVectorRange();
7373
auto end = ranges->end();
@@ -136,9 +136,7 @@ void TaintTrimEndOperator(const FunctionCallbackInfo<Value>& args) {
136136
return;
137137
}
138138

139-
v8::String::Utf8Value resultStr(isolate, args[1]);
140-
std::string cResultStr(*resultStr);
141-
int resultLength = cResultStr.length();
139+
int resultLength = TO_V8STRING(args[1])->Length();
142140

143141
auto resultRanges = transaction->GetSharedVectorRange();
144142
auto end = ranges->end();

src/utils/propagation.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ SharedRanges* getRangesInSlice(Transaction* transaction, TaintedObject* obj, int
1717
return newRanges;
1818
}
1919

20+
int resultLength = sliceEnd - sliceStart;
2021
for (auto it = oRanges->begin(); it != oRanges->end(); ++it) {
2122
auto oRange = *it;
2223
int start, end;
@@ -49,6 +50,10 @@ SharedRanges* getRangesInSlice(Transaction* transaction, TaintedObject* obj, int
4950
end = sliceEnd;
5051
}
5152

53+
if (end > resultLength) {
54+
end = resultLength;
55+
}
56+
5257
if (!newRanges) {
5358
newRanges = transaction->GetSharedVectorRange();
5459
}

test/js/substr.spec.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ const rangesTestCases = [
2525
start: 0,
2626
end: 3
2727
},
28+
{
29+
source: ':+-foobarbaz-+:',
30+
result: ':+-barbaz-+:',
31+
start: 3,
32+
end: undefined
33+
},
34+
{
35+
source: ':+-foobarbaz-+:',
36+
result: ':+-barbaz-+:',
37+
start: 3
38+
},
2839
{
2940
source: ':+-foobarbaz-+:',
3041
result: ':+-bar-+:',
@@ -113,6 +124,17 @@ const rangesTestCases = [
113124
result: 'b:+-a-+:z:+-z-+::+-z-+:',
114125
start: 6,
115126
end: 20
127+
},
128+
{
129+
source: 'foo:+-bar-+:b:+-a-+:z:+-z-+::+-z-+:',
130+
result: 'b:+-a-+:z:+-z-+::+-z-+:',
131+
start: 6,
132+
end: undefined
133+
},
134+
{
135+
source: 'foo:+-bar-+:b:+-a-+:z:+-z-+::+-z-+:',
136+
result: 'b:+-a-+:z:+-z-+::+-z-+:',
137+
start: 6
116138
}
117139
]
118140

@@ -227,13 +249,17 @@ describe('Check Ranges format', function () {
227249
TaintedUtils.removeTransaction(id)
228250
})
229251

230-
rangesTestCases.forEach(({ source, start, end, result }) => {
252+
rangesTestCases.forEach((testData) => {
253+
const { source, start, end, result } = testData
254+
231255
it(`Test ${source}`, function () {
232256
const inputString = taintFormattedString(id, source)
233257
assert.equal(TaintedUtils.isTainted(id, inputString), true, 'Not tainted')
234258
const res = inputString.substr(start, end)
235259

236-
const ret = TaintedUtils.substr(id, res, inputString, start, end)
260+
const ret = testData.hasOwnProperty('end')
261+
? TaintedUtils.substr(id, res, inputString, start, end)
262+
: TaintedUtils.substr(id, res, inputString, start)
237263
assert.equal(res, ret, 'Unexpected value')
238264

239265
const formattedResult = formatTaintedValue(id, ret)

test/js/substring.spec.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ const rangesTestCases = [
1919
start: -6,
2020
end: -3
2121
},
22+
{
23+
source: ':+-foobarbaz-+:',
24+
result: ':+-barbaz-+:',
25+
start: 3,
26+
end: undefined
27+
},
28+
{
29+
source: ':+-foobarbaz-+:',
30+
result: ':+-barbaz-+:',
31+
start: 3
32+
},
2233
{
2334
source: ':+-foobarbaz-+:',
2435
result: ':+-foo-+:',
@@ -78,6 +89,17 @@ const rangesTestCases = [
7889
start: 6,
7990
end: 9
8091
},
92+
{
93+
source: 'foo:+-bar-+:baz',
94+
result: ':+-r-+:baz',
95+
start: 5,
96+
end: undefined
97+
},
98+
{
99+
source: 'foo:+-bar-+:baz',
100+
result: ':+-r-+:baz',
101+
start: 5
102+
},
81103
{
82104
source: 'foo:+-bar-+:b:+-az-+:',
83105
result: 'b:+-az-+:',
@@ -227,13 +249,16 @@ describe('Check Ranges format', function () {
227249
TaintedUtils.removeTransaction(id)
228250
})
229251

230-
rangesTestCases.forEach(({ source, start, end, result }) => {
252+
rangesTestCases.forEach((testData) => {
253+
const { source, start, end, result } = testData
231254
it(`Test ${source}`, function () {
232255
const inputString = taintFormattedString(id, source)
233256
assert.equal(TaintedUtils.isTainted(id, inputString), true, 'Not tainted')
234257
const res = inputString.substring(start, end)
235258

236-
const ret = TaintedUtils.substring(id, res, inputString, start, end)
259+
const ret = testData.hasOwnProperty('end')
260+
? TaintedUtils.substring(id, res, inputString, start, end)
261+
: TaintedUtils.substring(id, res, inputString, start)
237262
assert.equal(res, ret, 'Unexpected value')
238263

239264
const formattedResult = formatTaintedValue(id, ret)

test/js/util.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,16 @@ function formatTaintedValue (transactionId, taintedValue) {
4747
return taintedValue
4848
}
4949
const ranges = TaintedUtils.getRanges(transactionId, taintedValue)
50+
5051
if (!ranges || ranges.length === 0) {
5152
return taintedValue
5253
}
5354
checkRangesOrder(ranges)
55+
56+
if (ranges[ranges.length - 1].end > taintedValue.length) {
57+
throw new Error(`Ranges out of value: max = ${taintedValue.length} and current = ${ranges[ranges.length - 1].end}`)
58+
}
59+
5460
return ranges.reduce((formattedString, range) => {
5561
formattedString =
5662
formattedString.slice(0, range.start + offset) +

0 commit comments

Comments
 (0)