ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)
Go aiai-agentsautonomous-agentsbug-bountydockergolanglinuxllmmcpnextjspenetration-testingpentestpostgresqlreareactred-teamsecuritysecurity-automationsqlitetypescript
fix(rsi): keep an unchanged negative lesson on a multi-company task
BaaRai2be58efd5096
diff --git a/db/lessons.go b/db/lessons.goindex 9ea2519..d322135 100644--- a/db/lessons.go+++ b/db/lessons.go@@ -196,15 +196,14 @@ func (d *DB) ExtractLessonsForTask(ctx context.Context, taskID int64) (int, erro}case "false_positive", "duplicate", "ignored", "risk_accepted":// The action text names the status, so a change inside this group is a- // different lesson. Withdraw this finding from every other negative- // lesson, and leave the lesson we are about to write alone so an- // adopted row is not retired and restored on a repeat run.+ // different lesson. Withdraw this finding from every negative lesson+ // with another action, and leave lessons with this action alone so an+ // adopted row is not retired and restored on a repeat run. The match is+ // 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 + ")"- keepKey := ""- if write {- keepKey = lessonDedupeKey("negative", categoryID, companyID, cue, lessonText(action, lessonActionMax))- }- if err := d.retractLessonEvidence(ctx, "finding", findingID, "negative", keepKey); err != nil {+ if err := d.retractLessonEvidence(ctx, "finding", findingID, "negative", lessonText(action, lessonActionMax)); err != nil {return 0, err}if write {@@ -252,8 +251,10 @@ func (d *DB) upsertLesson(ctx context.Context, key, kind, cue, action string, ca// retractLessonEvidence withdraws this source from lessons of fromKind and// retires any of those lessons that now have zero live evidence. The row stays,// with withdrawn_at set, so the lesson still names what it rested on. Shared-// lessons that still have other live evidence stay active.-func (d *DB) retractLessonEvidence(ctx context.Context, sourceKind string, sourceID int64, fromKind, keepKey string) error {+// lessons that still have other live evidence stay active. A non-empty+// keepAction spares lessons with that action, compared without case as the+// dedupe key does.+func (d *DB) retractLessonEvidence(ctx context.Context, sourceKind string, sourceID int64, fromKind, keepAction string) error {rows, err := d.QueryContext(ctx, `UPDATE rsi_lesson_evidence eSET withdrawn_at=now()@@ -263,8 +264,8 @@ func (d *DB) retractLessonEvidence(ctx context.Context, sourceKind string, sourcAND l.kind=$1AND e.source_kind=$2AND e.source_id=$3- AND ($4 = '' OR l.dedupe_key <> $4)- RETURNING e.lesson_id`, fromKind, sourceKind, sourceID, keepKey)+ AND ($4 = '' OR lower(l.action) <> lower($4))+ RETURNING e.lesson_id`, fromKind, sourceKind, sourceID, keepAction)if err != nil {return err}diff --git a/db/lessons_test.go b/db/lessons_test.goindex 3d616ca..da64be5 100644--- a/db/lessons_test.go+++ b/db/lessons_test.go@@ -1069,6 +1069,82 @@ func TestMultiCompanyExtractStillRetracts(t *testing.T) {}}+// A second company stops new writes, but the disposition did not change: the+// negative lesson written while the task had one company keeps its evidence.+func TestMultiCompanyExtractKeepsUnchangedNegative(t *testing.T) {+ d, err := Open(testDSN(t))+ if err != nil {+ SkipPGUnavailable(t, err)+ }+ t.Cleanup(func() { d.Close() })++ stamp := time.Now().UnixNano()+ cs := d.Companies()+ companyA, _, err := cs.UpsertCompany(fmt.Sprintf("LessonKeepNegA %d", stamp), "")+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { cleanupCompany(d, companyA) })+ companyB, _, err := cs.UpsertCompany(fmt.Sprintf("LessonKeepNegB %d", stamp), "")+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { cleanupCompany(d, companyB) })+ task, err := d.CreateTaskWithOptions("negative then multi", "goal", TaskCreateOptions{+ Name: fmt.Sprintf("lesson-keep-neg-%d", stamp), CompanyIDs: []int64{companyA},+ })+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { cleanupErr(t, "task", d.DeleteTask(task.ID)) })+ cue := fmt.Sprintf("xss-keep-neg-%d", stamp)+ label := fmt.Sprintf("reflected in search %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',$3,'high','false_positive')+ RETURNING id`, task.ID, cue, label).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)+ }+ neg := findLessonByAction(t, d, task.ID, "negative", "false_positive", label)+ if _, err := d.Exec(`UPDATE rsi_lessons SET status='adopted' WHERE id=$1`, neg.ID); err != nil {+ t.Fatal(err)+ }++ if _, err := d.Exec(`INSERT INTO task_scope(task_id, kind, company_id, source) VALUES ($1,'company',$2,'manual')`, task.ID, companyB); err != nil {+ t.Fatal(err)+ }+ n, err := d.ExtractLessonsForTask(t.Context(), task.ID)+ if err != nil {+ t.Fatal(err)+ }+ if n != 0 {+ t.Fatalf("multi-company extract wrote %d lessons", n)+ }+ status, support := lessonStatusSupport(t, d, neg.ID)+ if status != "adopted" || support != 1 {+ t.Fatalf("unchanged negative after a second company: status=%s support=%d, want adopted 1", status, support)+ }++ // A status change inside the negative group still withdraws it.+ if _, err := d.SetFindingStatus(fid, FindingDuplicate); err != nil {+ t.Fatal(err)+ }+ if _, err := d.ExtractLessonsForTask(t.Context(), task.ID); err != nil {+ t.Fatal(err)+ }+ status, support = lessonStatusSupport(t, d, neg.ID)+ if status != "retired" || support != 0 {+ t.Fatalf("negative after false_positive -> 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 int64diff --git a/docs/rsi-lessons.md b/docs/rsi-lessons.mdindex f7731e9..281e5e1 100644--- a/docs/rsi-lessons.md+++ b/docs/rsi-lessons.md@@ -67,8 +67,9 @@ stopped retest produces nothing.A task with more than one company in `task_scope` does not gain a new lesson.Retraction still runs, so a lesson written while the task had one company can-be withdrawn after a second company is added. Per-finding company attribution-is a later change.+be withdrawn after a second company is added. It withdraws only on a changed+disposition: a finding whose status did not move keeps its evidence. Per-finding+company attribution is a later change.## Selection