Skip to content

Commit f174348

Browse files
Prevent object contructor setting internal flags (#802)
The evaluator internally represents functions and lambdas using objects tagged with the properties `_jsonata_function` or `_jsonata_lambda`. This commit prevents expressions from setting these flags in the object constructor, which could potentially interfere with the processing of the expression. Signed-off-by: Andrew Coleman <andrew_coleman@uk.ibm.com>
1 parent 91532d4 commit f174348

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/jsonata.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,16 @@ var jsonata = (function() {
945945
}
946946

947947
if (key !== undefined) {
948+
// reject any attempts to set the internal JSONata flags
949+
if (key === '_jsonata_lambda' || key === '_jsonata_function') {
950+
// this is a restriction of this implementation rather than JSONata itself
951+
throw {
952+
code: "D1013",
953+
stack: (new Error()).stack,
954+
position: expr.position,
955+
value: key
956+
};
957+
}
948958
var entry = {data: item, exprIndex: pairIndex};
949959
if (Object.prototype.hasOwnProperty.call(groups, key)) {
950960
// a value already exists in this slot
@@ -2018,6 +2028,7 @@ var jsonata = (function() {
20182028
"T1010": "The matcher function argument passed to function {{token}} does not return the correct object structure",
20192029
"D1011": "Stack overflow. Check for non-terminating recursive function. Consider rewriting as tail-recursive",
20202030
"D1012": "Evaluation timeout after {{value}} milliseconds. Check for infinite loop",
2031+
"D1013": "Object property names starting with _jsonata_ are reserved for internal use: {{value}}",
20212032
"T2001": "The left side of the {{token}} operator must evaluate to a number",
20222033
"T2002": "The right side of the {{token}} operator must evaluate to a number",
20232034
"T2003": "The left side of the range operator (..) must evaluate to an integer",

test/implementation-tests.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,21 @@ describe("Tests that include infinite recursion", () => {
11341134
});
11351135
});
11361136

1137+
describe("Tests invalid object creation", () => {
1138+
it("prevents creating an object mimicking a lambda", () => {
1139+
const expr = jsonata('($lambda = {"_jsonata_lambda": true}; $lambda())');
1140+
expect(expr.evaluate()).to.eventually.be.rejected.to.deep.contain({
1141+
code: "D1013",
1142+
});
1143+
})
1144+
it("prevents creating an object mimicking a function", () => {
1145+
const expr = jsonata('($fn = {"_jsonata_function": true}; $fn())');
1146+
expect(expr.evaluate()).to.eventually.be.rejected.to.deep.contain({
1147+
code: "D1013",
1148+
});
1149+
})
1150+
})
1151+
11371152
describe("Tests that use internal frame push callbacks", () => {
11381153
describe("frame push callback bound to expression", function() {
11391154
it("calls callback when new frame created", function(done) {

0 commit comments

Comments
 (0)