Skip to content

Commit a58c36d

Browse files
authored
fix(misconf): move disabled checks filtering after analyzer scan (#9002)
Signed-off-by: nikpivkin <[email protected]>
1 parent 64aea25 commit a58c36d

File tree

6 files changed

+31
-135
lines changed

6 files changed

+31
-135
lines changed

pkg/fanal/analyzer/imgconf/dockerfile/dockerfile.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,17 @@ import (
1414
"github.com/aquasecurity/trivy/pkg/fanal/image"
1515
"github.com/aquasecurity/trivy/pkg/fanal/types"
1616
"github.com/aquasecurity/trivy/pkg/iac/detection"
17+
"github.com/aquasecurity/trivy/pkg/log"
1718
"github.com/aquasecurity/trivy/pkg/mapfs"
1819
"github.com/aquasecurity/trivy/pkg/misconf"
20+
"github.com/aquasecurity/trivy/pkg/set"
1921
"github.com/aquasecurity/trivy/pkg/version/doc"
2022
)
2123

22-
var disabledChecks = []misconf.DisabledCheck{
23-
{
24-
ID: "DS007", Scanner: string(analyzer.TypeHistoryDockerfile),
25-
Reason: "See " + doc.URL("docs/target/container_image", "disabled-checks"),
26-
},
27-
{
28-
ID: "DS016", Scanner: string(analyzer.TypeHistoryDockerfile),
29-
Reason: "See " + doc.URL("docs/target/container_image", "disabled-checks"),
30-
},
31-
}
24+
var (
25+
disabledChecks = set.New("AVD-DS-0007", "AVD-DS-0016")
26+
reason = "See " + doc.URL("docs/target/container_image", "disabled-checks")
27+
)
3228

3329
const analyzerVersion = 1
3430

@@ -41,7 +37,6 @@ type historyAnalyzer struct {
4137
}
4238

4339
func newHistoryAnalyzer(opts analyzer.ConfigAnalyzerOptions) (analyzer.ConfigAnalyzer, error) {
44-
opts.MisconfScannerOption.DisabledChecks = append(opts.MisconfScannerOption.DisabledChecks, disabledChecks...)
4540
s, err := misconf.NewScanner(detection.FileTypeDockerfile, opts.MisconfScannerOption)
4641
if err != nil {
4742
return nil, xerrors.Errorf("misconfiguration scanner error: %w", err)
@@ -72,8 +67,10 @@ func (a *historyAnalyzer) Analyze(ctx context.Context, input analyzer.ConfigAnal
7267
return nil, nil
7368
}
7469

70+
misconfig := misconfs[0]
71+
misconfig.Failures = filterDisabledChecks(misconfig.Failures)
7572
return &analyzer.ConfigAnalysisResult{
76-
Misconfiguration: &misconfs[0],
73+
Misconfiguration: &misconfig,
7774
}, nil
7875
}
7976

@@ -174,3 +171,16 @@ func (a *historyAnalyzer) Type() analyzer.Type {
174171
func (a *historyAnalyzer) Version() int {
175172
return analyzerVersion
176173
}
174+
175+
func filterDisabledChecks(results types.MisconfResults) types.MisconfResults {
176+
var filtered types.MisconfResults
177+
for _, r := range results {
178+
if disabledChecks.Contains(r.AVDID) {
179+
log.WithPrefix("image history analyzer").Info("Skip disabled check",
180+
log.String("ID", r.AVDID), log.String("reason", reason))
181+
continue
182+
}
183+
filtered = append(filtered, r)
184+
}
185+
return filtered
186+
}

pkg/iac/rego/load.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,6 @@ func (s *Scanner) isModuleApplicable(module *ast.Module, metadata *StaticMetadat
378378
return false
379379
}
380380

381-
// ignore disabled built-in checks
382-
if IsBuiltinNamespace(getModuleNamespace(module)) && s.disabledCheckIDs.Contains(metadata.ID) {
383-
return false
384-
}
385-
386381
if len(metadata.InputOptions.Selectors) == 0 && !metadata.Library {
387382
s.logger.Warn(
388383
"Module has no input selectors - it will be loaded for all inputs",

pkg/iac/rego/options.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,6 @@ func WithCustomSchemas(schemas map[string][]byte) options.ScannerOption {
106106
}
107107
}
108108

109-
// WithDisabledCheckIDs disables checks by their ID (ID field in metadata)
110-
func WithDisabledCheckIDs(ids ...string) options.ScannerOption {
111-
return func(s options.ConfigurableScanner) {
112-
if ss, ok := s.(*Scanner); ok {
113-
ss.disabledCheckIDs.Append(ids...)
114-
}
115-
}
116-
}
117-
118109
func WithIncludeDeprecatedChecks(include bool) options.ScannerOption {
119110
return func(s options.ConfigurableScanner) {
120111
if ss, ok := s.(*Scanner); ok {

pkg/iac/rego/scanner.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ type Scanner struct {
6767
embeddedLibs map[string]*ast.Module
6868
embeddedChecks map[string]*ast.Module
6969
customSchemas map[string][]byte
70-
71-
disabledCheckIDs set.Set[string]
7270
}
7371

7472
func (s *Scanner) trace(heading string, input any) {
@@ -94,14 +92,13 @@ func NewScanner(opts ...options.ScannerOption) *Scanner {
9492
LoadAndRegister()
9593

9694
s := &Scanner{
97-
regoErrorLimit: ast.CompileErrorLimitDefault,
98-
ruleNamespaces: builtinNamespaces.Clone(),
99-
runtimeValues: addRuntimeValues(),
100-
logger: log.WithPrefix("rego"),
101-
customSchemas: make(map[string][]byte),
102-
disabledCheckIDs: set.New[string](),
103-
moduleMetadata: make(map[string]*StaticMetadata),
104-
trivyVersion: app.Version(),
95+
regoErrorLimit: ast.CompileErrorLimitDefault,
96+
ruleNamespaces: builtinNamespaces.Clone(),
97+
runtimeValues: addRuntimeValues(),
98+
logger: log.WithPrefix("rego"),
99+
customSchemas: make(map[string][]byte),
100+
moduleMetadata: make(map[string]*StaticMetadata),
101+
trivyVersion: app.Version(),
105102
}
106103

107104
for _, opt := range opts {

pkg/iac/rego/scanner_test.go

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,86 +1107,3 @@ deny {
11071107
})
11081108
}
11091109
}
1110-
1111-
func Test_RegoScanner_WithDisabledCheckIDs(t *testing.T) {
1112-
1113-
check := `# METADATA
1114-
# custom:
1115-
# id: TEST-001
1116-
# avd_id: AVD-TEST-001
1117-
# severity: LOW
1118-
# provider: aws
1119-
# service: s3
1120-
# short_code: test
1121-
package builtin.test
1122-
1123-
deny {
1124-
true
1125-
}
1126-
`
1127-
1128-
tests := []struct {
1129-
name string
1130-
disabledChecks []string
1131-
inputCheck string
1132-
expected bool
1133-
}{
1134-
{
1135-
name: "no disabled checks",
1136-
expected: true,
1137-
inputCheck: check,
1138-
},
1139-
{
1140-
name: "disable check by ID",
1141-
disabledChecks: []string{"TEST-001"},
1142-
inputCheck: check,
1143-
},
1144-
{
1145-
name: "disabling a non-existent check",
1146-
disabledChecks: []string{"FOO"},
1147-
expected: true,
1148-
inputCheck: check,
1149-
},
1150-
{
1151-
name: "one of the identifiers does not exist",
1152-
disabledChecks: []string{"FOO", "TEST-001"},
1153-
inputCheck: check,
1154-
},
1155-
{
1156-
name: "do not disable user checks with builtin IDs",
1157-
inputCheck: `# METADATA
1158-
# custom:
1159-
# id: TEST-001
1160-
# avd_id: AVD-TEST-001
1161-
# severity: LOW
1162-
# provider: aws
1163-
# service: s3
1164-
# short_code: test
1165-
package user.test
1166-
1167-
deny {
1168-
true
1169-
}
1170-
`,
1171-
disabledChecks: []string{"TEST-001"},
1172-
expected: true,
1173-
},
1174-
}
1175-
1176-
for _, tt := range tests {
1177-
t.Run(tt.name, func(t *testing.T) {
1178-
1179-
scanner := rego.NewScanner(
1180-
rego.WithPolicyReader(strings.NewReader(tt.inputCheck)),
1181-
rego.WithDisabledCheckIDs(tt.disabledChecks...),
1182-
rego.WithPolicyNamespaces("user"),
1183-
)
1184-
1185-
require.NoError(t, scanner.LoadPolicies(nil))
1186-
results, err := scanner.ScanInput(t.Context(), types.SourceYAML, rego.Input{})
1187-
require.NoError(t, err)
1188-
1189-
require.Equal(t, tt.expected, len(results.GetFailed()) > 0)
1190-
})
1191-
}
1192-
}

pkg/misconf/scanner.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ var enablediacTypes = map[detection.FileType]types.ConfigType{
5252
detection.FileTypeYAML: types.YAML,
5353
}
5454

55-
type DisabledCheck struct {
56-
ID string
57-
Scanner string // For logging
58-
Reason string // For logging
59-
}
60-
6155
type ScannerOption struct {
6256
Trace bool
6357
Namespaces []string
@@ -82,9 +76,8 @@ type ScannerOption struct {
8276
FilePatterns []string
8377
ConfigFileSchemas []*ConfigFileSchema
8478

85-
DisabledChecks []DisabledCheck
86-
SkipFiles []string
87-
SkipDirs []string
79+
SkipFiles []string
80+
SkipDirs []string
8881

8982
RegoScanner *rego.Scanner
9083
}
@@ -236,17 +229,10 @@ func InitRegoScanner(opt ScannerOption) (*rego.Scanner, error) {
236229
}
237230

238231
func initRegoOptions(opt ScannerOption) ([]options.ScannerOption, error) {
239-
disabledCheckIDs := lo.Map(opt.DisabledChecks, func(check DisabledCheck, _ int) string {
240-
log.Info("Check disabled", log.Prefix(log.PrefixMisconfiguration), log.String("ID", check.ID),
241-
log.String("scanner", check.Scanner), log.String("reason", check.Reason))
242-
return check.ID
243-
})
244-
245232
opts := []options.ScannerOption{
246233
rego.WithEmbeddedPolicies(!opt.DisableEmbeddedPolicies),
247234
rego.WithEmbeddedLibraries(!opt.DisableEmbeddedLibraries),
248235
rego.WithIncludeDeprecatedChecks(opt.IncludeDeprecatedChecks),
249-
rego.WithDisabledCheckIDs(disabledCheckIDs...),
250236
rego.WithTrivyVersion(app.Version()),
251237
}
252238

0 commit comments

Comments
 (0)