fix(rsi): write retest cautions only for a finding with an outcome

The caution branch ran after the status switch, for every status. A pending
or in_progress finding with a completed fixed or inconclusive retest got a
caution lesson, while docs/rsi-lessons.md says pending and in-progress
findings produce nothing. The caution is now written in the confirmed /
resolved / fixed branch only.

A negative disposition (false_positive, duplicate, ignored, risk_accepted)
now also withdraws the cautions written from the finding's retests. The doc
says a reversed disposition withdraws the finding's evidence, including every
retest of the finding, and a caution from a retest is that evidence: it
tells the next task to recheck a finding the operator has since rejected.
Nothing in the code or the doc says a caution should outlive that. Confirming
again restores it, as for the positive lessons. Reopening (pending /
in_progress) still retracts nothing.
This commit is contained in:
BaaRai
2026-11-21 08:50:38 +01:00
parent aa4a1bfab9
commit 9a4116be71
3 changed files with 117 additions and 8 deletions
+11 -5
View File
@@ -99,7 +99,8 @@ func lessonExtractCompanyID(companyIDs []int64) int64 {
// retests. It is idempotent for the same disposition: the evidence primary key
// means a second run adds no support. A later run after a reversed disposition
// retracts that finding's evidence from the opposite-kind lesson, including
// every retest of the finding, and retires the lesson when nothing else
// every retest of the finding (a negative disposition also withdraws its
// retest cautions), and retires the lesson when nothing else
// supports it. Confirming again restores the evidence; a retired lesson is
// un-retired only when it has no remaining live evidence. Pending and in_progress
// produce nothing and retract nothing. A task with more than one company does
@@ -190,6 +191,14 @@ func (d *DB) ExtractLessonsForTask(ctx context.Context, taskID int64) (int, erro
return 0, err
}
}
// A caution needs an outcome too: pending and in_progress write
// nothing, and a negative disposition withdraws it below.
if retestID > 0 && (verdict == "fixed" || verdict == "inconclusive") {
if err := add("caution", "Recheck before reporting "+cue+": a prior retest was "+verdict,
"retest", retestID, "verdict="+verdict); err != nil {
return 0, err
}
}
}
if err := d.retractLessonEvidence(ctx, "finding", findingID, "negative", ""); err != nil {
return 0, err
@@ -223,10 +232,7 @@ func (d *DB) ExtractLessonsForTask(ctx context.Context, taskID int64) (int, erro
if err := d.retractFindingRetestEvidence(ctx, findingID, "positive"); err != nil {
return 0, err
}
}
if write && retestID > 0 && (verdict == "fixed" || verdict == "inconclusive") {
if err := add("caution", "Recheck before reporting "+cue+": a prior retest was "+verdict,
"retest", retestID, "verdict="+verdict); err != nil {
if err := d.retractFindingRetestEvidence(ctx, findingID, "caution"); err != nil {
return 0, err
}
}
+102
View File
@@ -1224,6 +1224,108 @@ func TestNegativeStatusSurvivesActionCap(t *testing.T) {
}
}
// A pending or in-progress finding has no outcome yet, so even a completed
// retest on it writes no caution.
func TestCautionNeedsFindingOutcome(t *testing.T) {
d, err := Open(testDSN(t))
if err != nil {
SkipPGUnavailable(t, err)
}
t.Cleanup(func() { d.Close() })
stamp := time.Now().UnixNano()
task, err := d.CreateTask(fmt.Sprintf("lesson caution open %d", stamp), "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { cleanupErr(t, "task", d.DeleteTask(task.ID)) })
cue := fmt.Sprintf("xss-caution-open-%d", stamp)
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM rsi_lessons WHERE cue=$1`, cue) })
for _, status := range []string{FindingPending, FindingInProgress} {
var fid int64
if err := d.QueryRow(`
INSERT INTO findings(task_id,vulnclass,name,summary,severity,status)
VALUES ($1,$2,'Reflected XSS','reflected in search','high',$3)
RETURNING id`, task.ID, cue, status).Scan(&fid); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, fid) })
for _, verdict := range []string{"fixed", "inconclusive"} {
rid := insertLessonRetestStatus(t, d, fid, "completed", verdict)
if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {
t.Fatal(err)
}
if n := retestEvidence(t, d, rid); n != 0 {
t.Fatalf("finding status=%s, completed %s retest wrote %d evidence rows", status, verdict, n)
}
}
}
var lessons int
if err := d.QueryRow(`SELECT COUNT(*) FROM rsi_lessons WHERE cue=$1`, cue).Scan(&lessons); err != nil {
t.Fatal(err)
}
if lessons != 0 {
t.Fatalf("open findings produced %d lessons", lessons)
}
}
// A reversed disposition withdraws the finding's evidence, and a caution from
// its retest is part of that evidence. Confirming again restores it.
func TestFalsePositiveWithdrawsRetestCaution(t *testing.T) {
d, err := Open(testDSN(t))
if err != nil {
SkipPGUnavailable(t, err)
}
t.Cleanup(func() { d.Close() })
stamp := time.Now().UnixNano()
task, err := d.CreateTask(fmt.Sprintf("lesson caution fp %d", stamp), "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { cleanupErr(t, "task", d.DeleteTask(task.ID)) })
cue := fmt.Sprintf("xss-caution-fp-%d", stamp)
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM rsi_lessons WHERE cue=$1`, cue) })
var fid int64
if err := d.QueryRow(`
INSERT INTO findings(task_id,vulnclass,name,summary,severity,status)
VALUES ($1,$2,'Reflected XSS','reflected in search','high','confirmed')
RETURNING id`, task.ID, cue).Scan(&fid); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, fid) })
insertLessonRetestStatus(t, d, fid, "completed", "inconclusive")
if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {
t.Fatal(err)
}
caution := findLessonByAction(t, d, task.ID, "caution", "Recheck", cue)
if status, support := lessonStatusSupport(t, d, caution.ID); status != "candidate" || support != 1 {
t.Fatalf("caution after confirmed: status=%s support=%d, want candidate 1", status, support)
}
if _, err := d.SetFindingStatus(fid, FindingFalsePositive); err != nil {
t.Fatal(err)
}
if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {
t.Fatal(err)
}
if status, support := lessonStatusSupport(t, d, caution.ID); status != "retired" || support != 0 {
t.Fatalf("caution after false_positive: status=%s support=%d, want retired 0", status, support)
}
if _, err := d.SetFindingStatus(fid, FindingConfirmed); err != nil {
t.Fatal(err)
}
if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {
t.Fatal(err)
}
if status, support := lessonStatusSupport(t, d, caution.ID); status != "candidate" || support != 1 {
t.Fatalf("caution after re-confirm: status=%s support=%d, want candidate 1", status, support)
}
}
func insertLessonRetest(t *testing.T, d *DB, findingID int64, verdict string) int64 {
t.Helper()
var id int64
+4 -3
View File
@@ -39,8 +39,9 @@ disposition withdraws that finding's evidence from the opposite-kind lesson,
including every retest of the finding, and retires the lesson when its live
support hits zero. The evidence row stays, with `withdrawn_at` set. Confirming
again clears that timestamp and restores the evidence; a retired lesson is
un-retired only when it has no remaining live evidence. A change inside the
negative group (`false_positive`, `duplicate`, `ignored`, `risk_accepted`)
un-retired only when it has no remaining live evidence. A negative disposition
also withdraws the cautions written from the finding's retests. A change inside
the negative group (`false_positive`, `duplicate`, `ignored`, `risk_accepted`)
withdraws the finding from the previous negative lesson before writing the
new action text.
@@ -56,7 +57,7 @@ Rules:
| finding `confirmed` / `resolved` / `fixed` | positive | `Confirmed <vulnclass>: <summary>` |
| retest `reproduced` on such a finding | positive | `Reproduced <vulnclass> on retest: <summary>` |
| finding `false_positive` / `duplicate` / `ignored` / `risk_accepted` | negative | `Do not re-report without new mechanics: <summary> (prior <status>)` |
| retest `fixed` / `inconclusive` | caution | `Recheck before reporting <vulnclass>: a prior retest was <verdict>` |
| retest `fixed` / `inconclusive` on a `confirmed` / `resolved` / `fixed` finding | caution | `Recheck before reporting <vulnclass>: a prior retest was <verdict>` |
Pending and in-progress findings produce nothing: there is no outcome yet.
Empty cue, action, or source is refused by the gate.