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

+11 -1
View File
@@ -24,7 +24,7 @@
| `ListStackResources` | List resource summaries |
| `GetTemplate` | Retrieve the template body |
| `GetTemplateSummary` | Summarize a template's Parameters, Resources and Transform sections, by StackName, TemplateBody or TemplateURL |
| `ValidateTemplate` | Refuses a template holding a `null` value (see [Null values](#null-values)); otherwise returns success without validating further |
| `ValidateTemplate` | Refuses a body that is no template (see [Template format](#template-format)) and a template holding a `null` value (see [Null values](#null-values)); otherwise returns success without validating further |
| `ListStacks` | List stacks by status |
| `ListExports` | - |
| `SetStackPolicy` | Accepted; no-op (stub — stack policies are not enforced) |
@@ -631,6 +631,16 @@ intrinsic's reference page:
or Fn::GetAZs.` In a nested stack's template the same rules fail the child's resource with the
same message when the child is created, and the parent rolls back.
## Template format
As on AWS, a body is read as YAML, of which JSON is a subset, so JSON with a trailing comma or
unquoted keys is still a template. A body that reads as neither is refused by `ValidateTemplate`,
`CreateStack`, `UpdateStack`, `CreateChangeSet` and `GetTemplateSummary`, before anything is
created, with `ValidationError` `Template format error: JSON not well-formed. (line L, column C)`
when it starts with `{` and ends with `}`, and `Template format error: YAML not well-formed. (line
L, column C)` otherwise, at the position where the parser stopped. A body that reads as a scalar or
a list is `Template format error: unsupported structure.`
## Null values
As on AWS, a template holding a `null` anywhere (JSON `null`, YAML `~` or a key with no value) is
@@ -401,7 +401,7 @@ public class CloudFormationService implements ResourceProvider {
Map<String, String> tags, String region, String accountId,
boolean attachToReviewInProgressStack, boolean fromPreviousTemplate) {
String resolvedTemplate = resolveTemplate(templateBody, templateUrl);
requireNoNullValues(resolvedTemplate);
requireValidTemplate(resolvedTemplate);
boolean isCreateType = "CREATE".equalsIgnoreCase(changeSetType);
// An update names its stack by name or stack ID; the map is keyed by name. One that names no
// live stack is keyed as given, so it lands in the missing-stack branch below.
@@ -1567,13 +1567,7 @@ public class CloudFormationService implements ResourceProvider {
"One of StackName, TemplateBody or TemplateURL must be specified.", 400);
}
}
JsonNode template;
try {
template = parseTemplate(resolvedBody);
} catch (Exception e) {
throw new AwsException("ValidationError", "Template format error: " + e.getMessage(), 400);
}
return buildTemplateSummary(template);
return buildTemplateSummary(readTemplate(resolvedBody));
}
/** Each parameter the template declares, with its {@link #parameterType}. */
@@ -3939,13 +3933,28 @@ public class CloudFormationService implements ResourceProvider {
}
}
private JsonNode parseTemplate(String templateBody) throws Exception {
String trimmed = templateBody != null ? templateBody.trim() : "{}";
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
return objectMapper.readTree(trimmed);
private JsonNode parseTemplate(String templateBody) throws CloudFormationTemplateParser.TemplateFormatException {
return new CloudFormationTemplateParser(objectMapper).parse(templateBody);
}
/**
* {@code templateBody} parsed, refused as CloudFormation refuses a body that is no template
* (measured, eu-west-1, 2026-10-21, for ValidateTemplate, CreateStack, UpdateStack,
* CreateChangeSet, GetTemplateSummary, CreateStackSet and UpdateStackSet):
* one that reads as neither JSON nor YAML, with the parser's position, and one that reads as a
* scalar or a list. A null template passes, for the checks that answer it.
*/
private JsonNode readTemplate(String templateBody) {
JsonNode parsed;
try {
parsed = parseTemplate(templateBody);
} catch (CloudFormationTemplateParser.TemplateFormatException e) {
throw new AwsException("ValidationError", "Template format error: " + e.getMessage(), 400);
}
// YAML template — use CF-aware parser to handle !Sub, !Ref, !GetAtt etc.
return new CloudFormationYamlParser(objectMapper).parse(trimmed);
if (parsed != null && !parsed.isNull() && !parsed.isObject()) {
throw new AwsException("ValidationError", "Template format error: unsupported structure.", 400);
}
return parsed;
}
private String resolveTemplate(String templateBody, String templateUrl) {
@@ -4493,14 +4502,23 @@ public class CloudFormationService implements ResourceProvider {
return value;
}
/** ValidateTemplate's check of a template: the null values AWS refuses there. */
/** ValidateTemplate's check of a template: one that is no template, and the null values AWS refuses there. */
public void validateTemplate(String templateBody, String templateUrl) {
String resolvedBody = resolveTemplateBody(templateBody, templateUrl);
if (resolvedBody != null) {
requireNoNullValues(resolvedBody);
requireValidTemplate(resolvedBody);
}
}
/**
* The checks ValidateTemplate, CreateStack, UpdateStack, CreateChangeSet, CreateStackSet and
* UpdateStackSet make of a template before anything is created or kept: a body that is no
* template ({@link #readTemplate}), then its null values ({@link #requireNoNullValues}).
*/
void requireValidTemplate(String templateBody) {
requireNoNullValues(readTemplate(templateBody));
}
/**
* Refuses a template that holds a null value anywhere, as AWS does when ValidateTemplate,
* CreateStack, UpdateStack or CreateChangeSet is requested, before anything is created
@@ -4508,16 +4526,7 @@ public class CloudFormationService implements ResourceProvider {
* templates", naming the first null in document order. Below a resource's Properties the path
* says Type where Properties would be, as AWS's does; a null Properties is named as it is.
*/
void requireNoNullValues(String templateBody) {
JsonNode parsed;
try {
parsed = parseTemplate(templateBody);
} catch (Exception e) {
// As validateResourceDependencies: a template that does not parse fails where it is
// processed, with the parser's error.
LOG.debugv("Skipping null value validation; template did not parse: {0}", e.getMessage());
return;
}
private static void requireNoNullValues(JsonNode parsed) {
if (parsed == null || parsed.isNull()) {
// Measured: a template that is null as a whole ("null", "~") lacks its resources first.
throw new AwsException("ValidationError",
@@ -0,0 +1,67 @@
package io.github.hectorvent.floci.services.cloudformation;
import com.fasterxml.jackson.core.JsonLocation;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.yaml.snakeyaml.error.Mark;
import org.yaml.snakeyaml.error.MarkedYAMLException;
import org.yaml.snakeyaml.error.YAMLException;
/**
* A template body read as CloudFormation reads it (measured, eu-west-1, 2026-10-21): as YAML, of
* which JSON is a subset, so a body that is not strict JSON, such as {@code {a: 1}} or a trailing
* comma, still reads as YAML's flow style. A body that reads as neither is "JSON not well-formed"
* when it starts with {@code {} and ends with {@code }}, at the JSON parser's position, and "YAML
* not well-formed" otherwise, at the YAML parser's; both positions are 1-based.
*/
final class CloudFormationTemplateParser {
private final ObjectMapper objectMapper;
CloudFormationTemplateParser(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
/** A body that reads as neither JSON nor YAML, with CloudFormation's words for it. */
static final class TemplateFormatException extends Exception {
TemplateFormatException(String message, Throwable cause) {
super(message, cause);
}
}
JsonNode parse(String templateBody) throws TemplateFormatException {
String body = templateBody != null ? templateBody : "{}";
String trimmed = body.trim();
JsonProcessingException jsonError = null;
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
try {
return objectMapper.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS).readTree(body);
} catch (JsonProcessingException e) {
jsonError = e;
}
}
try {
// The CF-aware parser turns !Sub, !Ref, !GetAtt and the other short forms into their maps.
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("YAML not well-formed." + position(yamlError), yamlError);
}
}
/** 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)) {
return "";
}
Mark mark = marked.getProblemMark() != null ? marked.getProblemMark() : marked.getContextMark();
return mark == null ? "" : " (line " + (mark.getLine() + 1) + ", column " + (mark.getColumn() + 1) + ")";
}
}
@@ -30,7 +30,7 @@ public class CloudFormationYamlParser {
this.objectMapper = objectMapper;
}
public JsonNode parse(String yamlContent) throws Exception {
public JsonNode parse(String yamlContent) {
LoaderOptions opts = new LoaderOptions();
opts.setMaxAliasesForCollections(200);
Yaml yaml = new Yaml(new CfnConstructor(opts));
@@ -133,7 +133,7 @@ public class StackSetService implements PassedRoleLookup {
if ("SELF_MANAGED".equals(resolvedPermissionModel) && (autoDeploymentEnabled || retainStacksOnAccountRemoval)) {
throw new AwsException("ValidationError", "AutoDeployment is valid only for SERVICE_MANAGED StackSets.", 400);
}
requireNoNullValues(templateBody);
requireValidTemplate(templateBody);
// Every instance is deployed with these parameters, so a template parameter left without a
// value would fail each one; refuse the stack set instead.
cfnService.requireParameterValues(templateBody, parameters);
@@ -212,7 +212,7 @@ public class StackSetService implements PassedRoleLookup {
// Checked against the template and parameters the update leaves the stack set with, before
// either is changed or an operation is recorded.
String keptTemplate = templateBody != null && !templateBody.isBlank() ? templateBody : ss.getTemplateBody();
requireNoNullValues(keptTemplate);
requireValidTemplate(keptTemplate);
cfnService.requireParameterValues(keptTemplate,
parameters != null && !parameters.isEmpty() ? parameters : ss.getParameters());
String operationId = reserveOperation(name, "UPDATE", requestedOperationId).getOperationId();
@@ -724,13 +724,14 @@ public class StackSetService implements PassedRoleLookup {
}
/**
* Refuses a template holding a null value before the stack set stores it, as CreateStackSet and
* UpdateStackSet do (measured, eu-west-1, 2026-10-20). StackSets passes CloudFormation's message
* on with the details of the service exception it came in.
* Refuses a body that is no template, or a template holding a null value, before the stack set
* stores it, as CreateStackSet and UpdateStackSet do (measured, eu-west-1, 2026-10-20 and
* 2026-10-21). StackSets passes CloudFormation's message on with the details of the service
* exception it came in.
*/
private void requireNoNullValues(String templateBody) {
private void requireValidTemplate(String templateBody) {
try {
cfnService.requireNoNullValues(templateBody);
cfnService.requireValidTemplate(templateBody);
} catch (AwsException e) {
throw new AwsException(e.getErrorCode(), CloudFormationTemplateEngine.sdkErrorMessage(
e.getMessage(), "AmazonCloudFormation", e.getHttpStatus(), e.getErrorCode()), e.getHttpStatus());
@@ -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());
}
}