diff --git a/db/lessons.go b/db/lessons.go index 2f608bc..cb497bf 100644 --- a/db/lessons.go +++ b/db/lessons.go @@ -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 } } diff --git a/db/lessons_test.go b/db/lessons_test.go index 07ee169..aa27947 100644 --- a/db/lessons_test.go +++ b/db/lessons_test.go @@ -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 diff --git a/docs/rsi-lessons.md b/docs/rsi-lessons.md index f635137..7be9106 100644 --- a/docs/rsi-lessons.md +++ b/docs/rsi-lessons.md @@ -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 : ` | | retest `reproduced` on such a finding | positive | `Reproduced on retest: ` | | finding `false_positive` / `duplicate` / `ignored` / `risk_accepted` | negative | `Do not re-report without new mechanics: (prior )` | -| retest `fixed` / `inconclusive` | caution | `Recheck before reporting : a prior retest was ` | +| retest `fixed` / `inconclusive` on a `confirmed` / `resolved` / `fixed` finding | caution | `Recheck before reporting : a prior retest was ` | Pending and in-progress findings produce nothing: there is no outcome yet. Empty cue, action, or source is refused by the gate.