Skip to content

Commit d82472c

Browse files
committed
fix(history): cite the reviewed occurrence in false-positive feedback
1 parent 8c87a63 commit d82472c

3 files changed

Lines changed: 79 additions & 14 deletions

File tree

‎plugins/codex-security/scripts/workbench_db.py‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,20 +1905,11 @@ def set_finding_triage(connection: sqlite3.Connection, args: argparse.Namespace)
19051905
scan = require_scan(connection, occurrence["scan_id"])
19061906
scan_history.require_finding_checkout_owner(connection, scan)
19071907
indexed_finding = _indexed_scan_findings(connection, scan).get(occurrence["id"])
1908-
triaged_occurrences = [occurrence]
1908+
triaged_occurrences = finding_results.triage_occurrences(
1909+
connection, occurrence, indexed_finding
1910+
)
19091911
verification_ids = {occurrence["id"]}
19101912
verification_loaded = False
1911-
if indexed_finding is not None:
1912-
triaged_occurrences = connection.execute(
1913-
"""
1914-
SELECT occurrences.*
1915-
FROM finding_occurrences AS occurrences
1916-
JOIN scans ON scans.id = occurrences.scan_id
1917-
WHERE occurrences.id IN (SELECT value FROM json_each(?))
1918-
ORDER BY scans.rowid, occurrences.id
1919-
""",
1920-
(json.dumps(sorted(indexed_finding["occurrence_ids"])),),
1921-
).fetchall()
19221913
if args.status == "closed":
19231914
for checked_occurrence in triaged_occurrences:
19241915
remediation = remediation_state.latest_attempt(connection, checked_occurrence["id"])
@@ -1958,6 +1949,10 @@ def set_finding_triage(connection: sqlite3.Connection, args: argparse.Namespace)
19581949
admission_sequence = connection.execute(
19591950
"SELECT COALESCE(MAX(sequence), 0) FROM scan_admissions"
19601951
).fetchone()[0]
1952+
# The latest decision supplies feedback evidence. Append the reviewed occurrence last.
1953+
decision_source = (
1954+
indexed_finding["decision_occurrence_id"] if indexed_finding else occurrence["id"]
1955+
)
19611956
for triaged_occurrence in triaged_occurrences:
19621957
previous_triage = connection.execute(
19631958
"SELECT status, close_reason, note, "
@@ -1969,6 +1964,10 @@ def set_finding_triage(connection: sqlite3.Connection, args: argparse.Namespace)
19691964
).fetchone()
19701965
changed = (
19711966
previous_triage is None
1967+
or (
1968+
triaged_occurrence["id"] == occurrence["id"]
1969+
and decision_source != occurrence["id"]
1970+
)
19721971
or (indexed_finding is not None and indexed_finding["status"] != args.status)
19731972
or previous_triage["admission_sequence"] != admission_sequence
19741973
or (
@@ -1979,6 +1978,7 @@ def set_finding_triage(connection: sqlite3.Connection, args: argparse.Namespace)
19791978
!= (args.status, close_reason, note)
19801979
)
19811980
if changed:
1981+
decision_source = triaged_occurrence["id"]
19821982
connection.execute(
19831983
"""
19841984
INSERT INTO finding_decisions (

‎plugins/codex-security/scripts/workbench_finding_results.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import argparse
6+
import json
67
import os
78
import re
89
import sqlite3
@@ -305,6 +306,22 @@ def scan_finding_triage(
305306
}
306307

307308

309+
def triage_occurrences(
310+
connection: sqlite3.Connection, reviewed: sqlite3.Row, indexed_finding: dict[str, Any] | None
311+
) -> list[sqlite3.Row]:
312+
if indexed_finding is None:
313+
return [reviewed]
314+
return connection.execute(
315+
"""
316+
SELECT occurrences.* FROM finding_occurrences AS occurrences
317+
JOIN scans ON scans.id = occurrences.scan_id
318+
WHERE occurrences.id IN (SELECT value FROM json_each(?))
319+
ORDER BY occurrences.id = ?, scans.rowid, occurrences.id
320+
""",
321+
(json.dumps(sorted(indexed_finding["occurrence_ids"])), reviewed["id"]),
322+
).fetchall()
323+
324+
308325
def finding_triage_result(
309326
connection: sqlite3.Connection,
310327
occurrence_id: str,

‎plugins/codex-security/tests/test_workbench_owner_feedback.py‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
from pathlib import Path
99

1010
import pytest
11-
from test_workbench_scan_history import compare_scan_pair, create_cli_scan
12-
from workbench_test_support import initialize_git_repository, run_workbench
11+
from test_workbench_scan_history import (
12+
compare_scan_pair,
13+
confirmed_match,
14+
create_cli_scan,
15+
save_scan_matches,
16+
)
17+
from workbench_test_support import initialize_git_repository, run_workbench, set_triage
1318

1419
SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "workbench_db.py"
1520

@@ -50,6 +55,49 @@ def test_previous_owner_cannot_mutate_or_replay_finding_actions(tmp_path, comman
5055
assert connection.execute("SELECT * FROM finding_triage").fetchall() == before
5156

5257

58+
def test_feedback_cites_the_selected_occurrence_of_an_existing_match(tmp_path):
59+
state, root, repository = tmp_path / "state", tmp_path / "scans", tmp_path / "repository"
60+
repository.mkdir()
61+
scans = [
62+
create_cli_scan(
63+
state, root, repository, identity_anchor=name, relative_path=f"src/{name}.py"
64+
)
65+
for name in ("first", "second")
66+
]
67+
findings = [finding(state, scan) for scan in scans]
68+
save_scan_matches(state, *scans, confirmed_match(*(row["occurrenceId"] for row in findings)))
69+
current = create_cli_scan(state, root, repository, complete=False)
70+
note = "Reviewed this synthetic occurrence."
71+
for index, expected_decisions in ((0, 2), (1, 3), (1, 3)):
72+
reviewed = findings[index]
73+
set_triage(
74+
state,
75+
reviewed["occurrenceId"],
76+
"closed",
77+
"--close-reason",
78+
"false_positive",
79+
"--note",
80+
note,
81+
)
82+
(feedback,) = run_workbench(state, "get-scan-feedback", "--scan-id", current["scanId"])[
83+
"falsePositives"
84+
]
85+
assert feedback["sourceScanId"] == scans[index]["scanId"]
86+
assert feedback["findingId"] == reviewed["findingId"]
87+
assert feedback["locations"][0]["path"] == reviewed["locations"][0]["path"]
88+
with sqlite3.connect(state / "workbench.sqlite3") as connection:
89+
assert (
90+
connection.execute("SELECT COUNT(*) FROM finding_decisions").fetchone()[0]
91+
== expected_decisions
92+
)
93+
assert (
94+
connection.execute(
95+
"SELECT COUNT(*) FROM finding_triage WHERE status = 'closed'"
96+
).fetchone()[0]
97+
== 2
98+
)
99+
100+
53101
def test_replacement_checkout_does_not_receive_previous_owner_feedback(tmp_path):
54102
state, root, repository = tmp_path / "state", tmp_path / "scans", tmp_path / "repository"
55103
repository.mkdir()

0 commit comments

Comments
 (0)