Skip to content

tfExpression/HCL renderer: object keys are rendered into expressions without escaping #350

Description

@so0k

Latent bug (pre-existing at the merge base of #296)

TFExpression.resolveExpressionPart (packages/cdktn/src/tfExpression.ts) renders object arguments key by key as:

`"${key}" = ${this.resolveArg(context, arg[key])}`

The key is interpolated into the quoted Terraform string literal verbatim. A key containing a quote, backslash, control character, ${...}, or %{...} therefore either produces an invalid Terraform expression or silently turns the literal key into a Terraform template sequence.

Repro scenarios

// 1. Quote in key: renders {"he"llo" = "x"} - invalid HCL
new TerraformOutput(stack, "out", {
  value: Fn.lookup(Token.asAny({ 'he"llo': "x" }), "other"),
});

// 2. Template marker in key: renders {"${injected}" = "x"} - the key
//    becomes a template interpolation instead of the literal string
Fn.lookup(Token.asAny({ "${injected}": "x" }), "other");

// 3. Backslash in key: renders {"a\b" = ...} - "\b" is an invalid (or
//    unintended control) escape in the resulting Terraform string literal
Fn.lookup(Token.asAny({ "a\\b": "x" }), "other");

Scope

  • The direct-object branch of resolveExpressionPart (pre-existing).
  • PR feat: support newer provider plugin-protocol features via targetVersions (RFC-04) #296 fixed the newly added resolvable-resolved-object branch (whole-collection tokens like Token.asAny({...}) passed to provider functions) to serialize keys as proper quoted-string literals (JSON.stringify + $${/%%{ template-marker escaping) — the same treatment should be applied here.
  • Audit the HCL renderer's map/object rendering for the same pattern while at it.

See the discussion in #296 (review comment on tfExpression.ts and #296 (comment)).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions