Skip to content

bug(terraform): TF_VAR_ values for variables of complex types are not parsed as HCL #11614

Description

@nikpivkin

Trivy reads every TF_VAR_ environment variable as a plain string. Terraform does this only for variables of primitive types (string, number, bool) and for variables without a type. For list, set, map, object and tuple types Terraform parses the value as an HCL expression.

In Trivy the string then fails the conversion to the declared type. The variable gets no value at all, so even its default is not used.

Example variables.tf:

variable "tags" {
  type    = map(string)
  default = {}
}

Terraform 1.17.1 reads the value as a map:

$ echo var.tags | TF_VAR_tags='{env="prod"}' terraform console
tomap({
  "env" = "prod"
})

With the same environment variable, Trivy leaves var.tags unknown:

TF_VAR_tags='{env="prod"}' trivy config .

Expected behavior is to parse TF_VAR_ values the same way Terraform does, as HCL for complex types and as a literal string for primitive types.

References:

Activity

  1. added
    kind/bug
    scan/misconfiguration
    on Nov 10, 2026
  2. added theissue type on Nov 10, 2026
  3. self-assigned this
    on Nov 10, 2026
  4. Aakarsh133 commented on Nov 19, 2026

    @Aakarsh133
    Contributor

    Hey @nikpivkin are you working on this, or can i provide the fix?

  5. nikpivkin commented on Nov 19, 2026

    @nikpivkin
    ContributorAuthor

    Go ahead, @Aakarsh133, thanks! Please parse environment values in loadTFVars, where we know they came from TF_VAR_. Please add a regression test to ensure that a string passed as a module argument is not re-parsed as HCL.

  6. Aakarsh133 commented on Nov 19, 2026

    @Aakarsh133
    Contributor

    I believe the core beginning of the bug was not reported, but i can make sense of it now, if you look at loadTFVars, when split := strings.Split(env, "=") is called, then For TF_VAR_tags={env="prod"} it will be split as TF_VAR_tags and {env as we see here, {env is truncated right before the second =, when it should have been {env="prod"}
    Even if you put a pipeline to parse it as HCL it still isn't enough because the input itself will be broken.
    I suggest that this can be fixed with using strings.Cut(env, "=") instead.

    After this is done i will add HCL parsing for complex types and regression tests

  7. nikpivkin commented on Nov 20, 2026

    @nikpivkin
    ContributorAuthor

    The split on = is fixed in #11515, but that PR looks abandoned. The author has not replied since it was opened and has not signed the CLA. Include the strings.Cut fix in your PR.

  8. Aakarsh133 commented on Nov 20, 2026

    @Aakarsh133
    Contributor

    Hi @nikpivkin, should i add a logger *log.Logger to loadTFVars for logging the cases such as “invalid HCL, skip the variable”. I believe similar implementation have already been used?

  9. Aakarsh133 commented on Nov 20, 2026

    @Aakarsh133
    Contributor

    Also here is what I plan to add as regression tests, all in load_vars_test.go. The two existing Test_TFVarsFile tests stay unchanged.

    Unit table test for loadEnvVars:

    • map(string) with {env="prod"} and list(string) with ["a","b"] are parsed as HCL
    • a string containing = (a=b) keeps its full value (covers the strings.Cut fix)
    • number, bool and untyped variables stay literal strings
    • invalid HCL and an expression with a reference ([var.x]) are skipped with a warning

    End-to-end tests through the parser:

    • TF_VAR_tags={env="prod"} on a map(string) resolves var.tags["env"] to prod (the scenario from the issue)
    • an invalid value falls back to the variable's default
    • a tfvars file still overrides the environment variable

    The end-to-end tests are there to check that the parsed value converts to the declared type, which the unit table can't show. If you think this is more than needed, I can drop the literal type rows and keep only the map, a=b, invalid-value and precedence cases.

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

Metadata

Metadata

Assignees

Labels

kind/bugscan/misconfiguration

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions