Skip to content

[Feature] Continuation callbacks to avoid malicious scripts #478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/embedding-extending.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ console.log("Started");
// Prints "Started", then "Finished with 19"
```

If `continuationCallback()` is supplied, `expression.evaluate()` returns `undefined`, the expression is run asynchronously calling continuationCallback after every step which should resolve to true else it will cancel the execution and the `Error` in `callback` will reflect as operation cancelled.

```javascript
let stepCounter = 0;
async function continueExecution() {
stepCounter++;
return stepCounter<1000; //PREVENT INFINITE LOOPS
}
function completion(err, results) {
if (err) {
console.error(err);
return;
}
console.log(`Completed in ${stepCounter} steps with result:${results}`);
}
const getBit = jsonata(`($x:= λ($c,$n,$b){ $c=$b?$n%2:$x($c+1,$floor($n/2),$b)};$x(0,number,bitIndex))`);

getBit.evaluate({ "number": 10000000, "bitIndex": 0 }, undefined, completion, continueExecution); //NORMAL
// Prints Completed in 17 steps with result:0

getBit.evaluate({ "number": 10000000, "bitIndex": -1 }, undefined, completion, continueExecution); //INFINITE LOOP
// Prints { code: 'U2020', message: 'Operation cancelled.' }
```

### expression.assign(name, value)

Permanently binds a value to a name in the expression, similar to how `bindings` worked above. Modifies `expression` in place and returns `undefined`. Useful in a JSONata expression factory.
Expand Down
1 change: 1 addition & 0 deletions jsonata.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ declare namespace jsonata {
interface Expression {
evaluate(input: any, bindings?: Record<string, any>): any;
evaluate(input: any, bindings: Record<string, any> | undefined, callback: (err: JsonataError, resp: any) => void): void;
evaluate(input: any, bindings: Record<string, any> | undefined, callback: (err: JsonataError, resp: any) => void, continuationCallback: () => Promise<boolean>): void;
assign(name: string, value: any): void;
registerFunction(name: string, implementation: (this: Focus, ...args: any[]) => any, signature?: string): void;
ast(): ExprNode;
Expand Down
16 changes: 13 additions & 3 deletions src/jsonata.js
Original file line number Diff line number Diff line change
Expand Up @@ -2006,7 +2006,8 @@ var jsonata = (function() {
"D3138": "The $single() function expected exactly 1 matching result. Instead it matched more.",
"D3139": "The $single() function expected exactly 1 matching result. Instead it matched 0.",
"D3140": "Malformed URL passed to ${{{functionName}}}(): {{value}}",
"D3141": "{{{message}}}"
"D3141": "{{{message}}}",
"U2020": "Operation Cancelled"
};

/**
Expand Down Expand Up @@ -2062,7 +2063,7 @@ var jsonata = (function() {
}, '<:n>'));

return {
evaluate: function (input, bindings, callback) {
evaluate: function (input, bindings, callback, continuationCallback = function(){return Promise.resolve(true)}) {
// throw if the expression compiled with syntax errors
if(typeof errors !== 'undefined') {
var err = {
Expand Down Expand Up @@ -2110,7 +2111,16 @@ var jsonata = (function() {
if (result.done) {
callback(null, result.value);
} else {
result.value.then(thenHandler).catch(catchHandler);
continuationCallback().then((proceed) => {
if (proceed === true) {
result.value.then(thenHandler).catch(catchHandler);
}
else {
var err = { code: 'U2020' };
populateMessage(err);
callback(err, null);
}
}).catch(catchHandler);
}
};
it = evaluate(ast, input, exec_env);
Expand Down
38 changes: 38 additions & 0 deletions test/async-continuation-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict";

var jsonata = require('../src/jsonata');
var chai = require("chai");
var expect = chai.expect;
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);

var jsonataContinuationPromise = function (expr, data, continuationCallback) {
return new Promise(function (resolve, reject) {
expr.evaluate(data, undefined, function (error, response) {
if (error) reject(error);
resolve(response);
}, continuationCallback);
});
};

describe('Invoke JSONata with continuation callback', function () {
describe('Get bit value form valid bit index', function () {
it('should return valid result', function () {
let stepCounter = 0;
const continuationCallback = () => { stepCounter++; return Promise.resolve(stepCounter < 100); };
const getBit = jsonata(`($x:= λ($c,$n,$b){ $c=$b?$n%2:$x($c+1,$floor($n/2),$b)};$x(0,number,bitIndex))`);
const data = { "number": 10000000, "bitIndex": 0 };
expect(jsonataContinuationPromise(getBit, data, continuationCallback)).to.eventually.deep.equal(0);
});
});

describe('Get bit value form invalid bit index', function () {
it('should return error rejected', function () {
let stepCounter = 0;
const continuationCallback = () => { stepCounter++; return Promise.resolve(stepCounter < 100); };
const getBit = jsonata(`($x:= λ($c,$n,$b){ $c=$b?$n%2:$x($c+1,$floor($n/2),$b)};$x(0,number,bitIndex))`);
const data = { "number": 10000000, "bitIndex": -1 };
expect(jsonataContinuationPromise(getBit, data, continuationCallback)).to.eventually.be.rejected;
});
});
});