internal/codescan/scope.go
144 lines · 4105 bytes
1package codescan
2
3import (
4 "bufio"
5 "bytes"
6 "path"
7 "strings"
8
9 "gitbay.org/gitbay/internal/ci"
10 "gitbay.org/gitbay/internal/store"
11)
12
13// Scope is what a repository sets aside from what its scan reports.
14type Scope struct {
15 // Ignore are path globs, as CI paths take them (security-ignore).
16 Ignore []string
17 // Vendored are the .gitattributes patterns Vendored returns.
18 Vendored []string
19 // ExcludeRules are rule ids or families (security-exclude-rule).
20 ExcludeRules []string
21 // Uses reports whether the scanned commit depends on any of packages,
22 // written as library lists them. Nil uses everything.
23 Uses func(packages []string) bool
24 // Read returns a file at the scanned commit, for rules whose hits are
25 // checked against the source. Nil checks nothing.
26 Read func(path string) ([]byte, error)
27}
28
29// Aside counts the findings a scope set aside: Ignored by path or rule,
30// Unused by the library a rule is about, Quoted by reading the template an
31// unquoted-attribute hit is in.
32type Aside struct {
33 Ignored int `json:"ignored,omitempty"`
34 Unused map[string]int `json:"unused,omitempty"`
35 Quoted int `json:"quoted,omitempty"`
36}
37
38// Any reports whether anything was set aside.
39func (a Aside) Any() bool {
40 return a.Ignored > 0 || len(a.Unused) > 0 || a.Quoted > 0
41}
42
43// Apply splits findings into those reported, at most MaxFindings of them,
44// and counts of those set aside. It reports whether reported ones were
45// dropped for the cap.
46func (s Scope) Apply(findings []store.SecurityFinding) ([]store.SecurityFinding, Aside, bool) {
47 var kept []store.SecurityFinding
48 var aside Aside
49 truncated := false
50 unquoted := map[string]map[int]bool{}
51 for _, f := range findings {
52 if s.ignored(f) {
53 aside.Ignored++
54 continue
55 }
56 if l, ok := libraryFor(f.Rule); ok && s.Uses != nil && !s.Uses(l.packages) {
57 if aside.Unused == nil {
58 aside.Unused = map[string]int{}
59 }
60 aside.Unused[l.name()]++
61 continue
62 }
63 if ruleIn(f.Rule, unquotedAttrRule) && !s.unquoted(f, unquoted) {
64 aside.Quoted++
65 continue
66 }
67 if len(kept) == MaxFindings {
68 truncated = true
69 continue
70 }
71 kept = append(kept, f)
72 }
73 return kept, aside, truncated
74}
75
76// unquoted reports whether an unquoted-attribute hit is on a line where the
77// template has an unquoted attribute expression, or the file cannot be
78// read to say. Files are read once, into seen.
79func (s Scope) unquoted(f store.SecurityFinding, seen map[string]map[int]bool) bool {
80 if s.Read == nil || f.Path == "" || f.Line == 0 {
81 return true
82 }
83 lines, read := seen[f.Path]
84 if !read {
85 if src, err := s.Read(f.Path); err == nil {
86 if l, ok := unquotedLines(src); ok {
87 lines = l
88 }
89 }
90 seen[f.Path] = lines
91 }
92 return lines == nil || lines[f.Line]
93}
94
95func (s Scope) ignored(f store.SecurityFinding) bool {
96 for _, g := range s.Ignore {
97 if ci.Match(g, f.Path) {
98 return true
99 }
100 }
101 for _, p := range s.Vendored {
102 if attrMatch(p, f.Path) {
103 return true
104 }
105 }
106 for _, r := range s.ExcludeRules {
107 if ruleIn(f.Rule, r) {
108 return true
109 }
110 }
111 return false
112}
113
114// Vendored returns the patterns a .gitattributes file marks
115// linguist-vendored or linguist-generated. Unset and false values are left
116// out.
117func Vendored(gitattributes []byte) []string {
118 var out []string
119 sc := bufio.NewScanner(bytes.NewReader(gitattributes))
120 for sc.Scan() {
121 fields := strings.Fields(sc.Text())
122 if len(fields) < 2 || strings.HasPrefix(fields[0], "#") {
123 continue
124 }
125 for _, a := range fields[1:] {
126 if a == "linguist-vendored" || a == "linguist-generated" || a == "linguist-vendored=true" || a == "linguist-generated=true" {
127 out = append(out, fields[0])
128 break
129 }
130 }
131 }
132 return out
133}
134
135// attrMatch matches a path the way git matches a .gitattributes pattern: one
136// without a slash names files by base name at any depth, one with a slash is
137// relative to the top.
138func attrMatch(pattern, file string) bool {
139 if !strings.Contains(pattern, "/") {
140 ok, err := path.Match(pattern, path.Base(file))
141 return err == nil && ok
142 }
143 return ci.Match(strings.TrimPrefix(pattern, "/"), file)
144}