From 478bdcae820daf1dbc44e39348b024385aac4cd0 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Fri, 27 Nov 2026 01:02:28 +0100 Subject: [PATCH] fix(tools): degrade stale or invalid evidence_hint_id cleanly in report_finding (#550) --- agent/finding_hint_degrade_test.go | 112 +++++++++++++++++++++++++++++ agent/finding_workflow.go | 9 ++- agent/tools.go | 33 ++++++--- 3 files changed, 144 insertions(+), 10 deletions(-) diff --git a/agent/finding_hint_degrade_test.go b/agent/finding_hint_degrade_test.go index 0ecf7242..a8270dbc 100644 --- a/agent/finding_hint_degrade_test.go +++ b/agent/finding_hint_degrade_test.go @@ -107,3 +107,115 @@ func TestReportFindingNonHintNodeDegradesToExplicitRefs(t *testing.T) { t.Fatalf("explicit proof ref did not survive: %+v", rec.got) } } + +// Regression for #550: absent, null, empty, zero, or negative evidence_hint_id +// must succeed even when no explicit traffic_refs are supplied (the default +// workflow where the report agent binds traffic afterward). +func TestReportFindingAbsentOrZeroHintIDWithoutTrafficRefs(t *testing.T) { + prev := FindingTrafficBindingEnabled() + t.Cleanup(func() { SetFindingTrafficBindingEnabled(prev) }) + SetFindingTrafficBindingEnabled(func() bool { return true }) + + d := testDB(t) + defer d.Close() + task, err := d.CreateTask("absent hint id", "fixture", nil, 0, 0) + if err != nil { + t.Fatal(err) + } + defer func() { + _, _ = d.DeleteFindingsByTask(task.ID) + _ = d.DeleteTask(task.ID) + }() + + cases := []struct { + name string + body string + }{ + {"omitted", `{"vulnclass":"SQLI","severity":"high","summary":"omitted hint","evidence":"UNION rows"}`}, + {"zero", `{"vulnclass":"SQLI","severity":"high","summary":"zero hint","evidence":"UNION rows","evidence_hint_id":0}`}, + {"null", `{"vulnclass":"SQLI","severity":"high","summary":"null hint","evidence":"UNION rows","evidence_hint_id":null}`}, + {"empty_string", `{"vulnclass":"SQLI","severity":"high","summary":"empty hint","evidence":"UNION rows","evidence_hint_id":""}`}, + {"negative", `{"vulnclass":"SQLI","severity":"high","summary":"negative hint","evidence":"UNION rows","evidence_hint_id":-5}`}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + ts := NewToolSet(nil, "") + ts.ts, ts.taskID = d.Exploration(task.ExplorationID), task.ID + rec := &capturingRecorder{} + ts.SetFindingRecorder(rec) + + res, err := ts.addFinding().Call(t.Context(), json.RawMessage(tc.body), nil) + if err != nil { + t.Fatalf("report_finding returned error: %v", err) + } + if got := res.Flatten(); !strings.HasPrefix(got, "finding recorded: ") { + t.Fatalf("%s rejected: %q", tc.name, got) + } + if len(rec.got) != 0 { + t.Fatalf("%s unexpectedly bound refs: %+v", tc.name, rec.got) + } + }) + } +} + +// Regression for #550: a stale/deleted or non-hint evidence_hint_id passed +// without explicit traffic_refs must degrade cleanly (record the finding as +// not_bound with an explanatory evidence_note) instead of discarding it. +func TestReportFindingStaleOrNonHintIDWithoutTrafficRefsDegradesCleanly(t *testing.T) { + prev := FindingTrafficBindingEnabled() + t.Cleanup(func() { SetFindingTrafficBindingEnabled(prev) }) + SetFindingTrafficBindingEnabled(func() bool { return true }) + + d := testDB(t) + defer d.Close() + task, err := d.CreateTask("stale hint id", "fixture", nil, 0, 0) + if err != nil { + t.Fatal(err) + } + defer func() { + _, _ = d.DeleteFindingsByTask(task.ID) + _ = d.DeleteTask(task.ID) + }() + exp := d.Exploration(task.ExplorationID) + factID, err := exp.AddNode(db.KindFact, map[string]any{"summary": "not a hint"}, 0, "confirmed", "worker", nil) + if err != nil { + t.Fatal(err) + } + + for _, tc := range []struct { + name string + hintID int64 + }{ + {"stale_nonexistent", 999999}, + {"non_hint_fact_node", factID}, + } { + t.Run(tc.name, func(t *testing.T) { + rec := &capturingRecorder{} + ts := NewToolSet(nil, "") + ts.ts, ts.taskID = exp, task.ID + ts.SetFindingRecorder(rec) + + body, _ := json.Marshal(map[string]any{ + "vulnclass": "XSS", + "severity": "medium", + "summary": "stale hint finding", + "evidence": "reflected payload", + "evidence_hint_id": tc.hintID, + }) + res, err := ts.addFinding().Call(t.Context(), json.RawMessage(body), nil) + if err != nil { + t.Fatalf("report_finding returned error: %v", err) + } + got := res.Flatten() + if !strings.HasPrefix(got, "finding recorded: ") { + t.Fatalf("stale/non-hint evidence_hint_id discarded finding: %q", got) + } + if !strings.Contains(got, "Ignored stale or non-local evidence_hint_id=") { + t.Fatalf("expected explanatory evidence_note, got: %q", got) + } + if len(rec.got) != 0 { + t.Fatalf("expected 0 refs on degraded stale hint, got %+v", rec.got) + } + }) + } +} diff --git a/agent/finding_workflow.go b/agent/finding_workflow.go index a44a689d..50fa8461 100644 --- a/agent/finding_workflow.go +++ b/agent/finding_workflow.go @@ -2,6 +2,7 @@ package agent import ( "encoding/json" + "errors" "fmt" "sync" @@ -227,6 +228,12 @@ func HintTrafficSchema() map[string]any { return map[string]any{"type": "array", "description": "Optional: verified traffic references corresponding to the specific vulnerability in this hint, order preserved; after handoff, report_finding can carry these references via evidence_hint_id.", "items": obj(map[string]any{"traffic_id": str("real traffic ID"), "role": str("baseline / proof / verification / supporting"), "note": str("which conclusion this traffic supports")}, "traffic_id")} } +// errEvidenceHintNotFound marks an evidence_hint_id that does not resolve to a +// local hint node of the current task (e.g. stale/deleted ID, inherited hint, or +// non-hint node ID). report_finding degrades cleanly instead of discarding the +// finding (#550). +var errEvidenceHintNotFound = errors.New("evidence_hint_id is not a local hint node") + func (t *ToolSet) findingRefsFromHint(hintID int64, explicit []db.TrafficRef) ([]db.TrafficRef, error) { if hintID <= 0 { return db.NormalizeTrafficRefs(explicit) @@ -236,7 +243,7 @@ func (t *ToolSet) findingRefsFromHint(hintID int64, explicit []db.TrafficRef) ([ return nil, err } if n == nil || n.Kind != db.KindHint { - return nil, fmt.Errorf("evidence_hint_id=%d must be a hint node of this task (inherited hints cannot be bound directly)", hintID) + return nil, fmt.Errorf("%w: evidence_hint_id=%d must be a hint node of this task (inherited hints cannot be bound directly)", errEvidenceHintNotFound, hintID) } var payload struct { Refs []db.TrafficRef `json:"traffic_refs"` diff --git a/agent/tools.go b/agent/tools.go index 1f7d7eb8..52dd7c0f 100644 --- a/agent/tools.go +++ b/agent/tools.go @@ -3,6 +3,7 @@ package agent import ( "context" "encoding/json" + "errors" "fmt" "log/slog" "strconv" @@ -1602,16 +1603,23 @@ func (t *ToolSet) addFinding() actool.CoreTool { if !findingTrafficBindingEnabled() { a.TrafficRefs, a.EvidenceHintID = nil, nil } - // A model routinely emits "evidence_hint_id": 0 meaning "absent" — the field - // is optional, so 0 must mean "no hint", not an error. A hint id that does - // not resolve to a local hint node (passing a fact node number is the common - // case) must not sink an otherwise complete finding either: when explicit - // traffic_refs are present they are sufficient on their own, so degrade to - // them and keep the finding. Only a call with neither usable hint nor usable - // refs fails. - refs, err := t.findingRefsFromHint(pid(a.EvidenceHintID), a.TrafficRefs) + // A model routinely emits "evidence_hint_id": 0 (or null/"") meaning "absent", + // or passes a stale/non-hint node ID. Because evidence_hint_id is optional and + // findings without bound traffic are valid (the report agent binds traffic later), + // a stale or non-local hint ID must degrade cleanly to whatever valid explicit + // traffic_refs were passed (including none) instead of discarding the finding (#550). + hintID := pid(a.EvidenceHintID) + var ignoredHintNote string + refs, err := t.findingRefsFromHint(hintID, a.TrafficRefs) if err != nil { - if degraded, derr := db.NormalizeTrafficRefs(a.TrafficRefs); derr == nil && len(degraded) > 0 { + if errors.Is(err, errEvidenceHintNotFound) { + degraded, derr := db.NormalizeTrafficRefs(a.TrafficRefs) + if derr != nil { + return actool.Errorf(derr.Error()), nil + } + refs = degraded + ignoredHintNote = fmt.Sprintf("Ignored stale or non-local evidence_hint_id=%d; finding recorded without hint references.", hintID) + } else if degraded, derr := db.NormalizeTrafficRefs(a.TrafficRefs); derr == nil && len(degraded) > 0 { refs = degraded } else { return actool.Errorf(err.Error()), nil @@ -1682,6 +1690,13 @@ func (t *ToolSet) addFinding() actool.CoreTool { result.EvidenceNote = "Finding saved without bound traffic. Automatic agent traffic binding is off: this finding remains visible but cannot close a goal unless traffic is bound manually later (or a non-HTTP goal is proven with an observed tool_output fact)." } } + if ignoredHintNote != "" { + if result.EvidenceNote != "" { + result.EvidenceNote = ignoredHintNote + " " + result.EvidenceNote + } else { + result.EvidenceNote = ignoredHintNote + } + } raw, _ := json.Marshal(result) return actool.Text(fmt.Sprintf("finding recorded: %d\n%s", recorded.NodeID, raw)), nil })