fix(cloudformation): refuse a template body that does not parse (#6179)

* fix(cloudformation): refuse a template body that does not parse

ValidateTemplate answered success for a body that reads as neither JSON nor
YAML, since the null value check skipped a template it could not parse.
CloudFormation refuses such a body at the request, and so does Floci now, in
ValidateTemplate, CreateStack, UpdateStack, CreateChangeSet and
GetTemplateSummary, all measured with the same texts.

CloudFormation reads a body as YAML, of which JSON is a subset, so the new
CloudFormationTemplateParser tries strict JSON first (trailing tokens refused)
and falls back to the CF-aware YAML parser, which also reads JSON with a
trailing comma or unquoted keys, as AWS does. When both fail, a body that
starts with { and ends with } is "JSON not well-formed" at Jackson's position,
anything else "YAML not well-formed" at SnakeYAML's, both 1-based and taken
from the body as sent, so leading blank lines keep their line numbers. A body
that reads as a scalar or a list is "unsupported structure.".

Stack sets keep their null check skipping a body that does not parse, since
CreateStackSet was not measured with one. A template that reads but has no
Resources member is left as it was: that is a check of a template, not of
its format.

* fix(cloudformation): refuse a body that is no template in stack sets too

CreateStackSet and UpdateStackSet stored a body that does not parse, whose
instances then could not deploy: CreateStackInstances and UpdateStackSet
stopped halfway with the change set's 400. Measured on CloudFormation, both
refuse such a body before keeping it, with the same texts followed by the
service exception's details, as they refuse a null value, and an update
leaves the previous body in place. They now go through the same check as the
other operations, so a stored body always parses.

The JSON message also needs the body to start with {, not only to end with }:
[{"a": 1} is YAML not well-formed at line 1, column 10 on CloudFormation.
This commit is contained in:
Alessandro Lattao authored and GitHub committed 2026-10-21 16:02:31 -04:00
1 parent 09343917c1
commit a6433d521a
7 files changed
+245 -41

No files matched your search

@@ -22,11 +22,11 @@ import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* A template holding a null value is refused when the operation is requested, before anything is
* created. Measured on CloudFormation (eu-west-1, 2026-10-20): ValidateTemplate, CreateStack,
* UpdateStack and CreateChangeSet answer 400 ValidationError "[path] 'null' values are not allowed
* in templates", the path naming the first null in document order, with Type where a resource's
* Properties would be.
* A template holding a null value, or a body that is no template, is refused when the operation is
* requested, before anything is created. Measured on CloudFormation (eu-west-1, 2026-10-20):
* ValidateTemplate, CreateStack, UpdateStack and CreateChangeSet answer 400 ValidationError "[path]
* 'null' values are not allowed in templates", the path naming the first null in document order,
* with Type where a resource's Properties would be.
*/
@QuarkusTest
class CloudFormationNullValuesIntegrationTest {
@@ -73,6 +73,7 @@ class CloudFormationNullValuesIntegrationTest {
+ " QueueName:\n DelaySeconds: ~\n", "/Resources/Q/Type/QueueName"));
}
/** Measured for a null value (2026-10-20) and for a body that is no template (2026-10-21). */
@Test
void anUpdateAndAChangeSetAreRefusedToo() {
String name = "cfn-null-update-" + System.nanoTime();
@@ -89,6 +90,11 @@ class CloudFormationNullValuesIntegrationTest {
.formParam("Action", "CreateChangeSet").formParam("StackName", name)
.formParam("ChangeSetName", "with-null").formParam("TemplateBody", template)
.when().post("/"), message);
assertRefused(request("UpdateStack", name, "[1, 2]"), "Template format error: unsupported structure.");
assertRefused(given().contentType("application/x-www-form-urlencoded").header("Authorization", AUTH)
.formParam("Action", "CreateChangeSet").formParam("StackName", name)
.formParam("ChangeSetName", "not-a-template").formParam("TemplateBody", "Resources:\n A: [unclosed\n")
.when().post("/"), "Template format error: YAML not well-formed. (line 3, column 1)");
given().contentType("application/x-www-form-urlencoded").header("Authorization", AUTH)
.formParam("Action", "DeleteStack").formParam("StackName", name)
@@ -106,9 +112,47 @@ class CloudFormationNullValuesIntegrationTest {
assertRefused(request("CreateStack", "cfn-null-whole-" + System.nanoTime(), template), message);
}
/**
* Measured (eu-west-1, 2026-10-21): a body that reads as neither JSON nor YAML, or as a scalar or
* a list, is no template, alike for the three calls that read one, and no stack is created.
*/
@ParameterizedTest(name = "{0}")
@MethodSource("notTemplates")
void aBodyThatIsNoTemplateIsRefused(String body, String message) {
String name = "cfn-format-" + System.nanoTime();
for (String action : List.of("ValidateTemplate", "CreateStack", "GetTemplateSummary")) {
assertRefused(request(action, "CreateStack".equals(action) ? name : null, body),
"Template format error: " + message);
}
given().contentType("application/x-www-form-urlencoded").header("Authorization", AUTH)
.formParam("Action", "DescribeStacks").formParam("StackName", name)
.when().post("/").then().statusCode(400);
}
static Stream<Arguments> notTemplates() {
return Stream.of(
Arguments.of("{\"Resources\": {\"A\": {\"Type\": \"AWS::SNS::Topic\"}}",
"JSON not well-formed. (line 1, column 49)"),
Arguments.of("Resources:\n A: [unclosed\n", "YAML not well-formed. (line 3, column 1)"),
Arguments.of("Resources:\n A:\n Type: AWS::SNS::Topic\n Properties: !Bogus x\n",
"YAML not well-formed. (line 4, column 17)"),
Arguments.of("just a string", "unsupported structure."),
Arguments.of("[1, 2]", "unsupported structure."));
}
/** Measured: CloudFormation reads a body as YAML, so JSON with a trailing comma is still a template. */
@Test
void aBodyThatIsNotStrictJsonStillReadsAsYaml() {
request("ValidateTemplate", null,
"{\"Resources\": {\"H\": {\"Type\": \"AWS::CloudFormation::WaitConditionHandle\",}}}")
.then().statusCode(200);
}
/**
* Measured: CreateStackSet and UpdateStackSet refuse the template before the stack set keeps it,
* passing CloudFormation's message on with the service exception's details.
* passing CloudFormation's message on with the service exception's details, for a null value
* (2026-10-20) and for a body that is no template (2026-10-21).
*/
@Test
void aStackSetRefusesATemplateWithANullValueBeforeKeepingIt() {
@@ -125,6 +169,15 @@ class CloudFormationNullValuesIntegrationTest {
Response update = stackSet("UpdateStackSet", name, queue("{\"QueueName\": null}"));
update.then().statusCode(400);
assertThat(update.xmlPath().getString("ErrorResponse.Error.Message"), startsWith(prefix));
String details = " (Service: AmazonCloudFormation; Status Code: 400; Error Code: ValidationError; Request ID: ";
Response notATemplate = stackSet("CreateStackSet", name + "-list", "[1, 2]");
notATemplate.then().statusCode(400);
assertThat(notATemplate.xmlPath().getString("ErrorResponse.Error.Message"),
startsWith("Template format error: unsupported structure." + details));
Response unparsable = stackSet("UpdateStackSet", name, "{\"Resources\": {\"A\": {\"Type\": \"AWS::SNS::Topic\"}}");
unparsable.then().statusCode(400);
assertThat(unparsable.xmlPath().getString("ErrorResponse.Error.Message"),
startsWith("Template format error: JSON not well-formed. (line 1, column 49)" + details));
given().contentType("application/x-www-form-urlencoded").header("Authorization", AUTH)
.formParam("Action", "DescribeStackSet").formParam("StackSetName", name)
@@ -0,0 +1,64 @@
package io.github.hectorvent.floci.services.cloudformation;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** The bodies measured on CloudFormation (eu-west-1, 2026-10-21) and the words it answers for each. */
class CloudFormationTemplateParserTest {
private final CloudFormationTemplateParser parser = new CloudFormationTemplateParser(new ObjectMapper());
@ParameterizedTest
@CsvSource(delimiter = '|', value = {
"{\"Resources\": {|YAML not well-formed. (line 1, column 16)",
"Resources:\\n A: [unclosed\\n|YAML not well-formed. (line 3, column 1)",
"Resources:\\n\\tA: x\\n|YAML not well-formed. (line 2, column 1)",
"Resources: {A: {Type: AWS::SNS::Topic}|YAML not well-formed. (line 1, column 39)",
"Resources:\\n A:\\n Type: AWS::SNS::Topic\\n Properties: !Bogus x\\n|YAML not well-formed. (line 4, column 17)",
"{\"a\": 1|YAML not well-formed. (line 1, column 8)",
" \\n{\"a\": 1|YAML not well-formed. (line 2, column 8)",
"{\"Resources\": {\"A\": {\"Type\": \"AWS::SNS::Topic\"}}} trailing|YAML not well-formed. (line 1, column 51)",
"[1, 2|YAML not well-formed. (line 1, column 6)",
"{\"Resources\": {\"A\": {\"Type\": \"AWS::SNS::Topic\"}}|JSON not well-formed. (line 1, column 49)",
"{\"a\": 1}}|JSON not well-formed. (line 1, column 9)",
"{\"a\": [1}|JSON not well-formed. (line 1, column 9)",
"[{\"a\": 1}|YAML not well-formed. (line 1, column 10)",
})
void aBodyThatIsNeitherJsonNorYamlIsRefusedWhereCloudFormationStops(String body, String message) {
CloudFormationTemplateParser.TemplateFormatException refused = assertThrows(
CloudFormationTemplateParser.TemplateFormatException.class,
() -> parser.parse(body.replace("\\n", "\n").replace("\\t", "\t")));
assertEquals(message, refused.getMessage());
}
@Test
void whatIsNotStrictJsonStillReadsAsYaml() throws Exception {
assertEquals(1, parser.parse("{a: 1}").path("a").asInt());
assertEquals(1, parser.parse("{\"a\": 1, }").path("a").asInt());
JsonNode trailingComma = parser.parse("{\n \"Resources\": {\n \"A\": {\"Type\": \"AWS::SNS::Topic\",}\n }\n}");
assertEquals("AWS::SNS::Topic", trailingComma.path("Resources").path("A").path("Type").asText());
}
@Test
void aScalarOrAListReadsAsWhatItIs() throws Exception {
assertTrue(parser.parse("just a string").isTextual());
assertTrue(parser.parse("42").isNumber());
assertTrue(parser.parse("[1, 2]").isArray());
}
@Test
void duplicateKeysAreTolerated() throws Exception {
String json = "{\"Resources\": {\"A\": {\"Type\": \"AWS::SNS::Topic\"}, \"A\": {\"Type\": \"AWS::SNS::Topic\"}}}";
assertEquals("AWS::SNS::Topic", parser.parse(json).path("Resources").path("A").path("Type").asText());
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());
}
}