Skip to content

Commit 3d1b16f

Browse files
committed
Log the strict-blocking comparison only on runs that compare
The no-manifest message was logged when the mode was chosen, before the SCM event type was known. Skipped pull request events (closed, for example) printed that every manifest would be compared and then exited without a scan, and SCM branch pipelines printed it before creating a full scan. Log it after the event check, only when the run goes on to compare. Document the wider reach of the change: without --scm, default-branch pushes and non-git target paths now compare under --strict-blocking and can fail on existing blocking alerts. Note that --reach pull request scans need a default-branch scan made with --reach, and reword the API Mode line. Add tests for GitLab merge request and branch pipelines, an API-mode default-branch push, and the log message. The branch pipeline tests now return a blocking alert from the full scan, so they fail if blocking were left on.
1 parent f19dc3f commit 3d1b16f

4 files changed

Lines changed: 173 additions & 23 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
## 2.13.1
44

5+
### Upgrade notes
6+
7+
- Under `--strict-blocking`, runs without `--scm` now compare against the head
8+
scan even when no manifest changed, and fail on existing blocking alerts. That
9+
covers default-branch pushes in Bitbucket, Azure Pipelines, Jenkins and other
10+
pipelines that don't set `--scm`, and target paths that aren't git
11+
repositories. Those runs used to pass with a full scan. `--ignore-commit-files`
12+
already behaved this way.
13+
514
### Fixed
615

716
- `--strict-blocking` fails on existing blocking alerts when the change touches no

‎docs/cli-reference.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,8 @@ Security issues detected by Socket Security:
652652
### Important Notes
653653
654654
- **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).
655-
- **No manifest changed**: When the changed files include no supported manifest, the CLI still runs the comparison over every manifest in the target path, as with `--ignore-commit-files`, instead of falling back to a full scan. Code-only pull requests fail on existing blocking alerts and list them in the PR/MR comment. This adds the comparison and existing-alert requests to those runs.
655+
- **No manifest changed**: When the changed files include no supported manifest, the CLI still runs the comparison over every manifest in the target path, as with `--ignore-commit-files`, instead of falling back to a full scan. Code-only pull requests fail on existing blocking alerts and list them in the PR/MR comment. Without `--scm`, this also applies to default-branch pushes and to target paths that aren't git repositories, so those runs fail on existing blocking alerts where they used to pass. This adds the comparison and existing-alert requests to those runs.
656+
- **With `--reach`**: The comparison uses the last default-branch scan made with `--reach` as its baseline. If the default branch is scanned without `--reach`, the baseline is empty and every alert is reported as new. The exit code is the same under `--strict-blocking`, but the PR/MR comment says the change introduced them. Scan the default branch with `--reach` too.
656657
- **Error-Level Only**: Only fails on `error=True` alerts (blocking severity), not warnings.
657658
- **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.
658659
- **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.
@@ -763,7 +764,7 @@ The CLI determines which files to scan based on the following logic:
763764
### Scanning Modes
764765
765766
- **Differential Mode**: When manifest files are detected in changes, performs a diff scan with PR/MR comment integration
766-
- **API Mode**: When no manifest files are in changes, creates a full scan report without PR comments but still scans the entire repository. Not used with `--strict-blocking`
767+
- **API Mode**: When no manifest files are in changes, creates a full scan report without PR comments but still scans the entire repository. With `--strict-blocking`, the CLI runs a comparison instead (see Automatic Fallback above); SCM branch pipelines still create a full scan
767768
- **Force Mode**: With `--ignore-commit-files`, always runs a comparison regardless of which files changed, over every manifest in the target path
768769
- **Forced Diff Mode**: With `--enable-diff`, forces differential mode even when using `--integration api` (without SCM integration)
769770

‎socketsecurity/socketcli.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,12 +663,10 @@ def main_code(config: Optional[CliConfig] = None):
663663
# against the baseline reports them. A change that touches no manifest would
664664
# otherwise get a full scan with blocking turned off and pass every time, so
665665
# run the comparison over the whole target path, as --ignore-commit-files does.
666+
strict_forced_comparison = False
666667
if config.strict_blocking and not has_supported_files and not force_diff_mode:
667668
force_diff_mode = True
668-
log.info(
669-
"No supported manifest in the changed files; --strict-blocking compares "
670-
"every manifest in the target path against the baseline to check existing alerts"
671-
)
669+
strict_forced_comparison = True
672670

673671
# Case 3: If no supported files or files are empty, force API mode (no PR comments)
674672
# BUT: Don't force API mode if we're in force_diff_mode
@@ -773,6 +771,14 @@ def _is_unprocessed(c):
773771
return True
774772

775773
scm_event_type = scm.check_event_type() if scm is not None else None
774+
# Logged only once the run is known to compare: skipped SCM events exit inside
775+
# check_event_type, comment events don't scan, and SCM branch pipelines still
776+
# create a full scan.
777+
if strict_forced_comparison and scm_event_type in (None, "diff"):
778+
log.info(
779+
"No supported manifest in the changed files; --strict-blocking compares "
780+
"every manifest in the target path against the baseline to check existing alerts"
781+
)
776782
# Every branch below except the SCM full-scan one produces a comparison, or
777783
# is already covered by force_api_mode. See the blocking guard after the
778784
# scan for why this is tracked.

‎tests/core/test_strict_blocking_no_manifest_change.py‎

Lines changed: 151 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@
1919
from socketsecurity.core import Core
2020
from socketsecurity.core.classes import Diff, Issue, Package
2121
from socketsecurity.core.scm.github import Github
22+
from socketsecurity.core.scm.gitlab import Gitlab
2223

2324
TOKEN_VARS = (
24-
"SOCKET_SECURITY_API_KEY", "SOCKET_SECURITY_API_TOKEN", "SOCKET_API_KEY", "SOCKET_API_TOKEN",
25-
"GH_API_TOKEN", "GITLAB_TOKEN",
25+
"SOCKET_SECURITY_API_KEY",
26+
"SOCKET_SECURITY_API_TOKEN",
27+
"SOCKET_API_KEY",
28+
"SOCKET_API_TOKEN",
29+
"GH_API_TOKEN",
30+
"GITLAB_TOKEN",
31+
"CI_JOB_TOKEN",
32+
"CI_MERGE_REQUEST_IID",
2633
)
34+
STRICT_COMPARISON_LOG = "--strict-blocking compares every manifest in the target path"
2735
POLICIES = [
2836
pytest.param([], 3, id="default"),
2937
pytest.param(["--exit-code-on-api-error", "100"], 100, id="remap"),
@@ -69,8 +77,12 @@ def cli_runtime(monkeypatch):
6977
sdk = MagicMock()
7078
monkeypatch.setattr(socketcli, "build_socket_sdk", Mock(return_value=sdk))
7179
monkeypatch.setattr(socketcli, "CliClient", Mock())
72-
monkeypatch.setattr(socketcli, "StreamingLogs", lambda **kwargs: nullcontext(Mock()))
73-
git_repo = SimpleNamespace(changed_files=["README.md"], is_default_branch=False, remote_url=None)
80+
monkeypatch.setattr(
81+
socketcli, "StreamingLogs", lambda **kwargs: nullcontext(Mock())
82+
)
83+
git_repo = SimpleNamespace(
84+
changed_files=["README.md"], is_default_branch=False, remote_url=None
85+
)
7486
in_git_repo = {"value": True}
7587

7688
def apply_git_context(config):
@@ -83,7 +95,9 @@ def apply_git_context(config):
8395
monkeypatch.setattr(
8496
Core,
8597
"has_manifest_files",
86-
lambda self, files: any(f.endswith(("package.json", "package-lock.json")) for f in files),
98+
lambda self, files: any(
99+
f.endswith(("package.json", "package-lock.json")) for f in files
100+
),
87101
)
88102
monkeypatch.setattr(Core, "find_files", Mock(return_value=["package.json"]))
89103
diff = Diff(id="compared")
@@ -96,16 +110,29 @@ def apply_git_context(config):
96110
def run(flags=(), *, git=True):
97111
in_git_repo["value"] = git
98112
argv = [
99-
"socketcli", "--api-token", "test", "--repo", "repo", "--branch", "feature",
100-
"--integration", "api", *flags,
113+
"socketcli",
114+
"--api-token",
115+
"test",
116+
"--repo",
117+
"repo",
118+
"--branch",
119+
"feature",
120+
"--integration",
121+
"api",
122+
*flags,
101123
]
102124
monkeypatch.setattr(sys, "argv", argv)
103125
with pytest.raises(SystemExit) as result:
104126
socketcli.cli()
105127
return result.value.code
106128

107129
return SimpleNamespace(
108-
sdk=sdk, git_repo=git_repo, diff=diff, compare=compare, full_scan=full_scan, run=run,
130+
sdk=sdk,
131+
git_repo=git_repo,
132+
diff=diff,
133+
compare=compare,
134+
full_scan=full_scan,
135+
run=run,
109136
)
110137

111138

@@ -130,12 +157,17 @@ def test_no_existing_blocker_passes_a_change_without_manifests(cli_runtime, mode
130157
def test_disable_blocking_still_wins(cli_runtime):
131158
cli_runtime.diff.unchanged_alerts = [_existing_blocker()]
132159

133-
assert cli_runtime.run(["--enable-diff", "--strict-blocking", "--disable-blocking"]) == 0
160+
assert (
161+
cli_runtime.run(["--enable-diff", "--strict-blocking", "--disable-blocking"])
162+
== 0
163+
)
134164
cli_runtime.compare.assert_called_once()
135165

136166

137167
@pytest.mark.parametrize("mode, git", MODES)
138-
def test_without_strict_blocking_the_full_scan_fallback_is_unchanged(cli_runtime, mode, git):
168+
def test_without_strict_blocking_the_full_scan_fallback_is_unchanged(
169+
cli_runtime, mode, git
170+
):
139171
cli_runtime.full_scan.return_value = Diff(
140172
id="full", report_url="", diff_url="", new_alerts=[_existing_blocker()]
141173
)
@@ -165,9 +197,15 @@ def test_changed_manifest_still_compares(cli_runtime):
165197

166198

167199
@pytest.mark.parametrize("policy, infrastructure_code", POLICIES)
168-
def test_unreadable_existing_alerts_use_the_infrastructure_code(cli_runtime, policy, infrastructure_code):
169-
package = Package(id="pkg", type="npm", name="pkg", version="1", score={}, alerts=[])
170-
cli_runtime.sdk.fullscans.stream.return_value = SimpleNamespace(success=True, artifacts={})
200+
def test_unreadable_existing_alerts_use_the_infrastructure_code(
201+
cli_runtime, policy, infrastructure_code
202+
):
203+
package = Package(
204+
id="pkg", type="npm", name="pkg", version="1", score={}, alerts=[]
205+
)
206+
cli_runtime.sdk.fullscans.stream.return_value = SimpleNamespace(
207+
success=True, artifacts={}
208+
)
171209

172210
def compare(*args, **kwargs):
173211
core = Core.__new__(Core)
@@ -177,7 +215,10 @@ def compare(*args, **kwargs):
177215

178216
cli_runtime.compare.side_effect = compare
179217

180-
assert cli_runtime.run(["--enable-diff", "--strict-blocking", *policy]) == infrastructure_code
218+
assert (
219+
cli_runtime.run(["--enable-diff", "--strict-blocking", *policy])
220+
== infrastructure_code
221+
)
181222

182223

183224
@pytest.fixture
@@ -193,7 +234,9 @@ def github_pull_request(cli_runtime, monkeypatch):
193234
return add_comments
194235

195236

196-
def test_code_only_pull_request_comments_on_existing_blockers(cli_runtime, github_pull_request):
237+
def test_code_only_pull_request_comments_on_existing_blockers(
238+
cli_runtime, github_pull_request
239+
):
197240
cli_runtime.diff.unchanged_alerts = [_existing_blocker()]
198241

199242
assert cli_runtime.run(["--scm", "github", "--strict-blocking"]) == 1
@@ -214,13 +257,104 @@ def test_code_only_pull_request_without_strict_blocking_skips_the_comment(
214257
github_pull_request.assert_not_called()
215258

216259

217-
def test_branch_pipeline_still_creates_a_full_scan(cli_runtime, monkeypatch):
260+
def test_branch_pipeline_still_creates_a_full_scan(cli_runtime, monkeypatch, caplog):
218261
# Non-PR SCM pipelines have no baseline; --strict-blocking does not change that.
262+
# The full scan carries a blocking alert, so exit 0 proves blocking was turned off.
219263
monkeypatch.setenv("GH_API_TOKEN", "test")
220264
monkeypatch.setenv("GITHUB_EVENT_NAME", "push")
221265
monkeypatch.setenv("GITHUB_REPOSITORY", "acme/repo")
222266
monkeypatch.delenv("PR_NUMBER", raising=False)
267+
cli_runtime.full_scan.return_value = Diff(
268+
id="full", report_url="", diff_url="", new_alerts=[_existing_blocker()]
269+
)
270+
271+
assert (
272+
cli_runtime.run(["--scm", "github", "--pr-number", "0", "--strict-blocking"])
273+
== 0
274+
)
275+
cli_runtime.full_scan.assert_called_once()
276+
cli_runtime.compare.assert_not_called()
277+
assert STRICT_COMPARISON_LOG not in caplog.text
278+
279+
280+
def test_api_mode_default_branch_push_compares(cli_runtime):
281+
# Without --scm there is no event type, so a default-branch push with no manifest
282+
# change runs the comparison too, and existing blocking alerts fail it.
283+
cli_runtime.git_repo.is_default_branch = True
284+
cli_runtime.diff.unchanged_alerts = [_existing_blocker()]
285+
286+
assert cli_runtime.run(["--default-branch", "--strict-blocking"]) == 1
287+
cli_runtime.compare.assert_called_once()
288+
assert cli_runtime.compare.call_args.args[1].make_default_branch is True
289+
cli_runtime.full_scan.assert_not_called()
290+
291+
292+
def test_comparison_is_logged_when_it_runs(cli_runtime, caplog):
293+
cli_runtime.diff.unchanged_alerts = [_existing_blocker()]
294+
295+
cli_runtime.run(["--strict-blocking"])
296+
297+
assert STRICT_COMPARISON_LOG in caplog.text
298+
299+
300+
def test_skipped_pull_request_event_logs_no_comparison(
301+
cli_runtime, monkeypatch, caplog
302+
):
303+
# A skipped event exits before any scan, so the comparison message must not appear.
304+
monkeypatch.setenv("GH_API_TOKEN", "test")
305+
monkeypatch.setenv("GITHUB_EVENT_NAME", "pull_request")
306+
monkeypatch.setenv("EVENT_ACTION", "closed")
307+
monkeypatch.setenv("GITHUB_REPOSITORY", "acme/repo")
308+
monkeypatch.setenv("PR_NUMBER", "7")
309+
310+
assert cli_runtime.run(["--scm", "github", "--strict-blocking"]) == 0
311+
cli_runtime.compare.assert_not_called()
312+
cli_runtime.full_scan.assert_not_called()
313+
assert STRICT_COMPARISON_LOG not in caplog.text
314+
315+
316+
@pytest.fixture
317+
def gitlab_pipeline(cli_runtime, monkeypatch):
318+
monkeypatch.setenv("GITLAB_TOKEN", "glpat-test")
319+
monkeypatch.setenv("CI_PROJECT_NAME", "repo")
320+
monkeypatch.setenv("CI_DEFAULT_BRANCH", "main")
321+
monkeypatch.setenv("CI_API_V4_URL", "https://gitlab.example.com/api/v4")
322+
monkeypatch.setenv("CI_PROJECT_URL", "https://gitlab.example.com/acme/repo")
323+
monkeypatch.setattr(Gitlab, "get_comments_for_pr", Mock(return_value={}))
324+
add_comments = Mock()
325+
monkeypatch.setattr(Gitlab, "add_socket_comments", add_comments)
326+
return add_comments
327+
328+
329+
def test_code_only_merge_request_comments_on_existing_blockers(
330+
cli_runtime, gitlab_pipeline, monkeypatch
331+
):
332+
monkeypatch.setenv("CI_PIPELINE_SOURCE", "merge_request_event")
333+
monkeypatch.setenv("CI_MERGE_REQUEST_IID", "7")
334+
monkeypatch.setenv("CI_MERGE_REQUEST_PROJECT_ID", "42")
335+
monkeypatch.setenv("CI_MERGE_REQUEST_SOURCE_BRANCH_NAME", "feature")
336+
cli_runtime.diff.unchanged_alerts = [_existing_blocker()]
337+
338+
assert cli_runtime.run(["--scm", "gitlab", "--strict-blocking"]) == 1
339+
cli_runtime.compare.assert_called_once()
340+
cli_runtime.full_scan.assert_not_called()
341+
gitlab_pipeline.assert_called_once()
342+
security_comment, _, _, post_security, _ = gitlab_pipeline.call_args.args
343+
assert post_security is True
344+
assert "handlebars@4.0.11" in security_comment
345+
assert "Existing alerts" in security_comment
346+
347+
348+
def test_gitlab_branch_pipeline_still_creates_a_full_scan(
349+
cli_runtime, gitlab_pipeline, monkeypatch, caplog
350+
):
351+
monkeypatch.setenv("CI_PIPELINE_SOURCE", "push")
352+
cli_runtime.full_scan.return_value = Diff(
353+
id="full", report_url="", diff_url="", new_alerts=[_existing_blocker()]
354+
)
223355

224-
assert cli_runtime.run(["--scm", "github", "--pr-number", "0", "--strict-blocking"]) == 0
356+
assert cli_runtime.run(["--scm", "gitlab", "--strict-blocking"]) == 0
225357
cli_runtime.full_scan.assert_called_once()
226358
cli_runtime.compare.assert_not_called()
359+
gitlab_pipeline.assert_not_called()
360+
assert STRICT_COMPARISON_LOG not in caplog.text

0 commit comments

Comments
 (0)