mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-11-28 10:41:50 +08:00
feat(findings): add manual remark and finding edit support
- Persist findings.remark with archive-restore compatibility, full-text search, and Markdown/CSV/JSON export
- Extend PATCH /api/exploration/findings/{id} to update name, vulnclass, severity, status, summary, evidence, report, and remark with atomic upfront validation
- Add dedicated Notes (remark) card and Edit Finding dialog on the finding detail page, plus remark preview/inline editing in the findings list and task findings tab
Fixes #198
This commit is contained in:
1 parent
253a8eeac3
commit
835e289ee2
21 files changed
+975
-43
No files matched your search
@@ -364,6 +364,7 @@ type FindingDTO struct {
|
||||
Status string `json:"status"` // pending | in_progress | confirmed | resolved | fixed | false_positive | ignored | duplicate | risk_accepted
|
||||
Summary string `json:"summary"`
|
||||
Evidence string `json:"evidence"`
|
||||
Remark string `json:"remark,omitempty"` // 人工备注
|
||||
Report string `json:"report,omitempty"` // 详细报告(Markdown);仅详情接口返回,列表为空
|
||||
ReportConversationID *int64 `json:"report_conversation_id,omitempty"`
|
||||
|
||||
@@ -464,6 +465,8 @@ func findingDTOsForOwner(taskID, description string, in []*db.Node, meta map[int
|
||||
if m, ok := meta[n.ID]; ok {
|
||||
d.FindingID = i64s(m.ID)
|
||||
d.Status = m.Status
|
||||
d.Remark = m.Remark
|
||||
d.Evidence = m.Evidence
|
||||
d.TrafficCount = m.TrafficCount
|
||||
d.Assets = findingAssetDTOs(m.AssetIDs, assets)
|
||||
}
|
||||
@@ -502,6 +505,7 @@ func findingFromDB(f *db.DBFinding, assets map[int64]*db.Asset) FindingDTO {
|
||||
Status: status,
|
||||
Summary: f.Summary,
|
||||
Evidence: f.Evidence,
|
||||
Remark: f.Remark,
|
||||
Report: f.Report,
|
||||
ReportConversationID: f.ReportConversationID,
|
||||
TS: rfc3339(f.CreatedAt),
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Autumn-27/artex/db"
|
||||
"github.com/Autumn-27/artex/report"
|
||||
)
|
||||
|
||||
func TestReportExportsIncludeFindingRemark(t *testing.T) {
|
||||
now := time.Date(2026, 11, 27, 10, 0, 0, 0, time.UTC)
|
||||
f := &db.DBFinding{
|
||||
ID: 42,
|
||||
Name: "后台越权导出",
|
||||
VulnClass: "IDOR",
|
||||
Severity: db.SeverityHigh,
|
||||
Status: db.FindingConfirmed,
|
||||
Summary: "普通用户可导出管理员报表",
|
||||
Remark: "已与业务确认,本周五前修复",
|
||||
Evidence: "GET /api/export?id=1 HTTP/1.1",
|
||||
CreatedAt: now,
|
||||
}
|
||||
|
||||
combined := report.FindingsMarkdown([]*db.DBFinding{f}, now)
|
||||
if !strings.Contains(combined, "**备注:**\n\n已与业务确认,本周五前修复") {
|
||||
t.Fatalf("combined markdown missing remark:\n%s", combined)
|
||||
}
|
||||
single := report.SingleFindingMarkdown(f, now)
|
||||
if !strings.Contains(single, "## 备注\n\n已与业务确认,本周五前修复") {
|
||||
t.Fatalf("single markdown missing remark:\n%s", single)
|
||||
}
|
||||
csvOut := string(report.FindingsCSV([]*db.DBFinding{f}))
|
||||
if !strings.Contains(csvOut, "备注") || !strings.Contains(csvOut, "已与业务确认,本周五前修复") {
|
||||
t.Fatalf("csv missing remark:\n%s", csvOut)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPatchFindingManualEditAndRemark(t *testing.T) {
|
||||
dsn, _, err := db.DSN()
|
||||
if err != nil {
|
||||
t.Skip("test postgres not configured")
|
||||
}
|
||||
pg, err := db.Open(dsn)
|
||||
if err != nil {
|
||||
t.Skipf("postgres unavailable (%v) — skipping", err)
|
||||
}
|
||||
t.Cleanup(func() { pg.Close() })
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
s := &Server{
|
||||
ctx: ctx,
|
||||
m: &Manager{pg: pg, dir: t.TempDir(), tasks: map[string]*Task{}},
|
||||
}
|
||||
|
||||
vc := fmt.Sprintf("__test_patch_finding_%d__", time.Now().UnixNano())
|
||||
fid, err := pg.AddFinding(0, 0, vc, "原始标题", db.SeverityMedium, "原始摘要", "原始证据", "worker", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("AddFinding: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _, _ = pg.Exec(`DELETE FROM findings WHERE id=$1`, fid) })
|
||||
|
||||
patch := func(id string, payload string) *httptest.ResponseRecorder {
|
||||
req := httptest.NewRequest(http.MethodPatch, "/api/exploration/findings/"+id, bytes.NewBufferString(payload))
|
||||
req.SetPathValue("id", id)
|
||||
rec := httptest.NewRecorder()
|
||||
s.patchFinding(rec, req)
|
||||
return rec
|
||||
}
|
||||
idStr := fmt.Sprintf("%d", fid)
|
||||
|
||||
// Empty payload must return 400.
|
||||
if w := patch(idStr, `{}`); w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("empty patch: want 400 got %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// Over-limit remark must return 400 and must NOT partially mutate name.
|
||||
tooLong := strings.Repeat("注", db.MaxFindingRemarkRunes+1)
|
||||
badBody, _ := json.Marshal(map[string]string{"name": "不应生效", "remark": tooLong})
|
||||
if w := patch(idStr, string(badBody)); w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("over-limit remark: want 400 got %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
before, _ := pg.GetFinding(fid)
|
||||
if before.Name != "原始标题" || before.Remark != "" {
|
||||
t.Fatalf("validation failure mutated row: %+v", before)
|
||||
}
|
||||
|
||||
// Edit all fields at once.
|
||||
fullBody, _ := json.Marshal(map[string]string{
|
||||
"name": "手工修改后的标题",
|
||||
"vulnclass": vc + "_edited",
|
||||
"severity": db.SeverityCritical,
|
||||
"status": db.FindingConfirmed,
|
||||
"summary": "手工修改后的摘要",
|
||||
"evidence": " indented evidence line\nline 2",
|
||||
"report": "## 手工报告\n\n细节说明",
|
||||
"remark": " 需要复核支付接口权限 ",
|
||||
})
|
||||
w := patch(idStr, string(fullBody))
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("full patch: want 200 got %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
var dto FindingDTO
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &dto); err != nil {
|
||||
t.Fatalf("decode dto: %v", err)
|
||||
}
|
||||
if dto.Name != "手工修改后的标题" || dto.VulnClass != vc+"_edited" || dto.Severity != db.SeverityCritical ||
|
||||
dto.Status != db.FindingConfirmed || dto.Summary != "手工修改后的摘要" ||
|
||||
dto.Evidence != " indented evidence line\nline 2" || dto.Report != "## 手工报告\n\n细节说明" ||
|
||||
dto.Remark != "需要复核支付接口权限" {
|
||||
t.Fatalf("unexpected patched dto: %+v", dto)
|
||||
}
|
||||
|
||||
// Clearing remark to empty string persists cleanly.
|
||||
w = patch(idStr, `{"remark":" "}`)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("clear remark: want 200 got %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
var cleared FindingDTO
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &cleared); err != nil {
|
||||
t.Fatalf("decode cleared dto: %v", err)
|
||||
}
|
||||
if cleared.Remark != "" || cleared.Name != "手工修改后的标题" {
|
||||
t.Fatalf("unexpected cleared dto: %+v", cleared)
|
||||
}
|
||||
}
|
||||
+90
-14
@@ -18,6 +18,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/Autumn-27/artex/agent"
|
||||
"github.com/Autumn-27/artex/db"
|
||||
@@ -2620,10 +2621,10 @@ func (s *Server) findingLineage(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, 200, map[string]any{"nodes": taskNodeDTOs(nodes), "edges": edgeDTOs(edges)})
|
||||
}
|
||||
|
||||
// patchFinding partially updates a finding: any subset of {status, severity}.
|
||||
// The id is the standalone findings-table id (DTO finding_id). Severity edits are
|
||||
// mirrored onto the originating exploration node so the per-task view stays in
|
||||
// sync. Returns the updated finding DTO.
|
||||
// patchFinding partially updates a finding: any subset of {status, severity, name, vulnclass,
|
||||
// summary, evidence, report, remark}. The id is the standalone findings-table id (DTO finding_id).
|
||||
// Core text fields are mirrored onto the originating exploration node so the per-task view stays
|
||||
// in sync. Returns the updated finding DTO.
|
||||
func (s *Server) patchFinding(w http.ResponseWriter, r *http.Request) {
|
||||
id := int64(atoiDefault(r.PathValue("id"), 0))
|
||||
if id <= 0 {
|
||||
@@ -2635,20 +2636,38 @@ func (s *Server) patchFinding(w http.ResponseWriter, r *http.Request) {
|
||||
Severity *string `json:"severity"`
|
||||
Name *string `json:"name"`
|
||||
VulnClass *string `json:"vulnclass"`
|
||||
Summary *string `json:"summary"`
|
||||
Evidence *string `json:"evidence"`
|
||||
Report *string `json:"report"`
|
||||
Remark *string `json:"remark"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeErr(w, 400, "bad json: "+err.Error())
|
||||
return
|
||||
}
|
||||
if body.Status == nil && body.Severity == nil && body.Name == nil && body.VulnClass == nil {
|
||||
writeErr(w, 400, "nothing to update: provide status/severity/name/vulnclass")
|
||||
if body.Status == nil && body.Severity == nil && body.Name == nil && body.VulnClass == nil &&
|
||||
body.Summary == nil && body.Evidence == nil && body.Report == nil && body.Remark == nil {
|
||||
writeErr(w, 400, "nothing to update: provide status/severity/name/vulnclass/summary/evidence/report/remark")
|
||||
return
|
||||
}
|
||||
if body.Status != nil {
|
||||
if !db.ValidFindingStatus(*body.Status) {
|
||||
writeErr(w, 400, "bad status: "+*body.Status)
|
||||
// Validate all fields before mutating so invalid input never leaves partial writes.
|
||||
if body.Status != nil && !db.ValidFindingStatus(*body.Status) {
|
||||
writeErr(w, 400, "bad status: "+*body.Status)
|
||||
return
|
||||
}
|
||||
if body.Severity != nil && !db.ValidSeverity(*body.Severity) {
|
||||
writeErr(w, 400, "bad severity: "+*body.Severity)
|
||||
return
|
||||
}
|
||||
var remark string
|
||||
if body.Remark != nil {
|
||||
remark = strings.TrimSpace(*body.Remark)
|
||||
if utf8.RuneCountInString(remark) > db.MaxFindingRemarkRunes {
|
||||
writeErr(w, 400, fmt.Sprintf("备注最多 %d 个字符", db.MaxFindingRemarkRunes))
|
||||
return
|
||||
}
|
||||
}
|
||||
if body.Status != nil {
|
||||
// 走带通知的版本:状态更新与「状态变更推送事件」在同一事务里落库,
|
||||
// 避免出现状态已改而推送事件丢失的窗口。事件登记失败不影响状态更新,
|
||||
// 所以只记日志、不向调用方报错。
|
||||
@@ -2666,10 +2685,6 @@ func (s *Server) patchFinding(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
if body.Severity != nil {
|
||||
if !db.ValidSeverity(*body.Severity) {
|
||||
writeErr(w, 400, "bad severity: "+*body.Severity)
|
||||
return
|
||||
}
|
||||
n, err := s.m.pg.SetFindingSeverity(id, *body.Severity)
|
||||
if err != nil {
|
||||
writeErr(w, 500, err.Error())
|
||||
@@ -2702,6 +2717,58 @@ func (s *Server) patchFinding(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if body.Summary != nil {
|
||||
n, err := s.m.pg.SetFindingSummary(id, strings.TrimSpace(*body.Summary))
|
||||
if err != nil {
|
||||
writeErr(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
writeErr(w, 404, "finding not found")
|
||||
return
|
||||
}
|
||||
}
|
||||
if body.Evidence != nil {
|
||||
ev := *body.Evidence
|
||||
if strings.TrimSpace(ev) == "" {
|
||||
ev = ""
|
||||
}
|
||||
n, err := s.m.pg.SetFindingEvidence(id, ev)
|
||||
if err != nil {
|
||||
writeErr(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
writeErr(w, 404, "finding not found")
|
||||
return
|
||||
}
|
||||
}
|
||||
if body.Report != nil {
|
||||
rep := *body.Report
|
||||
if strings.TrimSpace(rep) == "" {
|
||||
rep = ""
|
||||
}
|
||||
n, err := s.m.pg.SetFindingReport(id, rep)
|
||||
if err != nil {
|
||||
writeErr(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
writeErr(w, 404, "finding not found")
|
||||
return
|
||||
}
|
||||
}
|
||||
if body.Remark != nil {
|
||||
n, err := s.m.pg.SetFindingRemark(id, remark)
|
||||
if err != nil {
|
||||
writeErr(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
writeErr(w, 404, "finding not found")
|
||||
return
|
||||
}
|
||||
}
|
||||
f, err := s.m.pg.GetFinding(id)
|
||||
if err != nil {
|
||||
writeErr(w, 500, err.Error())
|
||||
@@ -2711,7 +2778,16 @@ func (s *Server) patchFinding(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, 404, "finding not found")
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, findingFromDB(f, s.resolveAssetIDs(f.AssetIDs)))
|
||||
dto := findingFromDB(f, s.resolveAssetIDs(f.AssetIDs))
|
||||
cid, err := s.m.pg.FindingCaseID(f.ID)
|
||||
if err != nil {
|
||||
writeErr(w, 500, err.Error())
|
||||
return
|
||||
}
|
||||
if cid > 0 {
|
||||
dto.CaseID = i64s(cid)
|
||||
}
|
||||
writeJSON(w, 200, dto)
|
||||
}
|
||||
|
||||
// deleteFinding removes a finding (findings row + originating exploration node).
|
||||
|
||||
Reference in new issue
Block a user