-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_pipeline_test.go
More file actions
613 lines (549 loc) · 17.1 KB
/
Copy pathfix_pipeline_test.go
File metadata and controls
613 lines (549 loc) · 17.1 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package sight
import (
"strings"
"testing"
)
// =====================================================================
// 1. NewFixPipeline creates pipeline with built-in rules
// =====================================================================
func TestNewFixPipeline_HasBuiltinRules(t *testing.T) {
p := NewFixPipeline()
if p == nil {
t.Fatal("expected non-nil FixPipeline")
}
if len(p.rules) != 7 {
t.Fatalf("expected 7 built-in rules, got %d", len(p.rules))
}
}
// =====================================================================
// 2. SQL injection finding generates parameterized query fix
// =====================================================================
func TestGenerateFixes_SQLInjection(t *testing.T) {
p := NewFixPipeline()
findings := []Finding{
{
Concern: "sql injection in query builder",
Severity: SeverityHigh,
File: "db/query.go",
Line: 42,
Message: "string concatenation used to build SQL query",
CWE: "CWE-89",
},
}
fixes := p.GenerateFixes(findings)
if len(fixes) != 1 {
t.Fatalf("expected 1 fix, got %d", len(fixes))
}
fix := fixes[0]
if fix.Category != "injection" {
t.Errorf("expected category 'injection', got %q", fix.Category)
}
if fix.Priority != 1 {
t.Errorf("expected priority 1, got %d", fix.Priority)
}
if fix.Confidence != 0.85 {
t.Errorf("expected confidence 0.85, got %f", fix.Confidence)
}
if !strings.Contains(fix.Title, "parameterized") {
t.Errorf("expected title to mention parameterized queries, got %q", fix.Title)
}
if !strings.Contains(fix.FixCode, "db.Query") {
t.Errorf("expected fix code to contain 'db.Query', got %q", fix.FixCode)
}
if fix.FindingID == "" {
t.Error("expected non-empty FindingID")
}
}
// =====================================================================
// 3. XSS finding generates escaping fix
// =====================================================================
func TestGenerateFixes_XSS(t *testing.T) {
p := NewFixPipeline()
findings := []Finding{
{
Concern: "xss vulnerability",
Severity: SeverityHigh,
File: "handlers/render.go",
Line: 15,
Message: "user input rendered without escaping",
CWE: "CWE-79",
},
}
fixes := p.GenerateFixes(findings)
if len(fixes) != 1 {
t.Fatalf("expected 1 fix, got %d", len(fixes))
}
fix := fixes[0]
if fix.Category != "xss" {
t.Errorf("expected category 'xss', got %q", fix.Category)
}
if fix.Priority != 1 {
t.Errorf("expected priority 1, got %d", fix.Priority)
}
if !strings.Contains(fix.Title, "Escape") && !strings.Contains(fix.Title, "sanitize") {
t.Errorf("expected title to mention escaping/sanitizing, got %q", fix.Title)
}
if !strings.Contains(fix.FixCode, "html.EscapeString") {
t.Errorf("expected fix code to mention html.EscapeString, got %q", fix.FixCode)
}
}
// =====================================================================
// 4. Hardcoded secret finding generates env var fix
// =====================================================================
func TestGenerateFixes_HardcodedSecret(t *testing.T) {
p := NewFixPipeline()
findings := []Finding{
{
Concern: "hardcoded credential in source",
Severity: SeverityCritical,
File: "config/secrets.go",
Line: 8,
Message: "API key hardcoded in source code",
CWE: "CWE-798",
},
}
fixes := p.GenerateFixes(findings)
if len(fixes) != 1 {
t.Fatalf("expected 1 fix, got %d", len(fixes))
}
fix := fixes[0]
if fix.Category != "auth" {
t.Errorf("expected category 'auth', got %q", fix.Category)
}
if fix.Priority != 2 {
t.Errorf("expected priority 2, got %d", fix.Priority)
}
if fix.EstimatedEffort != "trivial" {
t.Errorf("expected effort 'trivial', got %q", fix.EstimatedEffort)
}
if !strings.Contains(fix.FixCode, "os.Getenv") {
t.Errorf("expected fix code to mention os.Getenv, got %q", fix.FixCode)
}
}
// =====================================================================
// 5. Missing validation finding generates middleware fix
// =====================================================================
func TestGenerateFixes_MissingValidation(t *testing.T) {
p := NewFixPipeline()
findings := []Finding{
{
Concern: "missing input validation on request body",
Severity: SeverityMedium,
File: "handlers/users.go",
Line: 25,
Message: "unsanitized user input passed to business logic",
CWE: "CWE-20",
},
}
fixes := p.GenerateFixes(findings)
if len(fixes) != 1 {
t.Fatalf("expected 1 fix, got %d", len(fixes))
}
fix := fixes[0]
if fix.Category != "input-validation" {
t.Errorf("expected category 'input-validation', got %q", fix.Category)
}
if fix.Priority != 3 {
t.Errorf("expected priority 3, got %d", fix.Priority)
}
if fix.EstimatedEffort != "moderate" {
t.Errorf("expected effort 'moderate', got %q", fix.EstimatedEffort)
}
if !strings.Contains(fix.FixCode, "validator") {
t.Errorf("expected fix code to mention validator, got %q", fix.FixCode)
}
}
// =====================================================================
// 6. Weak crypto finding generates algorithm replacement fix
// =====================================================================
func TestGenerateFixes_WeakCrypto(t *testing.T) {
p := NewFixPipeline()
findings := []Finding{
{
Concern: "weak hash algorithm used for checksum",
Severity: SeverityHigh,
File: "auth/hash.go",
Line: 12,
Message: "md5 used for hashing sensitive data",
CWE: "CWE-327",
},
}
fixes := p.GenerateFixes(findings)
if len(fixes) != 1 {
t.Fatalf("expected 1 fix, got %d", len(fixes))
}
fix := fixes[0]
if fix.Category != "crypto" {
t.Errorf("expected category 'crypto', got %q", fix.Category)
}
if fix.Priority != 2 {
t.Errorf("expected priority 2, got %d", fix.Priority)
}
if !strings.Contains(fix.FixCode, "sha256") {
t.Errorf("expected fix code to mention sha256, got %q", fix.FixCode)
}
if !strings.Contains(fix.Title, "Replace") {
t.Errorf("expected title to mention replacement, got %q", fix.Title)
}
}
// =====================================================================
// 7. Path traversal finding generates filepath.Clean fix
// =====================================================================
func TestGenerateFixes_PathTraversal(t *testing.T) {
p := NewFixPipeline()
findings := []Finding{
{
Concern: "path traversal via user-controlled file path",
Severity: SeverityHigh,
File: "handlers/files.go",
Line: 30,
Message: "directory traversal possible through unsanitized input",
CWE: "CWE-22",
},
}
fixes := p.GenerateFixes(findings)
if len(fixes) != 1 {
t.Fatalf("expected 1 fix, got %d", len(fixes))
}
fix := fixes[0]
if fix.Category != "input-validation" {
t.Errorf("expected category 'input-validation', got %q", fix.Category)
}
if fix.Priority != 2 {
t.Errorf("expected priority 2, got %d", fix.Priority)
}
if !strings.Contains(fix.FixCode, "filepath.Clean") {
t.Errorf("expected fix code to mention filepath.Clean, got %q", fix.FixCode)
}
}
// =====================================================================
// 8. SSRF finding generates allowlist fix
// =====================================================================
func TestGenerateFixes_SSRF(t *testing.T) {
p := NewFixPipeline()
findings := []Finding{
{
Concern: "ssrf via user-supplied URL",
Severity: SeverityHigh,
File: "client/fetch.go",
Line: 18,
Message: "server-side request forgery possible through unvalidated URL",
CWE: "CWE-918",
},
}
fixes := p.GenerateFixes(findings)
if len(fixes) != 1 {
t.Fatalf("expected 1 fix, got %d", len(fixes))
}
fix := fixes[0]
if fix.Category != "ssrf" {
t.Errorf("expected category 'ssrf', got %q", fix.Category)
}
if fix.Priority != 2 {
t.Errorf("expected priority 2, got %d", fix.Priority)
}
if !strings.Contains(fix.FixCode, "allowedHosts") && !strings.Contains(fix.FixCode, "allowlist") {
t.Errorf("expected fix code to mention allowlist/allowedHosts, got %q", fix.FixCode)
}
}
// =====================================================================
// 9. Custom rule registration and matching
// =====================================================================
func TestAddRule_CustomRuleMatches(t *testing.T) {
p := NewFixPipeline()
customRule := FixRule{
MatchFn: func(f Finding) bool {
return strings.Contains(strings.ToLower(f.Concern), "no unit tests")
},
Generator: func(f Finding) FixSuggestion {
return FixSuggestion{
Title: "Add unit tests for uncovered code",
Description: "Missing test coverage",
FixCode: "func TestFoo(t *testing.T) { ... }",
Confidence: 0.75,
Category: "testing",
Severity: f.Severity.String(),
EstimatedEffort: "moderate",
Priority: 4,
}
},
}
p.AddRule(customRule)
findings := []Finding{
{
Concern: "no unit tests for handler",
Severity: SeverityLow,
File: "handlers/foo.go",
Line: 10,
Message: "function lacks test coverage",
},
}
fixes := p.GenerateFixes(findings)
// Only the custom rule should match -- none of the built-in rules match.
if len(fixes) != 1 {
t.Fatalf("expected 1 fix from custom rule, got %d", len(fixes))
}
if fixes[0].Category != "testing" {
t.Errorf("expected category 'testing', got %q", fixes[0].Category)
}
if fixes[0].Priority != 4 {
t.Errorf("expected priority 4, got %d", fixes[0].Priority)
}
}
func TestAddRule_ConcurrentSafety(t *testing.T) {
p := NewFixPipeline()
done := make(chan struct{})
go func() {
p.AddRule(FixRule{
MatchFn: func(Finding) bool { return false },
Generator: func(Finding) FixSuggestion { return FixSuggestion{} },
})
close(done)
}()
// Read rules concurrently
_ = p.GenerateFixes(nil)
<-done
}
// =====================================================================
// 10. Multiple findings sorted by priority then confidence
// =====================================================================
func TestGenerateFixes_SortedByPriorityThenConfidence(t *testing.T) {
p := NewFixPipeline()
findings := []Finding{
// SSRF: priority 2, confidence 0.80
{
Concern: "ssrf vulnerability",
Severity: SeverityHigh,
File: "a.go",
Line: 1,
Message: "server-side request forgery",
CWE: "CWE-918",
},
// SQL injection: priority 1, confidence 0.85
{
Concern: "sql injection",
Severity: SeverityHigh,
File: "b.go",
Line: 1,
Message: "sql injection via string concat",
CWE: "CWE-89",
},
// XSS: priority 1, confidence 0.85
{
Concern: "xss",
Severity: SeverityHigh,
File: "c.go",
Line: 1,
Message: "cross-site scripting",
CWE: "CWE-79",
},
// Input validation: priority 3, confidence 0.80
{
Concern: "missing validation",
Severity: SeverityMedium,
File: "d.go",
Line: 1,
Message: "input validation missing",
CWE: "CWE-20",
},
}
fixes := p.GenerateFixes(findings)
if len(fixes) != 4 {
t.Fatalf("expected 4 fixes, got %d", len(fixes))
}
// Priority 1 items should come first, then 2, then 3.
for i := 0; i < len(fixes)-1; i++ {
if fixes[i].Priority > fixes[i+1].Priority {
t.Errorf("fix[%d].Priority=%d > fix[%d].Priority=%d", i, fixes[i].Priority, i+1, fixes[i+1].Priority)
}
if fixes[i].Priority == fixes[i+1].Priority && fixes[i].Confidence < fixes[i+1].Confidence {
t.Errorf("same priority but fix[%d].Confidence=%f < fix[%d].Confidence=%f",
i, fixes[i].Confidence, i+1, fixes[i+1].Confidence)
}
}
}
// =====================================================================
// 11. Deduplication: same finding matched by multiple rules keeps highest confidence
// =====================================================================
func TestGenerateFixes_DeduplicationKeepsHighestConfidence(t *testing.T) {
p := NewFixPipeline()
// Register two custom rules that both match "sql injection" findings but
// produce different confidence values.
p.AddRule(FixRule{
MatchFn: func(f Finding) bool {
return strings.Contains(strings.ToLower(f.Concern), "sql injection")
},
Generator: func(f Finding) FixSuggestion {
return FixSuggestion{
Title: "Low-confidence custom fix",
Confidence: 0.50,
Category: "custom-low",
Priority: 1,
}
},
})
p.AddRule(FixRule{
MatchFn: func(f Finding) bool {
return strings.Contains(strings.ToLower(f.Concern), "sql injection")
},
Generator: func(f Finding) FixSuggestion {
return FixSuggestion{
Title: "High-confidence custom fix",
Confidence: 0.95,
Category: "custom-high",
Priority: 1,
}
},
})
findings := []Finding{
{
Concern: "sql injection in handler",
Severity: SeverityHigh,
File: "handler.go",
Line: 10,
Message: "sql injection",
CWE: "CWE-89",
},
}
fixes := p.GenerateFixes(findings)
// All rules (built-in + 2 custom) match, but they all produce a fix for
// the same finding ID. Only the highest-confidence one should survive.
if len(fixes) != 1 {
t.Fatalf("expected 1 deduplicated fix, got %d", len(fixes))
}
if fixes[0].Category != "custom-high" {
t.Errorf("expected highest-confidence fix to win, got category %q (confidence %f)",
fixes[0].Category, fixes[0].Confidence)
}
}
func TestGenerateFixes_DeduplicationSameConfidenceKeepsEarlierRule(t *testing.T) {
p := NewFixPipeline()
// Two custom rules with identical confidence -- the first registered should win.
p.AddRule(FixRule{
MatchFn: func(f Finding) bool {
return f.Concern == "custom-concern"
},
Generator: func(f Finding) FixSuggestion {
return FixSuggestion{
Title: "First rule",
Confidence: 0.80,
Category: "first",
Priority: 1,
}
},
})
p.AddRule(FixRule{
MatchFn: func(f Finding) bool {
return f.Concern == "custom-concern"
},
Generator: func(f Finding) FixSuggestion {
return FixSuggestion{
Title: "Second rule",
Confidence: 0.80,
Category: "second",
Priority: 1,
}
},
})
findings := []Finding{
{
Concern: "custom-concern",
Severity: SeverityMedium,
File: "test.go",
Line: 5,
Message: "test finding",
},
}
fixes := p.GenerateFixes(findings)
if len(fixes) != 1 {
t.Fatalf("expected 1 deduplicated fix, got %d", len(fixes))
}
if fixes[0].Category != "first" {
t.Errorf("expected earlier rule to win on tie, got category %q", fixes[0].Category)
}
}
// =====================================================================
// 12. No matching rules returns empty slice
// =====================================================================
func TestGenerateFixes_NoMatchingRules(t *testing.T) {
p := NewFixPipeline()
findings := []Finding{
{
Concern: "naming convention violation",
Severity: SeverityInfo,
File: "style.go",
Line: 1,
Message: "function name uses camelCase instead of snake_case",
},
}
fixes := p.GenerateFixes(findings)
if len(fixes) != 0 {
t.Errorf("expected 0 fixes for non-matching finding, got %d", len(fixes))
}
}
func TestGenerateFixes_EmptyFindings(t *testing.T) {
p := NewFixPipeline()
fixes := p.GenerateFixes(nil)
if len(fixes) != 0 {
t.Errorf("expected 0 fixes for nil findings, got %d", len(fixes))
}
fixes = p.GenerateFixes([]Finding{})
if len(fixes) != 0 {
t.Errorf("expected 0 fixes for empty findings, got %d", len(fixes))
}
}
// =====================================================================
// 13. String() output format
// =====================================================================
func TestFixSuggestion_String(t *testing.T) {
fix := FixSuggestion{
Title: "Use parameterized queries",
FindingID: "db/query.go:42:sql injection",
Severity: "high",
EstimatedEffort: "easy",
Priority: 1,
Confidence: 0.85,
Description: "Replace string concatenation with parameterized queries.",
FixCode: "query := \"SELECT * FROM t WHERE id = $1\"\nrows, err := db.Query(query, id)",
Category: "injection",
}
out := fix.String()
// Verify required sections are present.
checks := []struct {
label string
want string
}{
{"category+title", "[injection] Use parameterized queries"},
{"finding", "Finding: db/query.go:42:sql injection"},
{"severity", "Severity: high"},
{"effort", "Effort: easy"},
{"priority", "Priority: 1"},
{"confidence", "Confidence: 85%"},
{"description", "Description: Replace string concatenation with parameterized queries."},
}
for _, c := range checks {
if !strings.Contains(out, c.want) {
t.Errorf("String() missing %q\n\nGot:\n%s", c.label, out)
}
}
// FixCode section should be present.
if !strings.Contains(out, " Suggested Fix:") {
t.Error("String() missing 'Suggested Fix:' section")
}
}
func TestFixSuggestion_String_NoFixCode(t *testing.T) {
fix := FixSuggestion{
Title: "Informational note",
FindingID: "note.go:1:info",
Severity: "info",
Description: "Just a note.",
Category: "info",
}
out := fix.String()
if strings.Contains(out, "Suggested Fix:") {
t.Error("String() should not contain 'Suggested Fix:' when FixCode is empty")
}
if !strings.Contains(out, "[info] Informational note") {
t.Errorf("String() missing expected header, got:\n%s", out)
}
}