This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdetector.go
155 lines (143 loc) · 3.08 KB
/
detector.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package trojansourcedetector
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
)
// New creates a composite detector based on a configuration passed.
func New(config *Config) Detector {
var fileDetectors []SingleFileDetector
if config == nil {
config = &Config{}
config.Defaults()
}
if config.DetectBIDI {
fileDetectors = append(fileDetectors, &bidiDetector{})
}
if config.DetectUnicode {
fileDetectors = append(fileDetectors, &unicodeDetector{})
}
return &detector{
config: config,
fileDetectors: fileDetectors,
}
}
// Detector detects malicious unicode code points in a directory based on the configuration.
type Detector interface {
// Run runs the detection algorithm. The returned list of errors contains the violations of the
// passed rule set. If reading the directory fails, the list will contain an error entry without a file name.
Run() Errors
}
type detector struct {
fileDetectors []SingleFileDetector
config *Config
}
func (d *detector) Run() Errors {
lock := make(chan struct{}, d.config.Parallelism)
wg := &sync.WaitGroup{}
container := NewErrors()
err := filepath.Walk(d.config.Directory,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
path = filepath.ToSlash(path)
removePrefix := filepath.ToSlash(d.config.Directory)
if !strings.HasSuffix(removePrefix, "/") {
removePrefix = fmt.Sprintf("%s/", removePrefix)
}
reportedPath := strings.TrimPrefix(path, removePrefix)
if len(d.config.Include) != 0 {
match := false
for _, include := range d.config.Include {
matches, err := filepath.Match(include, reportedPath)
if err != nil {
return err
}
if matches {
match = true
break
}
}
if !match {
return nil
}
}
if len(d.config.Extensions) != 0 {
match := false
for _, ext := range d.config.Extensions {
actualExt := filepath.Ext(path)
if actualExt == ext {
match = true
break
}
}
if !match {
return nil
}
}
for _, exclude := range d.config.Exclude {
matches, err := filepath.Match(exclude, reportedPath)
if err != nil {
return err
}
if matches {
return nil
}
}
wg.Add(1)
go d.processFile(path, reportedPath, lock, wg, container)
return nil
})
wg.Wait()
if err != nil {
container.Add(
ErrIODirectory,
err.Error(),
"",
0,
0,
)
}
return container
}
func (d *detector) processFile(
path string,
reportedPath string,
lock chan struct{},
wg *sync.WaitGroup,
container Errors,
) {
lock <- struct{}{}
defer func() {
<-lock
wg.Done()
}()
fh, err := os.Open(path) //nolint:gosec
if err != nil {
container.Add(
ErrIOFile,
err.Error(),
path,
0,
0,
)
}
defer func() {
_ = fh.Close()
}()
for _, detector := range d.fileDetectors {
if _, err := fh.Seek(0, io.SeekStart); err != nil {
container.Add(ErrIOSeek, err.Error(), path, 0, 0)
continue
}
container.AddAll(detector.Detect(reportedPath, bufio.NewReader(fh)))
}
}