Skip to content

Commit 4e12722

Browse files
aqua-botnikpivkin
andauthored
fix(misconf): skip rewriting expr if attr is nil [backport: release/v0.64] (#9127)
Signed-off-by: nikpivkin <[email protected]> Co-authored-by: Nikita Pivkin <[email protected]>
1 parent 9a7d384 commit 4e12722

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

pkg/iac/adapters/terraform/aws/iam/convert.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ type wrappedDocument struct {
1717
}
1818

1919
func ParsePolicyFromAttr(attr *terraform.Attribute, owner *terraform.Block, modules terraform.Modules) (*iam.Document, error) {
20+
if attr == nil {
21+
return &iam.Document{
22+
Metadata: owner.GetMetadata(),
23+
}, nil
24+
}
2025
attr.RewriteExpr(func(e hclsyntax.Expression) hclsyntax.Expression {
2126
if te, ok := e.(*hclsyntax.TemplateExpr); ok {
2227
return &terraform.PartialTemplateExpr{TemplateExpr: te}

pkg/iac/adapters/terraform/aws/iam/policies.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package iam
22

33
import (
4+
"github.com/hashicorp/hcl/v2/hclsyntax"
5+
46
"github.com/aquasecurity/iamgo"
57
"github.com/aquasecurity/trivy/pkg/iac/providers/aws/iam"
68
"github.com/aquasecurity/trivy/pkg/iac/terraform"
@@ -140,11 +142,20 @@ func findAttachmentPolicy(modules terraform.Modules) func(resource *terraform.Bl
140142
}
141143
}
142144

143-
if block, err := modules.GetReferencedBlock(attr, resource); err == nil {
144-
return findPolicy(modules)(block)
145+
// Searching for a referenced block only makes sense for traversal expressions,
146+
// since only they can directly reference other blocks in the configuration.
147+
switch attr.HCLAttribute().Expr.(type) {
148+
case *hclsyntax.RelativeTraversalExpr, *hclsyntax.ScopeTraversalExpr:
149+
if block, err := modules.GetReferencedBlock(attr, resource); err == nil {
150+
return findPolicy(modules)(block)
151+
}
152+
}
153+
return &iam.Policy{
154+
Metadata: resource.GetMetadata(),
155+
Document: iam.Document{
156+
Metadata: resource.GetMetadata(),
157+
},
145158
}
146-
147-
return nil
148159
}
149160
}
150161

pkg/iac/adapters/terraform/aws/iam/roles_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,32 @@ resource "aws_iam_role_policy_attachment" "test" {
338338
},
339339
},
340340
},
341+
{
342+
name: "policy is template with unknown part",
343+
terraform: `resource "aws_iam_role" "default" {
344+
name = "test"
345+
}
346+
347+
resource "aws_iam_role_policy_attachment" "amazon_eks_cluster_policy" {
348+
role = aws_iam_role.default.name
349+
policy_arn = format("arn:%s:iam::aws:policy/AmazonEKSClusterPolicy", data.aws_partition.current.partition)
350+
}
351+
352+
353+
data "aws_partition" "current" {}
354+
`,
355+
expected: []iam.Role{
356+
{
357+
Name: iacTypes.StringTest("test"),
358+
Policies: []iam.Policy{
359+
{
360+
Name: iacTypes.StringTest(""),
361+
Document: iam.Document{},
362+
},
363+
},
364+
},
365+
},
366+
},
341367
}
342368

343369
for _, test := range tests {

pkg/iac/terraform/attribute.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,14 @@ func safeOp[T any](a *Attribute, fn func(cty.Value) T) T {
834834
// RewriteExpr applies the given function `transform` to the expression of the attribute,
835835
// recursively traversing and transforming it.
836836
func (a *Attribute) RewriteExpr(transform func(hclsyntax.Expression) hclsyntax.Expression) {
837-
a.hclAttribute.Expr = RewriteExpr(a.hclAttribute.Expr.(hclsyntax.Expression), transform)
837+
if a == nil || a.hclAttribute == nil {
838+
return
839+
}
840+
expr, ok := a.hclAttribute.Expr.(hclsyntax.Expression)
841+
if !ok {
842+
return
843+
}
844+
a.hclAttribute.Expr = RewriteExpr(expr, transform)
838845
}
839846

840847
// nolint: gocyclo

0 commit comments

Comments
 (0)