Protect against missing SpEL selection expression

Update InternalSpelExpressionParser to ensure that SPeL selections
('$[...]', '^[...]', '?[...]') always include a nested expression.

Issue: SPR-10328
This commit is contained in:
Phillip Webb
2013-02-25 14:25:47 -08:00
parent 9a6c6b9ee6
commit 82bd06f255
4 changed files with 19 additions and 3 deletions

View File

@@ -109,7 +109,8 @@ public enum SpelMessage {
OPERAND_NOT_DECREMENTABLE(Kind.ERROR,1067,"the expression component ''{0}'' does not support decrement"), //
NOT_ASSIGNABLE(Kind.ERROR,1068,"the expression component ''{0}'' is not assignable"), //
MISSING_CHARACTER(Kind.ERROR,1069,"missing expected character ''{0}''"),
LEFT_OPERAND_PROBLEM(Kind.ERROR,1070, "Problem parsing left operand");
LEFT_OPERAND_PROBLEM(Kind.ERROR,1070, "Problem parsing left operand"),
MISSING_SELECTION_EXPRESSION(Kind.ERROR, 1071, "A required selection expression has not been specified");
private Kind kind;
private int code;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@ import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
@@ -54,7 +55,9 @@ public class Selection extends SpelNodeImpl {
private final boolean nullSafe;
public Selection(boolean nullSafe, int variant,int pos,SpelNodeImpl expression) {
super(pos,expression);
super(pos, expression != null ? new SpelNodeImpl[] { expression }
: new SpelNodeImpl[] {});
Assert.notNull(expression, "Expression must not be null");
this.nullSafe = nullSafe;
this.variant = variant;
}

View File

@@ -557,6 +557,9 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser {
}
nextToken();
SpelNodeImpl expr = eatExpression();
if(expr == null) {
raiseInternalException(toPos(t), SpelMessage.MISSING_SELECTION_EXPRESSION);
}
eatToken(TokenKind.RSQUARE);
if (t.kind==TokenKind.SELECT_FIRST) {
constructedNodes.push(new Selection(nullSafeNavigation,Selection.FIRST,toPos(t),expr));

View File

@@ -27,6 +27,7 @@ import static org.junit.Assert.fail;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
@@ -1744,6 +1745,14 @@ public class SpelReproTests extends ExpressionTestCase {
parseExpression.getValue(context);
}
@Test
public void SPR_10328() throws Exception {
thrown.expect(SpelParseException.class);
thrown.expectMessage("EL1071E:(pos 2): A required selection expression has not been specified");
Expression exp = parser.parseExpression("$[]");
exp.getValue(Arrays.asList("foo", "bar", "baz"));
}
public static class BooleanHolder {
private Boolean simpleProperty = true;