Count newly detected findings once in approval policies

What does this MR do and why?

A scan_finding approval rule that mixes newly detected and pre-existing vulnerability_states (for example [new_needs_triage, detected]) could count one vulnerability twice against vulnerabilities_allowed. A finding missing from the target pipeline is counted as newly detected. If the project already tracks it as a detected vulnerability, VulnerabilitiesCountService counted it again as pre-existing.

This happens whenever the target branch's comparison pipeline has lost its findings, for example after its security report artifacts expire. Every finding then looks new, so a merge request within the limit gets blocked.

This MR removes the newly detected UUIDs from the set passed to VulnerabilitiesCountService, in both the regular and the grouped-scanner paths. The .uniq also stops a UUID present in both the source and target pipelines from being counted twice across count batches.

The change is behind the approval_policy_count_new_findings_once feature flag (gitlab_com_derisk, default off).

References

Screenshots or screen recordings

Not a UI change. Rails console output from the steps below:

Before (flag off) After (flag on)
approvals_required=1 violation=failed newly_detected=2 approvals_required=0 violation=none

How to set up and validate locally

  1. Create an approval policy for the default branch with this rule and 1 required approval:
    - type: scan_finding
      branch_type: default
      scanners: [dependency_scanning]
      severity_levels: [critical]
      vulnerability_states: [new_needs_triage, detected]
      vulnerabilities_allowed: 2
  2. Run a default-branch pipeline whose dependency scanning report has 2 critical findings, so they become detected vulnerabilities.
  3. Erase that pipeline's dependency scanning job, so its scan becomes purged.
  4. Open a merge request to the default branch whose pipeline reports the same 2 findings. The merge request is blocked: 2 newly detected + 2 pre-existing = 4 > 2.
  5. Enable the flag and run the merge request pipeline again:
    Feature.enable(:approval_policy_count_new_findings_once)
    The policy rule no longer requires approval: 2 findings, within the limit.

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Merge request reports

Loading
Loading