mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-12-01 15:13:50 +01:00
84 lines
2.8 KiB
Go
84 lines
2.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"mime"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"cyberstrike-ai/internal/database"
|
|
"cyberstrike-ai/internal/report"
|
|
"cyberstrike-ai/internal/security"
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// DownloadVulnerability exports exactly one record under the same RBAC boundary
|
|
// as its detail view. It does not create artifacts or require files permissions.
|
|
func (h *VulnerabilityHandler) DownloadVulnerability(c *gin.Context) {
|
|
format := c.DefaultQuery("format", "docx")
|
|
if format != "docx" && format != "md" {
|
|
c.JSON(400, gin.H{"error": "format must be md or docx"})
|
|
return
|
|
}
|
|
session, ok := security.CurrentSession(c)
|
|
if !ok || session.UserID == "" {
|
|
c.JSON(401, gin.H{"error": "unauthorized"})
|
|
return
|
|
}
|
|
if !security.SessionHasPermission(c, "vulnerability:read") || !h.db.UserCanAccessResource(session.UserID, session.ScopeFor("vulnerability:read"), "vulnerability", c.Param("id")) {
|
|
c.JSON(403, gin.H{"error": "无权访问该漏洞"})
|
|
return
|
|
}
|
|
var exists bool
|
|
if err := h.db.QueryRow("SELECT EXISTS(SELECT 1 FROM vulnerabilities WHERE id=?)", c.Param("id")).Scan(&exists); err != nil {
|
|
c.JSON(500, gin.H{"error": "读取漏洞失败"})
|
|
return
|
|
}
|
|
if !exists {
|
|
c.JSON(404, gin.H{"error": "漏洞不存在"})
|
|
return
|
|
}
|
|
v, err := h.db.GetVulnerability(c.Param("id"))
|
|
if err != nil {
|
|
h.logger.Warn("读取漏洞下载记录失败", zap.Error(err))
|
|
c.JSON(500, gin.H{"error": "读取漏洞失败"})
|
|
return
|
|
}
|
|
var data []byte
|
|
contentType := "text/markdown; charset=utf-8"
|
|
if format == "md" {
|
|
var b strings.Builder
|
|
appendVulnerabilityMarkdown(&b, v, "#")
|
|
data = []byte(b.String())
|
|
} else {
|
|
template := report.DefaultTemplate()
|
|
// A long vulnerability title remains intact in the detail; the cover is bounded.
|
|
title := []rune(v.Title)
|
|
if len(title) > 180 {
|
|
title = title[:180]
|
|
}
|
|
template.Title = string(title) + " 漏洞报告"
|
|
template.Scope = v.Target
|
|
template.Introduction = "本报告记录该漏洞的描述、证据与修复建议。"
|
|
template.IncludeSummary = false
|
|
data, err = report.Render(template, report.Snapshot{ConversationID: v.ConversationID, ConversationTitle: v.ConversationTag, GeneratedAt: time.Now(), Findings: []*database.Vulnerability{v}})
|
|
if err != nil {
|
|
h.logger.Warn("生成漏洞 Word 报告失败", zap.Error(err))
|
|
c.JSON(500, gin.H{"error": "生成 Word 报告失败"})
|
|
return
|
|
}
|
|
contentType = report.MIME
|
|
}
|
|
title := []rune(sanitizeExportName(v.Title))
|
|
if len(title) > 50 {
|
|
title = title[:50]
|
|
}
|
|
filename := string(title) + "_" + v.ID + "." + format
|
|
c.Header("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": filename}))
|
|
c.Header("Content-Length", strconv.Itoa(len(data)))
|
|
c.Header("Cache-Control", "no-store")
|
|
c.Data(http.StatusOK, contentType, data)
|
|
}
|