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

fix(server): should validate request payload: ensure id is not specified #291

Merged
merged 6 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
69 changes: 69 additions & 0 deletions src/main/java/io/cryostat/JsonRequestFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright The Cryostat Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cryostat;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.Provider;

@Provider
public class JsonRequestFilter implements ContainerRequestFilter {

private final ObjectMapper objectMapper = new ObjectMapper();

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
if (requestContext.getMediaType() != null
&& requestContext.getMediaType().isCompatible(MediaType.APPLICATION_JSON_TYPE)) {
byte[] jsonData = requestContext.getEntityStream().readAllBytes();
String json = new String(jsonData, StandardCharsets.UTF_8);
JsonNode rootNode = objectMapper.readTree(json);

if (containsIdField(rootNode)) {
requestContext.abortWith(
Response.status(Response.Status.BAD_REQUEST)
.entity("ID field cannot be specified in the request body.")
.build());
return;
}

requestContext.setEntityStream(
new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)));
}
}

private boolean containsIdField(JsonNode node) {
if (node.has("id")) {
return true;
}
if (node.isContainerNode()) {
for (JsonNode child : node) {
if (containsIdField(child)) {
return true;
}
}
}
return false;
}
}
82 changes: 82 additions & 0 deletions src/test/java/io/cryostat/JsonRequestFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright The Cryostat Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cryostat;

import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class JsonRequestFilterTest {
private JsonRequestFilter filter;
private ContainerRequestContext requestContext;

@BeforeEach
void setUp() {
filter = new JsonRequestFilter();
requestContext = mock(ContainerRequestContext.class);
}

@Test
void testRejectsPayloadWithId() throws Exception {

String[] testPayloads = {
andrewazores marked this conversation as resolved.
Show resolved Hide resolved
"{\"id\": 1}",
"{\n \"id\": 1\n}",
"{\n \"foo\": \"bar\",\n \"id\": 1\n}",
"[\n {\n \"id\": 1\n }\n]"
};

for (String payload : testPayloads) {
simulateRequest(payload);
verify(requestContext, times(1)).abortWith(any(Response.class));
reset(requestContext);
}
}

@Test
void testAllowsPayloadWithoutId() throws Exception {

String[] testPayloads = {
"{ \"message\": \"this text includes the string literal \\\"id\\\"\" }",
"{}",
"[]",
"{ \"foo\": \"bar\" }"
};

for (String payload : testPayloads) {
simulateRequest(payload);
verify(requestContext, never()).abortWith(any(Response.class));
reset(requestContext);
}
}

private void simulateRequest(String jsonPayload) throws Exception {

ByteArrayInputStream payloadStream =
new ByteArrayInputStream(jsonPayload.getBytes(StandardCharsets.UTF_8));
when(requestContext.getEntityStream()).thenReturn(payloadStream);
when(requestContext.getMediaType()).thenReturn(MediaType.APPLICATION_JSON_TYPE);
filter.filter(requestContext);
}
}
Loading