Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

A security review finds that your application evaluates SpEL expressions built from request parameters for dynamic filtering. What is the risk and how do you fix it?

Evaluating a SpEL expression that contains request data lets the caller run their own code inside our JVM, because a StandardEvaluationContext can reach any Java type and call any method. That is remote code execution. The real fix is to stop evaluating user input as an expression and translate requests into a fixed filter model; if an expression engine is truly needed, use SimpleEvaluationContext with validated input, and add checks so the pattern does not return.

Why this question is asked

SpEL, the Spring Expression Language, is a small language that Spring uses in @Value, @PreAuthorize, @Cacheable keys and event listener conditions. It is powerful, which is exactly why it is dangerous when the text of the expression comes from a user. Interviewers ask this scenario because it is a real class of vulnerability that has produced several published CVEs in Spring projects, and because the answer shows whether we treat input as data or as code. In this answer, we will recap what SpEL can do, reproduce the attack against a filtering endpoint, see why the default context is unsafe, and then go through the fixes from the weakest to the strongest, finishing with how to stop the pattern from coming back.

1A Quick Recap of SpEL and Evaluation Contexts

A SpEL expression is a string such as price > 100 or customer.name. An ExpressionParser turns the string into an Expression object, and calling getValue() runs it against a root object, the object whose properties the expression reads.

How much an expression may do is decided by its evaluation context. StandardEvaluationContext, the default when we call getValue(root), supports the whole language: property access, method calls on any object, constructors with new, and type references with T(...), which give access to static methods of any class on the classpath. SimpleEvaluationContext is a cut-down context made for data binding. It allows property access and a few operators, and it refuses type references, constructors and bean references. Notice that the danger is not in the parser; it is in the context we evaluate with.

2Reproducing the Problem

Here is the kind of code the security review found. The endpoint lets the client send a filter such as price > 100 and applies it to every product in memory.

Java
@GetMapping("/products")
List<Product> search(@RequestParam String filter) {
Expression e = parser.parseExpression(filter);
return products.stream()
.filter(p -> Boolean.TRUE.equals(e.getValue(p, Boolean.class)))
.toList();
}
// GET /products?filter=price > 100 -> works as intended
// GET /products?filter=T(java.lang.Runtime).getRuntime().exec('touch /tmp/pwned') != null
// -> runs a shell command on our server

Notice that the attacker does not need any special access. The expression reaches the Runtime class through T(...), calls exec(), and the comparison with null keeps the result a boolean so the code does not even fail. From here the attacker can read environment variables with our database password, open a reverse shell, or call any bean method reachable from the root object. So the risk is not a wrong filter result; it is full remote code execution with the rights of our application.

3Why Escaping and Blocklists Do Not Work

The first idea people have is to block dangerous words such as Runtime or exec. This fails because SpEL is a language: the same call can be written with reflection, string concatenation, or other classes such as ProcessBuilder, and a blocklist has to guess every spelling. Escaping does not help either, because there is no part of the input we want to treat as text; the whole string is the program.

There is also a second, smaller risk even without method calls: an expression can be built to use a lot of CPU or memory, for example by repeating a large string or building huge collections. Recent Spring versions limit the length of an expression (10,000 characters by default in Spring Framework 6.x, configurable through SpelParserConfiguration) and the size of repeated strings, but a limit on size is not a security boundary. The only safe rule is the first one: never evaluate an expression that contains user input.

4Fix 1: SimpleEvaluationContext When an Expression Is Unavoidable

Sometimes a product really needs users to write expressions, for example a rules screen used by administrators. Then we evaluate with SimpleEvaluationContext, which removes type references, constructors and arbitrary method calls.

Java
EvaluationContext ctx = SimpleEvaluationContext.forReadOnlyDataBinding().build();
parser.parseExpression("price > 100").getValue(ctx, product, Boolean.class); // true
parser.parseExpression("T(java.lang.Runtime).getRuntime()").getValue(ctx, product);
// SpelEvaluationException: type references are not supported in this context

This closes the remote code execution path, but we still validate the input: limit its length, allow only the property names we expect, and reject anything else before parsing. The reason is defence in depth; a restricted context reduces what an expression can do, it does not make arbitrary text a good interface.

5Fix 2: Replace the Expression With an Explicit Filter Model

The best fix for dynamic filtering is to drop the expression entirely. The client sends structured data: a field, an operator and a value. Our code checks each part against an allow-list and turns it into a query, so user input only ever becomes a bound parameter, never code.

Java
enum Field { PRICE("price"), CATEGORY("category"); final String column; Field(String c) { column = c; } }
enum Op { EQ, GT, LT }
record Filter(Field field, Op op, String value) {}
Specification<Product> toSpec(Filter f) {
return (root, query, cb) -> switch (f.op()) {
case EQ -> cb.equal(root.get(f.field().column), f.value());
case GT -> cb.greaterThan(root.get(f.field().column), f.value());
case LT -> cb.lessThan(root.get(f.field().column), f.value());
};
}
// GET /products?field=PRICE&op=GT&value=100 -> WHERE price > ? with 100 bound
// GET /products?field=Runtime&op=GT&value=x -> 400 Bad Request: no such Field
Step throughHandling a filter request safely
  1. Receive field, op, valuethree separate parameters, not one expression
  2. Field and op in the allow-list?
    YesBuild the queryNoReturn 400 Bad Request
  3. Build a Specificationthe value becomes a bound parameter
  4. Run the query in the databaseno user text is ever executed

4 steps in order. Play them, or step through them one at a time.

User input is turned into data that selects from options we wrote, never into code.

Notice what changed. The set of fields and operators is now in our code, so an unknown field fails at binding time with a 400, and the value goes to the database as a parameter. The filter also runs in the database instead of in memory, which is usually faster. This is the design I would choose: it is safe by construction, easy to document for the API, and easy to test.

6Keeping the Pattern Out of the Codebase

Fixing one endpoint is not enough; the same shortcut tends to appear again. We add the pattern to the code review checklist: any call to parseExpression whose argument is not a constant needs a reviewer to confirm where the text comes from. We also add a static analysis rule. Tools such as Semgrep, CodeQL or the FindSecBugs plugin for SpotBugs can flag expression parsing on data that flows from a request; check which rules your tool ships.

Finally, we search the existing code for other places that build expressions from external data, including @PreAuthorize or @Cacheable strings assembled at runtime, message templates, and configuration loaded from a database that users can edit. Each of those is the same vulnerability with a different entry point.

7Follow-up questions

Is SpEL in @Value or @PreAuthorize dangerous too?

Not by itself. Those expressions are written by developers and compiled into the code, so the text is trusted. They become dangerous only if part of the expression string is built from external input, for example by concatenating a request value into the expression instead of passing it as a method argument that the expression reads.

Does SimpleEvaluationContext make any user expression safe?

It removes the worst risk, because type references, constructors and arbitrary method calls are not supported. It still allows property access on the root object, so users could read any property we expose, and very large or deeply nested expressions can still cost CPU. We therefore combine it with an allow-list of properties, a length limit and, ideally, a structured filter model instead.

8Conclusion

In this answer, we saw that evaluating SpEL built from request parameters gives the caller the full language through StandardEvaluationContext, including T(...) type references and method calls, which means remote code execution. Blocklists cannot fix it because the input is a program. SimpleEvaluationContext with validated input limits the damage when expressions are truly required, but the real fix is a structured filter of allow-listed fields and operators turned into a query with bound values, backed by code review and static analysis so the pattern does not return.

What to say in the interviewAbout 90 seconds aloud

Open with

The risk is remote code execution. When we parse a SpEL expression from a request parameter and evaluate it with the default StandardEvaluationContext, the caller can use type references like T(java.lang.Runtime) and call any method, so they can run shell commands, read our secrets or call our beans.

Then, if they want more

A blocklist does not work because SpEL is a full language and there are many ways to write the same call. So my first rule is that user input is never evaluated as an expression.

For dynamic filtering I would replace the expression with a structured model: the client sends a field, an operator and a value, I check the field and operator against an enum allow-list, and I build a JPA Specification or a query where the value is a bound parameter. Unknown fields return a 400.

If the product genuinely needs user-written expressions, for example an admin rules screen, I would evaluate them with SimpleEvaluationContext, which does not support type references or constructors, and still validate the length and the property names.

Then I would make sure it does not come back: a review rule that any parseExpression call with a non-constant argument needs justification, a static analysis rule for SpEL injection in the build, and a search of the codebase for other places where expressions are built from external data.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A security review finds that your application evaluates SpEL expressions built from...": https://scrivio-interview-library.netlify.app/spring-boot/spring-configuration/cdf39d9dc9/