Browse Source

prevent truncated subexpressions + misc code cleanup (#763)

pull/747/head
Richard Startin 3 years ago committed by GitHub
parent
commit
ed4d2a82b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 45
      json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java
  2. 6
      json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue629.java

45
json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java

@ -72,8 +72,7 @@ public class PathCompiler {
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) {
@ -135,21 +134,26 @@ public class PathCompiler {
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;
}
}
@ -286,8 +290,8 @@ public class PathCompiler {
// 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();
@ -312,9 +316,6 @@ public class PathCompiler {
switch (c) {
case DOUBLE_QUOTE:
if (priorChar != '\\' && groupQuote > 0) {
if (groupQuote == 0) {
throw new InvalidPathException("Unexpected quote '\"' at character position: " + path.position());
}
groupQuote--;
}
else {
@ -349,7 +350,7 @@ public class PathCompiler {
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:
@ -367,7 +368,7 @@ public class PathCompiler {
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;
@ -431,7 +432,7 @@ public class PathCompiler {
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);
}

6
json-path/src/test/java/com/jayway/jsonpath/internal/function/Issue629.java

@ -6,13 +6,15 @@ import org.junit.Test;
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:"));
@ -23,7 +25,7 @@ public class Issue629 {
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:"));

Loading…
Cancel
Save