Skip to content

Commit

Permalink
Switched jsonpath library to jsonpath-plus. Fixes #1318
Browse files Browse the repository at this point in the history
  • Loading branch information
n1474335 committed Sep 9, 2022
1 parent d90d845 commit 1dd1b83
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 148 deletions.
135 changes: 10 additions & 125 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
"js-sha3": "^0.8.0",
"jsesc": "^3.0.2",
"json5": "^2.2.1",
"jsonpath": "^1.1.1",
"jsonpath-plus": "^7.2.0",
"jsonwebtoken": "^8.5.1",
"jsqr": "^1.4.0",
"jsrsasign": "^10.5.23",
Expand Down
33 changes: 21 additions & 12 deletions src/core/operations/JPathExpression.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @license Apache-2.0
*/

import jpath from "jsonpath";
import {JSONPath} from "jsonpath-plus";
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";

Expand All @@ -27,14 +27,20 @@ class JPathExpression extends Operation {
this.outputType = "string";
this.args = [
{
"name": "Query",
"type": "string",
"value": ""
name: "Query",
type: "string",
value: ""
},
{
"name": "Result delimiter",
"type": "binaryShortString",
"value": "\\n"
name: "Result delimiter",
type: "binaryShortString",
value: "\\n"
},
{
name: "Prevent eval",
type: "boolean",
value: true,
description: "Evaluated expressions are disabled by default for security reasons"
}
];
}
Expand All @@ -45,18 +51,21 @@ class JPathExpression extends Operation {
* @returns {string}
*/
run(input, args) {
const [query, delimiter] = args;
let results,
obj;
const [query, delimiter, preventEval] = args;
let results, jsonObj;

try {
obj = JSON.parse(input);
jsonObj = JSON.parse(input);
} catch (err) {
throw new OperationError(`Invalid input JSON: ${err.message}`);
}

try {
results = jpath.query(obj, query);
results = JSONPath({
path: query,
json: jsonObj,
preventEval: preventEval
});
} catch (err) {
throw new OperationError(`Invalid JPath expression: ${err.message}`);
}
Expand Down
35 changes: 25 additions & 10 deletions tests/operations/tests/Code.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ TestRegister.addTests([
{
name: "JPath Expression: Empty expression",
input: JSON.stringify(JSON_TEST_DATA),
expectedOutput: "Invalid JPath expression: we need a path",
expectedOutput: "",
recipeConfig: [
{
"op": "JPath expression",
"args": ["", "\n"]
"args": ["", "\n", true]
}
],
},
Expand All @@ -205,7 +205,7 @@ TestRegister.addTests([
recipeConfig: [
{
"op": "JPath expression",
"args": ["$.store.book[*].author", "\n"]
"args": ["$.store.book[*].author", "\n", true]
}
],
},
Expand All @@ -223,7 +223,7 @@ TestRegister.addTests([
recipeConfig: [
{
"op": "JPath expression",
"args": ["$..title", "\n"]
"args": ["$..title", "\n", true]
}
],
},
Expand All @@ -238,7 +238,7 @@ TestRegister.addTests([
recipeConfig: [
{
"op": "JPath expression",
"args": ["$.store.*", "\n"]
"args": ["$.store.*", "\n", true]
}
],
},
Expand All @@ -249,7 +249,7 @@ TestRegister.addTests([
recipeConfig: [
{
"op": "JPath expression",
"args": ["$..book[-1:]", "\n"]
"args": ["$..book[-1:]", "\n", true]
}
],
},
Expand All @@ -263,7 +263,7 @@ TestRegister.addTests([
recipeConfig: [
{
"op": "JPath expression",
"args": ["$..book[:2]", "\n"]
"args": ["$..book[:2]", "\n", true]
}
],
},
Expand All @@ -277,7 +277,7 @@ TestRegister.addTests([
recipeConfig: [
{
"op": "JPath expression",
"args": ["$..book[?(@.isbn)]", "\n"]
"args": ["$..book[?(@.isbn)]", "\n", false]
}
],
},
Expand All @@ -292,7 +292,7 @@ TestRegister.addTests([
recipeConfig: [
{
"op": "JPath expression",
"args": ["$..book[?(@.price<30 && @.category==\"fiction\")]", "\n"]
"args": ["$..book[?(@.price<30 && @.category==\"fiction\")]", "\n", false]
}
],
},
Expand All @@ -306,9 +306,24 @@ TestRegister.addTests([
recipeConfig: [
{
"op": "JPath expression",
"args": ["$..book[?(@.price<10)]", "\n"]
"args": ["$..book[?(@.price<10)]", "\n", false]
}
],
},
{
name: "JPath Expression: Script-based expression",
input: "[{}]",
recipeConfig: [
{
"op": "JPath expression",
"args": [
"$..[?(({__proto__:[].constructor}).constructor(\"self.postMessage({action:'bakeComplete',data:{bakeId:1,dish:{type:1,value:''},duration:1,error:false,id:undefined,inputNum:2,progress:1,result:'<iframe/onload=debugger>',type: 'html'}});\")();)]",
"\n",
true
]
}
],
expectedOutput: "Invalid JPath expression: Eval [?(expr)] prevented in JSONPath expression."
},
{
name: "CSS selector",
Expand Down

0 comments on commit 1dd1b83

Please sign in to comment.