Light, fluffy, and always free - The AWS Local Emulator alternative
fix(core): accept only JSON objects as JSON protocol request bodies (#3509)
Niels Kootstra9f9a828439a0
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.javaindex a8fb8afa5c..3ad4b3b93f 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJson11Controller.java@@ -276,10 +276,18 @@ public class AwsJson11Controller {JsonNode request;try {- request = strictBodyReader.readTree(body);+ // No payload is how an operation without input goes over the wire, so it reads as {}.+ request = body == null || body.isBlank()+ ? objectMapper.createObjectNode()+ : strictBodyReader.readTree(body);} catch (JsonProcessingException e) {return JsonErrorResponseUtils.createSerializationErrorResponse();}+ // Input is always a structure: a bare null, array or scalar parses but is still a+ // client serialization error rather than something every handler must guard against.+ if (!request.isObject()) {+ return JsonErrorResponseUtils.createSerializationErrorResponse();+ }try {String region = regionResolver.resolveRegion(httpHeaders);diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.javaindex a858986302..d125ce5a64 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsJsonController.java@@ -111,10 +111,18 @@ public class AwsJsonController {JsonNode request;try {- request = strictBodyReader.readTree(body);+ // No payload is how an operation without input goes over the wire, so it reads as {}.+ request = body == null || body.isBlank()+ ? objectMapper.createObjectNode()+ : strictBodyReader.readTree(body);} catch (JsonProcessingException e) {return JsonErrorResponseUtils.createSerializationErrorResponse();}+ // Input is always a structure: a bare null, array or scalar parses but is still a+ // client serialization error rather than something every handler must guard against.+ if (!request.isObject()) {+ return JsonErrorResponseUtils.createSerializationErrorResponse();+ }Response response;try {diff --git a/src/test/java/io/github/hectorvent/floci/core/common/JsonMalformedInputIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/JsonMalformedInputIntegrationTest.javaindex a49ec3216d..0743fe395e 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/JsonMalformedInputIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/JsonMalformedInputIntegrationTest.java@@ -97,6 +97,92 @@ class JsonMalformedInputIntegrationTest {.statusCode(200);}+ // ── Dispatcher: the body must be a JSON object; no body at all is the wire form of "no input" ──++ @Test+ void emptyBodyOnJson11IsReadAsEmptyInput() {+ req(JSON_1_1, "TrentService.ListKeys", "")+ .when().post("/")+ .then()+ .statusCode(200);+ }++ @Test+ void whitespaceOnlyBodyOnJson11IsReadAsEmptyInput() {+ req(JSON_1_1, "TrentService.ListKeys", " \n ")+ .when().post("/")+ .then()+ .statusCode(200);+ }++ @Test+ void emptyBodyOnJson11NoInputOperationBehavesLikeEmptyObject() {+ // The awsJson1_1 protocol requires services to accept no payload for operations that+ // define no input, so an empty body must be answered exactly like {}.+ int statusWithEmptyObject = req(JSON_1_1, "AWSOrganizationsV20161128.DescribeOrganization", "{}")+ .when().post("/")+ .then().extract().statusCode();++ req(JSON_1_1, "AWSOrganizationsV20161128.DescribeOrganization", "")+ .when().post("/")+ .then()+ .statusCode(statusWithEmptyObject)+ .body("__type", not(equalTo("SerializationException")));+ }++ @Test+ void emptyBodyOnJson10IsReadAsEmptyInput() {+ req(JSON_1_0, "DynamoDB_20120810.ListTables", "")+ .when().post("/")+ .then()+ .statusCode(200);+ }++ @Test+ void jsonNullBodyOnJson11IsSerializationException() {+ req(JSON_1_1, "TrentService.ListKeys", "null")+ .when().post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("SerializationException"));+ }++ @Test+ void arrayBodyOnJson11IsSerializationException() {+ req(JSON_1_1, "TrentService.ListKeys", "[]")+ .when().post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("SerializationException"));+ }++ @Test+ void scalarBodyOnJson11IsSerializationException() {+ req(JSON_1_1, "TrentService.ListKeys", "\"ListKeys\"")+ .when().post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("SerializationException"));+ }++ @Test+ void jsonNullBodyOnJson10IsSerializationException() {+ req(JSON_1_0, "DynamoDB_20120810.ListTables", "null")+ .when().post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("SerializationException"));+ }++ @Test+ void arrayBodyOnJson10IsSerializationException() {+ req(JSON_1_0, "DynamoDB_20120810.ListTables", "[]")+ .when().post("/")+ .then()+ .statusCode(400)+ .body("__type", equalTo("SerializationException"));+ }+// ── KMS: base64 blob fields ──@Test