Skip to content

Commit c0d7aa2

Browse files
committed
[Security] Include an empty json object in an json array when FLS filters out all fields (#30709)
Prior to this change an json array element with no fields would be omitted from json array. Nested inner hits source filtering relies on the fact that the json array element numbering remains untouched and this causes AOOB exceptions in the ES side during the fetch phase without this change. Closes #30624
1 parent 7c06712 commit c0d7aa2

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReader.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,7 @@ private static List<Object> filter(Iterable<?> iterable, CharacterRunAutomaton i
193193
continue;
194194
}
195195
Map<String, Object> filteredValue = filter((Map<String, ?>)value, includeAutomaton, state);
196-
if (filteredValue.isEmpty() == false) {
197-
filtered.add(filteredValue);
198-
}
196+
filtered.add(filteredValue);
199197
} else if (value instanceof Iterable) {
200198
List<Object> filteredValue = filter((Iterable<?>) value, includeAutomaton, initialState);
201199
if (filteredValue.isEmpty() == false) {

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/FieldSubsetReaderTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,22 @@ public void testSourceFiltering() {
716716
expected.put("foo", subArray);
717717

718718
assertEquals(expected, filtered);
719+
720+
// json array objects that have no matching fields should be left empty instead of being removed:
721+
// (otherwise nested inner hit source filtering fails with AOOB)
722+
map = new HashMap<>();
723+
map.put("foo", "value");
724+
List<Map<?, ?>> values = new ArrayList<>();
725+
values.add(Collections.singletonMap("foo", "1"));
726+
values.add(Collections.singletonMap("baz", "2"));
727+
map.put("bar", values);
728+
729+
include = new CharacterRunAutomaton(Automatons.patterns("bar.baz"));
730+
filtered = FieldSubsetReader.filter(map, include, 0);
731+
732+
expected = new HashMap<>();
733+
expected.put("bar", Arrays.asList(new HashMap<>(), Collections.singletonMap("baz", "2")));
734+
assertEquals(expected, filtered);
719735
}
720736

721737
/**

0 commit comments

Comments
 (0)