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. 37
      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

37
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 '..'"); fail("Path must not end with a '.' or '..'");
} }
LinkedList<Predicate> filterStack = new LinkedList<Predicate>(asList(filters)); LinkedList<Predicate> filterStack = new LinkedList<Predicate>(asList(filters));
Path p = new PathCompiler(ci, filterStack).compile(); return new PathCompiler(ci, filterStack).compile();
return p;
} catch (Exception e) { } catch (Exception e) {
InvalidPathException ipe; InvalidPathException ipe;
if (e instanceof InvalidPathException) { if (e instanceof InvalidPathException) {
@ -135,22 +134,27 @@ public class PathCompiler {
switch (c) { switch (c) {
case OPEN_SQUARE_BRACKET: case OPEN_SQUARE_BRACKET:
return readBracketPropertyToken(appender) || if (!readBracketPropertyToken(appender) && !readArrayToken(appender) && !readWildCardToken(appender)
readArrayToken(appender) || && !readFilterToken(appender) && !readPlaceholderToken(appender)) {
readWildCardToken(appender) ||
readFilterToken(appender) ||
readPlaceholderToken(appender) ||
fail("Could not parse token starting at position " + path.position() + ". Expected ?, ', 0-9, * "); fail("Could not parse token starting at position " + path.position() + ". Expected ?, ', 0-9, * ");
}
return true;
case PERIOD: case PERIOD:
return readDotToken(appender) || if (!readDotToken(appender)) {
fail("Could not parse token starting at position " + path.position()); fail("Could not parse token starting at position " + path.position());
}
return true;
case WILDCARD: case WILDCARD:
return readWildCardToken(appender) || if (!readWildCardToken(appender)) {
fail("Could not parse token starting at position " + path.position()); fail("Could not parse token starting at position " + path.position());
}
return true;
default: default:
return readPropertyOrFunctionToken(appender) || if (!readPropertyOrFunctionToken(appender)) {
fail("Could not parse token starting at position " + path.position()); 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 // Parenthesis starts at 1 since we're marking the start of a function call, the close paren will denote the
// last parameter boundary // last parameter boundary
Integer groupParen = 1, groupBracket = 0, groupBrace = 0, groupQuote = 0; int groupParen = 1, groupBracket = 0, groupBrace = 0, groupQuote = 0;
Boolean endOfStream = false; boolean endOfStream = false;
char priorChar = 0; char priorChar = 0;
List<Parameter> parameters = new ArrayList<Parameter>(); List<Parameter> parameters = new ArrayList<Parameter>();
StringBuilder parameter = new StringBuilder(); StringBuilder parameter = new StringBuilder();
@ -312,9 +316,6 @@ public class PathCompiler {
switch (c) { switch (c) {
case DOUBLE_QUOTE: case DOUBLE_QUOTE:
if (priorChar != '\\' && groupQuote > 0) { if (priorChar != '\\' && groupQuote > 0) {
if (groupQuote == 0) {
throw new InvalidPathException("Unexpected quote '\"' at character position: " + path.position());
}
groupQuote--; groupQuote--;
} }
else { else {
@ -349,7 +350,7 @@ public class PathCompiler {
case CLOSE_PARENTHESIS: case CLOSE_PARENTHESIS:
groupParen--; groupParen--;
//CS304 Issue link: https://github.com/json-path/JsonPath/issues/620 //CS304 Issue link: https://github.com/json-path/JsonPath/issues/620
if (0 > groupParen ) { if (0 > groupParen || priorChar == '(') {
parameter.append(c); parameter.append(c);
} }
case COMMA: case COMMA:
@ -367,7 +368,7 @@ public class PathCompiler {
param = new Parameter(parameter.toString()); param = new Parameter(parameter.toString());
break; break;
case PATH: case PATH:
LinkedList<Predicate> predicates = new LinkedList<Predicate>(); LinkedList<Predicate> predicates = new LinkedList<>();
PathCompiler compiler = new PathCompiler(parameter.toString(), predicates); PathCompiler compiler = new PathCompiler(parameter.toString(), predicates);
param = new Parameter(compiler.compile()); param = new Parameter(compiler.compile());
break; break;
@ -431,7 +432,7 @@ public class PathCompiler {
Collection<Predicate> predicates = new ArrayList<Predicate>(); Collection<Predicate> predicates = new ArrayList<Predicate>();
for (String token : tokens) { for (String token : tokens) {
token = token != null ? token.trim() : token; token = token != null ? token.trim() : null;
if (!"?".equals(token == null ? "" : token)) { if (!"?".equals(token == null ? "" : token)) {
throw new InvalidPathException("Expected '?' but found " + 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 java.io.IOException;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class Issue629 { public class Issue629 {
@Test @Test
public void testUncloseParenthesis() throws IOException { public void testUncloseParenthesis() throws IOException {
try { try {
JsonPath jsonPath = JsonPath.compile("$.A.B.C.D("); JsonPath jsonPath = JsonPath.compile("$.A.B.C.D(");
assert(false); fail("accepted jsonpath with unclosed parentheses");
} }
catch (Exception e) { catch (Exception e) {
assertTrue(e.getMessage().startsWith("Arguments to function:")); assertTrue(e.getMessage().startsWith("Arguments to function:"));
@ -23,7 +25,7 @@ public class Issue629 {
public void testUncloseParenthesisWithNestedCall() throws IOException { public void testUncloseParenthesisWithNestedCall() throws IOException {
try { try {
JsonPath jsonPath = JsonPath.compile("$.A.B.C.sum(D()"); JsonPath jsonPath = JsonPath.compile("$.A.B.C.sum(D()");
assert(false); fail("accepted jsonpath with unclosed parentheses");
} }
catch (Exception e) { catch (Exception e) {
assertTrue(e.getMessage().startsWith("Arguments to function:")); assertTrue(e.getMessage().startsWith("Arguments to function:"));

Loading…
Cancel
Save