Skip to content

Commit c3192f0

Browse files
authored
fix(misconf): handle source prefix to ignore (#6945)
Signed-off-by: nikpivkin <[email protected]>
1 parent ec68c9a commit c3192f0

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

pkg/iac/ignore/parse.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ type RuleSectionParser interface {
1919
}
2020

2121
// Parse parses the configuration file and returns the Rules
22-
func Parse(src, path string, parsers ...RuleSectionParser) Rules {
22+
func Parse(src, path, sourcePrefix string, parsers ...RuleSectionParser) Rules {
2323
var rules Rules
2424
for i, line := range strings.Split(src, "\n") {
2525
line = strings.TrimSpace(line)
26-
rng := types.NewRange(path, i+1, i+1, "", nil)
26+
rng := types.NewRange(path, i+1, i+1, sourcePrefix, nil)
2727
lineIgnores := parseLine(line, rng, parsers)
2828
for _, lineIgnore := range lineIgnores {
2929
rules = append(rules, lineIgnore)

pkg/iac/ignore/rule_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ test #trivy:ignore:rule-4
239239

240240
for _, tt := range tests {
241241
t.Run(tt.name, func(t *testing.T) {
242-
rules := ignore.Parse(tt.src, filename)
242+
rules := ignore.Parse(tt.src, "", filename)
243243
got := rules.Ignore(tt.args.metadata, tt.args.ids, nil)
244244
assert.Equal(t, tt.shouldIgnore, got)
245245
})
@@ -329,7 +329,7 @@ func TestRules_IgnoreWithCustomIgnorer(t *testing.T) {
329329

330330
for _, tt := range tests {
331331
t.Run(tt.name, func(t *testing.T) {
332-
rules := ignore.Parse(tt.src, filename, tt.parser)
332+
rules := ignore.Parse(tt.src, filename, "", tt.parser)
333333
got := rules.Ignore(tt.args.metadata, tt.args.ids, tt.args.ignorers)
334334
assert.Equal(t, tt.shouldIgnore, got)
335335
})

pkg/iac/scanners/cloudformation/parser/parser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (p *Parser) ParseFile(ctx context.Context, fsys fs.FS, path string) (fctx *
171171
if err := yaml.Unmarshal(content, fctx); err != nil {
172172
return nil, NewErrInvalidContent(path, err)
173173
}
174-
fctx.Ignores = ignore.Parse(string(content), path)
174+
fctx.Ignores = ignore.Parse(string(content), path, "")
175175
case JsonSourceFormat:
176176
if err := jfather.Unmarshal(content, fctx); err != nil {
177177
return nil, NewErrInvalidContent(path, err)

pkg/iac/scanners/terraform/ignore_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package terraform
22

33
import (
4+
"context"
45
"fmt"
56
"strings"
67
"testing"
78

89
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
911

12+
"github.com/aquasecurity/trivy/internal/testutil"
1013
"github.com/aquasecurity/trivy/pkg/iac/providers"
1114
"github.com/aquasecurity/trivy/pkg/iac/rules"
1215
"github.com/aquasecurity/trivy/pkg/iac/scan"
16+
"github.com/aquasecurity/trivy/pkg/iac/scanners/options"
1317
"github.com/aquasecurity/trivy/pkg/iac/severity"
1418
"github.com/aquasecurity/trivy/pkg/iac/terraform"
1519
)
@@ -748,3 +752,56 @@ func Test_IgnoreInlineByAVDID(t *testing.T) {
748752
}
749753
}
750754
}
755+
756+
func TestIgnoreRemoteTerraformResource(t *testing.T) {
757+
758+
fsys := testutil.CreateFS(t, map[string]string{
759+
"main.tf": `module "bucket" {
760+
source = "git::https://github.com/test/bucket"
761+
}`,
762+
".terraform/modules/modules.json": `{
763+
"Modules": [
764+
{ "Key": "", "Source": "", "Dir": "." },
765+
{
766+
"Key": "bucket",
767+
"Source": "git::https://github.com/test/bucket",
768+
"Dir": ".terraform/modules/bucket"
769+
}
770+
]
771+
}
772+
`,
773+
".terraform/modules/bucket/main.tf": `
774+
# trivy:ignore:test-0001
775+
resource "aws_s3_bucket" "test" {
776+
bucket = ""
777+
}
778+
`,
779+
})
780+
781+
check := `# METADATA
782+
# title: Test
783+
# custom:
784+
# id: test-0001
785+
# avdid: test-0001
786+
787+
package user.test0001
788+
789+
deny[res] {
790+
bucket := input.aws.s3.buckets[_]
791+
bucket.name.value == ""
792+
res := result.new("Empty bucket name!", bucket)
793+
}`
794+
795+
localScanner := New(
796+
options.ScannerWithEmbeddedPolicies(false),
797+
options.ScannerWithEmbeddedLibraries(true),
798+
options.ScannerWithRegoOnly(true),
799+
options.ScannerWithPolicyNamespaces("user"),
800+
options.ScannerWithPolicyReader(strings.NewReader(check)),
801+
ScannerWithDownloadsAllowed(false),
802+
ScannerWithSkipCachedModules(true),
803+
)
804+
results, err := localScanner.ScanFS(context.TODO(), fsys, ".")
805+
require.NoError(t, err)
806+
assert.Empty(t, results.GetFailed())
807+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ func (p *Parser) readBlocks(files []sourceFile) (terraform.Blocks, ignore.Rules,
301301
fileIgnores := ignore.Parse(
302302
string(file.file.Bytes),
303303
file.path,
304+
p.moduleSource,
304305
&ignore.StringMatchParser{
305306
SectionKey: "ws",
306307
},

0 commit comments

Comments
 (0)