39
39
import org .thymeleaf .expression .IExpressionObjects ;
40
40
import org .thymeleaf .spring5 .context .IThymeleafBindStatus ;
41
41
import org .thymeleaf .spring5 .util .FieldUtils ;
42
+ import org .thymeleaf .spring5 .util .SpringStandardExpressionUtils ;
42
43
import org .thymeleaf .spring5 .util .SpringValueFormatter ;
43
44
import org .thymeleaf .spring5 .util .SpringVersionUtils ;
44
45
import org .thymeleaf .standard .expression .IStandardConversionService ;
@@ -177,7 +178,8 @@ public final Object evaluate(
177
178
/*
178
179
* OBTAIN THE EXPRESSION (SpelExpression OBJECT) FROM THE CACHE, OR PARSE IT
179
180
*/
180
- final ComputedSpelExpression exp = obtainComputedSpelExpression (configuration , expression , spelExpression );
181
+ final ComputedSpelExpression exp =
182
+ obtainComputedSpelExpression (configuration , expression , spelExpression , expContext );
181
183
182
184
183
185
/*
@@ -298,7 +300,9 @@ public final Object evaluate(
298
300
299
301
300
302
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 ) {
302
306
303
307
if (expression instanceof VariableExpression ) {
304
308
@@ -308,7 +312,7 @@ private static ComputedSpelExpression obtainComputedSpelExpression(
308
312
if (cachedExpression != null && cachedExpression instanceof ComputedSpelExpression ) {
309
313
return (ComputedSpelExpression ) cachedExpression ;
310
314
}
311
- cachedExpression = getExpression (configuration , spelExpression );
315
+ cachedExpression = getExpression (configuration , spelExpression , expContext );
312
316
if (cachedExpression != null ) {
313
317
vexpression .setCachedExpression (cachedExpression );
314
318
}
@@ -324,20 +328,22 @@ private static ComputedSpelExpression obtainComputedSpelExpression(
324
328
if (cachedExpression != null && cachedExpression instanceof ComputedSpelExpression ) {
325
329
return (ComputedSpelExpression ) cachedExpression ;
326
330
}
327
- cachedExpression = getExpression (configuration , spelExpression );
331
+ cachedExpression = getExpression (configuration , spelExpression , expContext );
328
332
if (cachedExpression != null ) {
329
333
vexpression .setCachedExpression (cachedExpression );
330
334
}
331
335
return (ComputedSpelExpression ) cachedExpression ;
332
336
333
337
}
334
338
335
- return getExpression (configuration , spelExpression );
339
+ return getExpression (configuration , spelExpression , expContext );
336
340
337
341
}
338
342
339
343
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 ) {
341
347
342
348
ComputedSpelExpression exp = null ;
343
349
ICache <ExpressionCacheKey , Object > cache = null ;
@@ -357,9 +363,16 @@ private static ComputedSpelExpression getExpression(final IEngineConfiguration c
357
363
PARSER_WITH_COMPILED_SPEL != null && SpringStandardExpressions .isSpringELCompilerEnabled (configuration )?
358
364
PARSER_WITH_COMPILED_SPEL : PARSER_WITHOUT_COMPILED_SPEL ;
359
365
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
+
361
372
final boolean mightNeedExpressionObjects = StandardExpressionUtils .mightNeedExpressionObjects (spelExpression );
362
373
374
+ final SpelExpression spelExpressionObject = (SpelExpression ) spelExpressionParser .parseExpression (spelExpression );
375
+
363
376
exp = new ComputedSpelExpression (spelExpressionObject , mightNeedExpressionObjects );
364
377
365
378
if (cache != null && null != exp ) {
0 commit comments