Skip to content

Commit f284338

Browse files
danielfernandezchriskellet
authored andcommittedApr 16, 2024
Implemented thymeleaf/thymeleaf#809 for Spring 5 (SpEL)
1 parent 6a08f74 commit f284338

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed
 

‎thymeleaf-spring5/src/main/java/org/thymeleaf/spring5/expression/SPELVariableExpressionEvaluator.java

+20-7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.thymeleaf.expression.IExpressionObjects;
4040
import org.thymeleaf.spring5.context.IThymeleafBindStatus;
4141
import org.thymeleaf.spring5.util.FieldUtils;
42+
import org.thymeleaf.spring5.util.SpringStandardExpressionUtils;
4243
import org.thymeleaf.spring5.util.SpringValueFormatter;
4344
import org.thymeleaf.spring5.util.SpringVersionUtils;
4445
import org.thymeleaf.standard.expression.IStandardConversionService;
@@ -177,7 +178,8 @@ public final Object evaluate(
177178
/*
178179
* OBTAIN THE EXPRESSION (SpelExpression OBJECT) FROM THE CACHE, OR PARSE IT
179180
*/
180-
final ComputedSpelExpression exp = obtainComputedSpelExpression(configuration, expression, spelExpression);
181+
final ComputedSpelExpression exp =
182+
obtainComputedSpelExpression(configuration, expression, spelExpression, expContext);
181183

182184

183185
/*
@@ -298,7 +300,9 @@ public final Object evaluate(
298300

299301

300302
private static ComputedSpelExpression obtainComputedSpelExpression(
301-
final IEngineConfiguration configuration, final IStandardVariableExpression expression, final String spelExpression) {
303+
final IEngineConfiguration configuration,
304+
final IStandardVariableExpression expression, final String spelExpression,
305+
final StandardExpressionExecutionContext expContext) {
302306

303307
if (expression instanceof VariableExpression) {
304308

@@ -308,7 +312,7 @@ private static ComputedSpelExpression obtainComputedSpelExpression(
308312
if (cachedExpression != null && cachedExpression instanceof ComputedSpelExpression) {
309313
return (ComputedSpelExpression) cachedExpression;
310314
}
311-
cachedExpression = getExpression(configuration, spelExpression);
315+
cachedExpression = getExpression(configuration, spelExpression, expContext);
312316
if (cachedExpression != null) {
313317
vexpression.setCachedExpression(cachedExpression);
314318
}
@@ -324,20 +328,22 @@ private static ComputedSpelExpression obtainComputedSpelExpression(
324328
if (cachedExpression != null && cachedExpression instanceof ComputedSpelExpression) {
325329
return (ComputedSpelExpression) cachedExpression;
326330
}
327-
cachedExpression = getExpression(configuration, spelExpression);
331+
cachedExpression = getExpression(configuration, spelExpression, expContext);
328332
if (cachedExpression != null) {
329333
vexpression.setCachedExpression(cachedExpression);
330334
}
331335
return (ComputedSpelExpression) cachedExpression;
332336

333337
}
334338

335-
return getExpression(configuration, spelExpression);
339+
return getExpression(configuration, spelExpression, expContext);
336340

337341
}
338342

339343

340-
private static ComputedSpelExpression getExpression(final IEngineConfiguration configuration, final String spelExpression) {
344+
private static ComputedSpelExpression getExpression(
345+
final IEngineConfiguration configuration,
346+
final String spelExpression, final StandardExpressionExecutionContext expContext) {
341347

342348
ComputedSpelExpression exp = null;
343349
ICache<ExpressionCacheKey, Object> cache = null;
@@ -357,9 +363,16 @@ private static ComputedSpelExpression getExpression(final IEngineConfiguration c
357363
PARSER_WITH_COMPILED_SPEL != null && SpringStandardExpressions.isSpringELCompilerEnabled(configuration)?
358364
PARSER_WITH_COMPILED_SPEL : PARSER_WITHOUT_COMPILED_SPEL;
359365

360-
final SpelExpression spelExpressionObject = (SpelExpression) spelExpressionParser.parseExpression(spelExpression);
366+
if (expContext.getRestrictInstantiationAndStatic()
367+
&& SpringStandardExpressionUtils.containsSpELInstantiationOrStatic(spelExpression)) {
368+
throw new TemplateProcessingException(
369+
"Instantiation of new objects and access to static classes is forbidden in this context");
370+
}
371+
361372
final boolean mightNeedExpressionObjects = StandardExpressionUtils.mightNeedExpressionObjects(spelExpression);
362373

374+
final SpelExpression spelExpressionObject = (SpelExpression) spelExpressionParser.parseExpression(spelExpression);
375+
363376
exp = new ComputedSpelExpression(spelExpressionObject, mightNeedExpressionObjects);
364377

365378
if (cache != null && null != exp) {

‎thymeleaf-spring5/src/main/java/org/thymeleaf/spring5/util/SpringExpressionUtils.java ‎thymeleaf-spring5/src/main/java/org/thymeleaf/spring5/util/SpringStandardExpressionUtils.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @since 3.0.12
2727
*
2828
*/
29-
public final class SpringExpressionUtils {
29+
public final class SpringStandardExpressionUtils {
3030

3131

3232
private static final char[] NEW_ARRAY = "wen".toCharArray(); // Inverted "new"
@@ -37,7 +37,7 @@ public static boolean containsSpELInstantiationOrStatic(final String expression)
3737

3838
/*
3939
* Checks whether the expression contains instantiation of objects ("new SomeClass") or makes use of
40-
* static methods ("@SomeClass@") as both are forbidden in certain contexts in restricted mode.
40+
* static methods ("T(SomeClass)") as both are forbidden in certain contexts in restricted mode.
4141
*/
4242

4343
final int explen = expression.length();
@@ -95,7 +95,7 @@ public static boolean containsSpELInstantiationOrStatic(final String expression)
9595

9696

9797

98-
private SpringExpressionUtils() {
98+
private SpringStandardExpressionUtils() {
9999
super();
100100
}
101101

0 commit comments

Comments
 (0)