diff --git a/src/functions.js b/src/functions.js index e1284986..6f1d035a 100644 --- a/src/functions.js +++ b/src/functions.js @@ -1724,6 +1724,15 @@ const functions = (() => { if (!Array.isArray(arg2)) { arg2 = [arg2]; } + const size = arg1.length + arg2.length; + if(this.options && size > this.options.sequence) { + throw { + code: "D2015", + stack: (new Error()).stack, + value: size + }; + } + return arg1.concat(arg2); } diff --git a/src/jsonata.js b/src/jsonata.js index 34477704..ad59f6b0 100644 --- a/src/jsonata.js +++ b/src/jsonata.js @@ -514,6 +514,7 @@ var jsonata = (function() { var result; var focus = { + options: environment.base.options, createSequence: environment.base.createSequence }; switch (expr.value) { @@ -600,6 +601,7 @@ var jsonata = (function() { */ function evaluateWildcard(expr, input, environment) { var focus = { + options: environment.base.options, createSequence: environment.base.createSequence }; var results = focus.createSequence(); @@ -913,10 +915,12 @@ var jsonata = (function() { var groups = Object.create(null); var reduce = input && input.tupleStream ? true : false; var focus = { + options: environment.base.options, createSequence: environment.base.createSequence }; // group the input sequence by 'key' expression if (!Array.isArray(input)) { + options: environment.base.options, input = focus.createSequence(input); } // if the array is empty, add an undefined entry to enable literal JSON object to be generated @@ -995,6 +999,7 @@ var jsonata = (function() { } var result = Object.create(null); var focus = { + options: environment.base.options, createSequence: environment.base.createSequence }; Object.assign(result, tupleStream[0]); @@ -1532,6 +1537,7 @@ var jsonata = (function() { var focus = { environment: environment, input: input, + options: environment.base.options, createSequence: environment.base.createSequence }; // the `focus` is passed in as the `this` for the invoked function diff --git a/test/implementation-tests.js b/test/implementation-tests.js index 102334f0..11070c14 100644 --- a/test/implementation-tests.js +++ b/test/implementation-tests.js @@ -1121,6 +1121,16 @@ describe("Tests that include infinite recursion", () => { code: "D2015", }); }); + + it("prevents appending large sequences", function() { + const options = { + 'sequence': 1000 + } + const expr = jsonata('$append([0..600], [0..600]) ~> $count()', options); + expect(expr.evaluate()).to.eventually.be.rejected.to.deep.contain({ + code: "D2015", + }); + }); }); });