|
| 1 | +"""`--strict-blocking` on a change that touches no manifest. |
| 2 | +
|
| 3 | +Without `--ignore-commit-files`, the CLI reads the changed files to decide whether |
| 4 | +to compare. When none is a supported manifest it used to create a full scan, turn |
| 5 | +blocking off and warn that `--strict-blocking` needs diff mode, so a code-only pull |
| 6 | +request passed even with existing blocking alerts. Under `--strict-blocking` the |
| 7 | +comparison now runs anyway, the way `--ignore-commit-files` runs it. Runs without |
| 8 | +the flag keep the full-scan fallback. |
| 9 | +""" |
| 10 | + |
| 11 | +import sys |
| 12 | +from contextlib import nullcontext |
| 13 | +from types import SimpleNamespace |
| 14 | +from unittest.mock import MagicMock, Mock |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from socketsecurity import socketcli |
| 19 | +from socketsecurity.core import Core |
| 20 | +from socketsecurity.core.classes import Diff, Issue, Package |
| 21 | +from socketsecurity.core.scm.github import Github |
| 22 | + |
| 23 | +TOKEN_VARS = ( |
| 24 | + "SOCKET_SECURITY_API_KEY", "SOCKET_SECURITY_API_TOKEN", "SOCKET_API_KEY", "SOCKET_API_TOKEN", |
| 25 | + "GH_API_TOKEN", "GITLAB_TOKEN", |
| 26 | +) |
| 27 | +POLICIES = [ |
| 28 | + pytest.param([], 3, id="default"), |
| 29 | + pytest.param(["--exit-code-on-api-error", "100"], 100, id="remap"), |
| 30 | + pytest.param(["--disable-blocking"], 0, id="nonblocking"), |
| 31 | +] |
| 32 | +# Each mode reaches the no-manifest fallback by a different path. |
| 33 | +MODES = [ |
| 34 | + pytest.param(["--enable-diff"], True, id="enable-diff"), |
| 35 | + pytest.param([], True, id="api-default"), |
| 36 | + pytest.param(["--files", '["src/index.js"]'], True, id="files-without-manifest"), |
| 37 | + pytest.param([], False, id="not-a-git-repo"), |
| 38 | +] |
| 39 | + |
| 40 | + |
| 41 | +def _existing_blocker() -> Issue: |
| 42 | + return Issue( |
| 43 | + pkg_name="handlebars", |
| 44 | + pkg_version="4.0.11", |
| 45 | + pkg_type="npm", |
| 46 | + severity="critical", |
| 47 | + title="Critical CVE", |
| 48 | + description="Prototype pollution", |
| 49 | + type="criticalCVE", |
| 50 | + url="https://socket.dev/npm/package/handlebars/overview/4.0.11", |
| 51 | + manifests="package-lock.json", |
| 52 | + props={"ghsaId": "GHSA-w457-6q6x-cgp9"}, |
| 53 | + key="existing-key", |
| 54 | + purl="pkg:npm/handlebars@4.0.11", |
| 55 | + error=True, |
| 56 | + warn=False, |
| 57 | + ignore=False, |
| 58 | + monitor=False, |
| 59 | + suggestion="Upgrade handlebars", |
| 60 | + next_step_title="Next steps", |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture |
| 65 | +def cli_runtime(monkeypatch): |
| 66 | + for name in TOKEN_VARS: |
| 67 | + monkeypatch.delenv(name, raising=False) |
| 68 | + monkeypatch.setenv("BUILDKITE", "false") |
| 69 | + sdk = MagicMock() |
| 70 | + monkeypatch.setattr(socketcli, "build_socket_sdk", Mock(return_value=sdk)) |
| 71 | + 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) |
| 74 | + in_git_repo = {"value": True} |
| 75 | + |
| 76 | + def apply_git_context(config): |
| 77 | + if not in_git_repo["value"]: |
| 78 | + config.ignore_commit_files = True |
| 79 | + return False, None |
| 80 | + return True, git_repo |
| 81 | + |
| 82 | + monkeypatch.setattr(socketcli, "apply_git_context", apply_git_context) |
| 83 | + monkeypatch.setattr( |
| 84 | + Core, |
| 85 | + "has_manifest_files", |
| 86 | + lambda self, files: any(f.endswith(("package.json", "package-lock.json")) for f in files), |
| 87 | + ) |
| 88 | + monkeypatch.setattr(Core, "find_files", Mock(return_value=["package.json"])) |
| 89 | + diff = Diff(id="compared") |
| 90 | + compare = Mock(return_value=diff) |
| 91 | + full_scan = Mock(return_value=Diff(id="full", report_url="", diff_url="")) |
| 92 | + monkeypatch.setattr(Core, "create_new_diff", compare) |
| 93 | + monkeypatch.setattr(Core, "create_full_scan_with_report_url", full_scan) |
| 94 | + monkeypatch.setattr(socketcli.OutputHandler, "handle_output", Mock()) |
| 95 | + |
| 96 | + def run(flags=(), *, git=True): |
| 97 | + in_git_repo["value"] = git |
| 98 | + argv = [ |
| 99 | + "socketcli", "--api-token", "test", "--repo", "repo", "--branch", "feature", |
| 100 | + "--integration", "api", *flags, |
| 101 | + ] |
| 102 | + monkeypatch.setattr(sys, "argv", argv) |
| 103 | + with pytest.raises(SystemExit) as result: |
| 104 | + socketcli.cli() |
| 105 | + return result.value.code |
| 106 | + |
| 107 | + return SimpleNamespace( |
| 108 | + sdk=sdk, git_repo=git_repo, diff=diff, compare=compare, full_scan=full_scan, run=run, |
| 109 | + ) |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.parametrize("mode, git", MODES) |
| 113 | +def test_existing_blocker_fails_a_change_without_manifests(cli_runtime, mode, git): |
| 114 | + cli_runtime.diff.unchanged_alerts = [_existing_blocker()] |
| 115 | + |
| 116 | + assert cli_runtime.run([*mode, "--strict-blocking"], git=git) == 1 |
| 117 | + cli_runtime.compare.assert_called_once() |
| 118 | + cli_runtime.full_scan.assert_not_called() |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.parametrize("mode, git", MODES) |
| 122 | +def test_no_existing_blocker_passes_a_change_without_manifests(cli_runtime, mode, git): |
| 123 | + cli_runtime.diff.unchanged_alerts = [Issue(error=False, warn=True)] |
| 124 | + |
| 125 | + assert cli_runtime.run([*mode, "--strict-blocking"], git=git) == 0 |
| 126 | + cli_runtime.compare.assert_called_once() |
| 127 | + cli_runtime.full_scan.assert_not_called() |
| 128 | + |
| 129 | + |
| 130 | +def test_disable_blocking_still_wins(cli_runtime): |
| 131 | + cli_runtime.diff.unchanged_alerts = [_existing_blocker()] |
| 132 | + |
| 133 | + assert cli_runtime.run(["--enable-diff", "--strict-blocking", "--disable-blocking"]) == 0 |
| 134 | + cli_runtime.compare.assert_called_once() |
| 135 | + |
| 136 | + |
| 137 | +@pytest.mark.parametrize("mode, git", MODES) |
| 138 | +def test_without_strict_blocking_the_full_scan_fallback_is_unchanged(cli_runtime, mode, git): |
| 139 | + cli_runtime.full_scan.return_value = Diff( |
| 140 | + id="full", report_url="", diff_url="", new_alerts=[_existing_blocker()] |
| 141 | + ) |
| 142 | + |
| 143 | + assert cli_runtime.run(mode, git=git) == 0 |
| 144 | + cli_runtime.full_scan.assert_called_once() |
| 145 | + cli_runtime.compare.assert_not_called() |
| 146 | + |
| 147 | + |
| 148 | +def test_no_fallback_warning_under_strict_blocking(cli_runtime, caplog): |
| 149 | + cli_runtime.diff.unchanged_alerts = [_existing_blocker()] |
| 150 | + |
| 151 | + cli_runtime.run(["--enable-diff", "--strict-blocking"]) |
| 152 | + |
| 153 | + assert "Falling back to full scan mode" not in caplog.text |
| 154 | + assert "only supported in diff mode" not in caplog.text |
| 155 | + |
| 156 | + |
| 157 | +def test_changed_manifest_still_compares(cli_runtime): |
| 158 | + cli_runtime.git_repo.changed_files = ["package.json"] |
| 159 | + cli_runtime.diff.unchanged_alerts = [_existing_blocker()] |
| 160 | + |
| 161 | + assert cli_runtime.run(["--enable-diff", "--strict-blocking"]) == 1 |
| 162 | + assert cli_runtime.run(["--enable-diff"]) == 0 |
| 163 | + assert cli_runtime.compare.call_count == 2 |
| 164 | + cli_runtime.full_scan.assert_not_called() |
| 165 | + |
| 166 | + |
| 167 | +@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={}) |
| 171 | + |
| 172 | + def compare(*args, **kwargs): |
| 173 | + core = Core.__new__(Core) |
| 174 | + core.sdk = cli_runtime.sdk |
| 175 | + core.config = SimpleNamespace(org_slug="acme") |
| 176 | + core.add_existing_alerts_from_full_scan({package.id: package}, "new") |
| 177 | + |
| 178 | + cli_runtime.compare.side_effect = compare |
| 179 | + |
| 180 | + assert cli_runtime.run(["--enable-diff", "--strict-blocking", *policy]) == infrastructure_code |
| 181 | + |
| 182 | + |
| 183 | +@pytest.fixture |
| 184 | +def github_pull_request(cli_runtime, monkeypatch): |
| 185 | + monkeypatch.setenv("GH_API_TOKEN", "test") |
| 186 | + monkeypatch.setenv("GITHUB_EVENT_NAME", "pull_request") |
| 187 | + monkeypatch.setenv("EVENT_ACTION", "synchronize") |
| 188 | + monkeypatch.setenv("GITHUB_REPOSITORY", "acme/repo") |
| 189 | + monkeypatch.setenv("PR_NUMBER", "7") |
| 190 | + monkeypatch.setattr(Github, "get_comments_for_pr", Mock(return_value={})) |
| 191 | + add_comments = Mock() |
| 192 | + monkeypatch.setattr(Github, "add_socket_comments", add_comments) |
| 193 | + return add_comments |
| 194 | + |
| 195 | + |
| 196 | +def test_code_only_pull_request_comments_on_existing_blockers(cli_runtime, github_pull_request): |
| 197 | + cli_runtime.diff.unchanged_alerts = [_existing_blocker()] |
| 198 | + |
| 199 | + assert cli_runtime.run(["--scm", "github", "--strict-blocking"]) == 1 |
| 200 | + cli_runtime.compare.assert_called_once() |
| 201 | + github_pull_request.assert_called_once() |
| 202 | + security_comment, _, _, post_security, _ = github_pull_request.call_args.args |
| 203 | + assert post_security is True |
| 204 | + assert "handlebars@4.0.11" in security_comment |
| 205 | + assert "Existing alerts" in security_comment |
| 206 | + |
| 207 | + |
| 208 | +def test_code_only_pull_request_without_strict_blocking_skips_the_comment( |
| 209 | + cli_runtime, github_pull_request |
| 210 | +): |
| 211 | + assert cli_runtime.run(["--scm", "github"]) == 0 |
| 212 | + cli_runtime.full_scan.assert_called_once() |
| 213 | + cli_runtime.compare.assert_not_called() |
| 214 | + github_pull_request.assert_not_called() |
| 215 | + |
| 216 | + |
| 217 | +def test_branch_pipeline_still_creates_a_full_scan(cli_runtime, monkeypatch): |
| 218 | + # Non-PR SCM pipelines have no baseline; --strict-blocking does not change that. |
| 219 | + monkeypatch.setenv("GH_API_TOKEN", "test") |
| 220 | + monkeypatch.setenv("GITHUB_EVENT_NAME", "push") |
| 221 | + monkeypatch.setenv("GITHUB_REPOSITORY", "acme/repo") |
| 222 | + monkeypatch.delenv("PR_NUMBER", raising=False) |
| 223 | + |
| 224 | + assert cli_runtime.run(["--scm", "github", "--pr-number", "0", "--strict-blocking"]) == 0 |
| 225 | + cli_runtime.full_scan.assert_called_once() |
| 226 | + cli_runtime.compare.assert_not_called() |
0 commit comments