|
| 1 | +// Package scan implements functions and helpers to ensure the proper scan of the specified files |
| 2 | +package scan |
| 3 | + |
| 4 | +import ( |
| 5 | + "encoding/json" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/Checkmarx/kics/pkg/engine/secrets" |
| 10 | + "github.com/Checkmarx/kics/pkg/model" |
| 11 | +) |
| 12 | + |
| 13 | +func maskPreviewLines(secretsPath string, scanResults *Results) error { |
| 14 | + secretsRegexRulesContent, err := getSecretsRegexRules(secretsPath) |
| 15 | + if err != nil { |
| 16 | + return err |
| 17 | + } |
| 18 | + |
| 19 | + var allRegexQueries secrets.RegexRuleStruct |
| 20 | + |
| 21 | + err = json.Unmarshal([]byte(secretsRegexRulesContent), &allRegexQueries) |
| 22 | + if err != nil { |
| 23 | + return err |
| 24 | + } |
| 25 | + |
| 26 | + allowRules, err := secrets.CompileRegex(allRegexQueries.AllowRules) |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + |
| 31 | + rules, err := compileRegexQueries(allRegexQueries.Rules) |
| 32 | + if err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + |
| 36 | + for i := range scanResults.Results { |
| 37 | + item := scanResults.Results[i] |
| 38 | + hideSecret(item.VulnLines, &allowRules, &rules) |
| 39 | + } |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +func compileRegexQueries(allRegexQueries []secrets.RegexQuery) ([]secrets.RegexQuery, error) { |
| 44 | + for i := range allRegexQueries { |
| 45 | + compiledRegexp, err := regexp.Compile(allRegexQueries[i].RegexStr) |
| 46 | + if err != nil { |
| 47 | + return allRegexQueries, err |
| 48 | + } |
| 49 | + allRegexQueries[i].Regex = compiledRegexp |
| 50 | + |
| 51 | + for j := range allRegexQueries[i].AllowRules { |
| 52 | + allRegexQueries[i].AllowRules[j].Regex = regexp.MustCompile(allRegexQueries[i].AllowRules[j].RegexStr) |
| 53 | + } |
| 54 | + } |
| 55 | + return allRegexQueries, nil |
| 56 | +} |
| 57 | + |
| 58 | +func hideSecret(lines *[]model.CodeLine, allowRules *[]secrets.AllowRule, rules *[]secrets.RegexQuery) { |
| 59 | + for idx, line := range *lines { |
| 60 | + for i := range *rules { |
| 61 | + rule := (*rules)[i] |
| 62 | + |
| 63 | + isSecret, groups := isSecret(line.Line, &rule, allowRules) |
| 64 | + // if not a secret skip to next line |
| 65 | + if !isSecret { |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + if len(rule.Entropies) == 0 { |
| 70 | + maskSecret(&rule, lines, idx) |
| 71 | + } |
| 72 | + |
| 73 | + if len(groups[0]) > 0 { |
| 74 | + for _, entropy := range rule.Entropies { |
| 75 | + // if matched group does not exist continue |
| 76 | + if len(groups[0]) <= entropy.Group { |
| 77 | + return |
| 78 | + } |
| 79 | + isMatch, _ := secrets.CheckEntropyInterval( |
| 80 | + entropy, |
| 81 | + groups[0][entropy.Group], |
| 82 | + ) |
| 83 | + if isMatch { |
| 84 | + maskSecret(&rule, lines, idx) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func maskSecret(rule *secrets.RegexQuery, lines *[]model.CodeLine, idx int) { |
| 93 | + if rule.SpecialMask == "all" { |
| 94 | + (*lines)[idx].Line = "<SECRET-MASKED-ON-PURPOSE>" |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + regex := rule.RegexStr |
| 99 | + line := (*lines)[idx] |
| 100 | + |
| 101 | + if len(rule.SpecialMask) > 0 { |
| 102 | + regex = "(.+)" + rule.SpecialMask |
| 103 | + } |
| 104 | + |
| 105 | + var re = regexp.MustCompile(regex) |
| 106 | + match := re.FindString(line.Line) |
| 107 | + |
| 108 | + if len(rule.SpecialMask) > 0 { |
| 109 | + match = line.Line[len(match):] |
| 110 | + } |
| 111 | + |
| 112 | + if match != "" { |
| 113 | + (*lines)[idx].Line = strings.Replace(line.Line, match, "<SECRET-MASKED-ON-PURPOSE>", 1) |
| 114 | + } else { |
| 115 | + (*lines)[idx].Line = "<SECRET-MASKED-ON-PURPOSE>" |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// repurposed isSecret from inspector |
| 120 | +func isSecret(line string, rule *secrets.RegexQuery, allowRules *[]secrets.AllowRule) (isSecretRet bool, groups [][]string) { |
| 121 | + if secrets.IsAllowRule(line, *allowRules) { |
| 122 | + return false, [][]string{} |
| 123 | + } |
| 124 | + |
| 125 | + groups = rule.Regex.FindAllStringSubmatch(line, -1) |
| 126 | + |
| 127 | + for _, group := range groups { |
| 128 | + splitedText := strings.Split(line, "\n") |
| 129 | + max := -1 |
| 130 | + for i, splited := range splitedText { |
| 131 | + if len(groups) < rule.Multiline.DetectLineGroup { |
| 132 | + if strings.Contains(splited, group[rule.Multiline.DetectLineGroup]) && i > max { |
| 133 | + max = i |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + if max == -1 { |
| 138 | + continue |
| 139 | + } |
| 140 | + secret, newGroups := isSecret(strings.Join(append(splitedText[:max], splitedText[max+1:]...), "\n"), rule, allowRules) |
| 141 | + if !secret { |
| 142 | + continue |
| 143 | + } |
| 144 | + groups = append(groups, newGroups...) |
| 145 | + } |
| 146 | + |
| 147 | + if len(groups) > 0 { |
| 148 | + return true, groups |
| 149 | + } |
| 150 | + return false, [][]string{} |
| 151 | +} |
0 commit comments