Skip to content

Commit

Permalink
Replace eval() for parsing JSON strings (#465)
Browse files Browse the repository at this point in the history
* Replace eval() with JSON.parse() for parsing JSON strings

* Replace eval with safe-eval for parsing JSON strings

* Replace eval with better-eval for parsing JSON strings

* Use assert without strict for comparing objects created by better-eval

* Use assert without strict for comparing objects created by better-eval

* Use assert without strict for comparing objects created by better-eval

---------

Co-authored-by: ilesh garish <111810784+sfc-gh-igarish@users.noreply.github.com>
  • Loading branch information
sfc-gh-ext-simba-lf and sfc-gh-igarish authored Apr 17, 2023
1 parent ca2f5e6 commit 0c9622a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/connection/result/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var SfTimestamp = require('./sf_timestamp');
var SqlTypes = require('./data_types').SqlTypes;
var bigInt = require('big-integer');
var { XMLParser, XMLValidator } = require("fast-xml-parser");
var betterEval = require("better-eval");

var NULL_UPPERCASE = 'NULL';

Expand Down Expand Up @@ -551,7 +552,7 @@ function convertRawVariant(rawColumnValue, column, context)
{
try
{
ret = eval("(" + rawColumnValue + ")");
ret = betterEval("(" + rawColumnValue + ")");
}
catch (parseError)
{
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"async": "^3.2.3",
"aws-sdk": "^2.878.0",
"axios": "^0.27.2",
"better-eval": "^1.3.0",
"big-integer": "^1.6.43",
"bignumber.js": "^2.4.0",
"binascii": "0.0.2",
Expand Down
10 changes: 8 additions & 2 deletions test/integration/testDataType.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ describe('Test DataType', function ()
connection,
selectVariant,
[{'COLA': {a: 1, b: [1, 2, 3, -Infinity, undefined], c: {a: 1}}}],
callback
callback,
null,
true,
false
);
}],
done
Expand All @@ -259,7 +262,10 @@ describe('Test DataType', function ()
connection,
selectArray,
[{'COLA': ['a', 1]}],
callback
callback,
null,
true,
false
);
}],
done
Expand Down
12 changes: 10 additions & 2 deletions test/integration/testUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ module.exports.checkError = function (err)
assert.ok(!err, JSON.stringify(err));
};

module.exports.executeQueryAndVerify = function (connection, sql, expected, callback, bindArray, normalize)
module.exports.executeQueryAndVerify = function (connection, sql, expected, callback, bindArray, normalize, strict)
{
// Sometimes we may not want to normalize the row first
normalize = (typeof normalize !== "undefined" && normalize != null) ? normalize : true;
strict = (typeof strict !== "undefined" && strict != null) ? strict : true;
var executeOptions = {};
executeOptions.sqlText = sql;
executeOptions.complete = function (err, stmt)
Expand All @@ -67,7 +68,14 @@ module.exports.executeQueryAndVerify = function (connection, sql, expected, call
var row;
while ((row = stream.read()) !== null)
{
assert.deepStrictEqual(normalize ? normalizeRowObject(row) : row, expected[rowCount]);
if (strict)
{
assert.deepStrictEqual(normalize ? normalizeRowObject(row) : row, expected[rowCount]);
}
else
{
assert.deepEqual(normalize ? normalizeRowObject(row) : row, expected[rowCount]);
}
rowCount++;
}
});
Expand Down
12 changes: 6 additions & 6 deletions test/unit/connection/result/result_test_variant.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ describe('Result: test variant', function ()
function (row)
{
// variant
assert.deepStrictEqual(row.getColumnValue('C1'), {a: 1});
assert.strictEqual(
assert.deepEqual(row.getColumnValue('C1'), {a: 1});
assert.equal(
row.getColumnValueAsString('C1'), JSON.stringify({a: 1}));

// object
assert.deepStrictEqual(row.getColumnValue('C2'), {a: 1});
assert.strictEqual(
assert.deepEqual(row.getColumnValue('C2'), {a: 1});
assert.equal(
row.getColumnValueAsString('C2'), JSON.stringify({a: 1}));

// array
assert.deepStrictEqual(row.getColumnValue('C3'), [1, 2]);
assert.strictEqual(
assert.deepEqual(row.getColumnValue('C3'), [1, 2]);
assert.equal(
row.getColumnValueAsString('C3'), JSON.stringify([1, 2]));
},
function (result)
Expand Down

0 comments on commit 0c9622a

Please sign in to comment.