fix(rsi): keep the status in a capped negative lesson action

The negative action is the label followed by "(prior <status>)", 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.
This commit is contained in:
BaaRai
2026-11-21 08:50:38 +01:00
parent 2be58efd50
commit aa4a1bfab9
3 changed files with 90 additions and 1 deletions
+7 -1
View File
@@ -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
}
+79
View File
@@ -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
+4
View File
@@ -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 <status>)` 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.