You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug(terraform): TF_VAR_ values for variables of complex types are not parsed as HCL #11614
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.
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.
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
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.
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?
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.
Activity
Aakarsh133 commentedon Nov 19, 2026
Hey @nikpivkin are you working on this, or can i provide the fix?
nikpivkin commentedon Nov 19, 2026
Go ahead, @Aakarsh133, thanks! Please parse environment values in
loadTFVars, where we know they came fromTF_VAR_. Please add a regression test to ensure that a string passed as a module argument is not re-parsed as HCL.Aakarsh133 commentedon Nov 19, 2026
I believe the core beginning of the bug was not reported, but i can make sense of it now, if you look at
loadTFVars, whensplit := strings.Split(env, "=")is called, then ForTF_VAR_tags={env="prod"}it will be split asTF_VAR_tagsand{envas we see here,{envis 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
nikpivkin commentedon Nov 20, 2026
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 thestrings.Cutfix in your PR.Aakarsh133 commentedon Nov 20, 2026
Hi @nikpivkin, should i add a logger *log.Logger to
loadTFVarsfor logging the cases such as “invalid HCL, skip the variable”. I believe similar implementation have already been used?Aakarsh133 commentedon Nov 20, 2026
Also here is what I plan to add as regression tests, all in
load_vars_test.go. The two existingTest_TFVarsFiletests stay unchanged.Unit table test for
loadEnvVars:map(string)with{env="prod"}andlist(string)with["a","b"]are parsed as HCLstringcontaining=(a=b) keeps its full value (covers thestrings.Cutfix)number,booland untyped variables stay literal strings[var.x]) are skipped with a warningEnd-to-end tests through the parser:
TF_VAR_tags={env="prod"}on amap(string)resolvesvar.tags["env"]toprod(the scenario from the issue)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.