Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(misconf): handle source prefix to ignore #6945

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/iac/ignore/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ type RuleSectionParser interface {
}

// Parse parses the configuration file and returns the Rules
func Parse(src, path string, parsers ...RuleSectionParser) Rules {
func Parse(src, path, sourcePrefix string, parsers ...RuleSectionParser) Rules {
var rules Rules
for i, line := range strings.Split(src, "\n") {
line = strings.TrimSpace(line)
rng := types.NewRange(path, i+1, i+1, "", nil)
rng := types.NewRange(path, i+1, i+1, sourcePrefix, nil)
lineIgnores := parseLine(line, rng, parsers)
for _, lineIgnore := range lineIgnores {
rules = append(rules, lineIgnore)
Expand Down
4 changes: 2 additions & 2 deletions pkg/iac/ignore/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ test #trivy:ignore:rule-4

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

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rules := ignore.Parse(tt.src, filename, tt.parser)
rules := ignore.Parse(tt.src, filename, "", tt.parser)
got := rules.Ignore(tt.args.metadata, tt.args.ids, tt.args.ignorers)
assert.Equal(t, tt.shouldIgnore, got)
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/iac/scanners/cloudformation/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (p *Parser) ParseFile(ctx context.Context, fsys fs.FS, path string) (fctx *
if err := yaml.Unmarshal(content, fctx); err != nil {
return nil, NewErrInvalidContent(path, err)
}
fctx.Ignores = ignore.Parse(string(content), path)
fctx.Ignores = ignore.Parse(string(content), path, "")
case JsonSourceFormat:
if err := jfather.Unmarshal(content, fctx); err != nil {
return nil, NewErrInvalidContent(path, err)
Expand Down
57 changes: 57 additions & 0 deletions pkg/iac/scanners/terraform/ignore_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package terraform

import (
"context"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/aquasecurity/trivy/internal/testutil"
"github.com/aquasecurity/trivy/pkg/iac/providers"
"github.com/aquasecurity/trivy/pkg/iac/rules"
"github.com/aquasecurity/trivy/pkg/iac/scan"
"github.com/aquasecurity/trivy/pkg/iac/scanners/options"
"github.com/aquasecurity/trivy/pkg/iac/severity"
"github.com/aquasecurity/trivy/pkg/iac/terraform"
)
Expand Down Expand Up @@ -748,3 +752,56 @@ func Test_IgnoreInlineByAVDID(t *testing.T) {
}
}
}

func TestIgnoreRemoteTerraformResource(t *testing.T) {

fsys := testutil.CreateFS(t, map[string]string{
"main.tf": `module "bucket" {
source = "git::https://github.com/test/bucket"
}`,
".terraform/modules/modules.json": `{
"Modules": [
{ "Key": "", "Source": "", "Dir": "." },
{
"Key": "bucket",
"Source": "git::https://github.com/test/bucket",
"Dir": ".terraform/modules/bucket"
}
]
}
`,
".terraform/modules/bucket/main.tf": `
# trivy:ignore:test-0001
resource "aws_s3_bucket" "test" {
bucket = ""
}
`,
})

check := `# METADATA
# title: Test
# custom:
# id: test-0001
# avdid: test-0001

package user.test0001

deny[res] {
bucket := input.aws.s3.buckets[_]
bucket.name.value == ""
res := result.new("Empty bucket name!", bucket)
}`

localScanner := New(
options.ScannerWithEmbeddedPolicies(false),
options.ScannerWithEmbeddedLibraries(true),
options.ScannerWithRegoOnly(true),
options.ScannerWithPolicyNamespaces("user"),
options.ScannerWithPolicyReader(strings.NewReader(check)),
ScannerWithDownloadsAllowed(false),
ScannerWithSkipCachedModules(true),
)
results, err := localScanner.ScanFS(context.TODO(), fsys, ".")
require.NoError(t, err)
assert.Empty(t, results.GetFailed())
}
1 change: 1 addition & 0 deletions pkg/iac/scanners/terraform/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ func (p *Parser) readBlocks(files []sourceFile) (terraform.Blocks, ignore.Rules,
fileIgnores := ignore.Parse(
string(file.file.Bytes),
file.path,
p.moduleSource,
&ignore.StringMatchParser{
SectionKey: "ws",
},
Expand Down