From 188371eabc2ffa36785a0c07e845348643ca477d Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Mon, 23 Nov 2026 00:18:41 +0530 Subject: [PATCH] fix(cloudformation): return ValidationError when JSON parse error has no location (#9213) --- .../CloudFormationTemplateParser.java | 9 +++++--- ...oudFormationNullValuesIntegrationTest.java | 21 +++++++++++++++++++ .../CloudFormationTemplateParserTest.java | 11 ++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/floci/aws/services/cloudformation/CloudFormationTemplateParser.java b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationTemplateParser.java index 3bea21d8ed..a044a30a3b 100644 --- a/src/main/java/io/floci/aws/services/cloudformation/CloudFormationTemplateParser.java +++ b/src/main/java/io/floci/aws/services/cloudformation/CloudFormationTemplateParser.java @@ -48,14 +48,17 @@ final class CloudFormationTemplateParser { return new CloudFormationYamlParser(objectMapper).parse(body); } catch (YAMLException yamlError) { if (jsonError != null && trimmed.startsWith("{") && trimmed.endsWith("}")) { - JsonLocation at = jsonError.getLocation(); - throw new TemplateFormatException("JSON not well-formed. (line " + at.getLineNr() - + ", column " + at.getColumnNr() + ")", jsonError); + throw new TemplateFormatException("JSON not well-formed." + position(jsonError), jsonError); } throw new TemplateFormatException("YAML not well-formed." + position(yamlError), yamlError); } } + private static String position(JsonProcessingException error) { + JsonLocation at = error.getLocation(); + return at == null ? "" : " (line " + at.getLineNr() + ", column " + at.getColumnNr() + ")"; + } + /** Where the YAML parser stopped, 1-based as CloudFormation counts; nothing for an error with no mark. */ private static String position(YAMLException error) { if (!(error instanceof MarkedYAMLException marked)) { diff --git a/src/test/java/io/floci/aws/services/cloudformation/CloudFormationNullValuesIntegrationTest.java b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationNullValuesIntegrationTest.java index 09efb2884d..1db38be159 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/CloudFormationNullValuesIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationNullValuesIntegrationTest.java @@ -1,5 +1,6 @@ package io.floci.aws.services.cloudformation; +import io.floci.aws.testing.AwsTestClients; import io.floci.aws.testing.CfnStackWaits; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.Response; @@ -9,6 +10,9 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.cloudformation.CloudFormationClient; +import software.amazon.awssdk.services.cloudformation.model.CloudFormationException; import java.util.List; import java.util.stream.Stream; @@ -20,6 +24,7 @@ import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.startsWith; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * A template holding a null value, or a body that is no template, is refused when the operation is @@ -204,6 +209,22 @@ class CloudFormationNullValuesIntegrationTest { } } + @Test + void aJsonTemplateNestedBeyondTheStreamConstraintIsRefusedWithValidationError() { + String deep = "{\"Resources\":{\"H\":{\"Type\":\"AWS::CloudFormation::WaitConditionHandle\"}},\"Metadata\":" + + "{\"k\":".repeat(1000) + "1" + "}".repeat(1000) + "}"; + try (CloudFormationClient client = AwsTestClients.create( + CloudFormationClient.builder(), "test", Region.US_EAST_1)) { + CloudFormationException refused = assertThrows(CloudFormationException.class, + () -> client.validateTemplate(r -> r.templateBody(deep))); + + assertEquals(400, refused.statusCode()); + assertEquals("ValidationError", refused.awsErrorDetails().errorCode()); + assertEquals("Template format error: JSON not well-formed.", + refused.awsErrorDetails().errorMessage()); + } + } + private static Response templateUrl(String action, String url) { RequestSpecification request = given().contentType("application/x-www-form-urlencoded") .header("Authorization", AUTH).formParam("Action", action).formParam("TemplateURL", url); diff --git a/src/test/java/io/floci/aws/services/cloudformation/CloudFormationTemplateParserTest.java b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationTemplateParserTest.java index f3ce8cb532..862c359334 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/CloudFormationTemplateParserTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationTemplateParserTest.java @@ -61,4 +61,15 @@ class CloudFormationTemplateParserTest { String yaml = "Resources:\n A:\n Type: AWS::SNS::Topic\n A:\n Type: AWS::SNS::Topic\n"; assertEquals("AWS::SNS::Topic", parser.parse(yaml).path("Resources").path("A").path("Type").asText()); } + + @Test + void jsonNestedBeyondTheStreamConstraintIsRefusedWithoutALocation() throws Exception { + assertEquals(1, parser.parse("{\"k\":".repeat(1000) + "1" + "}".repeat(1000)).path("k").size()); + + CloudFormationTemplateParser.TemplateFormatException refused = assertThrows( + CloudFormationTemplateParser.TemplateFormatException.class, + () -> parser.parse("{\"k\":".repeat(1001) + "1" + "}".repeat(1001))); + + assertEquals("JSON not well-formed.", refused.getMessage()); + } }