341 lines
14 KiB
Go
341 lines
14 KiB
Go
package handler
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"strings"
|
||
"testing"
|
||
|
||
"cyberstrike-ai/internal/agentruntime"
|
||
"cyberstrike-ai/internal/database"
|
||
"cyberstrike-ai/internal/deepassessment"
|
||
"cyberstrike-ai/internal/security"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
func TestDeepAssessmentUsesProjectAssetsFromTheSharedLibrary(t *testing.T) {
|
||
h, task := deepAssessmentFixture(t)
|
||
project, err := h.db.CreateProject(&database.Project{Name: "Shared library"})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
other, err := h.db.CreateProject(&database.Project{Name: "Other"})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if _, err = h.db.Exec(`UPDATE projects SET scope_json=? WHERE id=?`, `{"targets":["https://scope.example.com/health"],"exclude":["https://skip.example.com/secret"]}`, project.ID); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
owned := &database.Asset{ProjectID: project.ID, Host: "https://owned.example.com"}
|
||
extra := &database.Asset{ProjectID: project.ID, Host: "https://extra.example.com"}
|
||
foreign := &database.Asset{ProjectID: other.ID, Host: "https://foreign.example.net"}
|
||
if _, err = h.db.UpsertAssets([]*database.Asset{owned, extra, foreign}, task.OwnerID, true); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
router := assessmentAssetRouter(h, task.OwnerID)
|
||
body := `{"name":"shared","objective":"评估 https://owned.example.com/login","project_id":"` + project.ID + `"}`
|
||
request := httptest.NewRequest("POST", "/api/deep-assessments", strings.NewReader(body))
|
||
request.Header.Set("Content-Type", "application/json")
|
||
response := httptest.NewRecorder()
|
||
router.ServeHTTP(response, request)
|
||
if response.Code != http.StatusCreated {
|
||
t.Fatal(response.Code, response.Body.String())
|
||
}
|
||
var created struct {
|
||
Task deepassessment.Task `json:"task"`
|
||
}
|
||
if err = json.Unmarshal(response.Body.Bytes(), &created); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
linked := linkedAssetNames(t, h.db, created.Task.ID)
|
||
for _, name := range []string{"owned.example.com", "extra.example.com", "scope.example.com"} {
|
||
if !linked[name] {
|
||
t.Fatalf("shared library asset %s was not linked: %v", name, linked)
|
||
}
|
||
}
|
||
if linked["foreign.example.net"] || linked["skip.example.com"] {
|
||
t.Fatalf("asset outside the project or an excluded target was linked: %v", linked)
|
||
}
|
||
if !assetInProject(t, h.db, project.ID, "scope.example.com") || assetInProject(t, h.db, project.ID, "skip.example.com") {
|
||
t.Fatal("project scope was not written through the shared asset library")
|
||
}
|
||
|
||
collection, err := h.db.CreateAssetCollection(project.ID, "only extra", "", "", task.OwnerID, []string{extra.ID})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
narrow := `{"name":"narrow","objective":"inspect fixture","project_id":"` + project.ID + `","collection_id":"` + collection.ID + `"}`
|
||
request = httptest.NewRequest("POST", "/api/deep-assessments", strings.NewReader(narrow))
|
||
request.Header.Set("Content-Type", "application/json")
|
||
response = httptest.NewRecorder()
|
||
router.ServeHTTP(response, request)
|
||
if response.Code != http.StatusCreated {
|
||
t.Fatal(response.Code, response.Body.String())
|
||
}
|
||
if err = json.Unmarshal(response.Body.Bytes(), &created); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
narrowed := linkedAssetNames(t, h.db, created.Task.ID)
|
||
if !narrowed["extra.example.com"] || narrowed["owned.example.com"] || narrowed["scope.example.com"] {
|
||
t.Fatalf("collection did not narrow the same library: %v", narrowed)
|
||
}
|
||
}
|
||
|
||
func TestDeepAssessmentWritesDiscoveriesBackToTheSharedLibrary(t *testing.T) {
|
||
h, task := deepAssessmentFixture(t)
|
||
project, err := h.db.CreateProject(&database.Project{Name: "Discovery"})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if _, err = h.db.Exec(`UPDATE projects SET scope_json=? WHERE id=?`, `{"exclude":["https://skip.example.com"]}`, project.ID); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
seed := &database.Asset{ProjectID: project.ID, Host: "https://example.com"}
|
||
created := &database.Asset{ProjectID: project.ID, Host: "created.example.net"}
|
||
if _, err = h.db.UpsertAssets([]*database.Asset{seed, created}, task.OwnerID, true); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err = h.deepAssessments.Store.Change(task.ID, "", "fixture", "", func(saved *deepassessment.Task) error {
|
||
saved.ProjectID = project.ID
|
||
saved.Objective = "评估 https://example.com"
|
||
return nil
|
||
}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
current, err := h.deepAssessments.Store.Get(task.ID)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
output := "https://api.example.com/admin\nhttp://8.8.8.8/\nhttps://skip.example.com/secret\n"
|
||
if err = h.deepAssessmentRuntime.ingestToolSurface(current, "httpx", agentruntime.ToolResult{Content: output}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if !assetInProject(t, h.db, project.ID, "api.example.com") || assetInProject(t, h.db, project.ID, "8.8.8.8") || assetInProject(t, h.db, project.ID, "skip.example.com") {
|
||
t.Fatal("discovery was not limited to the shared project scope")
|
||
}
|
||
linked := linkedAssetNames(t, h.db, task.ID)
|
||
if !linked["api.example.com"] {
|
||
t.Fatal("new service was not added to the attack surface", linked)
|
||
}
|
||
var endpoint string
|
||
if err = h.db.QueryRow(`SELECT path FROM asset_endpoints WHERE asset_id=(SELECT id FROM assets WHERE domain=? AND project_id=?)`, "api.example.com", project.ID).Scan(&endpoint); err != nil || endpoint != "/admin" {
|
||
t.Fatal(endpoint, err)
|
||
}
|
||
payload, _ := json.Marshal(map[string]any{"action": "created", "asset": map[string]string{"id": created.ID, "domain": "created.example.net"}})
|
||
if err = h.deepAssessmentRuntime.ingestToolSurface(current, "create_asset", agentruntime.ToolResult{Content: string(payload)}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
linked = linkedAssetNames(t, h.db, task.ID)
|
||
if !linked["created.example.net"] {
|
||
t.Fatal("asset written to the shared library was not linked", linked)
|
||
}
|
||
preview, err := h.deepAssessmentRuntime.context(current)
|
||
if err != nil || !strings.Contains(preview, "api.example.com") || !strings.Contains(preview, "linked_asset_preview") {
|
||
t.Fatal(err, preview)
|
||
}
|
||
}
|
||
|
||
func TestDeepAssessmentUnassociatedProjectExtractsDeclaredTargetsAndNormalizesAssetIDs(t *testing.T) {
|
||
h, task := deepAssessmentFixture(t)
|
||
router := assessmentAssetRouter(h, task.OwnerID)
|
||
body := `{"name":"unassociated","objective":"对https://hb.icbc.com.cn/icbc/spac/spac.do的授信打分卡接口做未授权访问与越权测试等渗透测试,并给出可复现证据"}`
|
||
request := httptest.NewRequest("POST", "/api/deep-assessments", strings.NewReader(body))
|
||
request.Header.Set("Content-Type", "application/json")
|
||
response := httptest.NewRecorder()
|
||
router.ServeHTTP(response, request)
|
||
if response.Code != http.StatusCreated {
|
||
t.Fatal(response.Code, response.Body.String())
|
||
}
|
||
var created struct {
|
||
Task deepassessment.Task `json:"task"`
|
||
}
|
||
if err := json.Unmarshal(response.Body.Bytes(), &created); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if created.Task.Scope != "" {
|
||
t.Fatalf("empty scope should remain empty rather than duplicating objective: %q", created.Task.Scope)
|
||
}
|
||
linked := linkedAssetNames(t, h.db, created.Task.ID)
|
||
if !linked["hb.icbc.com.cn"] {
|
||
t.Fatalf("declared target was not linked on unassociated-project task: %v", linked)
|
||
}
|
||
var endpoint string
|
||
if err := h.db.QueryRow(`SELECT path FROM asset_endpoints WHERE asset_id=(SELECT asset_id FROM deep_assessment_assets WHERE task_id=? LIMIT 1)`, created.Task.ID).Scan(&endpoint); err != nil || endpoint != "/icbc/spac/spac.do" {
|
||
t.Fatalf("declared endpoint mismatch: %q err=%v", endpoint, err)
|
||
}
|
||
saved, err := h.deepAssessments.Store.Get(created.Task.ID)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
steps := []deepassessment.Step{{
|
||
ID: "s1",
|
||
Title: "Check spac.do auth",
|
||
Instruction: "Verify unauthorized access",
|
||
AssetIDs: []string{"https://hb.icbc.com.cn/icbc/spac/spac.do"},
|
||
}}
|
||
if err := h.deepAssessmentRuntime.validateIntentAssets(saved, steps); err != nil {
|
||
t.Fatalf("hostname/URL asset_ids was not normalized to linked UUID: %v", err)
|
||
}
|
||
if len(steps[0].AssetIDs) != 1 || strings.Contains(steps[0].AssetIDs[0], "hb.icbc.com.cn") {
|
||
t.Fatalf("expected normalized asset UUID, got %+v", steps[0].AssetIDs)
|
||
}
|
||
unmatched := []deepassessment.Step{{
|
||
ID: "s2",
|
||
Title: "Unstored host",
|
||
Instruction: "Verify",
|
||
AssetIDs: []string{"https://unstored.example.org/api"},
|
||
}}
|
||
if err := h.deepAssessmentRuntime.validateIntentAssets(saved, unmatched); err == nil || !strings.Contains(err.Error(), "asset_ids: []") {
|
||
t.Fatalf("expected actionable asset_ids: [] hint, got %v", err)
|
||
}
|
||
}
|
||
|
||
func assessmentAssetRouter(h *AgentHandler, user string) *gin.Engine {
|
||
gin.SetMode(gin.TestMode)
|
||
router := gin.New()
|
||
assetHandler := NewAssetHandler(h.db, h.logger)
|
||
router.Use(func(c *gin.Context) {
|
||
c.Set(security.ContextSessionKey, security.Session{
|
||
UserID: user,
|
||
Scope: database.RBACScopeAll,
|
||
Permissions: map[string]bool{
|
||
"assessments:read": true, "assessments:write": true,
|
||
"project:read": true, "asset:read": true, "asset:write": true,
|
||
},
|
||
})
|
||
})
|
||
router.Use(security.RBACMiddleware(h.db))
|
||
router.GET("/api/assets", assetHandler.List)
|
||
router.POST("/api/deep-assessments", h.CreateDeepAssessment)
|
||
return router
|
||
}
|
||
|
||
func TestClassicChatPrecipitatedAssetsSyncIntoProjectScopeAndAssessment(t *testing.T) {
|
||
h, task := deepAssessmentFixture(t)
|
||
project, err := h.db.CreateProject(&database.Project{Name: "货讯通科技"})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
// 1. Simulate an unassigned asset created before the conversation was bound to the project.
|
||
unassigned := &database.Asset{Host: "https://portal.hxt-example.com"}
|
||
if _, err = h.db.UpsertAssets([]*database.Asset{unassigned}, task.OwnerID, true); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
// 2. Simulate classic chat conversation bound to the project with target facts, attack-chain nodes, vulns, and tool output.
|
||
conv, err := h.db.CreateConversation("货讯通渗透", database.ConversationCreateMeta{ProjectID: project.ID})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
_ = h.db.SetResourceOwner("conversation", conv.ID, task.OwnerID)
|
||
if _, err = h.db.UpsertProjectFact(&database.ProjectFact{
|
||
ProjectID: project.ID,
|
||
FactKey: "target/primary",
|
||
Category: "target",
|
||
Summary: "主域 hxt-example.com,发现 portal.hxt-example.com 与 api.hxt-example.com:8443",
|
||
Body: "使用 exploit.py 读取 config.json,参考 https://github.com/projectdiscovery/httpx",
|
||
Confidence: "tentative",
|
||
SourceConversationID: conv.ID,
|
||
}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err = h.db.SaveAttackChainNode(conv.ID, "node_1", "target", "edi.hxt-example.com", "", `{"target":"https://edi.hxt-example.com/ws"}`, 0); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if _, err = h.db.CreateVulnerability(&database.Vulnerability{
|
||
ConversationID: conv.ID,
|
||
ProjectID: project.ID,
|
||
Title: "订单接口越权",
|
||
Severity: "high",
|
||
Target: "https://tms.hxt-example.com/api/orders",
|
||
}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
// 3. Selecting the project in CreateDrawer queries GET /api/assets?project_id=...&sync_project_scope=1.
|
||
router := assessmentAssetRouter(h, task.OwnerID)
|
||
req := httptest.NewRequest("GET", "/api/assets?project_id="+project.ID+"&page=1&page_size=6&sync_project_scope=1", nil)
|
||
resp := httptest.NewRecorder()
|
||
router.ServeHTTP(resp, req)
|
||
if resp.Code != http.StatusOK {
|
||
t.Fatalf("sync_project_scope status=%d body=%s", resp.Code, resp.Body.String())
|
||
}
|
||
var listed struct {
|
||
Assets []*database.Asset `json:"assets"`
|
||
Total int `json:"total"`
|
||
}
|
||
if err = json.Unmarshal(resp.Body.Bytes(), &listed); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if listed.Total < 5 {
|
||
t.Fatalf("expected >=5 synced project assets, got total=%d assets=%+v", listed.Total, listed.Assets)
|
||
}
|
||
for _, want := range []string{"hxt-example.com", "portal.hxt-example.com", "api.hxt-example.com", "edi.hxt-example.com", "tms.hxt-example.com"} {
|
||
if !assetInProject(t, h.db, project.ID, want) {
|
||
t.Fatalf("expected classic chat target %s in project asset library", want)
|
||
}
|
||
}
|
||
for _, noise := range []string{"exploit.py", "config.json", "github.com"} {
|
||
if assetInProject(t, h.db, project.ID, noise) {
|
||
t.Fatalf("noise host %s leaked into project asset library", noise)
|
||
}
|
||
}
|
||
|
||
// 4. Creating the deep assessment automatically links all synced project assets.
|
||
createBody := `{"name":"货讯通深度测评","objective":"全面评估认证与越权风险","project_id":"` + project.ID + `"}`
|
||
createReq := httptest.NewRequest("POST", "/api/deep-assessments", strings.NewReader(createBody))
|
||
createReq.Header.Set("Content-Type", "application/json")
|
||
createResp := httptest.NewRecorder()
|
||
router.ServeHTTP(createResp, createReq)
|
||
if createResp.Code != http.StatusCreated {
|
||
t.Fatalf("create deep assessment failed: %d %s", createResp.Code, createResp.Body.String())
|
||
}
|
||
var created struct {
|
||
Task deepassessment.Task `json:"task"`
|
||
}
|
||
if err = json.Unmarshal(createResp.Body.Bytes(), &created); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
linked := linkedAssetNames(t, h.db, created.Task.ID)
|
||
for _, want := range []string{"portal.hxt-example.com", "api.hxt-example.com", "edi.hxt-example.com", "tms.hxt-example.com"} {
|
||
if !linked[want] {
|
||
t.Fatalf("expected %s linked to deep assessment, got %v", want, linked)
|
||
}
|
||
}
|
||
}
|
||
|
||
func linkedAssetNames(t *testing.T, db *database.DB, taskID string) map[string]bool {
|
||
t.Helper()
|
||
rows, err := db.Query(`SELECT COALESCE(domain,''), COALESCE(ip,''), COALESCE(host,'') FROM assets JOIN deep_assessment_assets a ON a.asset_id=assets.id WHERE a.task_id=?`, taskID)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer rows.Close()
|
||
out := map[string]bool{}
|
||
for rows.Next() {
|
||
var domain, ip, host string
|
||
if err = rows.Scan(&domain, &ip, &host); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
for _, name := range []string{domain, ip, host} {
|
||
if name != "" {
|
||
out[name] = true
|
||
}
|
||
}
|
||
}
|
||
if err = rows.Err(); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
return out
|
||
}
|
||
|
||
func assetInProject(t *testing.T, db *database.DB, projectID, name string) bool {
|
||
t.Helper()
|
||
var count int
|
||
if err := db.QueryRow(`SELECT COUNT(*) FROM assets WHERE project_id=? AND (domain=? OR ip=? OR host=?)`, projectID, name, name, name).Scan(&count); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
return count > 0
|
||
}
|