fix(cloudformation): don't double-encode string-form SNS RedrivePolicy and FilterPolicy (#2325)

CDK emits AWS::SNS::Subscription RedrivePolicy/FilterPolicy and Step Functions
definitions as an already-serialized JSON string via Fn::Join. resolveNode
collapses the Fn::Join to a TextNode, and toString() on that node re-quotes and
re-escapes the JSON a second time, so the stored policy is double-encoded and
SNS can no longer parse it (dead-lettering silently fails).

Add CloudFormationTemplateEngine#resolveJsonAttribute, which unwraps textual
results while keeping normal serialization for plain object/array nodes, and
use it at every site that stores a resolved node as a JSON document:
SNS FilterPolicy/RedrivePolicy, Step Functions Definition, IAM inline policy
documents, and the SQS RedrivePolicy already fixed in #1939.

Verified: GetSubscriptionAttributes returns the literal policy JSON and
regression tests mirror the SQS pattern from #1939.
This commit is contained in:
MAN$I VERMA authored and GitHub committed 2026-08-15 12:17:06 -04:00
1 parent 3cfa983479
commit c12f3fae7d
8 files changed
+182 -6

No files matched your search

@@ -1768,7 +1768,7 @@ public class CloudFormationResourceProvisioner {
Map<String, String> attributes = new HashMap<>();
if (props.has("FilterPolicy") && !props.path("FilterPolicy").isNull()) {
attributes.put("FilterPolicy", engine.resolveNode(props.path("FilterPolicy")).toString());
attributes.put("FilterPolicy", engine.resolveJsonAttribute(props.path("FilterPolicy")));
}
if (props.has("FilterPolicyScope")) {
attributes.put("FilterPolicyScope", engine.resolve(props.path("FilterPolicyScope")));
@@ -1777,7 +1777,7 @@ public class CloudFormationResourceProvisioner {
attributes.put("RawMessageDelivery", engine.resolve(props.path("RawMessageDelivery")));
}
if (props.has("RedrivePolicy") && !props.path("RedrivePolicy").isNull()) {
attributes.put("RedrivePolicy", engine.resolveNode(props.path("RedrivePolicy")).toString());
attributes.put("RedrivePolicy", engine.resolveJsonAttribute(props.path("RedrivePolicy")));
}
var sub = snsService.subscribe(topicArn, protocol, endpoint, region, attributes);
@@ -4508,7 +4508,7 @@ public class CloudFormationResourceProvisioner {
if (hasDefinitionString) {
definition = resolveOptional(props, "DefinitionString", engine);
} else if (hasDefinition) {
definition = engine.resolveNode(props.get("Definition")).toString();
definition = engine.resolveJsonAttribute(props.get("Definition"));
} else {
JsonNode location = engine.resolveNode(props.get("DefinitionS3Location"));
String bucket = location.path("Bucket").asText(null);
@@ -145,6 +145,26 @@ public class CloudFormationTemplateEngine {
return node;
}
/**
* Resolves a node that must be stored as a JSON document (SNS/SQS RedrivePolicy and
* FilterPolicy, Step Functions definitions, IAM policy documents) to its string form.
* <p>
* resolveNode collapses intrinsics such as Fn::Join into a {@link TextNode}. Calling
* {@code toString()} on that node re-quotes and re-escapes the already-serialized JSON
* (e.g. CDK's Fn::Join-spliced RedrivePolicy), so the value is double-encoded and the
* downstream service can no longer parse it. Unwrapping textual results preserves the
* literal JSON while non-textual nodes (plain objects) keep the normal serialization.
*
* @see <a href="https://github.com/floci-io/floci/issues/2317">#2317</a>
*/
public String resolveJsonAttribute(JsonNode node) {
JsonNode resolved = resolveNode(node);
if (resolved == null || resolved.isNull() || resolved.isMissingNode()) {
return null;
}
return resolved.isTextual() ? resolved.asText() : resolved.toString();
}
private String resolveRef(String name) {
// Pseudo-parameters
return switch (name) {
@@ -166,7 +166,7 @@ public class IamRoleCfnProvisioner implements CfnResourceProvisioner {
+ " has no PolicyDocument.", 400);
}
iamService.putRolePolicy(resolvedRoleName, policyName,
ctx.engine().resolveNode(document).toString());
ctx.engine().resolveJsonAttribute(document));
inlineWrittenByThisAttempt.add(policyName);
}
}
@@ -85,8 +85,7 @@ public class SqsCfnProvisioner implements CfnResourceProvisioner {
// CDK commonly emits RedrivePolicy as an already-serialized string via Fn::Join,
// which resolveNode collapses to a TextNode — unwrap it instead of calling
// toString(), which would JSON-re-encode (quote/escape) the string a second time.
JsonNode redrivePolicy = ctx.engine().resolveNode(props.path("RedrivePolicy"));
attrs.put("RedrivePolicy", redrivePolicy.isTextual() ? redrivePolicy.asText() : redrivePolicy.toString());
attrs.put("RedrivePolicy", ctx.engine().resolveJsonAttribute(props.path("RedrivePolicy")));
}
}
Queue queue = sqsService.createQueue(queueName, attrs, ctx.region());