Skip to content

Commit 4924d61

Browse files
committed
Add mapping name validation
1 parent 357004d commit 4924d61

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/rolemapping/TransportGetRoleMappingsAction.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222

2323
import java.util.Arrays;
2424
import java.util.Comparator;
25-
import java.util.HashMap;
2625
import java.util.HashSet;
2726
import java.util.List;
28-
import java.util.Map;
2927
import java.util.Set;
3028
import java.util.stream.Stream;
3129

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/rolemapping/TransportPutRoleMappingAction.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public TransportPutRoleMappingAction(
3939

4040
@Override
4141
protected void doExecute(Task task, final PutRoleMappingRequest request, final ActionListener<PutRoleMappingResponse> listener) {
42+
validateMappingName(request.getName());
4243
if (clusterStateRoleMapper.hasMapping(request.getName())) {
4344
// Allow to define a mapping with the same name in the native role mapping store as the file_settings namespace, but add a
4445
// warning header to signal to the caller that this could be a problem.
@@ -54,4 +55,13 @@ protected void doExecute(Task task, final PutRoleMappingRequest request, final A
5455
ActionListener.wrap(created -> listener.onResponse(new PutRoleMappingResponse(created)), listener::onFailure)
5556
);
5657
}
58+
59+
private void validateMappingName(String mappingName) {
60+
String reservedSuffix = " (read only)";
61+
if (mappingName.endsWith(reservedSuffix)) {
62+
throw new IllegalArgumentException(
63+
"Invalid mapping name [" + mappingName + "]. [" + reservedSuffix + "] is not an allowed suffix"
64+
);
65+
}
66+
}
5767
}

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/action/rolemapping/TransportPutRoleMappingActionTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ public void testPutValidMapping() throws Exception {
9393
assertThat(mapping.getMetadata().get("dumb"), equalTo(true));
9494
}
9595

96+
public void testPutMappingWithInvalidName() {
97+
final FieldExpression expression = new FieldExpression("username", Collections.singletonList(new FieldExpression.FieldValue("*")));
98+
IllegalArgumentException illegalArgumentException = expectThrows(
99+
IllegalArgumentException.class,
100+
() -> put("anarchy (read only)", expression, "superuser", Collections.singletonMap("dumb", true))
101+
);
102+
103+
assertThat(
104+
illegalArgumentException.getMessage(),
105+
equalTo("Invalid mapping name [anarchy (read only)]. [ (read only)] is not an allowed suffix")
106+
);
107+
}
108+
96109
private PutRoleMappingResponse put(String name, FieldExpression expression, String role, Map<String, Object> metadata)
97110
throws Exception {
98111
final PutRoleMappingRequest request = new PutRoleMappingRequest();

0 commit comments

Comments
 (0)