Skip to content

Commit e966d9e

Browse files
dc-larsenlelia
andauthored
Check existing alerts on code-only changes with --strict-blocking (#462)
* 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. * 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. * Bump version to 2.13.3 2.13.2 was released from main, so this change needs the next patch version. * Clarify strict blocking on pushes with PR context * Include every package list in the comparison fixture --------- Co-authored-by: lelia <2418071+lelia@users.noreply.github.com>
1 parent ce03d4c commit e966d9e

6 files changed

Lines changed: 449 additions & 25 deletions

File tree

‎CHANGELOG.md‎

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

33
## 2.14.0
44

5+
### Upgrade notes
6+
7+
- Without a GitHub or GitLab SCM adapter, `--strict-blocking` now compares scans
8+
even when no manifest changed. This includes default-branch pushes and scans of
9+
non-git paths. These runs can now fail on Block findings or comparison errors
10+
and need the [API token scopes for comparison runs](docs/troubleshooting.md#api-token-permissions).
11+
512
### Changed
613

714
- Updated the Socket Python SDK (`socketdev`) to 3.6.4. This can increase
@@ -18,6 +25,9 @@
1825
- Comparisons with missing or invalid package lists fail with exit `3` by
1926
default. `--exit-code-on-api-error` can remap the code, and
2027
`--disable-blocking` takes precedence.
28+
- `--strict-blocking` checks existing Block findings on code-only changes.
29+
GitHub and GitLab pull request comments show those findings under Existing
30+
alerts.
2131

2232
## 2.13.2
2333

‎docs/ci-cd.md‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -500,23 +500,26 @@ pipelines:
500500

501501
## Scan type by pipeline
502502

503-
With `--scm github` or `--scm gitlab`, the detected event decides the scan type:
503+
With `--scm github` or `--scm gitlab`, the adapter uses the event and PR/MR
504+
context to select the scan type. A push given a PR/MR number can use the
505+
comparison path.
504506

505507
| Event | Scan | Blocks the build |
506508
|:------|:-----|:-----------------|
507509
| Pull request / merge request | Diff scan against the repository's baseline | Yes, on new blocking alerts; also existing blockers with `--strict-blocking` |
508510
| Any other pipeline, including default-branch pushes | Full scan | No |
509511

510-
A full scan has no baseline, so it cannot tell a newly introduced alert from one
511-
that was already there. Rather than block on a number that would mean something
512-
different depending on which output format was enabled, those runs behave as if
513-
`--disable-blocking` was supplied and report through the Dashboard instead. This
514-
matches how the CLI already treats a run with no supported manifest files.
515-
516-
The event type is authoritative once `--scm` is set: `--enable-diff` and
517-
`--ignore-commit-files` do not turn a branch pipeline into a comparison. To diff
518-
a branch build, drop `--scm` and use `--enable-diff` with `--integration`, which
519-
runs the comparison without the PR comment adapter.
512+
With changed-file detection, a PR/MR with no supported manifest change runs a
513+
full scan by default. Use `--strict-blocking` or `--ignore-commit-files` to
514+
compare it. Full scans report through the Dashboard. A branch pipeline without
515+
PR/MR context stays in full-scan mode even with `--enable-diff`,
516+
`--ignore-commit-files`, or `--strict-blocking`.
517+
518+
Without a GitHub or GitLab SCM adapter, `--strict-blocking` runs a comparison
519+
even when no supported manifest changed. This includes default-branch pushes and
520+
scans of non-git paths. A Block finding exits `1`. A comparison infrastructure
521+
error exits `3` by default. Check the [API token scopes for comparison runs](troubleshooting.md#api-token-permissions)
522+
when upgrading a pipeline that previously ran only full scans.
520523

521524
`--generate-license` and `--legal-format fossa` work on both paths; a full scan
522525
fetches the package list for them.

‎docs/cli-reference.md‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,12 @@ The CLI runs a pinned version of the Coana CLI, the analysis engine; it does **n
461461
#### Advanced Configuration
462462
| Parameter | Required | Default | Description |
463463
|:-------------------------|:---------|:--------|:----------------------------------------------------------------------|
464-
| `--ignore-commit-files` | False | False | Compare regardless of which files changed, scanning every manifest |
464+
| `--ignore-commit-files` | False | False | Skip the changed-file check for comparisons eligible under the SCM event rules. |
465465
| `--exit-code-on-api-error` | False | 3 | Remap infrastructure failures, including missing tokens, setup failures, incomplete scan data and unexpected runtime errors. Findings (`1`), usage errors (`2`) and Ctrl+C (`130`) are unaffected. `--disable-blocking` takes precedence. |
466466
| `--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`. |
467467
| `--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). |
468468
| `--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). |
469-
| `--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. |
469+
| `--strict-blocking` | False | False | Fail on new and existing Block findings, including on changes that touch no manifest. See [Strict Blocking Mode](#strict-blocking-mode). |
470470
| `--enable-diff` | False | False | Enable diff mode even when using `--integration api` (forces diff mode without SCM integration) |
471471
| `--scm` | False | api | Source control management type |
472472
| `--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. |
@@ -660,7 +660,8 @@ Security issues detected by Socket Security:
660660
661661
### Important Notes
662662
663-
- **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).
663+
- **Scan scope**. Strict mode compares the configured scan scope even when no supported manifest changed. Scan options such as `--sub-path` and `--reach-use-only-pregenerated-sboms` still control what the CLI submits. With `--scm`, GitHub and GitLab branch pipelines without PR/MR context perform non-blocking full scans. See [Scan type by pipeline](ci-cd.md#scan-type-by-pipeline).
664+
- **With `--reach`**. With no explicit baseline, the CLI looks for a default-branch head scan with reachability enabled. If none exists, the CLI compares against an empty baseline and classifies all findings as new. Scan the default branch with `--reach` too.
664665
- **Policy actions**. Warn findings are reported. If the same finding has both Warn and Block actions, Block takes precedence.
665666
- **Existing alerts come from the full scan**. The CLI fetches the current full scan when the comparison leaves out alerts on unchanged packages or when a dependency version changes. A failed fetch or a missing package in that response triggers the infrastructure-error exit code.
666667
- **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.
@@ -762,25 +763,26 @@ The CLI determines which files to scan based on the following logic:
762763
763764
2. **`--files` Parameter Override**: When specified, this parameter takes precedence over git commit detection. It accepts a JSON array of file paths to check for manifest files.
764765
765-
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.
766+
3. **`--ignore-commit-files` Flag**: Skip the changed-file check for comparisons, subject to the SCM event type.
766767
767-
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.
768+
4. **Automatic Fallback**: When the changed-file check finds no supported manifest, the CLI normally runs a full scan. Strict mode compares scans so it can evaluate existing findings. See [Scan type by pipeline](ci-cd.md#scan-type-by-pipeline) for SCM event rules.
768769
769-
> **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.
770+
The changed-file list and `--files` select the scan type. Manifest discovery
771+
uses the configured scan scope, including any `--sub-path` restrictions.
770772
771773
### Scanning Modes
772774
773775
- **Differential Mode**: When manifest files are detected in changes, performs a diff scan with PR/MR comment integration
774-
- **API Mode**: When no manifest files are in changes, creates a full scan report without PR comments but still scans the entire repository
775-
- **Force Mode**: With `--ignore-commit-files`, always runs a comparison regardless of which files changed, over every manifest in the target path
776+
- **Full Scan**: Reports findings to the Dashboard. SCM branch pipelines without PR/MR context use this mode.
777+
- **Force Mode**: With `--ignore-commit-files`, compares regardless of which files changed on pipelines eligible for comparison.
776778
- **Forced Diff Mode**: With `--enable-diff`, forces differential mode even when using `--integration api` (without SCM integration)
777779
778780
### Examples
779781
780782
- **Commit with manifest file**: If your commit includes changes to `package.json`, a differential scan will be triggered automatically with PR comment integration.
781-
- **Commit without manifest files**: If your commit only changes non-manifest files (like `.github/workflows/socket.yaml`), the CLI automatically switches to API mode and performs a full repository scan.
783+
- **Code-only commit**: The CLI runs a full scan by default. `--strict-blocking` selects a comparison on pipelines eligible for comparison.
782784
- **Using `--files`**: If you specify `--files '["package.json"]'`, the CLI will check if this file exists and is a manifest file before determining scan type.
783-
- **Using `--ignore-commit-files`**: This runs the comparison regardless of what's in your commit, over all manifest files in the target path. Use it when the changed-file check would otherwise skip a comparison you need.
785+
- **Using `--ignore-commit-files`**: Compare the configured scan scope regardless of which files changed when the pipeline supports comparison.
784786
- **Using `--enable-diff`**: Forces diff mode without SCM integration - useful when you want differential scanning but are using `--integration api`. For example: `socketcli --integration api --enable-diff --target-path /path/to/repo`
785787
- **Auto-detection**: Most CI/CD scenarios now work with just `socketcli --target-path /path/to/repo --scm github --pr-number $PR_NUM`
786788

‎socketsecurity/config.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,8 @@ def create_argument_parser() -> argparse.ArgumentParser:
10581058
"--strict-blocking",
10591059
dest="strict_blocking",
10601060
action="store_true",
1061-
help="Fail on ANY security policy violations (blocking severity), not just new ones. Only works in diff mode."
1061+
help="Fail on new and existing Block findings. Compare even when no manifest changed. "
1062+
"With --scm, branch pipelines without PR/MR context run non-blocking full scans."
10621063
)
10631064
advanced_group.add_argument(
10641065
"--enable-diff",

‎socketsecurity/socketcli.py‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,14 @@ def main_code(config: Optional[CliConfig] = None):
658658
log.debug(f"Error finding files in scan paths: {e}")
659659
discovered_scan_files = None
660660
has_supported_files = False
661-
661+
662+
# Strict blocking needs a comparison even when file selection finds no manifest.
663+
strict_forced_comparison = (
664+
config.strict_blocking and not has_supported_files and not force_diff_mode
665+
)
666+
if strict_forced_comparison:
667+
force_diff_mode = True
668+
662669
# Case 3: If no supported files or files are empty, force API mode (no PR comments)
663670
# BUT: Don't force API mode if we're in force_diff_mode
664671
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}")
@@ -762,6 +769,11 @@ def _is_unprocessed(c):
762769
return True
763770

764771
scm_event_type = scm.check_event_type() if scm is not None else None
772+
if strict_forced_comparison and scm_event_type in (None, "diff"):
773+
log.info(
774+
"No supported manifest triggered a comparison. "
775+
"--strict-blocking compares the configured scan scope to check existing alerts."
776+
)
765777
# Every branch below except the SCM full-scan one produces a comparison, or
766778
# is already covered by force_api_mode. See the blocking guard after the
767779
# scan for why this is tracked.
@@ -979,8 +991,6 @@ def _is_unprocessed(c):
979991
output_handler.handle_output(diff)
980992

981993
elif (config.enable_diff or force_diff_mode) and force_api_mode:
982-
# User requested diff mode but no manifest files were detected - this should not happen with new logic
983-
# but keeping as a safety net
984994
log.warning("--enable-diff was specified but no supported manifest files were detected in the changed files. Falling back to full scan mode.")
985995
_log_scan_mode_fallback(
986996
"diff",

0 commit comments

Comments
 (0)