Skip to content

Commit

Permalink
prevent truncated subexpressions + misc code cleanup (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardstartin committed Nov 7, 2021
1 parent fcf8bf6 commit ed4d2a8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
Expand Up @@ -72,8 +72,7 @@ public static Path compile(String path, final Predicate... filters) {
fail("Path must not end with a '.' or '..'");
}
LinkedList<Predicate> filterStack = new LinkedList<Predicate>(asList(filters));
Path p = new PathCompiler(ci, filterStack).compile();
return p;
return new PathCompiler(ci, filterStack).compile();
} catch (Exception e) {
InvalidPathException ipe;
if (e instanceof InvalidPathException) {
Expand Down Expand Up @@ -135,21 +134,26 @@ private boolean readNextToken(PathTokenAppender appender) {

switch (c) {
case OPEN_SQUARE_BRACKET:
return readBracketPropertyToken(appender) ||
readArrayToken(appender) ||
readWildCardToken(appender) ||
readFilterToken(appender) ||
readPlaceholderToken(appender) ||
fail("Could not parse token starting at position " + path.position() + ". Expected ?, ', 0-9, * ");
if (!readBracketPropertyToken(appender) && !readArrayToken(appender) && !readWildCardToken(appender)
&& !readFilterToken(appender) && !readPlaceholderToken(appender)) {
fail("Could not parse token starting at position " + path.position() + ". Expected ?, ', 0-9, * ");
}
return true;
case PERIOD:
return readDotToken(appender) ||
fail("Could not parse token starting at position " + path.position());
if (!readDotToken(appender)) {
fail("Could not parse token starting at position " + path.position());
}
return true;
case WILDCARD:
return readWildCardToken(appender) ||
fail("Could not parse token starting at position " + path.position());
if (!readWildCardToken(appender)) {
fail("Could not parse token starting at position " + path.position());
}
return true;
default:
return readPropertyOrFunctionToken(appender) ||
fail("Could not parse token starting at position " + path.position());
if (!readPropertyOrFunctionToken(appender)) {
fail("Could not parse token starting at position " + path.position());
}
return true;
}
}

Expand Down Expand Up @@ -286,8 +290,8 @@ private List<Parameter> parseFunctionParameters(String funcName) {

// Parenthesis starts at 1 since we're marking the start of a function call, the close paren will denote the
// last parameter boundary
Integer groupParen = 1, groupBracket = 0, groupBrace = 0, groupQuote = 0;
Boolean endOfStream = false;
int groupParen = 1, groupBracket = 0, groupBrace = 0, groupQuote = 0;
boolean endOfStream = false;
char priorChar = 0;
List<Parameter> parameters = new ArrayList<Parameter>();
StringBuilder parameter = new StringBuilder();
Expand All @@ -312,9 +316,6 @@ else if (isPathContext(c)) {
switch (c) {
case DOUBLE_QUOTE:
if (priorChar != '\\' && groupQuote > 0) {
if (groupQuote == 0) {
throw new InvalidPathException("Unexpected quote '\"' at character position: " + path.position());
}
groupQuote--;
}
else {
Expand Down Expand Up @@ -349,7 +350,7 @@ else if (isPathContext(c)) {
case CLOSE_PARENTHESIS:
groupParen--;
//CS304 Issue link: https://github.com/json-path/JsonPath/issues/620
if (0 > groupParen ) {
if (0 > groupParen || priorChar == '(') {
parameter.append(c);
}
case COMMA:
Expand All @@ -367,7 +368,7 @@ else if (isPathContext(c)) {
param = new Parameter(parameter.toString());
break;
case PATH:
LinkedList<Predicate> predicates = new LinkedList<Predicate>();
LinkedList<Predicate> predicates = new LinkedList<>();
PathCompiler compiler = new PathCompiler(parameter.toString(), predicates);
param = new Parameter(compiler.compile());
break;
Expand Down Expand Up @@ -431,7 +432,7 @@ private boolean readPlaceholderToken(PathTokenAppender appender) {

Collection<Predicate> predicates = new ArrayList<Predicate>();
for (String token : tokens) {
token = token != null ? token.trim() : token;
token = token != null ? token.trim() : null;
if (!"?".equals(token == null ? "" : token)) {
throw new InvalidPathException("Expected '?' but found " + token);
}
Expand Down
Expand Up @@ -6,13 +6,15 @@
import java.io.IOException;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


public class Issue629 {
@Test
public void testUncloseParenthesis() throws IOException {
try {
JsonPath jsonPath = JsonPath.compile("$.A.B.C.D(");
assert(false);
fail("accepted jsonpath with unclosed parentheses");
}
catch (Exception e) {
assertTrue(e.getMessage().startsWith("Arguments to function:"));
Expand All @@ -23,7 +25,7 @@ public void testUncloseParenthesis() throws IOException {
public void testUncloseParenthesisWithNestedCall() throws IOException {
try {
JsonPath jsonPath = JsonPath.compile("$.A.B.C.sum(D()");
assert(false);
fail("accepted jsonpath with unclosed parentheses");
}
catch (Exception e) {
assertTrue(e.getMessage().startsWith("Arguments to function:"));
Expand Down

0 comments on commit ed4d2a8

Please sign in to comment.