|
| 1 | +"""Which GitHub events and pull_request actions run a scan. |
| 2 | +
|
| 3 | +A skipped run exits 0, and a required check takes the latest run on the head |
| 4 | +commit, so skipping an action that a workflow subscribes to can replace an |
| 5 | +earlier failure with a pass that never scanned anything. |
| 6 | +""" |
| 7 | + |
| 8 | +import logging |
| 9 | +import sys |
| 10 | +from contextlib import nullcontext |
| 11 | +from unittest.mock import MagicMock, Mock |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from socketsecurity import socketcli |
| 16 | +from socketsecurity.core.classes import Diff, Issue |
| 17 | +from socketsecurity.core.scm.github import ( |
| 18 | + PULL_REQUEST_SCAN_ACTIONS, |
| 19 | + Github, |
| 20 | + GithubConfig, |
| 21 | +) |
| 22 | +from socketsecurity.core.scm_comments import Comments |
| 23 | + |
| 24 | +CONTEXT_VARIABLES = ( |
| 25 | + "BUILDKITE", "BUILDKITE_PULL_REQUEST", "DEFAULT_BRANCH", "EVENT_ACTION", |
| 26 | + "GITHUB_EVENT_NAME", "GITHUB_EVENT_PATH", "GITHUB_REPOSITORY", "PR_NUMBER", |
| 27 | + "SOCKET_SECURITY_API_KEY", "SOCKET_SECURITY_API_TOKEN", "SOCKET_API_KEY", "SOCKET_API_TOKEN", |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture(autouse=True) |
| 32 | +def clear_context(monkeypatch): |
| 33 | + for variable in CONTEXT_VARIABLES: |
| 34 | + monkeypatch.delenv(variable, raising=False) |
| 35 | + monkeypatch.setenv("GH_API_TOKEN", "test-token") |
| 36 | + monkeypatch.setenv("GITHUB_REPOSITORY", "acme/repo") |
| 37 | + |
| 38 | + |
| 39 | +def _github(monkeypatch, event_name, action=None): |
| 40 | + monkeypatch.setenv("GITHUB_EVENT_NAME", event_name) |
| 41 | + if action is not None: |
| 42 | + monkeypatch.setenv("EVENT_ACTION", action) |
| 43 | + return Github(client=object(), config=GithubConfig.from_env(pr_number="7")) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize("action", ["opened", "synchronize", "reopened", "ready_for_review", "Reopened"]) |
| 47 | +def test_pull_request_actions_that_change_the_head_result_are_scanned(monkeypatch, action): |
| 48 | + assert _github(monkeypatch, "pull_request", action).check_event_type() == "diff" |
| 49 | + |
| 50 | + |
| 51 | +def test_scanned_actions_are_the_documented_set(): |
| 52 | + assert PULL_REQUEST_SCAN_ACTIONS == ("opened", "synchronize", "reopened", "ready_for_review") |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize("action", ["closed", "labeled", "edited", None]) |
| 56 | +def test_other_pull_request_actions_skip_with_a_warning(monkeypatch, caplog, action): |
| 57 | + github = _github(monkeypatch, "pull_request", action) |
| 58 | + |
| 59 | + with caplog.at_level(logging.INFO, logger="socketdev"), pytest.raises(SystemExit) as result: |
| 60 | + github.check_event_type() |
| 61 | + |
| 62 | + assert result.value.code == 0 |
| 63 | + warning = next(r for r in caplog.records if r.levelno == logging.WARNING) |
| 64 | + assert f"'{action}' is not scanned" in warning.getMessage() |
| 65 | + assert "No scan ran" in warning.getMessage() |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize("event_name, expected", [ |
| 69 | + ("pull_request_target", "runs against the base branch"), |
| 70 | + ("merge_group", "'merge_group' is not scanned"), |
| 71 | + ("schedule", "'schedule' is not scanned"), |
| 72 | +]) |
| 73 | +def test_unscanned_events_skip_with_a_warning(monkeypatch, caplog, event_name, expected): |
| 74 | + github = _github(monkeypatch, event_name, "opened") |
| 75 | + |
| 76 | + with caplog.at_level(logging.INFO, logger="socketdev"), pytest.raises(SystemExit) as result: |
| 77 | + github.check_event_type() |
| 78 | + |
| 79 | + assert result.value.code == 0 |
| 80 | + warnings = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING] |
| 81 | + assert any(expected in message and "No scan ran" in message for message in warnings) |
| 82 | + |
| 83 | + |
| 84 | +def _blocking_alert() -> Issue: |
| 85 | + return Issue( |
| 86 | + pkg_name="lodash", pkg_version="4.17.11", pkg_type="npm", severity="critical", |
| 87 | + title="Critical CVE", description="", type="criticalCVE", url="https://socket.dev/test", |
| 88 | + manifests="package.json", props={}, key="key", purl="pkg:npm/lodash@4.17.11", |
| 89 | + error=True, warn=False, ignore=False, monitor=False, |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.parametrize("action, scanned, expected_exit", [ |
| 94 | + ("reopened", True, 1), |
| 95 | + ("ready_for_review", True, 1), |
| 96 | + ("synchronize", True, 1), |
| 97 | + ("closed", False, 0), |
| 98 | +]) |
| 99 | +def test_reopened_pull_request_blocks_like_synchronize(monkeypatch, action, scanned, expected_exit): |
| 100 | + """A reopened PR with an existing blocker fails instead of passing unscanned.""" |
| 101 | + monkeypatch.setattr(sys, "argv", [ |
| 102 | + "socketcli", "--api-token", "test", "--scm", "github", "--strict-blocking", |
| 103 | + "--repo", "repo", "--branch", "feature", "--pr-number", "7", |
| 104 | + "--files", '["package.json"]', |
| 105 | + ]) |
| 106 | + monkeypatch.setenv("GITHUB_EVENT_NAME", "pull_request") |
| 107 | + monkeypatch.setenv("EVENT_ACTION", action) |
| 108 | + monkeypatch.setenv("DEFAULT_BRANCH", "false") |
| 109 | + |
| 110 | + diff = Diff(id="scan", new_alerts=[], unchanged_alerts=[_blocking_alert()]) |
| 111 | + diff.diff_url = "https://socket.dev/diff" |
| 112 | + monkeypatch.setattr(Github, "get_comments_for_pr", lambda self: Comments.check_for_socket_comments({})) |
| 113 | + posted = Mock() |
| 114 | + monkeypatch.setattr(Github, "add_socket_comments", posted) |
| 115 | + monkeypatch.setattr(Github, "handle_ignore_reactions", Mock()) |
| 116 | + sdk = MagicMock() |
| 117 | + sdk.org.get.return_value = {"organizations": {"org-id": {"slug": "acme"}}} |
| 118 | + monkeypatch.setattr(socketcli, "build_socket_sdk", Mock(return_value=sdk)) |
| 119 | + monkeypatch.setattr(socketcli, "CliClient", Mock(return_value=Mock())) |
| 120 | + monkeypatch.setattr(socketcli, "StreamingLogs", lambda **kwargs: nullcontext(Mock())) |
| 121 | + monkeypatch.setattr(socketcli, "apply_git_context", lambda config: (False, None)) |
| 122 | + scan = Mock(return_value=diff) |
| 123 | + monkeypatch.setattr(socketcli.Core, "create_new_diff", scan) |
| 124 | + monkeypatch.setattr(socketcli.Core, "has_manifest_files", Mock(return_value=True)) |
| 125 | + monkeypatch.setattr(socketcli.OutputHandler, "handle_output", Mock()) |
| 126 | + |
| 127 | + with pytest.raises(SystemExit) as result: |
| 128 | + socketcli.cli() |
| 129 | + |
| 130 | + assert result.value.code == expected_exit |
| 131 | + assert scan.called is scanned |
| 132 | + assert posted.called is scanned |
0 commit comments