fix(cloudformation): return ValidationError when JSON parse error has no location (#9213)

This commit is contained in:
Koushik Dutta authored and GitHub committed 2026-11-22 14:48:41 -04:00
1 parent 78ea3c90d9
commit 188371eabc
3 files changed
+38 -3

No files matched your search

@@ -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)) {
@@ -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);
@@ -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());
}
}