Skip to content

Commit

Permalink
fix(codegen): certain expressions were marked as safe to evaluate
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Mar 14, 2022
1 parent 9ac2b79 commit 3ffbc62
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 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
@@ -1,6 +1,6 @@
{
"name": "nimma",
"version": "0.1.8",
"version": "0.2.0",
"description": "Scalable JSONPath engine.",
"keywords": [
"json",
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/codegen.test.mjs
@@ -1,4 +1,5 @@
import chai from 'chai';
import forEach from 'mocha-each';

import Nimma from '../core/index.mjs';
import { jsonPathPlus } from '../fallbacks/index.mjs';
Expand Down Expand Up @@ -1283,4 +1284,14 @@ export default function (input, callbacks) {
`);
});
});

forEach([
'$..[?(@.a)]..[?(@.b)]..c..d',
'$..[?(@.ab)]..[?(@.cb)]..c..d',
'$.paths.*.*[responses,requestBody]..content..schema.properties.*~',
]).it('should consider %s expression as unsafe', expression => {
expect(generate.bind(null, [expression], { unsafe: false })).to.throw(
`Error parsing ${expression}`,
);
});
});
7 changes: 5 additions & 2 deletions src/codegen/iterator.mjs
Expand Up @@ -16,9 +16,12 @@ function isBailable(nodes) {
} else if (isMemberExpression(node)) {
i++;
let hadFlatMemberExpressions = false;
let deepNodes = 1;
for (; i < nodes.length - 1; i++) {
const node = nodes[i];
if (!isDeep(node)) {
if (isDeep(node)) {
deepNodes++;
} else {
hadFlatMemberExpressions ||=
isMemberExpression(node) || isWildcardExpression(node);
continue;
Expand All @@ -35,7 +38,7 @@ function isBailable(nodes) {
return isDeep(nodes[nodes.length - 1])
? hadFlatMemberExpressions ||
isWildcardExpression(nodes[nodes.length - 1])
: false;
: deepNodes > 1;
} else {
deep = true;
}
Expand Down

0 comments on commit 3ffbc62

Please sign in to comment.