Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for null array DataType #1157

Merged
merged 14 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ public boolean validate() {
return false;
}

//null element in the values of all the possible keys with DataType as ARRAY
for (Entry<String, DataType> allowedKey : allowedKeys.entrySet()) {
JsonNode value = contentAsNode.get(allowedKey.getKey());
if (value != null) {
if (hasNullArrayElement(value)) {
this.errorType = ErrorType.NULL_ARRAY_ELEMENT;
return false;
}
}
}
return valid;
}

Expand Down Expand Up @@ -253,6 +263,10 @@ public XContentBuilder errorsAsXContent(RestChannel channel) {
builder.field(entry.getKey(), entry.getValue());
}
break;
case NULL_ARRAY_ELEMENT:
builder.field("status", "error");
builder.field("reason", ErrorType.NULL_ARRAY_ELEMENT.getMessage());
break;
default:
builder.field("status", "error");
builder.field("reason", errorType.getMessage());
Expand Down Expand Up @@ -281,7 +295,8 @@ public static enum DataType {
public static enum ErrorType {
NONE("ok"), INVALID_CONFIGURATION("Invalid configuration"), INVALID_PASSWORD("Invalid password"), WRONG_DATATYPE("Wrong datatype"),
BODY_NOT_PARSEABLE("Could not parse content of request."), PAYLOAD_NOT_ALLOWED("Request body not allowed for this action."),
PAYLOAD_MANDATORY("Request body required for this action."), SECURITY_NOT_INITIALIZED("Security index not initialized");
PAYLOAD_MANDATORY("Request body required for this action."), SECURITY_NOT_INITIALIZED("Security index not initialized"),
NULL_ARRAY_ELEMENT("`null` is not allowed as json array element");

private String message;

Expand All @@ -297,4 +312,19 @@ public String getMessage() {
protected final boolean hasParams() {
return param != null && param.length > 0;
}
}

private boolean hasNullArrayElement(JsonNode node) {
for (JsonNode element: node) {
if(element.isNull()) {
if (node.isArray()) {
return true;
}
} else if (element.isContainerNode()) {
if (hasNullArrayElement(element)) {
return true;
}
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,15 @@ public void testActionGroupsApiForNonSuperAdmin() throws Exception {
Assert.assertEquals(HttpStatus.SC_NOT_FOUND, response.getStatusCode());

}

@Test
public void checkNullElementsInArray() throws Exception{
setup();
rh.keystore = "restapi/kirk-keystore.jks";
rh.sendAdminCertificate = true;

String body = FileHelper.loadFile("restapi/actiongroup_null_array_element.json");
HttpResponse response = rh.executePutRequest(ENDPOINT + "/CRUD_UT", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.opensearch.security.test.helper.rest.RestHelper.HttpResponse;
import org.opensearch.security.test.helper.file.FileHelper;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.Assert;
import com.google.common.collect.ImmutableList;

import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -212,4 +214,14 @@ public void testNodesDnApiAuditComplianceLogging() throws Exception {
assertThat(actualCategoryCounts, equalTo(expectedCategoryCounts));
}

}
@Test
public void checkNullElementsInArray() throws Exception{
setup();
rh.keystore = "restapi/kirk-keystore.jks";
rh.sendAdminCertificate = true;

String body = FileHelper.loadFile("restapi/nodesdn_null_array_element.json");
HttpResponse response = rh.executePutRequest(ENDPOINT+ "/nodesdn", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

package org.opensearch.security.dlic.rest.api;

import java.util.Arrays;
import java.util.List;

import org.junit.runner.RunWith;
Expand Down Expand Up @@ -489,7 +488,8 @@ public void testRolesApiForNonSuperAdmin() throws Exception {
Assert.assertEquals(HttpStatus.SC_FORBIDDEN, response.getStatusCode());

// Put read only roles
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_transport_client", "[{ \"op\": \"replace\", \"path\": \"/description\", \"value\": \"foo\" }]", new Header[0]);
response = rh.executePutRequest( ENDPOINT + "/roles/opendistro_security_transport_client",
FileHelper.loadFile("restapi/roles_captains.json"), new Header[0]);
Assert.assertEquals(HttpStatus.SC_FORBIDDEN, response.getStatusCode());

// Patch single read only roles
Expand All @@ -509,8 +509,9 @@ public void testRolesApiForNonSuperAdmin() throws Exception {
Assert.assertEquals(HttpStatus.SC_NOT_FOUND, response.getStatusCode());

// put hidden role
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_internal", "[{ \"op\": \"replace\", \"path\": \"/description\", \"value\": \"foo\" }]", new Header[0]);
Assert.assertEquals(HttpStatus.SC_NOT_FOUND, response.getStatusCode());
String body = FileHelper.loadFile("restapi/roles_captains.json");
response = rh.executePutRequest( ENDPOINT+ "/roles/opendistro_security_internal", body, new Header[0]);
Assert.assertEquals(org.apache.http.HttpStatus.SC_NOT_FOUND, response.getStatusCode());

// Patch single hidden roles
response = rh.executePatchRequest(ENDPOINT + "/roles/opendistro_security_internal", "[{ \"op\": \"replace\", \"path\": \"/description\", \"value\": \"foo\" }]", new Header[0]);
Expand All @@ -522,4 +523,35 @@ public void testRolesApiForNonSuperAdmin() throws Exception {

}

@Test
public void checkNullElementsInArray() throws Exception{
setup();
rh.keystore = "restapi/kirk-keystore.jks";
rh.sendAdminCertificate = true;

String body = FileHelper.loadFile("restapi/roles_null_array_element_cluster_permissions.json");
HttpResponse response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());

body = FileHelper.loadFile("restapi/roles_null_array_element_index_permissions.json");
response = rh.executePutRequest(ENDPOINT+ "/roles/opendistro_security_role_starfleet", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());

body = FileHelper.loadFile("restapi/roles_null_array_element_tenant_permissions.json");
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());

body = FileHelper.loadFile("restapi/roles_null_array_element_index_patterns.json");
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());

body = FileHelper.loadFile("restapi/roles_null_array_element_masked_fields.json");
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());

body = FileHelper.loadFile("restapi/roles_null_array_element_allowed_actions.json");
response = rh.executePutRequest(ENDPOINT + "/roles/opendistro_security_role_starfleet", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,26 @@ public void testRolesMappingApiForNonSuperAdmin() throws Exception {
Assert.assertEquals(HttpStatus.SC_NOT_FOUND, response.getStatusCode());

}

@Test
public void checkNullElementsInArray() throws Exception{
setup();
rh.keystore = "restapi/kirk-keystore.jks";
rh.sendAdminCertificate = true;

String body = FileHelper.loadFile("restapi/rolesmapping_null_array_element_users.json");
HttpResponse response = rh.executePutRequest(ENDPOINT + "/rolesmapping/opendistro_security_role_starfleet_captains",
body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());

body = FileHelper.loadFile("restapi/rolesmapping_null_array_element_backend_roles.json");
response = rh.executePutRequest(ENDPOINT + "/rolesmapping/opendistro_security_role_starfleet_captains",
body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());

body = FileHelper.loadFile("restapi/rolesmapping_null_array_element_hosts.json");
response = rh.executePutRequest(ENDPOINT + "/rolesmapping/opendistro_security_role_starfleet_captains",
body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -637,4 +637,16 @@ public void testUserApiForNonSuperAdmin() throws Exception {
Assert.assertEquals(HttpStatus.SC_NOT_FOUND, response.getStatusCode());

}

@Test
public void checkNullElementsInArray() throws Exception{
setup();
rh.keystore = "restapi/kirk-keystore.jks";
rh.sendAdminCertificate = true;

String body = FileHelper.loadFile("restapi/users_null_array_element.json");
HttpResponse response = rh.executePutRequest(ENDPOINT + "/internalusers/picard", body, new Header[0]);
Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"allowed_actions": [null, "OPENDISTRO_SECURITY_WRITE"]
}

3 changes: 3 additions & 0 deletions src/test/resources/restapi/nodesdn_null_array_element.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"nodes_dn" : [null]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"cluster_permissions" : ["cluster:monitor1*"],
"index_permissions" : [{
"index_patterns" : [ "*" ],
"masked_fields" : [ "abvfg", "*ip_dest*::/[0-9]{1,3}$/::XXX", "*ip_source*::/[0-9]{1,3}$/::XXX::/^[0-9]{1,3}/::***", "customer.name::SHA-512", "/[0-9]{1,3}/::SHA-512" ],
"allowed_actions" : [ "123", null ]
}],
"tenant_permissions" : [ ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"cluster_permissions" : [null],
"index_permissions" : [ {
"index_patterns" : [ "*" ],
"masked_fields" : [ "abvfg", "*ip_dest*::/[0-9]{1,3}$/::XXX", "*ip_source*::/[0-9]{1,3}$/::XXX::/^[0-9]{1,3}/::***", "customer.name::SHA-512", "/[0-9]{1,3}/::SHA-512" ],
"allowed_actions" : [ "123", "indices:data/read/*" ]
} ],
"tenant_permissions" : [ ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"cluster_permissions" : ["cluster:monitor1*"],
"index_permissions" : [{
"index_patterns" : [null],
"masked_fields" : [ "abvfg", "*ip_dest*::/[0-9]{1,3}$/::XXX", "*ip_source*::/[0-9]{1,3}$/::XXX::/^[0-9]{1,3}/::***", "customer.name::SHA-512", "/[0-9]{1,3}/::SHA-512" ],
"allowed_actions" : [ "123", "indices:data/read/*" ]
}],
"tenant_permissions" : [ ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cluster_permissions" : ["cluster:monitor1*"],
"index_permissions" : [null],
"tenant_permissions" : [ ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"cluster_permissions" : ["cluster:monitor1*"],
"index_permissions" : [{
"index_patterns" : [ "*" ],
"masked_fields" : [ "abvfg", null, "*ip_source*::/[0-9]{1,3}$/::XXX::/^[0-9]{1,3}/::***", "customer.name::SHA-512", "/[0-9]{1,3}/::SHA-512" ],
"allowed_actions" : [ "123", "indices:data/read/*" ]
}],
"tenant_permissions" : [ ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"cluster_permissions" : ["cluster:monitor1*"],
"index_permissions" : [{
"index_patterns" : [ "*" ],
"masked_fields" : [ "abvfg", "*ip_dest*::/[0-9]{1,3}$/::XXX", "*ip_source*::/[0-9]{1,3}$/::XXX::/^[0-9]{1,3}/::***", "customer.name::SHA-512", "/[0-9]{1,3}/::SHA-512" ],
"allowed_actions" : [ "123", "indices:data/read/*" ]
}],
"tenant_permissions" : [null]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"users": [
"user1",
"user2"
],
"backend_roles": [
"captains",
null
],
"hosts": [
"8.8.8.8",
"8.8.4.4"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"users": [
"user1",
"user2"
],
"backend_roles": [
"captains",
"role2"
],
"hosts": [
null,
"8.8.4.4"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"users": [
null
],
"backend_roles": [
"captains"
],
"hosts": [
"8.8.8.8"
]
}
5 changes: 5 additions & 0 deletions src/test/resources/restapi/users_null_array_element.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"backend_roles": [
null
]
}