Files
ARTEX-chixuweihu/db/finding_reproduction_test.go
T
MuCheng 93dc83165c feat(findings): structured reproduction steps with per-step traffic bind
Align finding detail reproduction with AntiFox: store reproduction JSONB
(entry_point/prerequisites/steps/provenance), bind traffic via step_index,
syntax-highlight HTTP in steps/traffic panel, fall back to legacy evidence
when reproduction is absent. Old findings are not backfilled.
2026-10-27 17:17:34 +08:00

72 lines
2.4 KiB
Go

package db
import (
"database/sql"
"encoding/json"
"testing"
)
func TestFindingReproductionAndTrafficStepIndex(t *testing.T) {
d, err := Open(testDSN(t))
if err != nil {
t.Skipf("postgres unavailable (%v) — skipping", err)
}
defer d.Close()
repro := json.RawMessage(`{"entry_point":"POST /upload.php","steps":[{"request":"POST /upload.php HTTP/1.1","expected":"201","proves":"upload accepted"}]}`)
fid, err := d.AddFinding(0, 0, "upload", "upload", SeverityHigh, "summary", "poc", "test", nil, repro)
if err != nil {
t.Fatal(err)
}
defer d.DeleteFinding(fid)
f, err := d.GetFinding(fid)
if err != nil || string(f.Reproduction) != string(repro) {
t.Fatalf("reproduction round trip: %#v, %v", f, err)
}
snapshot := TrafficEvidenceSnapshot{SourceTrafficID: "source-1", CapturedAt: 1, URL: "https://example.test/upload.php", Method: "POST", Status: 201, ReqHash: "req", RespHash: "resp"}
snapshot.ID = TrafficSnapshotID(snapshot)
prepared := []PreparedTrafficEvidence{{Ref: TrafficRef{TrafficID: "source-1", StepIndex: 2, Role: "proof"}, Snapshot: snapshot}}
if err := d.WithEvidenceTx(t.Context(), func(tx *sql.Tx) error {
if err := InsertEvidenceSnapshotTx(tx, snapshot); err != nil {
return err
}
return AddFindingTrafficTx(tx, fid, prepared)
}); err != nil {
t.Fatal(err)
}
traffic, err := d.GetFindingTraffic(t.Context(), fid)
if err != nil || len(traffic.Bindings) != 1 || traffic.Bindings[0].StepIndex != 2 {
t.Fatalf("step binding round trip: %#v, %v", traffic, err)
}
zero := 0
if err := d.EditFindingTrafficStep(t.Context(), fid, traffic.Bindings[0].ID, traffic.Version, nil, nil, &zero, false, nil); err != nil {
t.Fatal(err)
}
traffic, err = d.GetFindingTraffic(t.Context(), fid)
if err != nil || traffic.Bindings[0].StepIndex != 0 {
t.Fatalf("step edit: %#v, %v", traffic, err)
}
if err := d.EditFindingTraffic(t.Context(), fid, traffic.Bindings[0].ID, traffic.Version, nil, nil, true, nil); err != nil {
t.Fatal(err)
}
}
func TestFindingWithoutReproductionLoadsNull(t *testing.T) {
d, err := Open(testDSN(t))
if err != nil {
t.Skipf("postgres unavailable (%v) — skipping", err)
}
defer d.Close()
fid, err := d.AddFinding(0, 0, "legacy", "legacy", SeverityLow, "summary", "evidence", "test", nil)
if err != nil {
t.Fatal(err)
}
defer d.DeleteFinding(fid)
f, err := d.GetFinding(fid)
if err != nil {
t.Fatal(err)
}
if f.Reproduction != nil {
t.Fatalf("legacy reproduction = %s, want nil", f.Reproduction)
}
}