← Java / Spring · Contents

JAVA-016 / Advanced

SpEL injection: evaluating user input as expressions

A03:2021 Injection · CWE-917 Expression Language Injection

Why is this a problem?

T(java.lang.Runtime).getRuntime().exec(...) is executed.

How to fix it

java
SimpleEvaluationContext ctx = SimpleEvaluationContext.forReadOnlyDataBinding().build();
parser.parseExpression(userInput).getValue(ctx);  // Context disallowing T() and method invocation
// Avoid interpreting user input as an expression whenever possible

Further reading