Skip to content

Commit 12b3b45

Browse files
committed
Keep persistent alerts across dependency version changes
Load current alerts for added and updated packages in strict diff mode. Report findings omitted by the comparison as existing so a Block advisory that survives a version change still fails the run.
1 parent 79177a5 commit 12b3b45

7 files changed

Lines changed: 184 additions & 48 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
- A Block-action CVE on an unchanged package now fails the run even when
1818
another CVE of the same severity on that package is triaged to monitor or
1919
ignore. Before, the triaged CVE could stand in for both and the run passed.
20+
- `--strict-blocking` fails on Block alerts that persist across a dependency
21+
version change and lists them in the Existing alerts table.
2022
- In full-scan mode, the `--enable-json`, `--enable-sarif` and
2123
`--enable-gitlab-security` reports now list every advisory on a package.
2224
Before, they listed one per alert type and severity, so expect more alerts,

‎docs/cli-reference.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ Security issues detected by Socket Security:
669669
670670
- **Diff Mode Only**: The flag applies when a scan comparison runs, with or without SCM integration. A full scan logs a warning and does not evaluate existing violations; see [Scan type by pipeline](ci-cd.md#scan-type-by-pipeline).
671671
- **Error-Level Only**: Only fails on `error=True` alerts (blocking severity), not warnings.
672-
- **Existing alerts come from the full scan**: The scan comparison doesn't carry alerts on unchanged packages, so the CLI reads them from the new full scan, at the cost of one more API request per run. If that request fails or omits an unchanged package, the run fails with the infrastructure-error code rather than passing without them.
672+
- **Existing alerts come from the full scan**. The CLI reads alerts on unchanged packages and findings that persist across dependency version changes from the new full scan. This takes up to one additional full-scan fetch. A failed fetch or a missing package in that response triggers the infrastructure-error exit code.
673673
- **PR/MR comments**: Existing alerts are listed in their own table after the new ones, blocking first, with license violations grouped per package. `@SocketSecurity ignore` applies to them too. Very large sets are cut off before the comment reaches GitHub's size limit, with a link to the full report. Ignoring every displayed alert preserves the warning about omitted findings; `ignore-all` also clears omitted findings.
674674
- **Priority**: `--disable-blocking` takes precedence - if both flags are set, the build will always pass.
675675
- **First Scan**: On the very first scan of a repository, there are no "existing" violations, so behavior is identical to standard mode.

‎docs/troubleshooting.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Flags add:
123123
| `--reach` | `POST orgs/{org}/upload-manifest-files`, `POST tier1-reachability-scan/finalize` | `full-scans:create` |
124124
| `--generate-license` | `POST orgs/{org}/purl` | `packages:list` |
125125
| `--enable-json`, `--enable-sarif`, `--enable-gitlab-security` or `--generate-license` on a full scan | `GET orgs/{org}/full-scans/{id}` (scan contents) | `full-scans:list` |
126-
| `--strict-blocking`, `--enable-gitlab-security` or `--legal-format fossa` on a run that compares scans | `GET orgs/{org}/full-scans/{id}` (alerts on unchanged packages) | `full-scans:list` |
126+
| `--strict-blocking`, `--enable-gitlab-security` or `--legal-format fossa` on a run that compares scans | `GET orgs/{org}/full-scans/{id}` (existing alerts) | `full-scans:list` |
127127

128128
`--reach` also runs the analysis engine with the same token. The engine reports to the
129129
`tier1-reachability-scan/*` endpoints, which check `full-scans:create`.

‎socketsecurity/core/__init__.py‎

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,7 @@ def create_new_diff(
24002400
# Resolve the baseline scan: --base-scan-id / --base-commit-sha override
24012401
# the repository's head scan (None only when the repo has no head scan yet).
24022402
head_full_scan_id = self.resolve_base_full_scan_id(params)
2403+
has_baseline = head_full_scan_id is not None
24032404

24042405
# If no head scan exists, create an empty baseline scan
24052406
if head_full_scan_id is None:
@@ -2499,10 +2500,25 @@ def create_new_diff(
24992500
)
25002501

25012502
unchanged_packages = Core.get_unchanged_packages(packages, added_packages, removed_packages)
2502-
if unchanged_packages and self._requires_unchanged_alerts():
2503-
self.add_existing_alerts_from_full_scan(unchanged_packages, new_full_scan.id)
2503+
existing_changed_packages = {}
2504+
if has_baseline and getattr(self.cli_config, "strict_blocking", False):
2505+
# Keep the comparison's alerts intact for new-finding classification.
2506+
existing_changed_packages = {
2507+
package_id: copy.copy(package) for package_id, package in added_packages.items()
2508+
}
2509+
if self._requires_unchanged_alerts():
2510+
self.add_existing_alerts_from_full_scan(
2511+
unchanged_packages,
2512+
new_full_scan.id,
2513+
changed_packages=existing_changed_packages,
2514+
)
25042515

2505-
diff = self.create_diff_report(added_packages, removed_packages, unchanged_packages)
2516+
diff = self.create_diff_report(
2517+
added_packages,
2518+
removed_packages,
2519+
unchanged_packages,
2520+
existing_changed_packages=existing_changed_packages,
2521+
)
25062522
diff.packages = packages
25072523

25082524
base_socket = "https://socket.dev/dashboard/org"
@@ -2542,25 +2558,32 @@ def _requires_unchanged_alerts(self) -> bool:
25422558
)
25432559

25442560
def add_existing_alerts_from_full_scan(
2545-
self, unchanged_packages: Dict[str, Package], full_scan_id: str
2561+
self,
2562+
unchanged_packages: Dict[str, Package],
2563+
full_scan_id: str,
2564+
*,
2565+
changed_packages: Optional[Dict[str, Package]] = None,
25462566
) -> None:
2547-
"""Fill in the alerts on unchanged packages from the new full scan.
2567+
"""Load alerts for unchanged and optionally changed packages from the new full scan.
25482568
2549-
The cached diff-scans endpoint returns unchanged artifacts with an empty
2550-
``alerts`` list, because it keeps only the alerts it counts as new. The full
2551-
scan of the same commit carries every package's alerts with their policy
2552-
actions, so they are read from there. Packages are matched by artifact ID,
2553-
then by ecosystem, namespace, name and version.
2569+
The cached comparison keeps only new alerts, so it omits alerts that
2570+
persist across a version change as well as alerts on unchanged packages.
2571+
The full scan carries current alerts with their policy actions. Match
2572+
packages by artifact ID, then by ecosystem, namespace, name and version.
25542573
2555-
Skipped when any unchanged package already has alerts: the comparison then
2556-
came from an endpoint that includes them, such as the streaming fallback.
2557-
A failed fetch or an unmatched package raises, because reporting existing
2558-
alerts as absent would let ``--strict-blocking`` pass a run it should fail.
2574+
The streaming fallback can supply unchanged alerts itself. Changed
2575+
packages, when requested, always need the full scan. A failed fetch or
2576+
unmatched package raises so strict blocking can evaluate all existing alerts.
25592577
"""
2578+
packages_to_load = dict(changed_packages or {})
25602579
if any(package.alerts for package in unchanged_packages.values()):
25612580
log.debug("The comparison already includes alerts on unchanged packages")
2581+
else:
2582+
packages_to_load.update(unchanged_packages)
2583+
if not packages_to_load:
25622584
return
25632585

2586+
package_kind = "current" if changed_packages else "unchanged"
25642587
start = time.time()
25652588
by_id: Dict[str, List[dict]] = {}
25662589
by_identity: Dict[Tuple[str, str, str, str], List[dict]] = {}
@@ -2574,7 +2597,7 @@ def add_existing_alerts_from_full_scan(
25742597

25752598
matched = 0
25762599
alert_count = 0
2577-
for package in unchanged_packages.values():
2600+
for package in packages_to_load.values():
25782601
alerts = by_id.get(package.id)
25792602
if alerts is None:
25802603
alerts = by_identity.get(Core._package_identity(
@@ -2587,12 +2610,12 @@ def add_existing_alerts_from_full_scan(
25872610
alert_count += len(alerts)
25882611

25892612
log.info(
2590-
f"Loaded {alert_count} alerts for {matched} of {len(unchanged_packages)} "
2591-
f"unchanged packages from full scan {full_scan_id} in {time.time() - start:.2f}s"
2613+
f"Loaded {alert_count} alerts for {matched} of {len(packages_to_load)} "
2614+
f"{package_kind} packages from full scan {full_scan_id} in {time.time() - start:.2f}s"
25922615
)
2593-
if matched < len(unchanged_packages):
2616+
if matched < len(packages_to_load):
25942617
raise InfrastructureError(
2595-
f"{len(unchanged_packages) - matched} of {len(unchanged_packages)} unchanged packages "
2618+
f"{len(packages_to_load) - matched} of {len(packages_to_load)} {package_kind} packages "
25962619
f"were not found in full scan {full_scan_id}; unable to evaluate all existing alerts."
25972620
)
25982621

@@ -2628,7 +2651,9 @@ def create_diff_report(
26282651
added_packages: Dict[str, Package],
26292652
removed_packages: Dict[str, Package],
26302653
unchanged_packages: Optional[Dict[str, Package]] = None,
2631-
direct_only: bool = True
2654+
direct_only: bool = True,
2655+
*,
2656+
existing_changed_packages: Optional[Dict[str, Package]] = None,
26322657
) -> Diff:
26332658
"""
26342659
Creates a diff report comparing two sets of packages.
@@ -2637,14 +2662,15 @@ def create_diff_report(
26372662
1. Records new/removed packages (direct only by default)
26382663
2. Collects alerts from both sets of packages
26392664
3. Determines new capabilities introduced
2640-
4. Optionally collects alerts from unchanged packages for --strict-blocking
2665+
4. Optionally collects existing alerts for --strict-blocking
26412666
26422667
Args:
26432668
added_packages: Dict of packages added in new scan
26442669
removed_packages: Dict of packages removed in new scan
26452670
unchanged_packages: Dict of packages that didn't change (for --strict-blocking)
26462671
direct_only: If True, only direct dependencies are included in new/removed lists
26472672
(but alerts are still processed for all packages)
2673+
existing_changed_packages: Added/updated packages with alerts from the new full scan
26482674
26492675
Returns:
26502676
Diff object containing the comparison results
@@ -2712,10 +2738,25 @@ def create_diff_report(
27122738
alerts_in_removed_packages
27132739
)
27142740

2715-
# Get unchanged alerts (for --strict-blocking mode)
2741+
# Existing findings can also persist on a changed version.
27162742
diff.unchanged_alerts = Core.get_unchanged_alerts(
27172743
alerts_in_unchanged_packages
27182744
)
2745+
if existing_changed_packages:
2746+
current_changed_alerts: Dict[str, List[Issue]] = {}
2747+
for package in existing_changed_packages.values():
2748+
self.add_package_alerts_to_collection(
2749+
package=package,
2750+
alerts_collection=current_changed_alerts,
2751+
packages=added_packages,
2752+
)
2753+
reported_findings = {
2754+
alert.consolidation_key() for alert in (*diff.new_alerts, *diff.unchanged_alerts)
2755+
}
2756+
diff.unchanged_alerts.extend(
2757+
alert for alert in Core.get_unchanged_alerts(current_changed_alerts)
2758+
if alert.consolidation_key() not in reported_findings
2759+
)
27192760

27202761
# Get removed alerts (for completeness)
27212762
diff.removed_alerts = Core.get_removed_alerts(

‎socketsecurity/core/messages.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,7 @@ def security_comment_template(diff: Diff, config=None) -> str:
11831183

11841184
@staticmethod
11851185
def existing_comment_alerts(diff: Diff, config) -> list:
1186-
"""Alerts on unchanged packages to show in the PR comment.
1186+
"""Existing alerts to show in the PR comment.
11871187
11881188
Only under --strict-blocking, where they fail the run: without it they
11891189
don't affect the result, and listing them would bury the new ones.
@@ -1200,7 +1200,7 @@ def existing_comment_alerts(diff: Diff, config) -> list:
12001200
def existing_alerts_section(
12011201
alerts: list, diff: Diff, config, show_ignore: bool, view_report_url: str, size_so_far: int
12021202
) -> str:
1203-
"""Table of alerts on packages this change didn't touch, blocking ones first.
1203+
"""Table of existing alerts, with blocking alerts first.
12041204
12051205
License policy violations are grouped per package, as in the table of new
12061206
alerts.
@@ -1221,7 +1221,7 @@ def existing_alerts_section(
12211221
blocking = sum(1 for alert in alerts if alert.error)
12221222
section = f"""
12231223
<!-- start-socket-existing-alerts -->
1224-
**Existing alerts ({len(alerts)}).** These are on dependencies this change didn't add or update. `--strict-blocking` fails the run on the {blocking} with a **Block** action.
1224+
**Existing alerts ({len(alerts)}).** The baseline also contains these findings. `--strict-blocking` fails the run on the {blocking} with a **Block** action.
12251225
12261226
<table>
12271227
<thead>

0 commit comments

Comments
 (0)