mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-12-01 12:33:33 +01:00
151 lines
5.8 KiB
Go
151 lines
5.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"cyberstrike-ai/internal/database"
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func TestListVulnerabilitiesGroupedViews(t *testing.T) {
|
|
db, err := database.NewDB(filepath.Join(t.TempDir(), "grouped.db"), zap.NewNop())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
linked, err := db.CreateConversation("linked task", database.ConversationCreateMeta{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.CreateBatchQueue("queue", "queue", "", "eino_single", "manual", "", nil, "", 1,
|
|
[]map[string]interface{}{{"id": "task", "message": "task"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.UpdateBatchTaskStatus("queue", "task", "completed", linked.ID, "", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for i, item := range []struct {
|
|
id, target, severity, taskTag, conversationID string
|
|
}{
|
|
{"domain-old", "HTTPS://Example.COM.:443/login?keyword=%ZZ (旧接口);https://secondary.example.net/", "high", "a", ""},
|
|
{"domain-new", "example.com:8443(受影响的服务)", "high", "", linked.ID},
|
|
{"subdomain", "https://sub.example.com(旧记录)", "high", "a", ""},
|
|
{"ipv6", "https://[2001:0DB8::1]:8080/", "high", "b", linked.ID},
|
|
{"unknown", "/api/login", "high", "", ""},
|
|
{"filtered", "https://example.com/filtered", "low", "a", linked.ID},
|
|
} {
|
|
_, err := db.CreateVulnerability(&database.Vulnerability{
|
|
ID: item.id, Target: item.target, Title: item.id, Severity: item.severity,
|
|
TaskTag: item.taskTag, ConversationID: item.conversationID,
|
|
CreatedAt: time.Unix(int64(i+1), 0),
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
h := NewVulnerabilityHandler(db, zap.NewNop())
|
|
fetch := func(query string) ListVulnerabilitiesResponse {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest(http.MethodGet, "/api/vulnerabilities?limit=2&"+query, nil)
|
|
h.ListVulnerabilities(c)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, body = %s", w.Code, w.Body.String())
|
|
}
|
|
var result ListVulnerabilitiesResponse
|
|
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return result
|
|
}
|
|
|
|
for _, tc := range []struct {
|
|
query string
|
|
page, total, pages int
|
|
ids, hosts []string
|
|
}{
|
|
{"group_by=host&page=1", 1, 5, 3, []string{"domain-new", "domain-old", "domain-old"}, []string{"example.com", "example.com", "secondary.example.net"}},
|
|
{"group_by=host&page=4", 3, 5, 3, []string{"unknown"}, []string{""}},
|
|
{"group_by=task&page=1", 1, 3, 2, []string{"ipv6", "domain-new", "subdomain", "domain-old"}, nil},
|
|
{"group_by=task&page=2", 2, 3, 2, []string{"unknown"}, nil},
|
|
{"group_by=task&task_q=queue", 1, 1, 1, []string{"ipv6", "domain-new"}, nil},
|
|
{"group_by=none&page=1", 1, 5, 3, []string{"unknown", "ipv6"}, nil},
|
|
{"page=1", 1, 5, 3, []string{"unknown", "ipv6"}, nil},
|
|
} {
|
|
t.Run(tc.query, func(t *testing.T) {
|
|
result := fetch("severity=high&" + tc.query)
|
|
if result.Total != tc.total || result.TotalPages != tc.pages || result.Page != tc.page ||
|
|
result.PageSize != 2 || len(result.Vulnerabilities) != len(tc.ids) {
|
|
t.Fatalf("unexpected pagination: %+v", result)
|
|
}
|
|
for i, v := range result.Vulnerabilities {
|
|
if v.ID != tc.ids[i] {
|
|
t.Fatalf("record %d = %q, want %q", i, v.ID, tc.ids[i])
|
|
}
|
|
if tc.hosts == nil {
|
|
if v.TargetHost != nil {
|
|
t.Fatalf("ordinary listing returned target_host for %q", v.ID)
|
|
}
|
|
} else if v.TargetHost == nil || *v.TargetHost != tc.hosts[i] {
|
|
t.Fatalf("target_host for %q = %v, want %q", v.ID, v.TargetHost, tc.hosts[i])
|
|
} else if v.ID == "subdomain" && v.TargetDomain != "example.com" {
|
|
t.Fatalf("subdomain parent = %q", v.TargetDomain)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// Differing severities and timestamps ensure sorting selects entire groups
|
|
// before applying the page boundary, including when a filter hides rows.
|
|
for id, severity := range map[string]string{
|
|
"domain-old": "critical", "domain-new": "medium", "ipv6": "info",
|
|
} {
|
|
if _, err := db.Exec("UPDATE vulnerabilities SET severity = ? WHERE id = ?", severity, id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, tc := range []struct {
|
|
query string
|
|
ids []string
|
|
}{
|
|
{"group_by=host&sort=severity&page=1", []string{"domain-old", "domain-new", "filtered", "domain-old"}},
|
|
{"group_by=host&sort=severity&page=2", []string{"unknown", "subdomain"}},
|
|
{"group_by=task&sort=severity&page=1", []string{"domain-old", "subdomain", "unknown"}},
|
|
{"group_by=task&sort=severity&page=2", []string{"domain-new", "filtered", "ipv6"}},
|
|
{"group_by=host&sort=latest", []string{"filtered", "domain-new", "domain-old", "unknown"}},
|
|
{"group_by=task&sort=latest", []string{"filtered", "ipv6", "domain-new", "unknown"}},
|
|
{"group_by=none&sort=oldest", []string{"domain-old", "domain-new"}},
|
|
{"group_by=none&sort=oldest&page=2", []string{"subdomain", "ipv6"}},
|
|
{"group_by=host&sort=oldest", []string{"domain-old", "domain-new", "filtered", "domain-old"}},
|
|
{"group_by=task&sort=oldest", []string{"domain-old", "subdomain", "domain-new", "ipv6", "filtered"}},
|
|
{"group_by=none&sort=severity", []string{"domain-old", "unknown"}},
|
|
{"group_by=host&sort=severity&severity=high", []string{"unknown", "subdomain"}},
|
|
{"group_by=host&sort=latest&q=missing", []string{}},
|
|
{"group_by=host&host_q=example.com", []string{"filtered", "domain-new", "domain-old", "subdomain"}},
|
|
{"group_by=host&host_q=%25", []string{}},
|
|
{"group_by=host&host_q=missing", []string{}},
|
|
} {
|
|
t.Run(tc.query, func(t *testing.T) {
|
|
result := fetch(tc.query)
|
|
if len(result.Vulnerabilities) != len(tc.ids) {
|
|
t.Fatalf("got %d records, want %d", len(result.Vulnerabilities), len(tc.ids))
|
|
}
|
|
for i, id := range tc.ids {
|
|
if result.Vulnerabilities[i].ID != id {
|
|
t.Fatalf("record %d = %q, want %q", i, result.Vulnerabilities[i].ID, id)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|