From f19dc3faef73a4d61ce1e2398e4d14dc60723eff Mon Sep 17 00:00:00 2001 From: David Larsen Date: Fri, 20 Nov 2026 15:25:55 -0500 Subject: [PATCH 1/3] Check existing alerts under --strict-blocking when no manifest changed Without --ignore-commit-files, the CLI reads the changed files to decide whether to compare. When none is a supported manifest it sets force_api_mode, creates a full scan of the target path, and turns blocking off because a full scan has no baseline. That fallback exists so a change that cannot alter dependencies doesn't post a comment or block on alerts it didn't introduce. Under --strict-blocking it also skipped the check the flag exists for: a code-only pull request passed with a warning that --strict-blocking needs diff mode, even when the repository had existing blocking alerts. With --strict-blocking, a change that touches no manifest now runs the comparison over every manifest in the target path, the same way --ignore-commit-files does. Existing alerts are loaded from the new full scan and fail the run, and PR/MR comments list them. Failures reading them keep the infrastructure-error code. Runs without --strict-blocking keep the full-scan fallback, and SCM branch pipelines still create a full scan. --- CHANGELOG.md | 10 + docs/ci-cd.md | 4 +- docs/cli-reference.md | 7 +- pyproject.toml | 2 +- socketsecurity/__init__.py | 2 +- socketsecurity/config.py | 3 +- socketsecurity/socketcli.py | 18 +- ...test_strict_blocking_no_manifest_change.py | 226 ++++++++++++++++++ uv.lock | 2 +- 9 files changed, 263 insertions(+), 11 deletions(-) create mode 100644 tests/core/test_strict_blocking_no_manifest_change.py diff --git a/CHANGELOG.md b/CHANGELOG.md index ff28e72..0bb87e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## 2.13.1 + +### Fixed + +- `--strict-blocking` fails on existing blocking alerts when the change touches no + manifest. Without `--ignore-commit-files`, such a change used to get a full scan + with blocking turned off, so code-only pull requests passed. The comparison now + runs over every manifest in the target path, and PR/MR comments list the existing + alerts. + ## 2.13.0 ### Upgrade notes diff --git a/docs/ci-cd.md b/docs/ci-cd.md index 88210ed..82e9eaf 100644 --- a/docs/ci-cd.md +++ b/docs/ci-cd.md @@ -510,7 +510,9 @@ A full scan has no baseline, so it cannot tell a newly introduced alert from one that was already there. Rather than block on a number that would mean something different depending on which output format was enabled, those runs behave as if `--disable-blocking` was supplied and report through the Dashboard instead. This -matches how the CLI already treats a run with no supported manifest files. +matches how the CLI treats a change with no supported manifest files, except under +`--strict-blocking`: there a pull request or merge request that touches no manifest +still gets the comparison, so existing blocking alerts fail it. The event type is authoritative once `--scm` is set: `--enable-diff` and `--ignore-commit-files` do not turn a branch pipeline into a comparison. To diff diff --git a/docs/cli-reference.md b/docs/cli-reference.md index 066820d..78a1b47 100644 --- a/docs/cli-reference.md +++ b/docs/cli-reference.md @@ -450,7 +450,7 @@ The CLI runs a pinned version of the Coana CLI, the analysis engine; it does **n | `--disable-blocking` | False | False | Non-blocking CI mode: the CLI exits **0** even when blocking alerts are present (including with `--strict-blocking`). Also exits 0 on infrastructure errors (missing API or SCM token, Socket API failures, uncaught runtime errors) and interrupts, so the job is treated as successful while findings and errors are still logged. Invalid flags still exit **2**. Takes precedence over `--strict-blocking`. | | `--disable-ignore` | False | False | Disable support for `@SocketSecurity ignore` commands in PR comments. When set, alerts cannot be suppressed via comments and ignore instructions are hidden from comment output. See [Who can ignore an alert](#who-can-ignore-an-alert). | | `--ignore-authorization` | False | enforce | Who may suppress alerts with `@SocketSecurity ignore`. `enforce` requires write access and honors the command with a warning when the provider cannot report it; `strict` rejects it in that case; `off` honors any commenter. See [Who can ignore an alert](#who-can-ignore-an-alert). | -| `--strict-blocking` | False | False | Fail on ANY security policy violations (blocking severity), not just new ones. Only works in diff mode. See [Strict Blocking Mode](#strict-blocking-mode) for details. | +| `--strict-blocking` | False | False | Fail on ANY security policy violations (blocking severity), not just new ones. Runs a comparison even when no manifest changed. See [Strict Blocking Mode](#strict-blocking-mode) for details. | | `--enable-diff` | False | False | Enable diff mode even when using `--integration api` (forces diff mode without SCM integration) | | `--scm` | False | api | Source control management type | | `--timeout` | False | 1200 | Positive integer timeout in seconds for each API request. This is not a total CLI runtime limit and does not limit local discovery, Git, or reachability analysis. | @@ -652,6 +652,7 @@ Security issues detected by Socket Security: ### Important Notes - **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). +- **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. - **Error-Level Only**: Only fails on `error=True` alerts (blocking severity), not warnings. - **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. - **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. @@ -755,14 +756,14 @@ The CLI determines which files to scan based on the following logic: 3. **`--ignore-commit-files` Flag**: When set, the changed-file check is skipped entirely. The CLI runs the comparison regardless of what changed, and scans every manifest file in the target directory. -4. **Automatic Fallback**: If no manifest files are found in git commit changes and no `--files` are specified, the CLI automatically switches to "API mode" and performs a full repository scan. +4. **Automatic Fallback**: If no manifest files are found in git commit changes and no `--files` are specified, the CLI automatically switches to "API mode" and performs a full repository scan. With `--strict-blocking`, it runs the comparison instead, as `--ignore-commit-files` does, so existing blocking alerts are still checked. > **Important**: The CLI doesn't scan only the specified files - it uses them to determine whether a scan should be performed and what type of scan to run. When triggered, it searches the entire `--target-path` for all supported manifest files. ### Scanning Modes - **Differential Mode**: When manifest files are detected in changes, performs a diff scan with PR/MR comment integration -- **API Mode**: When no manifest files are in changes, creates a full scan report without PR comments but still scans the entire repository +- **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` - **Force Mode**: With `--ignore-commit-files`, always runs a comparison regardless of which files changed, over every manifest in the target path - **Forced Diff Mode**: With `--enable-diff`, forces differential mode even when using `--integration api` (without SCM integration) diff --git a/pyproject.toml b/pyproject.toml index 0924672..2ba92fb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "hatchling.build" [project] name = "socketsecurity" -version = "2.13.0" +version = "2.13.1" requires-python = ">= 3.11" license = {"file" = "LICENSE"} dependencies = [ diff --git a/socketsecurity/__init__.py b/socketsecurity/__init__.py index 08835ec..a3c8f69 100644 --- a/socketsecurity/__init__.py +++ b/socketsecurity/__init__.py @@ -1,4 +1,4 @@ __author__ = 'socket.dev' -__version__ = '2.13.0' +__version__ = '2.13.1' USER_AGENT_NAME = 'socket-python-cli' USER_AGENT = f'{USER_AGENT_NAME}/{__version__}' diff --git a/socketsecurity/config.py b/socketsecurity/config.py index a11ad25..d707fc4 100644 --- a/socketsecurity/config.py +++ b/socketsecurity/config.py @@ -1058,7 +1058,8 @@ def create_argument_parser() -> argparse.ArgumentParser: "--strict-blocking", dest="strict_blocking", action="store_true", - help="Fail on ANY security policy violations (blocking severity), not just new ones. Only works in diff mode." + help="Fail on ANY security policy violations (blocking severity), not just new ones. Runs a comparison even " + "when no manifest changed. SCM branch (non-PR/MR) pipelines create a full scan and do not block." ) advanced_group.add_argument( "--enable-diff", diff --git a/socketsecurity/socketcli.py b/socketsecurity/socketcli.py index 1a05a30..c24dffa 100644 --- a/socketsecurity/socketcli.py +++ b/socketsecurity/socketcli.py @@ -658,7 +658,18 @@ def main_code(config: Optional[CliConfig] = None): log.debug(f"Error finding files in scan paths: {e}") discovered_scan_files = None has_supported_files = False - + + # --strict-blocking fails on existing blocking alerts, and only a comparison + # against the baseline reports them. A change that touches no manifest would + # otherwise get a full scan with blocking turned off and pass every time, so + # run the comparison over the whole target path, as --ignore-commit-files does. + if config.strict_blocking and not has_supported_files and not force_diff_mode: + force_diff_mode = True + log.info( + "No supported manifest in the changed files; --strict-blocking compares " + "every manifest in the target path against the baseline to check existing alerts" + ) + # Case 3: If no supported files or files are empty, force API mode (no PR comments) # BUT: Don't force API mode if we're in force_diff_mode log.debug(f"files_to_check={files_to_check}, has_supported_files={has_supported_files}, force_diff_mode={force_diff_mode}, config.enable_diff={config.enable_diff}") @@ -979,8 +990,9 @@ def _is_unprocessed(c): output_handler.handle_output(diff) elif (config.enable_diff or force_diff_mode) and force_api_mode: - # User requested diff mode but no manifest files were detected - this should not happen with new logic - # but keeping as a safety net + # --enable-diff without --ignore-commit-files, and the change touched no + # manifest. --strict-blocking never reaches this branch: it sets + # force_diff_mode above so existing alerts are still evaluated. log.warning("--enable-diff was specified but no supported manifest files were detected in the changed files. Falling back to full scan mode.") _log_scan_mode_fallback( "diff", diff --git a/tests/core/test_strict_blocking_no_manifest_change.py b/tests/core/test_strict_blocking_no_manifest_change.py new file mode 100644 index 0000000..cc18e53 --- /dev/null +++ b/tests/core/test_strict_blocking_no_manifest_change.py @@ -0,0 +1,226 @@ +"""`--strict-blocking` on a change that touches no manifest. + +Without `--ignore-commit-files`, the CLI reads the changed files to decide whether +to compare. When none is a supported manifest it used to create a full scan, turn +blocking off and warn that `--strict-blocking` needs diff mode, so a code-only pull +request passed even with existing blocking alerts. Under `--strict-blocking` the +comparison now runs anyway, the way `--ignore-commit-files` runs it. Runs without +the flag keep the full-scan fallback. +""" + +import sys +from contextlib import nullcontext +from types import SimpleNamespace +from unittest.mock import MagicMock, Mock + +import pytest + +from socketsecurity import socketcli +from socketsecurity.core import Core +from socketsecurity.core.classes import Diff, Issue, Package +from socketsecurity.core.scm.github import Github + +TOKEN_VARS = ( + "SOCKET_SECURITY_API_KEY", "SOCKET_SECURITY_API_TOKEN", "SOCKET_API_KEY", "SOCKET_API_TOKEN", + "GH_API_TOKEN", "GITLAB_TOKEN", +) +POLICIES = [ + pytest.param([], 3, id="default"), + pytest.param(["--exit-code-on-api-error", "100"], 100, id="remap"), + pytest.param(["--disable-blocking"], 0, id="nonblocking"), +] +# Each mode reaches the no-manifest fallback by a different path. +MODES = [ + pytest.param(["--enable-diff"], True, id="enable-diff"), + pytest.param([], True, id="api-default"), + pytest.param(["--files", '["src/index.js"]'], True, id="files-without-manifest"), + pytest.param([], False, id="not-a-git-repo"), +] + + +def _existing_blocker() -> Issue: + return Issue( + pkg_name="handlebars", + pkg_version="4.0.11", + pkg_type="npm", + severity="critical", + title="Critical CVE", + description="Prototype pollution", + type="criticalCVE", + url="https://socket.dev/npm/package/handlebars/overview/4.0.11", + manifests="package-lock.json", + props={"ghsaId": "GHSA-w457-6q6x-cgp9"}, + key="existing-key", + purl="pkg:npm/handlebars@4.0.11", + error=True, + warn=False, + ignore=False, + monitor=False, + suggestion="Upgrade handlebars", + next_step_title="Next steps", + ) + + +@pytest.fixture +def cli_runtime(monkeypatch): + for name in TOKEN_VARS: + monkeypatch.delenv(name, raising=False) + monkeypatch.setenv("BUILDKITE", "false") + sdk = MagicMock() + monkeypatch.setattr(socketcli, "build_socket_sdk", Mock(return_value=sdk)) + monkeypatch.setattr(socketcli, "CliClient", Mock()) + monkeypatch.setattr(socketcli, "StreamingLogs", lambda **kwargs: nullcontext(Mock())) + git_repo = SimpleNamespace(changed_files=["README.md"], is_default_branch=False, remote_url=None) + in_git_repo = {"value": True} + + def apply_git_context(config): + if not in_git_repo["value"]: + config.ignore_commit_files = True + return False, None + return True, git_repo + + monkeypatch.setattr(socketcli, "apply_git_context", apply_git_context) + monkeypatch.setattr( + Core, + "has_manifest_files", + lambda self, files: any(f.endswith(("package.json", "package-lock.json")) for f in files), + ) + monkeypatch.setattr(Core, "find_files", Mock(return_value=["package.json"])) + diff = Diff(id="compared") + compare = Mock(return_value=diff) + full_scan = Mock(return_value=Diff(id="full", report_url="", diff_url="")) + monkeypatch.setattr(Core, "create_new_diff", compare) + monkeypatch.setattr(Core, "create_full_scan_with_report_url", full_scan) + monkeypatch.setattr(socketcli.OutputHandler, "handle_output", Mock()) + + def run(flags=(), *, git=True): + in_git_repo["value"] = git + argv = [ + "socketcli", "--api-token", "test", "--repo", "repo", "--branch", "feature", + "--integration", "api", *flags, + ] + monkeypatch.setattr(sys, "argv", argv) + with pytest.raises(SystemExit) as result: + socketcli.cli() + return result.value.code + + return SimpleNamespace( + sdk=sdk, git_repo=git_repo, diff=diff, compare=compare, full_scan=full_scan, run=run, + ) + + +@pytest.mark.parametrize("mode, git", MODES) +def test_existing_blocker_fails_a_change_without_manifests(cli_runtime, mode, git): + cli_runtime.diff.unchanged_alerts = [_existing_blocker()] + + assert cli_runtime.run([*mode, "--strict-blocking"], git=git) == 1 + cli_runtime.compare.assert_called_once() + cli_runtime.full_scan.assert_not_called() + + +@pytest.mark.parametrize("mode, git", MODES) +def test_no_existing_blocker_passes_a_change_without_manifests(cli_runtime, mode, git): + cli_runtime.diff.unchanged_alerts = [Issue(error=False, warn=True)] + + assert cli_runtime.run([*mode, "--strict-blocking"], git=git) == 0 + cli_runtime.compare.assert_called_once() + cli_runtime.full_scan.assert_not_called() + + +def test_disable_blocking_still_wins(cli_runtime): + cli_runtime.diff.unchanged_alerts = [_existing_blocker()] + + assert cli_runtime.run(["--enable-diff", "--strict-blocking", "--disable-blocking"]) == 0 + cli_runtime.compare.assert_called_once() + + +@pytest.mark.parametrize("mode, git", MODES) +def test_without_strict_blocking_the_full_scan_fallback_is_unchanged(cli_runtime, mode, git): + cli_runtime.full_scan.return_value = Diff( + id="full", report_url="", diff_url="", new_alerts=[_existing_blocker()] + ) + + assert cli_runtime.run(mode, git=git) == 0 + cli_runtime.full_scan.assert_called_once() + cli_runtime.compare.assert_not_called() + + +def test_no_fallback_warning_under_strict_blocking(cli_runtime, caplog): + cli_runtime.diff.unchanged_alerts = [_existing_blocker()] + + cli_runtime.run(["--enable-diff", "--strict-blocking"]) + + assert "Falling back to full scan mode" not in caplog.text + assert "only supported in diff mode" not in caplog.text + + +def test_changed_manifest_still_compares(cli_runtime): + cli_runtime.git_repo.changed_files = ["package.json"] + cli_runtime.diff.unchanged_alerts = [_existing_blocker()] + + assert cli_runtime.run(["--enable-diff", "--strict-blocking"]) == 1 + assert cli_runtime.run(["--enable-diff"]) == 0 + assert cli_runtime.compare.call_count == 2 + cli_runtime.full_scan.assert_not_called() + + +@pytest.mark.parametrize("policy, infrastructure_code", POLICIES) +def test_unreadable_existing_alerts_use_the_infrastructure_code(cli_runtime, policy, infrastructure_code): + package = Package(id="pkg", type="npm", name="pkg", version="1", score={}, alerts=[]) + cli_runtime.sdk.fullscans.stream.return_value = SimpleNamespace(success=True, artifacts={}) + + def compare(*args, **kwargs): + core = Core.__new__(Core) + core.sdk = cli_runtime.sdk + core.config = SimpleNamespace(org_slug="acme") + core.add_existing_alerts_from_full_scan({package.id: package}, "new") + + cli_runtime.compare.side_effect = compare + + assert cli_runtime.run(["--enable-diff", "--strict-blocking", *policy]) == infrastructure_code + + +@pytest.fixture +def github_pull_request(cli_runtime, monkeypatch): + monkeypatch.setenv("GH_API_TOKEN", "test") + monkeypatch.setenv("GITHUB_EVENT_NAME", "pull_request") + monkeypatch.setenv("EVENT_ACTION", "synchronize") + monkeypatch.setenv("GITHUB_REPOSITORY", "acme/repo") + monkeypatch.setenv("PR_NUMBER", "7") + monkeypatch.setattr(Github, "get_comments_for_pr", Mock(return_value={})) + add_comments = Mock() + monkeypatch.setattr(Github, "add_socket_comments", add_comments) + return add_comments + + +def test_code_only_pull_request_comments_on_existing_blockers(cli_runtime, github_pull_request): + cli_runtime.diff.unchanged_alerts = [_existing_blocker()] + + assert cli_runtime.run(["--scm", "github", "--strict-blocking"]) == 1 + cli_runtime.compare.assert_called_once() + github_pull_request.assert_called_once() + security_comment, _, _, post_security, _ = github_pull_request.call_args.args + assert post_security is True + assert "handlebars@4.0.11" in security_comment + assert "Existing alerts" in security_comment + + +def test_code_only_pull_request_without_strict_blocking_skips_the_comment( + cli_runtime, github_pull_request +): + assert cli_runtime.run(["--scm", "github"]) == 0 + cli_runtime.full_scan.assert_called_once() + cli_runtime.compare.assert_not_called() + github_pull_request.assert_not_called() + + +def test_branch_pipeline_still_creates_a_full_scan(cli_runtime, monkeypatch): + # Non-PR SCM pipelines have no baseline; --strict-blocking does not change that. + monkeypatch.setenv("GH_API_TOKEN", "test") + monkeypatch.setenv("GITHUB_EVENT_NAME", "push") + monkeypatch.setenv("GITHUB_REPOSITORY", "acme/repo") + monkeypatch.delenv("PR_NUMBER", raising=False) + + assert cli_runtime.run(["--scm", "github", "--pr-number", "0", "--strict-blocking"]) == 0 + cli_runtime.full_scan.assert_called_once() + cli_runtime.compare.assert_not_called() diff --git a/uv.lock b/uv.lock index a6271a3..aea1ca6 100644 --- a/uv.lock +++ b/uv.lock @@ -1334,7 +1334,7 @@ wheels = [ [[package]] name = "socketsecurity" -version = "2.13.0" +version = "2.13.1" source = { editable = "." } dependencies = [ { name = "beautifulsoup4" }, From 3d1b16f1c6342a27d0c93499b26076ee6e22f117 Mon Sep 17 00:00:00 2001 From: David Larsen Date: Fri, 20 Nov 2026 17:05:03 -0500 Subject: [PATCH 2/3] 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. --- CHANGELOG.md | 9 + docs/cli-reference.md | 5 +- socketsecurity/socketcli.py | 14 +- ...test_strict_blocking_no_manifest_change.py | 168 ++++++++++++++++-- 4 files changed, 173 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bb87e6..c928b10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ ## 2.13.1 +### Upgrade notes + +- Under `--strict-blocking`, runs without `--scm` now compare against the head + scan even when no manifest changed, and fail on existing blocking alerts. That + covers default-branch pushes in Bitbucket, Azure Pipelines, Jenkins and other + pipelines that don't set `--scm`, and target paths that aren't git + repositories. Those runs used to pass with a full scan. `--ignore-commit-files` + already behaved this way. + ### Fixed - `--strict-blocking` fails on existing blocking alerts when the change touches no diff --git a/docs/cli-reference.md b/docs/cli-reference.md index 78a1b47..eeff4c5 100644 --- a/docs/cli-reference.md +++ b/docs/cli-reference.md @@ -652,7 +652,8 @@ Security issues detected by Socket Security: ### Important Notes - **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). -- **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. +- **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. +- **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. - **Error-Level Only**: Only fails on `error=True` alerts (blocking severity), not warnings. - **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. - **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: ### Scanning Modes - **Differential Mode**: When manifest files are detected in changes, performs a diff scan with PR/MR comment integration -- **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` +- **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 - **Force Mode**: With `--ignore-commit-files`, always runs a comparison regardless of which files changed, over every manifest in the target path - **Forced Diff Mode**: With `--enable-diff`, forces differential mode even when using `--integration api` (without SCM integration) diff --git a/socketsecurity/socketcli.py b/socketsecurity/socketcli.py index c24dffa..0ab7caa 100644 --- a/socketsecurity/socketcli.py +++ b/socketsecurity/socketcli.py @@ -663,12 +663,10 @@ def main_code(config: Optional[CliConfig] = None): # against the baseline reports them. A change that touches no manifest would # otherwise get a full scan with blocking turned off and pass every time, so # run the comparison over the whole target path, as --ignore-commit-files does. + strict_forced_comparison = False if config.strict_blocking and not has_supported_files and not force_diff_mode: force_diff_mode = True - log.info( - "No supported manifest in the changed files; --strict-blocking compares " - "every manifest in the target path against the baseline to check existing alerts" - ) + strict_forced_comparison = True # Case 3: If no supported files or files are empty, force API mode (no PR comments) # BUT: Don't force API mode if we're in force_diff_mode @@ -773,6 +771,14 @@ def _is_unprocessed(c): return True scm_event_type = scm.check_event_type() if scm is not None else None + # Logged only once the run is known to compare: skipped SCM events exit inside + # check_event_type, comment events don't scan, and SCM branch pipelines still + # create a full scan. + if strict_forced_comparison and scm_event_type in (None, "diff"): + log.info( + "No supported manifest in the changed files; --strict-blocking compares " + "every manifest in the target path against the baseline to check existing alerts" + ) # Every branch below except the SCM full-scan one produces a comparison, or # is already covered by force_api_mode. See the blocking guard after the # scan for why this is tracked. diff --git a/tests/core/test_strict_blocking_no_manifest_change.py b/tests/core/test_strict_blocking_no_manifest_change.py index cc18e53..10ab91d 100644 --- a/tests/core/test_strict_blocking_no_manifest_change.py +++ b/tests/core/test_strict_blocking_no_manifest_change.py @@ -19,11 +19,19 @@ from socketsecurity.core import Core from socketsecurity.core.classes import Diff, Issue, Package from socketsecurity.core.scm.github import Github +from socketsecurity.core.scm.gitlab import Gitlab TOKEN_VARS = ( - "SOCKET_SECURITY_API_KEY", "SOCKET_SECURITY_API_TOKEN", "SOCKET_API_KEY", "SOCKET_API_TOKEN", - "GH_API_TOKEN", "GITLAB_TOKEN", + "SOCKET_SECURITY_API_KEY", + "SOCKET_SECURITY_API_TOKEN", + "SOCKET_API_KEY", + "SOCKET_API_TOKEN", + "GH_API_TOKEN", + "GITLAB_TOKEN", + "CI_JOB_TOKEN", + "CI_MERGE_REQUEST_IID", ) +STRICT_COMPARISON_LOG = "--strict-blocking compares every manifest in the target path" POLICIES = [ pytest.param([], 3, id="default"), pytest.param(["--exit-code-on-api-error", "100"], 100, id="remap"), @@ -69,8 +77,12 @@ def cli_runtime(monkeypatch): sdk = MagicMock() monkeypatch.setattr(socketcli, "build_socket_sdk", Mock(return_value=sdk)) monkeypatch.setattr(socketcli, "CliClient", Mock()) - monkeypatch.setattr(socketcli, "StreamingLogs", lambda **kwargs: nullcontext(Mock())) - git_repo = SimpleNamespace(changed_files=["README.md"], is_default_branch=False, remote_url=None) + monkeypatch.setattr( + socketcli, "StreamingLogs", lambda **kwargs: nullcontext(Mock()) + ) + git_repo = SimpleNamespace( + changed_files=["README.md"], is_default_branch=False, remote_url=None + ) in_git_repo = {"value": True} def apply_git_context(config): @@ -83,7 +95,9 @@ def apply_git_context(config): monkeypatch.setattr( Core, "has_manifest_files", - lambda self, files: any(f.endswith(("package.json", "package-lock.json")) for f in files), + lambda self, files: any( + f.endswith(("package.json", "package-lock.json")) for f in files + ), ) monkeypatch.setattr(Core, "find_files", Mock(return_value=["package.json"])) diff = Diff(id="compared") @@ -96,8 +110,16 @@ def apply_git_context(config): def run(flags=(), *, git=True): in_git_repo["value"] = git argv = [ - "socketcli", "--api-token", "test", "--repo", "repo", "--branch", "feature", - "--integration", "api", *flags, + "socketcli", + "--api-token", + "test", + "--repo", + "repo", + "--branch", + "feature", + "--integration", + "api", + *flags, ] monkeypatch.setattr(sys, "argv", argv) with pytest.raises(SystemExit) as result: @@ -105,7 +127,12 @@ def run(flags=(), *, git=True): return result.value.code return SimpleNamespace( - sdk=sdk, git_repo=git_repo, diff=diff, compare=compare, full_scan=full_scan, run=run, + sdk=sdk, + git_repo=git_repo, + diff=diff, + compare=compare, + full_scan=full_scan, + run=run, ) @@ -130,12 +157,17 @@ def test_no_existing_blocker_passes_a_change_without_manifests(cli_runtime, mode def test_disable_blocking_still_wins(cli_runtime): cli_runtime.diff.unchanged_alerts = [_existing_blocker()] - assert cli_runtime.run(["--enable-diff", "--strict-blocking", "--disable-blocking"]) == 0 + assert ( + cli_runtime.run(["--enable-diff", "--strict-blocking", "--disable-blocking"]) + == 0 + ) cli_runtime.compare.assert_called_once() @pytest.mark.parametrize("mode, git", MODES) -def test_without_strict_blocking_the_full_scan_fallback_is_unchanged(cli_runtime, mode, git): +def test_without_strict_blocking_the_full_scan_fallback_is_unchanged( + cli_runtime, mode, git +): cli_runtime.full_scan.return_value = Diff( id="full", report_url="", diff_url="", new_alerts=[_existing_blocker()] ) @@ -165,9 +197,15 @@ def test_changed_manifest_still_compares(cli_runtime): @pytest.mark.parametrize("policy, infrastructure_code", POLICIES) -def test_unreadable_existing_alerts_use_the_infrastructure_code(cli_runtime, policy, infrastructure_code): - package = Package(id="pkg", type="npm", name="pkg", version="1", score={}, alerts=[]) - cli_runtime.sdk.fullscans.stream.return_value = SimpleNamespace(success=True, artifacts={}) +def test_unreadable_existing_alerts_use_the_infrastructure_code( + cli_runtime, policy, infrastructure_code +): + package = Package( + id="pkg", type="npm", name="pkg", version="1", score={}, alerts=[] + ) + cli_runtime.sdk.fullscans.stream.return_value = SimpleNamespace( + success=True, artifacts={} + ) def compare(*args, **kwargs): core = Core.__new__(Core) @@ -177,7 +215,10 @@ def compare(*args, **kwargs): cli_runtime.compare.side_effect = compare - assert cli_runtime.run(["--enable-diff", "--strict-blocking", *policy]) == infrastructure_code + assert ( + cli_runtime.run(["--enable-diff", "--strict-blocking", *policy]) + == infrastructure_code + ) @pytest.fixture @@ -193,7 +234,9 @@ def github_pull_request(cli_runtime, monkeypatch): return add_comments -def test_code_only_pull_request_comments_on_existing_blockers(cli_runtime, github_pull_request): +def test_code_only_pull_request_comments_on_existing_blockers( + cli_runtime, github_pull_request +): cli_runtime.diff.unchanged_alerts = [_existing_blocker()] 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( github_pull_request.assert_not_called() -def test_branch_pipeline_still_creates_a_full_scan(cli_runtime, monkeypatch): +def test_branch_pipeline_still_creates_a_full_scan(cli_runtime, monkeypatch, caplog): # Non-PR SCM pipelines have no baseline; --strict-blocking does not change that. + # The full scan carries a blocking alert, so exit 0 proves blocking was turned off. monkeypatch.setenv("GH_API_TOKEN", "test") monkeypatch.setenv("GITHUB_EVENT_NAME", "push") monkeypatch.setenv("GITHUB_REPOSITORY", "acme/repo") monkeypatch.delenv("PR_NUMBER", raising=False) + cli_runtime.full_scan.return_value = Diff( + id="full", report_url="", diff_url="", new_alerts=[_existing_blocker()] + ) + + assert ( + cli_runtime.run(["--scm", "github", "--pr-number", "0", "--strict-blocking"]) + == 0 + ) + cli_runtime.full_scan.assert_called_once() + cli_runtime.compare.assert_not_called() + assert STRICT_COMPARISON_LOG not in caplog.text + + +def test_api_mode_default_branch_push_compares(cli_runtime): + # Without --scm there is no event type, so a default-branch push with no manifest + # change runs the comparison too, and existing blocking alerts fail it. + cli_runtime.git_repo.is_default_branch = True + cli_runtime.diff.unchanged_alerts = [_existing_blocker()] + + assert cli_runtime.run(["--default-branch", "--strict-blocking"]) == 1 + cli_runtime.compare.assert_called_once() + assert cli_runtime.compare.call_args.args[1].make_default_branch is True + cli_runtime.full_scan.assert_not_called() + + +def test_comparison_is_logged_when_it_runs(cli_runtime, caplog): + cli_runtime.diff.unchanged_alerts = [_existing_blocker()] + + cli_runtime.run(["--strict-blocking"]) + + assert STRICT_COMPARISON_LOG in caplog.text + + +def test_skipped_pull_request_event_logs_no_comparison( + cli_runtime, monkeypatch, caplog +): + # A skipped event exits before any scan, so the comparison message must not appear. + monkeypatch.setenv("GH_API_TOKEN", "test") + monkeypatch.setenv("GITHUB_EVENT_NAME", "pull_request") + monkeypatch.setenv("EVENT_ACTION", "closed") + monkeypatch.setenv("GITHUB_REPOSITORY", "acme/repo") + monkeypatch.setenv("PR_NUMBER", "7") + + assert cli_runtime.run(["--scm", "github", "--strict-blocking"]) == 0 + cli_runtime.compare.assert_not_called() + cli_runtime.full_scan.assert_not_called() + assert STRICT_COMPARISON_LOG not in caplog.text + + +@pytest.fixture +def gitlab_pipeline(cli_runtime, monkeypatch): + monkeypatch.setenv("GITLAB_TOKEN", "glpat-test") + monkeypatch.setenv("CI_PROJECT_NAME", "repo") + monkeypatch.setenv("CI_DEFAULT_BRANCH", "main") + monkeypatch.setenv("CI_API_V4_URL", "https://gitlab.example.com/api/v4") + monkeypatch.setenv("CI_PROJECT_URL", "https://gitlab.example.com/acme/repo") + monkeypatch.setattr(Gitlab, "get_comments_for_pr", Mock(return_value={})) + add_comments = Mock() + monkeypatch.setattr(Gitlab, "add_socket_comments", add_comments) + return add_comments + + +def test_code_only_merge_request_comments_on_existing_blockers( + cli_runtime, gitlab_pipeline, monkeypatch +): + monkeypatch.setenv("CI_PIPELINE_SOURCE", "merge_request_event") + monkeypatch.setenv("CI_MERGE_REQUEST_IID", "7") + monkeypatch.setenv("CI_MERGE_REQUEST_PROJECT_ID", "42") + monkeypatch.setenv("CI_MERGE_REQUEST_SOURCE_BRANCH_NAME", "feature") + cli_runtime.diff.unchanged_alerts = [_existing_blocker()] + + assert cli_runtime.run(["--scm", "gitlab", "--strict-blocking"]) == 1 + cli_runtime.compare.assert_called_once() + cli_runtime.full_scan.assert_not_called() + gitlab_pipeline.assert_called_once() + security_comment, _, _, post_security, _ = gitlab_pipeline.call_args.args + assert post_security is True + assert "handlebars@4.0.11" in security_comment + assert "Existing alerts" in security_comment + + +def test_gitlab_branch_pipeline_still_creates_a_full_scan( + cli_runtime, gitlab_pipeline, monkeypatch, caplog +): + monkeypatch.setenv("CI_PIPELINE_SOURCE", "push") + cli_runtime.full_scan.return_value = Diff( + id="full", report_url="", diff_url="", new_alerts=[_existing_blocker()] + ) - assert cli_runtime.run(["--scm", "github", "--pr-number", "0", "--strict-blocking"]) == 0 + assert cli_runtime.run(["--scm", "gitlab", "--strict-blocking"]) == 0 cli_runtime.full_scan.assert_called_once() cli_runtime.compare.assert_not_called() + gitlab_pipeline.assert_not_called() + assert STRICT_COMPARISON_LOG not in caplog.text From 009a605eeb70ef73ee2df37922c17bbcbbac7229 Mon Sep 17 00:00:00 2001 From: David Larsen Date: Fri, 20 Nov 2026 18:08:17 -0500 Subject: [PATCH 3/3] Bump version to 2.13.3 2.13.2 was released from main, so this change needs the next patch version. --- pyproject.toml | 2 +- socketsecurity/__init__.py | 2 +- uv.lock | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5c88c15..82c5bbf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "hatchling.build" [project] name = "socketsecurity" -version = "2.13.2" +version = "2.13.3" requires-python = ">= 3.11" license = {"file" = "LICENSE"} dependencies = [ diff --git a/socketsecurity/__init__.py b/socketsecurity/__init__.py index 7ba49e1..0871ade 100644 --- a/socketsecurity/__init__.py +++ b/socketsecurity/__init__.py @@ -1,4 +1,4 @@ __author__ = 'socket.dev' -__version__ = '2.13.2' +__version__ = '2.13.3' USER_AGENT_NAME = 'socket-python-cli' USER_AGENT = f'{USER_AGENT_NAME}/{__version__}' diff --git a/uv.lock b/uv.lock index 196ae3c..b3681e4 100644 --- a/uv.lock +++ b/uv.lock @@ -1334,7 +1334,7 @@ wheels = [ [[package]] name = "socketsecurity" -version = "2.13.2" +version = "2.13.3" source = { editable = "." } dependencies = [ { name = "beautifulsoup4" },