forked from Checkmarx/kics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_detect.go
73 lines (60 loc) · 1.98 KB
/
default_detect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package detector
import (
"strconv"
"strings"
"github.com/Checkmarx/kics/pkg/model"
"github.com/rs/zerolog"
)
const (
undetectedVulnerabilityLine = -1
)
type defaultDetectLine struct {
}
// DetectLine searches vulnerability line if kindDetectLine is not in detectors
func (d defaultDetectLine) DetectLine(file *model.FileMetadata, searchKey string,
outputLines int, logwithfields *zerolog.Logger) model.VulnerabilityLines {
detector := &DefaultDetectLineResponse{
CurrentLine: 0,
IsBreak: false,
FoundAtLeastOne: false,
ResolvedFile: file.FilePath,
ResolvedFiles: d.prepareResolvedFiles(file.ResolvedFiles),
}
var extractedString [][]string
extractedString = GetBracketValues(searchKey, extractedString, "")
sanitizedSubstring := searchKey
for idx, str := range extractedString {
sanitizedSubstring = strings.Replace(sanitizedSubstring, str[0], `{{`+strconv.Itoa(idx)+`}}`, -1)
}
lines := *file.LinesOriginalData
for _, key := range strings.Split(sanitizedSubstring, ".") {
substr1, substr2 := GenerateSubstrings(key, extractedString)
detector, lines = detector.DetectCurrentLine(substr1, substr2, 0, lines)
if detector.IsBreak {
break
}
}
if detector.FoundAtLeastOne {
return model.VulnerabilityLines{
Line: detector.CurrentLine + 1,
VulnLines: GetAdjacentVulnLines(detector.CurrentLine, outputLines, lines),
ResolvedFile: detector.ResolvedFile,
}
}
logwithfields.Warn().Msgf("Failed to detect line, query response %s", searchKey)
return model.VulnerabilityLines{
Line: undetectedVulnerabilityLine,
VulnLines: &[]model.CodeLine{},
ResolvedFile: detector.ResolvedFile,
}
}
func (d defaultDetectLine) prepareResolvedFiles(resFiles map[string]model.ResolvedFile) map[string]model.ResolvedFileSplit {
resolvedFiles := make(map[string]model.ResolvedFileSplit)
for f, res := range resFiles {
resolvedFiles[f] = model.ResolvedFileSplit{
Path: res.Path,
Lines: *res.LinesContent,
}
}
return resolvedFiles
}