Skip to content

fix(preprocess): JSON objects in calls outside a variable stay text (VTL templates) - #82

Merged
DavidWells merged 1 commit into
masterfrom
fix/json-arg-encoding-outside-variables
Oct 2, 2026
Merged

DavidWells merged 1 commit into
masterfrom
fix/json-arg-encoding-outside-variables

Conversation

@DavidWells

Copy link
Copy Markdown
Owner

Bug

In an API Gateway VTL mapping template like this:

#set($mfaMap = {
  "SMS_MFA": "SMS"
})

the object resolved to #set($mfaMap = __JSON_B64__…__), which shipped a broken template. Present in 1.4.1 through 1.4.8. The golden suite's leak detector (#81) catches it.

Cause

encodeJsonArgObjects (preprocess) encodes a JSON object passed as a call argument, so that its { } don't break variable matching. It ran on every name(… {…} …) in a string, but the decode only happens when a variable resolves. A call outside any variable was encoded and never decoded.

Fix

Only calls inside a ${…} are encoded. The new isInsideVariable is a brace-aware depth scan: inside a variable every {/} counts, so ${merge({"a":1})} still qualifies.

Tests

  • encodeJsonArgObjects unit cases: a VTL #set stays text, and when both appear only the in-variable call is encoded.
  • New serverlessGoldens fixture apigw-vtl: a realistic request/response template under Fn::If. It failed with the leak before the fix.
  • The golden harness can keep backslashes (keepBackslashes), so \n and \" in config values are compared exactly rather than folded to /. The BuildSpec goldens were regenerated; the diff is backslashes only.
  • Full suite green; typecheck clean; configx green.

…VTL #set($m = {...}))

encodeJsonArgObjects encoded every name(... {json} ...) in a string, but decoding only
happens when a variable resolves. A JSON object in a call outside any variable — an API
Gateway VTL template's #set($mfaMap = { "SMS_MFA": "SMS" }) — became
__JSON_B64__...__ in the resolved config. Now only calls inside a ${...} are encoded
(brace-aware depth scan, so ${merge({"a":1})} still counts).

Tests: encodeJsonArgObjects unit cases; serverlessGoldens apigw-vtl fixture (its leak
check failed before the fix). Golden harness can keep backslashes so \n and \" in config
values are compared exactly; BuildSpec goldens regenerated (backslashes only).
@DavidWells
DavidWells merged commit 30ab85b into master Oct 2, 2026
4 checks passed
@DavidWells
DavidWells deleted the fix/json-arg-encoding-outside-variables branch October 2, 2026 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant