Skip to content

Commit 72c53fc

Browse files
committed
Don't claim existing alerts fail the run under --disable-blocking
1 parent 5086da4 commit 72c53fc

3 files changed

Lines changed: 112 additions & 4 deletions

File tree

‎socketsecurity/core/messages.py‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,10 +1137,9 @@ def security_comment_template(diff: Diff, config=None) -> str:
11371137
"""
11381138
existing_header = existing_footer = ""
11391139
if existing_alerts:
1140-
blocking = sum(1 for alert in existing_alerts if alert.error)
11411140
existing_header = f"""
11421141
<!-- start-socket-existing-alerts -->
1143-
**Existing alerts ({len(existing_alerts)}).** The baseline also contains these findings. `--strict-blocking` fails the run on the {blocking} with a **Block** action.
1142+
**Existing alerts ({len(existing_alerts)}).** The baseline also contains these findings. {Messages.existing_alerts_effect(existing_alerts, config)}
11441143
11451144
{table_open}"""
11461145
existing_footer = table_close + "<!-- end-socket-existing-alerts -->\n"
@@ -1194,6 +1193,24 @@ def omitted_comment_alerts(section: str, count: int) -> str:
11941193
"to keep this comment within GitHub's size limit.\n"
11951194
)
11961195

1196+
@staticmethod
1197+
def existing_alerts_effect(existing_alerts: list, config) -> str:
1198+
"""Say whether the existing alerts fail this run.
1199+
1200+
Under ``--disable-blocking`` they are listed but don't change the exit code,
1201+
so the comment must not claim they fail the run.
1202+
"""
1203+
blocking = sum(1 for alert in existing_alerts if alert.error)
1204+
if not blocking:
1205+
return "None has a **Block** action, so they don't fail the run."
1206+
if getattr(config, "disable_blocking", False):
1207+
verb = "has" if blocking == 1 else "have"
1208+
return (
1209+
f"{blocking} {verb} a **Block** action but don't fail the run, "
1210+
"because `--disable-blocking` is set."
1211+
)
1212+
return f"`--strict-blocking` fails the run on the {blocking} with a **Block** action."
1213+
11971214
@staticmethod
11981215
def existing_comment_alerts(diff: Diff, config) -> list:
11991216
"""Existing alerts to show in the PR comment.

‎socketsecurity/output.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,12 @@ def build_summary_text(self, diff_report: Diff) -> str:
276276
if new_warning > 0:
277277
lines.append(f" - {warning_label}: {new_warning}")
278278
if unchanged_blocking > 0:
279-
lines.append(
280-
f" - EXISTING blocking issues: {unchanged_blocking} (causing failure due to --strict-blocking)"
279+
reason = (
280+
"not failing the run: --disable-blocking is set"
281+
if getattr(self.config, "disable_blocking", False)
282+
else "causing failure due to --strict-blocking"
281283
)
284+
lines.append(f" - EXISTING blocking issues: {unchanged_blocking} ({reason})")
282285
if unchanged_warning > 0:
283286
lines.append(f" - EXISTING warning issues: {unchanged_warning}")
284287
if is_full_scan:
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"""Existing alerts under ``--strict-blocking --disable-blocking`` are listed, not failed on.
2+
3+
Report-only pipelines run with both flags so the PR comment and console show existing
4+
alerts. Neither may claim those alerts fail the run when the exit code stays 0.
5+
"""
6+
from types import SimpleNamespace
7+
from unittest.mock import Mock
8+
9+
import pytest
10+
11+
from socketsecurity.config import CliConfig
12+
from socketsecurity.core.classes import Diff, Issue
13+
from socketsecurity.core.messages import Messages
14+
from socketsecurity.output import OutputHandler
15+
16+
17+
def _issue(name, error=True, warn=False):
18+
return Issue(
19+
pkg_type="npm",
20+
pkg_name=name,
21+
pkg_version="1.0.0",
22+
purl=f"{name}@1.0.0",
23+
type="malware",
24+
severity="critical",
25+
title="Known malware",
26+
description="Bad.",
27+
suggestion="Remove it.",
28+
url=f"https://socket.dev/npm/package/{name}",
29+
props={},
30+
key=f"key-{name}",
31+
error=error,
32+
warn=warn,
33+
)
34+
35+
36+
def _diff(existing):
37+
diff = Diff()
38+
diff.new_alerts = []
39+
diff.unchanged_alerts = list(existing)
40+
diff.diff_url = "https://socket.dev/diff"
41+
diff.report_url = "https://socket.dev/report"
42+
return diff
43+
44+
45+
def _comment(existing, disable_blocking):
46+
config = SimpleNamespace(
47+
strict_blocking=True, disable_blocking=disable_blocking, disable_ignore=False, scm="github"
48+
)
49+
return Messages.security_comment_template(_diff(existing), config)
50+
51+
52+
def _summary(existing, disable_blocking):
53+
config = Mock(spec=CliConfig)
54+
config.strict_blocking = True
55+
config.disable_blocking = disable_blocking
56+
return OutputHandler(config, Mock()).build_summary_text(_diff(existing))
57+
58+
59+
def test_comment_says_blocking_existing_alerts_fail_the_run():
60+
comment = _comment([_issue("delta"), _issue("gamma", error=False, warn=True)], disable_blocking=False)
61+
62+
assert "`--strict-blocking` fails the run on the 1 with a **Block** action." in comment
63+
64+
65+
@pytest.mark.parametrize("count, verb", [(1, "has"), (2, "have")])
66+
def test_comment_does_not_claim_a_failure_under_disable_blocking(count, verb):
67+
comment = _comment([_issue(f"pkg{i}") for i in range(count)], disable_blocking=True)
68+
69+
assert "fails the run" not in comment
70+
assert f"{count} {verb} a **Block** action but don't fail the run" in comment
71+
assert "`--disable-blocking` is set" in comment
72+
73+
74+
def test_comment_with_only_warnings_says_they_do_not_fail_the_run():
75+
comment = _comment([_issue("gamma", error=False, warn=True)], disable_blocking=False)
76+
77+
assert "None has a **Block** action, so they don't fail the run." in comment
78+
assert "fails the run on" not in comment
79+
80+
81+
@pytest.mark.parametrize("disable_blocking, reason", [
82+
(False, "causing failure due to --strict-blocking"),
83+
(True, "not failing the run: --disable-blocking is set"),
84+
])
85+
def test_console_summary_states_whether_existing_blockers_fail_the_run(disable_blocking, reason):
86+
summary = _summary([_issue("delta")], disable_blocking)
87+
88+
assert f"EXISTING blocking issues: 1 ({reason})" in summary

0 commit comments

Comments
 (0)