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(secret): convert severity for custom rules #6500

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
18 changes: 17 additions & 1 deletion pkg/fanal/secret/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,32 @@ func ParseConfig(configPath string) (*Config, error) {
}
defer f.Close()

logger.Info("Loading the config file s for secret scanning...")
logger.Info("Loading the config file for secret scanning...")

var config Config
if err = yaml.NewDecoder(f).Decode(&config); err != nil {
return nil, xerrors.Errorf("secrets config decode error: %w", err)
}

// Update severity for custom rules
for i := range config.CustomRules {
config.CustomRules[i].Severity = convertSeverity(logger, config.CustomRules[i].Severity)
}

return &config, nil
}

// convertSeverity checks the severity and converts it to uppercase or uses "UNKNOWN" for the wrong severity.
func convertSeverity(logger *log.Logger, severity string) string {
switch strings.ToLower(severity) {
case "low", "medium", "high", "critical", "unknown":
return strings.ToUpper(severity)
default:
logger.Warn("Incorrect severity", log.String("severity", severity))
return "UNKNOWN"
}
}

func NewScanner(config *Config) Scanner {
logger := log.WithPrefix("secret")

Expand Down
27 changes: 27 additions & 0 deletions pkg/fanal/secret/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,33 @@ func TestSecretScanner(t *testing.T) {
Findings: []types.SecretFinding{wantFinding8},
},
},
{
name: "add unknown severity when rule has no severity",
configPath: filepath.Join("testdata", "config-with-incorrect-severity.yaml"),
inputFilePath: filepath.Join("testdata", "secret.txt"),
want: types.Secret{
FilePath: filepath.Join("testdata", "secret.txt"),
Findings: []types.SecretFinding{wantFinding8},
},
},
{
name: "update severity if rule severity is not in uppercase",
configPath: filepath.Join("testdata", "config-with-non-uppercase-severity.yaml"),
inputFilePath: filepath.Join("testdata", "secret.txt"),
want: types.Secret{
FilePath: filepath.Join("testdata", "secret.txt"),
Findings: []types.SecretFinding{wantFinding8},
},
},
{
name: "use unknown severity when rule has incorrect severity",
configPath: filepath.Join("testdata", "config-with-incorrect-severity.yaml"),
inputFilePath: filepath.Join("testdata", "secret.txt"),
want: types.Secret{
FilePath: filepath.Join("testdata", "secret.txt"),
Findings: []types.SecretFinding{wantFinding8},
},
},
{
name: "invalid aws secrets",
configPath: filepath.Join("testdata", "skip-test.yaml"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
rules:
- id: rule1
category: general
title: Generic Rule
severity: bad
regex: (?i)(?P<key>(secret))(=|:).{0,5}['"](?P<secret>somevalue)['"]
secret-group-name: secret
disable-allow-rules:
- tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
rules:
- id: rule1
category: general
title: Generic Rule
severity: uNknown
regex: (?i)(?P<key>(secret))(=|:).{0,5}['"](?P<secret>somevalue)['"]
secret-group-name: secret
disable-allow-rules:
- tests