Skip to content

Commit 4eed245

Browse files
dc-larsenlelia
andauthored
Clarify --include-module-folders and document SBOM scans (#463)
* Document what --include-module-folders does The help text said "Enabling including module folders like node_modules", which doesn't say what a scan gains. The flag removes node_modules, bower_components and jspm_packages from the directories manifest discovery skips. Every package.json under them is then uploaded as its own project, and Socket resolves the dependencies each one declares, devDependencies included. The installed packages themselves are never recorded, so the flag can add hundreds of packages a project never installs and still misses a package that a postinstall script put in node_modules without a lockfile entry. Say that in the help text and the CLI reference, and add a troubleshooting entry for packages installed outside the lockfile: a CycloneDX SBOM of the installed tree saved in the scan root is picked up by manifest discovery and uploaded with the other manifests. No behavior change. Bumps the version to 2.13.1 for the version check. * Say every manifest under module folders is scanned, and shorten the help The flag adds every supported manifest under node_modules, bower_components and jspm_packages, at any depth, not only each package's top-level package.json. Those files are added to the same scan; no separate project is created. Say that in the reference, troubleshooting entry and changelog. Cut the help text to two sentences and point to the troubleshooting section on GitHub, since pip installs don't include the docs folder. * Bump version to 2.13.3 2.13.2 was released from main, so this change needs the next patch version. * Add the 2.14.0 upgrade guide to the README * Combine the 2.13 and 2.14 upgrade notes in the README --------- Co-authored-by: lelia <2418071+lelia@users.noreply.github.com>
1 parent b3dc63f commit 4eed245

7 files changed

Lines changed: 112 additions & 30 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454

5555
- The [CI guide](docs/ci-cd.md#required-github-checks) describes GitHub required
5656
checks and the GitHub Actions workflow migration.
57+
- The README's upgrade notes for 2.13 and 2.14 are combined into one short
58+
[section](README.md#upgrading-from-212-or-earlier).
59+
- Clarified that `--include-module-folders` discovers dependency manifests.
60+
Added a guide to scanning packages installed outside the lockfile with a
61+
CycloneDX SBOM.
5762

5863
## 2.13.2
5964

‎README.md‎

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -268,27 +268,29 @@ scan exit `0` on *both* findings and outages — the `soft_fail: 100` rule would
268268
never match, and real findings would stop blocking. That's usually not what you
269269
want.
270270

271-
### Upgrading to 2.13.0
272-
273-
Update pipeline rules and wrapper scripts that match specific exit codes:
274-
275-
| Outcome | Before 2.13.0 | From 2.13.0 |
276-
|---------|---------------|-------------|
277-
| Invalid flags or configuration | CLI validation: `1`; argparse: `2` | `2` for both; never suppressed by `--disable-blocking` |
278-
| Ctrl+C | `2` | `130`, or `0` with `--disable-blocking` |
279-
| Missing token or other setup failure | Fixed `2` or `3`, depending on the failure | Infrastructure-error code (`3` by default); both exit-code flags now apply |
280-
281-
Blocking findings still exit `1`. With `--strict-blocking`, existing blocking
282-
alerts can now fail jobs that previously passed. GitLab report findings also get
283-
new IDs once; see the [release notes](CHANGELOG.md#2130).
284-
285-
Configuration is validated before credentials or network setup. Use native
286-
JSON/TOML booleans such as `false`, a positive integer `timeout`, and an array of
287-
file paths for `files`. Invalid or unreadable configuration exits `2` even when
288-
credentials are missing or blocking is disabled.
289-
290-
For a compact comparison of failure-handling policies, see
291-
[Exit codes in CI](docs/ci-cd.md#exit-codes-in-ci).
271+
### Upgrading from 2.12 or earlier
272+
273+
2.13 and 2.14 change exit codes and what `--strict-blocking` checks:
274+
275+
- **Exit codes:** invalid flags or configuration exit `2`, Ctrl+C exits `130`,
276+
and setup failures such as a missing token use the infrastructure-error code
277+
(`3` by default). Blocking findings still exit `1`. See
278+
[Exit codes in CI](docs/ci-cd.md#exit-codes-in-ci).
279+
- **Config files** are validated before credentials or network setup. Use native
280+
JSON/TOML booleans, a positive integer `timeout` and an array for `files`.
281+
- **`--strict-blocking`** fails on existing Block alerts, counts each advisory
282+
separately, and compares code-only changes too, so pipelines can fail on
283+
alerts they used to pass. Without `--scm github` or `--scm gitlab`, that
284+
includes default-branch pushes, which need the `full-scans:list` and
285+
`repo:list` token scopes; see
286+
[API token permissions](docs/troubleshooting.md#api-token-permissions).
287+
- **GitHub Actions example:** if you copied it, put the
288+
[PR workflow](workflows/github-actions.yml) at the existing path and add the
289+
[push workflow](workflows/github-actions-push.yml) in the same change. See
290+
[Required GitHub checks](docs/ci-cd.md#required-github-checks).
291+
292+
GitLab report findings get new IDs once. The [release notes](CHANGELOG.md) have
293+
the details.
292294

293295
## Common gotchas
294296

‎docs/ci-cd.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ validating a new workflow.
9090
The CLI treats comparisons marked incomplete or returned with missing or
9191
invalid package lists as infrastructure failures. With `--strict-blocking`,
9292
incomplete existing-alert data is also an infrastructure failure. See the
93-
[2.13.0 migration table](../README.md#upgrading-to-2130) when updating existing CI rules.
93+
[upgrade notes](../README.md#upgrading-from-212-or-earlier) when updating existing CI rules.
9494

9595
## Config file usage in CI
9696

‎docs/cli-reference.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ docs.socket.dev use `SOCKET_SECURITY_API_KEY`. Either works. If both are set,
324324
| Parameter | Required | Default | Description |
325325
|:-------------------------|:---------|:--------|:------------------------------------------------------------------------------------------------------|
326326
| `--default-branch` | False | *auto* | Make this branch the default branch (auto-detected from git and CI environment when not specified) |
327-
| `--include-module-folders` | False | False | If enabled, re-includes the JS/TS module folders (`node_modules`, `bower_components`, `jspm_packages`) in manifest discovery. For other excluded directories, use `--include-dirs`. |
327+
| `--include-module-folders` | False | False | Includes supported manifests found under `node_modules`, `bower_components`, and `jspm_packages` in manifest discovery. Equivalent to `--include-dirs node_modules,bower_components,jspm_packages`. Other discovery exclusions still apply. For an inventory of installed packages, see [Packages installed outside the lockfile](troubleshooting.md#packages-installed-outside-the-lockfile). |
328328

329329
#### Output Configuration
330330
| Parameter | Required | Default | Description |

‎docs/troubleshooting.md‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
| Exit `0` despite expected blockers | Check `--disable-blocking`, remapping to `0`, and whether the run produced a full scan instead of a diff. Full scans do not gate on findings. |
1111

1212
See [Exit codes in CI](ci-cd.md#exit-codes-in-ci) for the flag combinations and
13-
[upgrade guidance](../README.md#upgrading-to-2130) for older pipeline rules.
13+
[upgrade notes](../README.md#upgrading-from-212-or-earlier) for older pipeline rules.
1414

1515
## Repository lookup or creation fails
1616

@@ -232,6 +232,27 @@ by the comparison step that follows.
232232
- Every vulnerability reported as `INSTALL ERROR`: the engine could not install your dependencies. Allow the ecosystem's package registry, or install dependencies in the target path before running `--reach`.
233233
- See **Reachability network access** in the [CLI reference](cli-reference.md) for the full list of hosts.
234234

235+
## Packages installed outside the lockfile
236+
237+
Packages present only on disk, such as packages that a `postinstall` script adds without updating the lockfile, can be missing from a manifest-based scan.
238+
239+
`--include-module-folders` lets the CLI discover supported manifests in `node_modules`, `bower_components`, and `jspm_packages`, including nested directories. Other directory and path exclusions still apply, and discovery skips symlinked directories. Socket resolves the dependencies the manifests declare, including `devDependencies`. This can add findings for dependencies absent from your installed tree. With `--reach`, the CLI also includes these files in the manifest upload for reachability analysis.
240+
241+
For npm projects, generate a CycloneDX software bill of materials (SBOM) from the installed tree. The example uses Node.js 20.18 or newer and npm 9 or newer. Run it from the scan root with a Socket API token configured.
242+
243+
```bash
244+
npm ci &&
245+
npx --yes @cyclonedx/cyclonedx-npm@7.0.1 --omit dev --output-format JSON --output-file installed.cdx.json &&
246+
socketcli --target-path .
247+
```
248+
249+
The example excludes development dependencies. To include them, remove `--omit dev` and run the install and generator without production-only npm settings or `NODE_ENV=production`. If npm reports a dependency-tree error, inspect it first. Use the generator's `--ignore-npm-errors` option only if the tree npm returns contains the packages you intend to scan.
250+
251+
The CLI discovers CycloneDX files such as `bom.json`, `*.cdx.json`, and `*.cyclonedx.json` (or their `.xml` equivalents) alongside the lockfile. Leave `--include-module-folders` off when you only want to add the SBOM.
252+
253+
- For PR comparisons, generate the SBOM with the same settings before the default-branch and PR scans. Add `--ignore-commit-files` to the PR scan so it runs a comparison even on commits that change no tracked manifest. A generated SBOM outside the Git diff does not trigger comparison mode on its own.
254+
- The SBOM covers packages present when it is generated. Packages fetched later or installed outside the tree, such as tools fetched into the npm cache by `npx`, are outside its scope.
255+
235256
## Dashboard vs CLI result counts
236257

237258
Differences in result counts can be valid, even when filtering appears similar.

‎socketsecurity/config.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,9 @@ def create_argument_parser() -> argparse.ArgumentParser:
11201120
dest="include_module_folders",
11211121
action="store_true",
11221122
default=False,
1123-
help="Enabling including module folders like node_modules"
1123+
help="Include manifests found under node_modules, bower_components, and jspm_packages. "
1124+
"Use a CycloneDX SBOM to inventory installed packages. "
1125+
"See https://github.com/SocketDev/socket-python-cli/blob/main/docs/troubleshooting.md"
11241126
)
11251127

11261128
# Reachability Configuration

‎tests/unit/test_include_dirs.py‎

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
"""Tests for --include-dirs (and the now-functional --include-module-folders).
2-
3-
Covers config parsing of the comma-separated directory names and that re-including a
4-
normally-excluded directory (e.g. build) lets Core.find_files discover manifests under it.
5-
"""
1+
"""Test directory selection for manifest discovery."""
2+
import sys
63
import types
4+
from contextlib import nullcontext
5+
from pathlib import Path
76
from unittest.mock import MagicMock
87

8+
import pytest
9+
10+
from socketsecurity import socketcli
911
from socketsecurity.config import CliConfig
1012
from socketsecurity.core import Core
1113
from socketsecurity.core.socket_config import (
@@ -95,3 +97,53 @@ def test_unexcluding_does_not_mutate_shared_defaults():
9597
assert "build" not in config.excluded_dirs
9698
assert default_exclude_dirs == before
9799
assert "build" in default_exclude_dirs
100+
101+
102+
@pytest.mark.parametrize(("flags", "include_node", "include_other"), [
103+
pytest.param([], False, False, id="default"),
104+
pytest.param(["--include-module-folders"], True, True, id="module-folders"),
105+
pytest.param(["--include-dirs", "node_modules,bower_components,jspm_packages"], True, True, id="include-dirs"),
106+
pytest.param(["--include-module-folders", "--exclude-paths", "node_modules"], False, True, id="exclude-paths"),
107+
])
108+
def test_cli_manifest_selection(tmp_path, monkeypatch, mocker, flags, include_node, include_other):
109+
root_files = {"package-lock.json", "installed.cdx.json"}
110+
node_files = {
111+
"node_modules/@scope/parent/package.json",
112+
"node_modules/@scope/parent/node_modules/child/yarn.lock",
113+
"node_modules/@scope/parent/requirements.txt",
114+
}
115+
other_modules = {"bower_components/pkg/package.json", "nested/jspm_packages/pkg/package.json"}
116+
excluded_files = {"node_modules/.package-lock.json", "vendor/requirements.txt", ".venv/requirements.txt"}
117+
for name in root_files | node_files | other_modules | excluded_files:
118+
manifest = tmp_path / name
119+
manifest.parent.mkdir(parents=True, exist_ok=True)
120+
manifest.touch()
121+
122+
sdk = MagicMock()
123+
sdk.org.get.return_value = {"organizations": {"org-id": {"slug": "test-org", "plan": "enterprise"}}}
124+
sdk.report.supported.return_value = {
125+
"npm": {name: {"pattern": name} for name in ("package.json", "package-lock.json", "yarn.lock")},
126+
"pypi": {"requirements": {"pattern": "requirements.txt"}},
127+
"cdx": {"json": {"pattern": "{bom,c{yclone,}dx[-.]*,*[-.]c{yclone,}dx}.json"}},
128+
}
129+
mocker.patch.object(socketcli, "build_socket_sdk", return_value=sdk)
130+
mocker.patch.object(socketcli, "CliClient")
131+
mocker.patch.object(socketcli, "StreamingLogs", return_value=nullcontext(MagicMock()))
132+
mocker.patch.object(socketcli, "apply_git_context", return_value=(False, None))
133+
upload = mocker.patch.object(Core, "create_full_scan", return_value=types.SimpleNamespace(id="scan"))
134+
monkeypatch.setattr(sys, "argv", [
135+
"socketcli", *BASE_ARGS, "--branch", "main", "--target-path", str(tmp_path), *flags,
136+
])
137+
138+
with pytest.raises(SystemExit) as exit_status:
139+
socketcli.cli()
140+
141+
assert exit_status.value.code == 0
142+
upload.assert_called_once()
143+
submitted = {Path(path).relative_to(tmp_path).as_posix() for path in upload.call_args.args[0]}
144+
expected = root_files.copy()
145+
if include_other:
146+
expected |= other_modules
147+
if include_node:
148+
expected |= node_files
149+
assert submitted == expected

0 commit comments

Comments
 (0)