mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix: handle blank content types in API Gateway models and OTLP metrics (#9723)
API Gateway's request-model lookup, the OTLP metrics endpoint and its access-denied response filter still parsed the Content-Type header themselves, and an empty or whitespace one throws there. They now read it through ResteasyRequests.mediaType, which a new HttpHeaders overload extends to resource methods, so a blank header acts as an absent one: API Gateway validates against the application/json model, the OTLP endpoint answers its 415, and a denial keeps its 403 and protobuf body.
This commit is contained in:
1 parent
093955f9f2
commit
b7a12c146b
7 files changed
+84
-6
No files matched your search
@@ -32,7 +32,7 @@ public class OtlpMetricsAuthResponseFilter implements ContainerResponseFilter {
|
||||
return;
|
||||
}
|
||||
String message = extractMessage(response);
|
||||
Response otlpError = OtlpMetricsResponses.error(request.getMediaType(), 403, 7, message);
|
||||
Response otlpError = OtlpMetricsResponses.error(ResteasyRequests.mediaType(request), 403, 7, message);
|
||||
response.setStatus(403);
|
||||
response.setEntity(otlpError.getEntity());
|
||||
String contentType = otlpError.getMediaType() != null
|
||||
|
||||
@@ -31,6 +31,12 @@ public final class ResteasyRequests {
|
||||
return contentType != null && contentType.isBlank() ? null : ctx.getMediaType();
|
||||
}
|
||||
|
||||
/** The same blank-header handling for a JAX-RS resource with injected headers. */
|
||||
public static MediaType mediaType(HttpHeaders headers) {
|
||||
String contentType = headers.getHeaderString(HttpHeaders.CONTENT_TYPE);
|
||||
return contentType != null && contentType.isBlank() ? null : headers.getMediaType();
|
||||
}
|
||||
|
||||
/** The RESTEasy Reactive context under a JAX-RS one, or null for a synthetic context. */
|
||||
public static ResteasyReactiveRequestContext of(ContainerRequestContext ctx) {
|
||||
return ctx instanceof ResteasyReactiveContainerRequestContext container
|
||||
|
||||
@@ -1314,9 +1314,9 @@ public class ApiGatewayExecuteController {
|
||||
if (validator.isValidateRequestBody()) {
|
||||
Map<String, String> requestModels = method.getRequestModels();
|
||||
if (requestModels != null && !requestModels.isEmpty()) {
|
||||
String contentType = headers.getMediaType() != null
|
||||
? headers.getMediaType().getType() + "/" + headers.getMediaType().getSubtype()
|
||||
: "application/json";
|
||||
MediaType mediaType = ResteasyRequests.mediaType(headers);
|
||||
String contentType = mediaType == null ? MediaType.APPLICATION_JSON
|
||||
: mediaType.getType() + "/" + mediaType.getSubtype();
|
||||
String modelName = requestModels.get(contentType);
|
||||
if (modelName == null) {
|
||||
modelName = requestModels.get("$default");
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.google.protobuf.DynamicMessage;
|
||||
import com.google.protobuf.util.JsonFormat;
|
||||
import io.floci.aws.core.common.IamOperation;
|
||||
import io.floci.aws.core.common.RequestContext;
|
||||
import io.floci.aws.core.common.ResteasyRequests;
|
||||
import io.floci.aws.core.common.ServiceRegistry;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
@@ -44,10 +45,10 @@ public class OtlpMetricsController {
|
||||
@POST
|
||||
@Consumes(MediaType.WILDCARD)
|
||||
public Response ingest(@Context HttpHeaders headers, InputStream input) {
|
||||
MediaType mediaType = ResteasyRequests.mediaType(headers);
|
||||
if (!serviceRegistry.isServiceEnabled("monitoring")) {
|
||||
return OtlpMetricsResponses.error(headers.getMediaType(), 404, 5, "CloudWatch metrics service is disabled.");
|
||||
return OtlpMetricsResponses.error(mediaType, 404, 5, "CloudWatch metrics service is disabled.");
|
||||
}
|
||||
MediaType mediaType = headers.getMediaType();
|
||||
boolean json = mediaType != null && mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
|
||||
boolean protobuf = mediaType != null && mediaType.isCompatible(MediaType.valueOf(PROTOBUF));
|
||||
if (!json && !protobuf) {
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
import com.google.rpc.Status;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.container.ContainerResponseContext;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class OtlpMetricsAuthResponseFilterTest {
|
||||
|
||||
@Test
|
||||
void blankContentTypeKeepsTheDeniedResponseInProtobuf() throws Exception {
|
||||
ContainerRequestContext request = mock(ContainerRequestContext.class);
|
||||
UriInfo uri = mock(UriInfo.class);
|
||||
when(request.getMethod()).thenReturn("POST");
|
||||
when(request.getUriInfo()).thenReturn(uri);
|
||||
when(uri.getPath()).thenReturn("v1/metrics");
|
||||
when(request.getHeaderString(HttpHeaders.CONTENT_TYPE)).thenReturn(" ");
|
||||
ContainerResponseContext response = mock(ContainerResponseContext.class);
|
||||
MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
|
||||
when(response.getHeaders()).thenReturn(headers);
|
||||
when(response.getStatus()).thenReturn(403);
|
||||
when(response.getEntity()).thenReturn(new AwsErrorResponse("AccessDeniedException", "Access denied"));
|
||||
|
||||
new OtlpMetricsAuthResponseFilter().filter(request, response);
|
||||
|
||||
verify(request, never()).getMediaType();
|
||||
verify(response).setStatus(403);
|
||||
assertEquals("application/x-protobuf", headers.getFirst(HttpHeaders.CONTENT_TYPE));
|
||||
ArgumentCaptor<byte[]> body = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(response).setEntity(body.capture());
|
||||
Status status = Status.parseFrom(body.getValue());
|
||||
assertEquals(7, status.getCode());
|
||||
assertEquals("Access denied", status.getMessage());
|
||||
}
|
||||
}
|
||||
+14
@@ -1,14 +1,18 @@
|
||||
package io.floci.aws.services.apigateway;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.config.EncoderConfig;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@QuarkusTest
|
||||
class ApiGatewayRequestModelContentTypeIntegrationTest {
|
||||
@@ -67,6 +71,16 @@ class ApiGatewayRequestModelContentTypeIntegrationTest {
|
||||
invoke("text/plain", "{\"fallback\":\"present\"}", 200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankContentTypeUsesTheJsonModel() {
|
||||
deploy(Map.of("application/json", "Named"));
|
||||
given().config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig()
|
||||
.appendDefaultContentCharsetToContentTypeIfUndefined(false)))
|
||||
.header("Content-Type", "").body("{\"unexpected\":true}".getBytes(StandardCharsets.UTF_8))
|
||||
.post("/execute-api/" + apiId + "/test/").then().statusCode(400)
|
||||
.body("message", equalTo("Invalid request body"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mediaTypeParametersKeepTheMatchingModel() {
|
||||
deploy(Map.of("application/json", "Named"));
|
||||
|
||||
+11
@@ -20,6 +20,8 @@ import io.opentelemetry.proto.metrics.v1.Sum;
|
||||
import io.opentelemetry.proto.metrics.v1.Summary;
|
||||
import io.opentelemetry.proto.metrics.v1.SummaryDataPoint;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.config.EncoderConfig;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -80,6 +82,15 @@ class OtlpMetricsIntegrationTest {
|
||||
.post("/v1/metrics").then().statusCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankContentTypeReturnsUnsupportedMediaType() throws Exception {
|
||||
byte[] body = given().config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig()
|
||||
.appendDefaultContentCharsetToContentTypeIfUndefined(false)))
|
||||
.header("Content-Type", "").post("/v1/metrics").then().statusCode(415)
|
||||
.contentType("application/x-protobuf").extract().asByteArray();
|
||||
assertEquals("Unsupported content type.", Status.parseFrom(body).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsRequestsWithTooManyDataPoints() throws Exception {
|
||||
Gauge.Builder gauge = Gauge.newBuilder();
|
||||
|
||||
Reference in new issue
Block a user