diff --git a/agent/tools.go b/agent/tools.go index 9901957..eca4ad1 100644 --- a/agent/tools.go +++ b/agent/tools.go @@ -1020,7 +1020,8 @@ func compactNode(n *db.Node) map[string]any { return m } -// compactFinding is compactNode plus the vuln-specific vulnclass/severity. +// compactFinding is compactNode plus the vuln-specific vulnclass/severity and +// the triage status mirrored onto the node payload by SetFindingStatus. func compactFinding(n *db.Node) map[string]any { var p map[string]any _ = json.Unmarshal(n.Payload, &p) @@ -1034,11 +1035,14 @@ func compactFinding(n *db.Node) map[string]any { if sv, ok := p["severity"]; ok && sv != nil && sv != "" { m["severity"] = sv } + if st, ok := p["status"]; ok && st != nil && st != "" { + m["status"] = st + } return m } func (t *ToolSet) listFindings() actool.CoreTool { - return t.readExpTool("list_findings", "List the [confirmed findings] of this task and directly related tasks (compact: id+task_id+intent_id+vulnclass+severity+summary+state). Related-task entries carry source_task_id/inherited=true and are read-only. Only findings here; for ordinary exploration facts use list_facts, for details node_detail(id).", + return t.readExpTool("list_findings", "List the [confirmed findings] of this task and directly related tasks (compact: id+task_id+intent_id+vulnclass+severity+summary+state+status). Related-task entries carry source_task_id/inherited=true and are read-only. Only findings here; for ordinary exploration facts use list_facts, for details node_detail(id).", obj(map[string]any{}), func(context.Context, json.RawMessage) (actool.Result, error) { f, _ := t.ts.ListByKindWithSources(db.KindFinding, 500) diff --git a/agent/tools_overview_test.go b/agent/tools_overview_test.go index 279f734..d60cb6d 100644 --- a/agent/tools_overview_test.go +++ b/agent/tools_overview_test.go @@ -1,6 +1,7 @@ package agent import ( + "encoding/json" "strings" "testing" "unicode/utf8" @@ -29,3 +30,25 @@ func TestOverviewTextBudgetIsFairAndUTF8Safe(t *testing.T) { t.Fatalf("exhausted budget returned more text: %q", tail) } } + +func TestCompactFindingIncludesStatus(t *testing.T) { + payload, _ := json.Marshal(map[string]any{ + "summary": "SQLi on /search", "vulnclass": "sqli", "severity": "high", "status": "false_positive", + }) + m := compactFinding(&db.Node{ID: 7, State: "confirmed", Payload: payload}) + if m["state"] != "confirmed" { + t.Fatalf("node state: %#v", m["state"]) + } + if m["status"] != "false_positive" { + t.Fatalf("status: %#v", m["status"]) + } + if m["vulnclass"] != "sqli" || m["severity"] != "high" || m["summary"] != "SQLi on /search" { + t.Fatalf("fields: %#v", m) + } + + plain, _ := json.Marshal(map[string]any{"summary": "x"}) + m = compactFinding(&db.Node{ID: 8, State: "confirmed", Payload: plain}) + if _, ok := m["status"]; ok { + t.Fatalf("empty payload should omit status: %#v", m) + } +} diff --git a/db/findings.go b/db/findings.go index 56e8e0b..7a242e6 100644 --- a/db/findings.go +++ b/db/findings.go @@ -709,20 +709,18 @@ func (d *DB) DeleteFindingsByTask(taskID int64) (int64, error) { return res.RowsAffected() } -// SetFindingStatus updates one finding's triage state. Returns rows affected. +// SetFindingStatus updates one finding's triage state and mirrors it onto the +// originating exploration node's payload under "status" (same path as +// setFindingCol). Agents read that node; the findings row alone is not enough. +// Returns rows affected. // -// Low-level setter: it only changes the status and does not register a push -// event. Production code changing status should go through -// SetFindingStatusWithNotify — calling this function directly silently disables -// the "status change push". It is kept so tests that do not care about -// notifications (parameter validation, retest flows) can drive the status on -// their own. +// Low-level setter: it does not register a push event. Production code changing +// status should go through SetFindingStatusWithNotify — calling this function +// directly silently disables the "status change push". It is kept so tests that +// do not care about notifications (parameter validation, retest flows) can +// drive the status on their own. func (d *DB) SetFindingStatus(id int64, status string) (int64, error) { - res, err := d.Exec(`UPDATE findings SET status=$1 WHERE id=$2`, status, id) - if err != nil { - return 0, err - } - return res.RowsAffected() + return d.setFindingCol(id, "status", "status", status) } // SetFindingReportByNodeID sets the Markdown report on the standalone finding row diff --git a/db/findings_test.go b/db/findings_test.go index d9c6783..a5e915d 100644 --- a/db/findings_test.go +++ b/db/findings_test.go @@ -1,6 +1,7 @@ package db import ( + "context" "encoding/json" "errors" "fmt" @@ -58,6 +59,69 @@ func TestDeleteFinding(t *testing.T) { } } +func TestSetFindingStatusMirrorsNodePayload(t *testing.T) { + d, err := Open(testDSN(t)) + if err != nil { + t.Skipf("postgres unavailable (%v) — skipping", err) + } + defer d.Close() + + tk, err := d.CreateTask("finding status node sync", "goal", nil, 0, 0) + if err != nil { + t.Fatal(err) + } + defer d.DeleteTask(tk.ID) + + es := d.Exploration(tk.ExplorationID) + nodeID, err := es.AddNode(KindFinding, map[string]any{"summary": "x", "severity": "high"}, 5, "confirmed", "worker", nil) + if err != nil { + t.Fatal(err) + } + fid, err := d.AddFinding(tk.ID, nodeID, "XSS", "reflected XSS", "high", "summary", "poc", "worker", nil) + if err != nil { + t.Fatal(err) + } + + n, err := d.SetFindingStatus(fid, FindingFalsePositive) + if err != nil || n != 1 { + t.Fatalf("SetFindingStatus: n=%d err=%v", n, err) + } + + var status, state string + var payload []byte + if err := d.QueryRow(`SELECT f.status, n.payload, n.state + FROM findings f JOIN exploration_nodes n ON n.id=f.node_id + WHERE f.id=$1`, fid).Scan(&status, &payload, &state); err != nil { + t.Fatal(err) + } + if status != FindingFalsePositive { + t.Fatalf("findings.status=%q", status) + } + if state != "confirmed" { + t.Fatalf("node state changed: %q", state) + } + var p map[string]any + if err := json.Unmarshal(payload, &p); err != nil { + t.Fatal(err) + } + if p["status"] != FindingFalsePositive { + t.Fatalf("payload status=%v payload=%s", p["status"], payload) + } + + if _, found, _, err := d.SetFindingStatusWithNotify(context.Background(), fid, FindingConfirmed); err != nil || !found { + t.Fatalf("SetFindingStatusWithNotify: found=%v err=%v", found, err) + } + if err := d.QueryRow(`SELECT payload FROM exploration_nodes WHERE id=$1`, nodeID).Scan(&payload); err != nil { + t.Fatal(err) + } + if err := json.Unmarshal(payload, &p); err != nil { + t.Fatal(err) + } + if p["status"] != FindingConfirmed { + t.Fatalf("tx path payload status=%v", p["status"]) + } +} + // TestFindingsPageAndStats exercises ListFindingsPage (filter/sort/paging) and // FindingStats against the live dev PG. It tags its rows with a unique vulnclass // so assertions are isolated from any pre-existing data, and cleans up after. diff --git a/db/notification.go b/db/notification.go index 1a386b5..9e0daaf 100644 --- a/db/notification.go +++ b/db/notification.go @@ -525,12 +525,12 @@ func (d *DB) SetFindingStatusWithNotify(ctx context.Context, id int64, status st func SetFindingStatusTx(ctx context.Context, tx *sql.Tx, id int64, status string) (from string, found bool, changed bool, notified bool, err error) { var ( vulnclass, name, severity, summary string - taskID sql.NullInt64 + taskID, nodeID sql.NullInt64 assetIDs []byte ) - scanErr := tx.QueryRowContext(ctx, `SELECT vulnclass, name, severity, summary, task_id, asset_ids, status + scanErr := tx.QueryRowContext(ctx, `SELECT vulnclass, name, severity, summary, task_id, asset_ids, status, node_id FROM findings WHERE id=$1 FOR UPDATE`, id). - Scan(&vulnclass, &name, &severity, &summary, &taskID, &assetIDs, &from) + Scan(&vulnclass, &name, &severity, &summary, &taskID, &assetIDs, &from, &nodeID) if scanErr == sql.ErrNoRows { return "", false, false, false, nil } @@ -546,6 +546,13 @@ FROM findings WHERE id=$1 FOR UPDATE`, id). if _, err := tx.ExecContext(ctx, `UPDATE findings SET status=$2 WHERE id=$1`, id, status); err != nil { return from, true, false, false, err } + if nodeID.Valid { + if _, err := tx.ExecContext(ctx, `UPDATE exploration_nodes + SET payload = jsonb_set(payload, '{status}', to_jsonb($1::text)) + WHERE id = $2`, status, nodeID.Int64); err != nil { + return from, true, false, false, err + } + } var assets []int64 _ = json.Unmarshal(assetIDs, &assets) notified = RecordNotificationEventTx(ctx, tx, notify.EventFindingStatusChanged, id, notify.Snapshot{