Skip to content

Commit b957599

Browse files
committed
Adds withAssignmentDisabled() to restrictive EvaluationContext.
1 parent c5cbbaf commit b957599

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/support/ShortcutConfigurable.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.HashMap;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.function.Supplier;
2425
import java.util.stream.Collectors;
2526

2627
import org.springframework.beans.factory.BeanFactory;
@@ -30,6 +31,7 @@
3031
import org.springframework.expression.ConstructorResolver;
3132
import org.springframework.expression.EvaluationContext;
3233
import org.springframework.expression.Expression;
34+
import org.springframework.expression.IndexAccessor;
3335
import org.springframework.expression.MethodResolver;
3436
import org.springframework.expression.OperatorOverloader;
3537
import org.springframework.expression.PropertyAccessor;
@@ -188,6 +190,7 @@ public GatewayEvaluationContext(BeanFactory beanFactory) {
188190
if (restrictive) {
189191
delegate = SimpleEvaluationContext.forPropertyAccessors(new RestrictivePropertyAccessor())
190192
.withMethodResolvers((context, targetObject, name, argumentTypes) -> null)
193+
.withAssignmentDisabled()
191194
.build();
192195
}
193196
else {
@@ -252,6 +255,21 @@ public Object lookupVariable(String name) {
252255
return delegate.lookupVariable(name);
253256
}
254257

258+
@Override
259+
public List<IndexAccessor> getIndexAccessors() {
260+
return delegate.getIndexAccessors();
261+
}
262+
263+
@Override
264+
public TypedValue assignVariable(String name, Supplier<TypedValue> valueSupplier) {
265+
return delegate.assignVariable(name, valueSupplier);
266+
}
267+
268+
@Override
269+
public boolean isAssignmentEnabled() {
270+
return delegate.isAssignmentEnabled();
271+
}
272+
255273
}
256274

257275
class RestrictivePropertyAccessor extends ReflectivePropertyAccessor {

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/ShortcutConfigurableTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@ public class ShortcutConfigurableTests {
4747

4848
private SpelExpressionParser parser;
4949

50+
@Test
51+
public void testNormalizeDefaultTypeWithSpelAssignmentAndInvalidInputFails() {
52+
parser = new SpelExpressionParser();
53+
ShortcutConfigurable shortcutConfigurable = new ShortcutConfigurable() {
54+
@Override
55+
public List<String> shortcutFieldOrder() {
56+
return Arrays.asList("bean", "arg1");
57+
}
58+
};
59+
Map<String, String> args = new HashMap<>();
60+
args.put("bean", "#{ @myMap['my.flag'] = true}");
61+
args.put("arg1", "val1");
62+
assertThatThrownBy(() -> {
63+
ShortcutType.DEFAULT.normalize(args, shortcutConfigurable, parser, this.beanFactory);
64+
}).isInstanceOf(SpelEvaluationException.class);
65+
}
66+
5067
@Test
5168
public void testNormalizeDefaultTypeWithSpelAndInvalidInputFails() {
5269
parser = new SpelExpressionParser();
@@ -221,6 +238,11 @@ public Bar bar() {
221238
return new Bar();
222239
}
223240

241+
@Bean
242+
public Map<String, Object> myMap() {
243+
return new HashMap<>();
244+
}
245+
224246
}
225247

226248
protected static class Bar {

0 commit comments

Comments
 (0)