Ensure MapAccessor#canWrite only returns true for a Map target

Closes gh-33265
This commit is contained in:
Sam Brannen
2024-07-23 18:26:53 +03:00
parent 4684a17f00
commit 4fa9781549
2 changed files with 17 additions and 1 deletions

View File

@@ -81,7 +81,7 @@ public class MapAccessor implements CompilablePropertyAccessor {
@Override
public boolean canWrite(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
return this.allowWrite;
return (this.allowWrite && target instanceof Map);
}
@Override

View File

@@ -81,6 +81,22 @@ class MapAccessorTests {
assertThat(ex.getValue(sec, testMap)).isEqualTo("bar2");
}
@Test
void canWrite() throws Exception {
StandardEvaluationContext context = new StandardEvaluationContext();
Map<String, Object> testMap = getSimpleTestMap();
MapAccessor mapAccessor = new MapAccessor();
assertThat(mapAccessor.canWrite(context, new Object(), "foo")).isFalse();
assertThat(mapAccessor.canWrite(context, testMap, "foo")).isTrue();
// Cannot actually write to an immutable Map, but MapAccessor cannot easily check for that.
assertThat(mapAccessor.canWrite(context, Map.of(), "x")).isTrue();
mapAccessor = new MapAccessor(false);
assertThat(mapAccessor.canWrite(context, new Object(), "foo")).isFalse();
assertThat(mapAccessor.canWrite(context, testMap, "foo")).isFalse();
}
@Test
void isWritable() {
Map<String, Object> testMap = getSimpleTestMap();