Skip to content

Commit 4fa9781

Browse files
committed
Ensure MapAccessor#canWrite only returns true for a Map target
Closes gh-33265
1 parent 4684a17 commit 4fa9781

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

spring-context/src/main/java/org/springframework/context/expression/MapAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public TypedValue read(EvaluationContext context, @Nullable Object target, Strin
8181

8282
@Override
8383
public boolean canWrite(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
84-
return this.allowWrite;
84+
return (this.allowWrite && target instanceof Map);
8585
}
8686

8787
@Override

spring-context/src/test/java/org/springframework/context/expression/MapAccessorTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ void compilationSupport() {
8181
assertThat(ex.getValue(sec, testMap)).isEqualTo("bar2");
8282
}
8383

84+
@Test
85+
void canWrite() throws Exception {
86+
StandardEvaluationContext context = new StandardEvaluationContext();
87+
Map<String, Object> testMap = getSimpleTestMap();
88+
89+
MapAccessor mapAccessor = new MapAccessor();
90+
assertThat(mapAccessor.canWrite(context, new Object(), "foo")).isFalse();
91+
assertThat(mapAccessor.canWrite(context, testMap, "foo")).isTrue();
92+
// Cannot actually write to an immutable Map, but MapAccessor cannot easily check for that.
93+
assertThat(mapAccessor.canWrite(context, Map.of(), "x")).isTrue();
94+
95+
mapAccessor = new MapAccessor(false);
96+
assertThat(mapAccessor.canWrite(context, new Object(), "foo")).isFalse();
97+
assertThat(mapAccessor.canWrite(context, testMap, "foo")).isFalse();
98+
}
99+
84100
@Test
85101
void isWritable() {
86102
Map<String, Object> testMap = getSimpleTestMap();

0 commit comments

Comments
 (0)