fix(tools): degrade stale or invalid evidence_hint_id cleanly in report_finding (#550)

This commit is contained in:
BaaRai
2026-11-27 01:08:45 +01:00
parent efe588a526
commit 478bdcae82
3 changed files with 144 additions and 10 deletions
+112
View File
@@ -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)
}
})
}
}
+8 -1
View File
@@ -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"`
+24 -9
View File
@@ -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
})