mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-12-01 12:13:24 +01:00
109 lines
3.6 KiB
Go
109 lines
3.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"io"
|
|
"mime"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"cyberstrike-ai/internal/database"
|
|
"cyberstrike-ai/internal/report"
|
|
"cyberstrike-ai/internal/security"
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func TestSingleVulnerabilityDownloadFormatsAndIsolation(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
db, err := database.NewDB(filepath.Join(t.TempDir(), "download.db"), zap.NewNop())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
user, err := db.CreateRBACUser("report-reader", "Reader", "hash", true, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
selected, err := db.CreateVulnerability(&database.Vulnerability{Title: "中文漏洞 <测试>", Severity: "high", Status: "open", Evidence: "sample <evidence> & value", Recommendation: "修复权限验证"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
other, err := db.CreateVulnerability(&database.Vulnerability{Title: "other-secret-finding", Severity: "low", Status: "confirmed"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = db.SetResourceOwner("vulnerability", selected.ID, user.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := NewVulnerabilityHandler(db, zap.NewNop())
|
|
session := security.Session{UserID: user.ID, Scope: database.RBACScopeAll, Permissions: map[string]bool{"vulnerability:read": true}, PermissionScopes: map[string]string{"vulnerability:read": database.RBACScopeOwn}}
|
|
request := func(id, format string, s security.Session) *httptest.ResponseRecorder {
|
|
r := gin.New()
|
|
r.Use(func(c *gin.Context) { c.Set(security.ContextSessionKey, s) }, security.RBACMiddleware(db))
|
|
r.GET("/api/vulnerabilities/:id/download", h.DownloadVulnerability)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest("GET", "/api/vulnerabilities/"+id+"/download?format="+format, nil))
|
|
return w
|
|
}
|
|
for _, format := range []string{"md", "docx"} {
|
|
w := request(selected.ID, format, session)
|
|
if w.Code != 200 {
|
|
t.Fatalf("%s: %d %s", format, w.Code, w.Body)
|
|
}
|
|
_, params, err := mime.ParseMediaType(w.Header().Get("Content-Disposition"))
|
|
if err != nil || !strings.Contains(params["filename"], "中文漏洞") || !strings.HasSuffix(params["filename"], "."+format) {
|
|
t.Fatalf("filename %v %v", params, err)
|
|
}
|
|
content := w.Body.String()
|
|
if format == "docx" {
|
|
if w.Header().Get("Content-Type") != report.MIME {
|
|
t.Fatal("incorrect DOCX MIME")
|
|
}
|
|
z, err := zip.NewReader(bytes.NewReader(w.Body.Bytes()), int64(w.Body.Len()))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, f := range z.File {
|
|
if f.Name == "word/document.xml" {
|
|
r, err := f.Open()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := io.ReadAll(r)
|
|
r.Close()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
content = string(b)
|
|
}
|
|
}
|
|
if !strings.Contains(content, "<evidence> & value") {
|
|
t.Fatal("evidence not escaped")
|
|
}
|
|
}
|
|
if !strings.Contains(content, selected.ID) || !strings.Contains(content, "修复权限验证") || strings.Contains(content, "other-secret-finding") {
|
|
t.Fatal("wrong finding exported")
|
|
}
|
|
}
|
|
if w := request(other.ID, "docx", session); w.Code != 403 {
|
|
t.Fatalf("unrelated global scope widened download: %d", w.Code)
|
|
}
|
|
denied := session
|
|
denied.Permissions = map[string]bool{}
|
|
if w := request(selected.ID, "docx", denied); w.Code != 403 {
|
|
t.Fatalf("missing permission: %d", w.Code)
|
|
}
|
|
if w := request(selected.ID, "pdf", session); w.Code != 400 {
|
|
t.Fatalf("invalid format: %d", w.Code)
|
|
}
|
|
all := session
|
|
all.PermissionScopes = nil
|
|
if w := request("missing", "docx", all); w.Code != 404 {
|
|
t.Fatalf("missing record: %d", w.Code)
|
|
}
|
|
}
|