-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_rules.go
More file actions
206 lines (189 loc) · 5.87 KB
/
Copy pathstatic_rules.go
File metadata and controls
206 lines (189 loc) · 5.87 KB
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
package sight
import (
"fmt"
"regexp"
"strings"
)
// StaticRule defines a pattern-based static analysis rule that can catch common
// issues without invoking an LLM. Rules are matched against diff content or full
// file content before the LLM review pass, saving tokens on obvious issues.
type StaticRule struct {
ID string // unique identifier, e.g. "SEC-GO-001"
Name string // short human-readable name
Description string // detailed explanation
Language string // "go", "python", "typescript", "javascript", "any"
Pattern *regexp.Regexp // primary detection pattern
Antipattern *regexp.Regexp // if this also matches the same line, suppress the finding
Severity string // "critical", "high", "medium", "low"
Category string // "security", "correctness", "performance"
CWE string // e.g., "CWE-89"
Fix string // suggested fix description
}
// StaticAnalyzer runs pattern-based rules against code before the LLM review.
type StaticAnalyzer struct {
Rules []StaticRule
}
// NewStaticAnalyzer creates a StaticAnalyzer preloaded with the default rule set.
func NewStaticAnalyzer() *StaticAnalyzer {
return &StaticAnalyzer{
Rules: defaultRules(),
}
}
// Analyze runs all matching rules against a unified diff string and returns findings.
// Only added lines (starting with "+") are checked. The language parameter filters
// rules to those matching the language or "any".
func (sa *StaticAnalyzer) Analyze(diff string, language string) []Finding {
var findings []Finding
// Parse diff to extract added lines with file and line info
type diffLine struct {
file string
lineNum int
text string
}
var lines []diffLine
var currentFile string
var lineNum int
for _, raw := range strings.Split(diff, "\n") {
if strings.HasPrefix(raw, "+++ b/") {
currentFile = strings.TrimPrefix(raw, "+++ b/")
lineNum = 0
continue
}
if strings.HasPrefix(raw, "+++ ") {
currentFile = strings.TrimPrefix(raw, "+++ ")
lineNum = 0
continue
}
if strings.HasPrefix(raw, "@@ ") {
// Parse hunk header for line number: @@ -a,b +c,d @@
lineNum = parseHunkNewStart(raw) - 1
continue
}
if strings.HasPrefix(raw, "+") && !strings.HasPrefix(raw, "+++") {
lineNum++
lines = append(lines, diffLine{
file: currentFile,
lineNum: lineNum,
text: raw[1:], // strip leading "+"
})
} else if !strings.HasPrefix(raw, "-") {
lineNum++
}
}
lang := strings.ToLower(language)
for _, rule := range sa.Rules {
if rule.Language != "any" && rule.Language != lang {
continue
}
for _, dl := range lines {
if rule.Pattern.MatchString(dl.text) {
// Check antipattern — if it matches, this is a false positive
if rule.Antipattern != nil && rule.Antipattern.MatchString(dl.text) {
continue
}
findings = append(findings, Finding{
Concern: "static:" + rule.Category,
Severity: ParseSeverity(rule.Severity),
File: dl.file,
Line: dl.lineNum,
Message: fmt.Sprintf("[%s] %s: %s", rule.ID, rule.Name, rule.Description),
Fix: rule.Fix,
CWE: rule.CWE,
})
}
}
}
return findings
}
// AnalyzeFile runs all matching rules against a full file's content.
// Each line is checked independently. The language parameter filters rules.
func (sa *StaticAnalyzer) AnalyzeFile(content string, language string) []Finding {
var findings []Finding
lang := strings.ToLower(language)
lines := strings.Split(content, "\n")
for _, rule := range sa.Rules {
if rule.Language != "any" && rule.Language != lang {
continue
}
for i, line := range lines {
if rule.Pattern.MatchString(line) {
if rule.Antipattern != nil && rule.Antipattern.MatchString(line) {
continue
}
findings = append(findings, Finding{
Concern: "static:" + rule.Category,
Severity: ParseSeverity(rule.Severity),
File: "",
Line: i + 1,
Message: fmt.Sprintf("[%s] %s: %s", rule.ID, rule.Name, rule.Description),
Fix: rule.Fix,
CWE: rule.CWE,
})
}
}
}
return findings
}
// AnalyzeFileWithPath is like AnalyzeFile but sets the File field on findings.
func (sa *StaticAnalyzer) AnalyzeFileWithPath(content string, language string, path string) []Finding {
findings := sa.AnalyzeFile(content, language)
for i := range findings {
findings[i].File = path
}
return findings
}
// DetectLanguage guesses the language from a file path extension.
func DetectLanguage(path string) string {
lower := strings.ToLower(path)
switch {
case strings.HasSuffix(lower, ".go"):
return "go"
case strings.HasSuffix(lower, ".py"):
return "python"
case strings.HasSuffix(lower, ".ts"):
return "typescript"
case strings.HasSuffix(lower, ".tsx"):
return "typescript"
case strings.HasSuffix(lower, ".js"):
return "javascript"
case strings.HasSuffix(lower, ".jsx"):
return "javascript"
case strings.HasSuffix(lower, ".rs"):
return "rust"
case strings.HasSuffix(lower, ".java"):
return "java"
case strings.HasSuffix(lower, ".c") || strings.HasSuffix(lower, ".h"):
return "c"
case strings.HasSuffix(lower, ".cpp") || strings.HasSuffix(lower, ".hpp") || strings.HasSuffix(lower, ".cc") || strings.HasSuffix(lower, ".cxx"):
return "cpp"
case strings.HasSuffix(lower, ".rb"):
return "ruby"
case strings.HasSuffix(lower, ".sql"):
return "sql"
default:
return "any"
}
}
// parseHunkNewStart extracts the new-file start line from a hunk header.
// Format: @@ -old,count +new,count @@
func parseHunkNewStart(header string) int {
// Find "+N" after the first space
idx := strings.Index(header, "+")
if idx < 0 {
return 1
}
rest := header[idx+1:]
var n int
for _, ch := range rest {
if ch >= '0' && ch <= '9' {
n = n*10 + int(ch-'0')
} else {
break
}
}
if n == 0 {
return 1
}
return n
}
// defaultRules and the built-in rule set live in static_rules_defaults.go.