Spel security fix; fixes gh-917
This commit is contained in:
@@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.ExpressionParser;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.expression.spel.support.SimpleEvaluationContext;
|
||||
|
||||
/**
|
||||
* Uses SPEL to evaluate the expression. If an exception is thrown will return
|
||||
@@ -37,9 +38,12 @@ class SpelTagValueExpressionResolver implements TagValueExpressionResolver {
|
||||
@Override
|
||||
public String resolve(String expression, Object parameter) {
|
||||
try {
|
||||
SimpleEvaluationContext context = SimpleEvaluationContext
|
||||
.forReadOnlyDataBinding()
|
||||
.build();
|
||||
ExpressionParser expressionParser = new SpelExpressionParser();
|
||||
Expression expressionToEvaluate = expressionParser.parseExpression(expression);
|
||||
return expressionToEvaluate.getValue(parameter, String.class);
|
||||
return expressionToEvaluate.getValue(context, parameter, String.class);
|
||||
} catch (Exception e) {
|
||||
log.error("Exception occurred while tying to evaluate the SPEL expression [" + expression + "]", e);
|
||||
}
|
||||
|
||||
@@ -27,10 +27,16 @@ public class SpelTagValueExpressionResolverTests {
|
||||
@Test
|
||||
public void should_use_spel_to_resolve_a_value() throws Exception {
|
||||
SpelTagValueExpressionResolver resolver = new SpelTagValueExpressionResolver();
|
||||
MyObject myObject = new MyObject();
|
||||
myObject.name = "hello";
|
||||
|
||||
String resolved = resolver.resolve("length() + 1", "foo");
|
||||
String resolved = resolver.resolve("name + ' world'", myObject);
|
||||
|
||||
then(resolved).isEqualTo("4");
|
||||
then(resolved).isEqualTo("hello world");
|
||||
}
|
||||
|
||||
public static class MyObject {
|
||||
public String name;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user