-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
211 lines (178 loc) · 3.89 KB
/
types.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package polylint
import (
"crypto/sha256"
"fmt"
"io"
"net/http"
"os"
"path"
"regexp"
)
type SeverityLevel int
type Scope string
type FnType string
const (
unknownSeverity SeverityLevel = iota
lowSeverity
mediumSeverity
highSeverity
)
const (
builtinType FnType = "builtin"
jsType FnType = "js"
wasmType FnType = "wasm"
)
const (
unknownScope Scope = "unknown"
pathScope Scope = "path"
fileScope Scope = "file"
lineScope Scope = "line"
)
type Ignore struct {
Id string
Scope Scope
SourceLineNo int
LineNo int
}
type FileReport struct {
Path string
Ignores []Ignore
Rules []Rule
Findings []Finding
}
type Finding struct {
Path string
Line string
LineIndex int
LineNo int
Rule Rule
RuleId string
}
type RuleFunc func(string, int, string) bool
type Rule struct {
Fn RuleFunc
Id string
Description string
Recommendation string
Severity SeverityLevel
// Link to the documentation for this Rule
Link string
// FilenameRegex is the regexp used to determine if this Rule should run on a given file
IncludePaths *regexp.Regexp
ExcludePaths *regexp.Regexp
Scope Scope
}
type Fn struct {
Type FnType
Scope Scope
Name string
Args []any
Body string
}
type RawFn struct {
Type string
Scope Scope
Name string
Args []any
Body string
// sha256: sha256 hash in hex form
Metadata map[string]any
}
func (f RawFn) GetMetadataHash() (string, error) {
hash, ok := f.Metadata["sha256"]
if !ok {
return "", fmt.Errorf("could not get sha256 hash from metadata for: %s", f.Body)
}
h, ok := hash.(string)
if !ok {
return "", fmt.Errorf("could not get sha256 hash from metadata for: %s", f.Body)
}
return h, nil
}
func (f RawFn) GetWASMFromUrl(url string) ([]byte, error) {
// Get the data
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Write the body to file
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, err
}
func (f RawFn) GetWASMFromPath(path string) ([]byte, error) {
// Create the file
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return content, nil
}
func (f RawFn) WriteWASMToCache(content []byte) (bool, error) {
// Make dir in ~/.local/cache/polylint/cache/SHA256
h := sha256.New()
h.Write(content)
bs := h.Sum(nil)
hex := fmt.Sprintf("%x", bs)
output_folder, err := f.CacheDirWASM()
if err != nil {
return false, err
}
os.MkdirAll(output_folder, 0755)
os.WriteFile(path.Join(output_folder, hex), content, 0755)
return true, nil
}
func (f RawFn) CacheDirWASM() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return path.Join(home, ".local", "cache", "polylint", "cache"), nil
}
func (f RawFn) CheckWASMHash(content []byte, hash string) bool {
h := sha256.New()
h.Write(content)
bs := h.Sum(nil)
actual := fmt.Sprintf("%x", bs)
comparison := actual == hash
if !comparison {
logz.Infof("Comparison failed for desired sha256 %s and actual: %s\n", hash, actual)
}
return comparison
}
func (f RawFn) GetWASMFromCache(hash string) ([]byte, error) {
dir, err := f.CacheDirWASM()
if err != nil {
return nil, err
}
// TODO: move to debug level logging
logz.Debugf("Success fetching file from cache %s\n", hash)
return os.ReadFile(path.Join(dir, hash))
}
type RawRule struct {
Id string
Description string
Recommendation string
Severity string
Link string
IncludePaths *regexp.Regexp `yaml:"include_paths"`
ExcludePaths *regexp.Regexp `yaml:"exclude_paths"`
Fn RawFn
}
type RawConfig struct {
Version string
Includes []IncludeRaw
Rules []RawRule
}
type IncludeRaw struct {
Path string `yaml:"path"`
// Hash of the file contents prefixed with the algorithm
Hash string `yaml:"hash"`
}
type ConfigFile struct {
Rules []Rule
Version string
}