From aa4a1bfab97df54d1ccd54a35a34b6fb60ff4fb3 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Sat, 21 Nov 2026 08:46:01 +0100 Subject: [PATCH] fix(rsi): keep the status in a capped negative lesson action The negative action is the label followed by "(prior )", and the whole string was cut at 320 runes. Past about 257 runes of summary the cut removed the suffix, so false_positive and duplicate produced the same action and the same dedupe key: a change inside the negative group reused the old lesson instead of writing a new one and withdrawing the old. The label is now cut to the room left by the prefix and the suffix, so the status is always in the action and in the key. A summary short enough to fit gives the same action as before, so existing keys do not change. --- db/lessons.go | 8 ++++- db/lessons_test.go | 79 +++++++++++++++++++++++++++++++++++++++++++++ docs/rsi-lessons.md | 4 +++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/db/lessons.go b/db/lessons.go index d322135..2f608bc 100644 --- a/db/lessons.go +++ b/db/lessons.go @@ -202,7 +202,13 @@ func (d *DB) ExtractLessonsForTask(ctx context.Context, taskID int64) (int, erro // on the action, not the dedupe key: the key carries the company, and a // task with several companies cannot rebuild the key it was written // under, though its disposition is unchanged. - action := "Do not re-report without new mechanics: " + label + " (prior " + status + ")" + // Cut the label, not the status, at the action cap: without the + // suffix, a long summary gives every negative status the same action + // and the same dedupe key. + prefix := "Do not re-report without new mechanics: " + suffix := " (prior " + status + ")" + room := lessonActionMax - utf8.RuneCountInString(prefix) - utf8.RuneCountInString(suffix) + action := prefix + lessonText(label, room) + suffix if err := d.retractLessonEvidence(ctx, "finding", findingID, "negative", lessonText(action, lessonActionMax)); err != nil { return 0, err } diff --git a/db/lessons_test.go b/db/lessons_test.go index da64be5..07ee169 100644 --- a/db/lessons_test.go +++ b/db/lessons_test.go @@ -1145,6 +1145,85 @@ func TestMultiCompanyExtractKeepsUnchangedNegative(t *testing.T) { } } +// liveNegativeLesson returns the one negative lesson that holds this finding as +// live evidence. +func liveNegativeLesson(t *testing.T, d *DB, findingID int64) (id int64, key, action string) { + t.Helper() + if err := d.QueryRow(` + SELECT l.id, l.dedupe_key, l.action FROM rsi_lessons l + JOIN rsi_lesson_evidence e ON e.lesson_id=l.id + WHERE l.kind='negative' AND e.source_kind='finding' AND e.source_id=$1 + AND e.withdrawn_at IS NULL`, findingID).Scan(&id, &key, &action); err != nil { + t.Fatalf("live negative lesson of finding %d: %v", findingID, err) + } + return id, key, action +} + +// A summary long enough to fill the action cap must not cut the status off: +// false_positive and duplicate would then share one action and one key. +func TestNegativeStatusSurvivesActionCap(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 long summary %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-long-%d", stamp) + t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM rsi_lessons WHERE cue=$1`, cue) }) + summary := fmt.Sprintf("long-%d-", stamp) + summary += strings.Repeat("x", 300-utf8.RuneCountInString(summary)) + if n := utf8.RuneCountInString(summary); n != 300 { + t.Fatalf("summary has %d runes, want 300", n) + } + var fid int64 + if err := d.QueryRow(` + INSERT INTO findings(task_id,vulnclass,name,summary,severity,status) + VALUES ($1,$2,'Reflected XSS',$3,'high','false_positive') + RETURNING id`, task.ID, cue, summary).Scan(&fid); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { cleanupExec(t, d, `DELETE FROM findings WHERE id=$1`, fid) }) + + if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil { + t.Fatal(err) + } + firstID, firstKey, firstAction := liveNegativeLesson(t, d, fid) + + if _, err := d.SetFindingStatus(fid, FindingDuplicate); err != nil { + t.Fatal(err) + } + n, err := d.ExtractLessonsForTask(t.Context(), task.ID) + if err != nil { + t.Fatal(err) + } + if n != 1 { + t.Fatalf("duplicate extract wrote %d lessons, want 1", n) + } + secondID, secondKey, secondAction := liveNegativeLesson(t, d, fid) + if secondID == firstID || secondKey == firstKey { + t.Fatalf("false_positive and duplicate share lesson %d / key %s", secondID, secondKey) + } + if !strings.HasSuffix(firstAction, "(prior false_positive)") { + t.Fatalf("false_positive action lost its status: %q", firstAction) + } + if !strings.HasSuffix(secondAction, "(prior duplicate)") { + t.Fatalf("duplicate action lost its status: %q", secondAction) + } + if n := utf8.RuneCountInString(secondAction); n > lessonActionMax { + t.Fatalf("action has %d runes, cap is %d", n, lessonActionMax) + } + status, support := lessonStatusSupport(t, d, firstID) + if status != "retired" || support != 0 { + t.Fatalf("false_positive lesson after duplicate: status=%s support=%d, want retired 0", 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 281e5e1..f635137 100644 --- a/docs/rsi-lessons.md +++ b/docs/rsi-lessons.md @@ -61,6 +61,10 @@ Rules: Pending and in-progress findings produce nothing: there is no outcome yet. Empty cue, action, or source is refused by the gate. +An action is capped at 320 runes. In a negative action the summary is cut, not +the `(prior )` suffix, so each negative status keeps its own action and +dedupe key. + A retest counts only when it is `completed` with a verdict; extraction reads the latest such retest. A verdict staged on a pending, running, failed, or stopped retest produces nothing.