mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-12-01 13:03:41 +01:00
146 lines
5.3 KiB
Go
146 lines
5.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"cyberstrike-ai/internal/database"
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func TestVulnerabilityBrowserRecordPagination(t *testing.T) {
|
|
db, err := database.NewDB(filepath.Join(t.TempDir(), "browser.db"), zap.NewNop())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
for i := 0; i < 55; i++ {
|
|
target, tag := "https://a.example.test", "shared"
|
|
if i == 28 {
|
|
target += " https://b.example.test"
|
|
}
|
|
if i >= 29 {
|
|
target, tag = fmt.Sprintf("https://host%d.test", i), fmt.Sprintf("task%d", i)
|
|
}
|
|
if i == 54 {
|
|
target, tag = "/api/login", ""
|
|
}
|
|
_, err := db.CreateVulnerability(&database.Vulnerability{ID: fmt.Sprintf("v%02d", i), Title: fmt.Sprintf("Finding %d", i), Target: target, TaskTag: tag, Severity: "low", Status: "open", CreatedAt: time.Unix(int64(i+1), 0)})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
h := NewVulnerabilityHandler(db, zap.NewNop())
|
|
fetch := func(view, selection string, page int, extra string) ListVulnerabilitiesResponse {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/vulnerabilities?pagination=records&summary=true&sort=latest&group_by=%s&selection=%s&limit=20&page=%d%s", view, url.QueryEscape(selection), page, extra), nil)
|
|
h.ListVulnerabilities(c)
|
|
if w.Code != 200 {
|
|
t.Fatalf("%d %s", w.Code, w.Body)
|
|
}
|
|
var result ListVulnerabilitiesResponse
|
|
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return result
|
|
}
|
|
for _, view := range []string{"host", "task"} {
|
|
first, second := fetch(view, "", 1, ""), fetch(view, "", 2, "")
|
|
if first.Total != 55 || len(first.Vulnerabilities) != 20 || len(second.Vulnerabilities) != 20 || len(first.Navigation) <= 20 {
|
|
t.Fatalf("%s: incomplete navigation or wrong record pagination", view)
|
|
}
|
|
if !reflect.DeepEqual(first.Navigation, second.Navigation) {
|
|
t.Fatal("navigation changed between record pages")
|
|
}
|
|
selection := "domain:example.test"
|
|
if view == "task" {
|
|
selection = "tag:shared"
|
|
}
|
|
first, second = fetch(view, selection, 1, ""), fetch(view, selection, 2, "")
|
|
if first.Total != 29 || second.Total != 29 || len(first.Vulnerabilities) != 20 || len(second.Vulnerabilities) != 9 || first.Selection != selection {
|
|
t.Fatalf("%s: expected 29 findings in pages of 20 and 9: %+v", view, second)
|
|
}
|
|
ids := map[string]bool{}
|
|
for _, v := range append(first.Vulnerabilities, second.Vulnerabilities...) {
|
|
if ids[v.ID] {
|
|
t.Fatal("duplicate finding", v.ID)
|
|
}
|
|
ids[v.ID] = true
|
|
}
|
|
if second.Vulnerabilities[0].ID != "v08" {
|
|
t.Fatal("incorrect chronological pagination")
|
|
}
|
|
last := fetch(view, selection, 99, "")
|
|
if last.Page != 2 || len(last.Vulnerabilities) != 9 {
|
|
t.Fatal("out-of-range page not clamped")
|
|
}
|
|
none := fetch(view, selection, 1, "&severity=critical")
|
|
if none.Total != 0 || len(none.Navigation) != 0 || none.Selection != "" {
|
|
t.Fatal("stale selection after filtering")
|
|
}
|
|
}
|
|
root := fetch("host", "domain:example.test", 1, "").Navigation
|
|
for _, node := range root {
|
|
if node.Key == "domain:example.test" && (node.Count != 29 || len(node.Children) != 2) {
|
|
t.Fatalf("multi-host finding counted twice: %+v", node)
|
|
}
|
|
}
|
|
unknown := fetch("host", "host:", 1, "")
|
|
if unknown.Total != 1 || unknown.Vulnerabilities[0].ID != "v54" {
|
|
t.Fatal("unknown host not selectable")
|
|
}
|
|
searched := fetch("host", "", 1, "&host_q=b.example.test")
|
|
if searched.Total != 1 || len(searched.Navigation) != 1 {
|
|
t.Fatal("host search must cover all records")
|
|
}
|
|
|
|
linked, err := db.CreateConversation("linked", 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)
|
|
}
|
|
if _, err := db.Exec("UPDATE vulnerabilities SET conversation_id=? WHERE id='v00'", linked.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for selection, id := range map[string]string{"queue:queue": "v00", "task:task": "v00", "tag:": "v54"} {
|
|
result := fetch("task", selection, 1, "")
|
|
if result.Total != 1 || result.Selection != selection || result.Vulnerabilities[0].ID != id {
|
|
t.Fatalf("task selection %s returned wrong records", selection)
|
|
}
|
|
}
|
|
// Navigation and records must obey the same access scope.
|
|
if _, err := db.Exec("UPDATE vulnerabilities SET owner_user_id='other'"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := db.Exec("UPDATE vulnerabilities SET owner_user_id='reader' WHERE id='v00'"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
access := database.RBACListAccess{UserID: "reader", Scope: "own"}
|
|
nav, err := db.VulnerabilityNavigationForAccess(database.VulnerabilityListFilter{}, access, "host")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(nav) != 1 || nav[0].Count != 1 {
|
|
t.Fatalf("navigation leaked unauthorized records: %+v", nav)
|
|
}
|
|
count, err := db.CountVulnerabilitiesForAccess(database.VulnerabilityListFilter{BrowserSelection: "domain:example.test"}, access)
|
|
if err != nil || count != 1 {
|
|
t.Fatalf("record access mismatch: %d %v", count, err)
|
|
}
|
|
}
|