Skip to content

Commit 23dc3a6

Browse files
authored
fix(misconf): allow null values only for tf variables (#8112)
Signed-off-by: nikpivkin <[email protected]>
1 parent a0429f7 commit 23dc3a6

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

pkg/iac/scanners/terraform/parser/load_module.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type ModuleDefinition struct {
2525
}
2626

2727
func (d *ModuleDefinition) inputVars() map[string]cty.Value {
28-
inputs := d.Definition.Values().AsValueMap()
28+
inputs := d.Definition.NullableValues().AsValueMap()
2929
if inputs == nil {
3030
return make(map[string]cty.Value)
3131
}

pkg/iac/scanners/terraform/parser/parser_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -2161,3 +2161,29 @@ resource "foo" "this" {
21612161
})
21622162
}
21632163
}
2164+
2165+
func TestAttrRefToNullVariable(t *testing.T) {
2166+
fsys := fstest.MapFS{
2167+
"main.tf": &fstest.MapFile{Data: []byte(`variable "name" {
2168+
type = string
2169+
default = null
2170+
}
2171+
2172+
resource "aws_s3_bucket" "example" {
2173+
bucket = var.name
2174+
}`)},
2175+
}
2176+
2177+
parser := New(fsys, "", OptionStopOnHCLError(true))
2178+
2179+
require.NoError(t, parser.ParseFS(context.TODO(), "."))
2180+
2181+
_, err := parser.Load(context.TODO())
2182+
require.NoError(t, err)
2183+
2184+
modules, _, err := parser.EvaluateAll(context.TODO())
2185+
require.NoError(t, err)
2186+
2187+
val := modules.GetResourcesByType("aws_s3_bucket")[0].GetAttribute("bucket").GetRawValue()
2188+
assert.Nil(t, val)
2189+
}

pkg/iac/terraform/block.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -569,13 +569,25 @@ func (b *Block) Attributes() map[string]*Attribute {
569569
return attributes
570570
}
571571

572+
func (b *Block) NullableValues() cty.Value {
573+
return b.values(true)
574+
}
575+
572576
func (b *Block) Values() cty.Value {
577+
return b.values(false)
578+
}
579+
580+
func (b *Block) values(allowNull bool) cty.Value {
573581
values := createPresetValues(b)
574582
for _, attribute := range b.GetAttributes() {
575583
if attribute.Name() == "for_each" {
576584
continue
577585
}
578-
values[attribute.Name()] = attribute.NullableValue()
586+
if allowNull {
587+
values[attribute.Name()] = attribute.NullableValue()
588+
} else {
589+
values[attribute.Name()] = attribute.Value()
590+
}
579591
}
580592
return cty.ObjectVal(postProcessValues(b, values))
581593
}

0 commit comments

Comments
 (0)