Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions openspec/changes/2130-the-toolrun-of-osv-scanner-and/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

## Decisions

- **Use a streamed SHA-256 over the database files in sorted filename order for the osv-scanner digest.**: The 36 MB database exceeds the file-size cap of a plain file hash. A streamed hash avoids loading the file into memory. Sorting by filename makes the digest deterministic across runs on the same database.
- **Use the SHA-256 of `ruff --show-settings` JSON output for the Ruff digest.**: The issue asks for the resolved configuration, not the raw file. `--show-settings` outputs the final merged configuration as JSON, which is the exact input Ruff uses for the scan.
- **Store both digests as optional string fields on the existing ToolRun record.**: The ToolRun record already lives in the SARIF run property bag (#1983). Adding a field is the minimal change. Optional keeps the record valid for tools that do not produce a digest.
- **Name the database files as the ruleset files of osv-scanner.**: The request already has this seam (`ruleset_files`), the record already turns it into `ruleset_digest`, and Trivy uses it for its database metadata. The database of osv-scanner is the file `all.zip` in one directory for each ecosystem (`osv-scalibr/PyPI/all.zip`), so the hook returns `sorted(read.glob("*/all.zip"))` of the `ToolDataDir` that `data_dirs()` gives. The digest covers the content and not the paths, so it is the same for the same database in another cache directory.
- **Digest a ruleset file below a read-only bind as a stream, with no size limit.**: The npm database is 210 MB, over the 50 MiB limit, and the sandbox executor refuses a file over the limit. A file below a bind is data that devops-cli or the user put in the cache of the tool. It is not a file of the reviewed tree. A stream needs no memory, so the limit has no reason there.
- **Keep the limit for every other file.**: A config file or a ruleset file of the reviewed tree is not trusted. `ARCHITECTURE.md` asks for a size check before a read, and the contract test pins the refusal. Only the ruleset files below a bind of the request skip the limit. A config file never does.
- **Make `file_digest` read as a stream.**: It is the one function that digests a file for the record and for the benchmarks. `hashlib.file_digest` gives the same SHA-256, so every digest stays the same.
- **No change to the Ruff scanner, and a limit that a person decides.**: The Ruff digest follows the config files that Ruff finds by name (`ruff.toml`, `.ruff.toml`, `pyproject.toml`). Criterion 2 is met for them. A base file named by `extend` is part of the resolved configuration, and the digest does not cover it, so criterion 2 is not met for a config that uses `extend`. The plan does not close this gap, for two reasons. Ruff has no command that lists its config files, and no output of Ruff is stable enough to digest (see Feasibility). To follow `extend`, devops-cli would need its own reader of the chain: parse each file with `tomllib`, resolve a relative path against the file that names it, and guard against a loop. That is a new hand-written helper, and it is more than the issue asks. The plan writes the limit into `docs/SELF_IMPROVEMENT.md`. A person decides whether a follow-up issue must cover the `extend` chain.

## Rejected alternatives

- **Use the database's modified time combined with a digest of its index.**: The modified time is not content-addressed: a file can be rewritten with the same mtime, or the mtime can change without a content change. A streamed content hash is more robust and matches the acceptance criterion that the digest changes when the database changes.
- **Hash the raw `ruff.toml` or `pyproject.toml` file for the Ruff digest.**: The issue and the proposed design both state the digest covers the resolved configuration, not the raw file. Ruff merges defaults with the project config, so the raw file does not capture the full input.
- **The modified time of the database with a digest of its index.**: The scratch download of osv-scanner 2.6.0 wrote one file, `osv-scalibr/PyPI/all.zip`, and no index or metadata file. The modified time changes when the same content is fetched again, so the digest would change with no change of the database. A digest of the zip directory entries would need a new reader of the zip format in devops-cli. A stream of the file is simpler and exact.
- **Lift the limit for all files of a record.**: A config file or a ruleset file of a reviewed tree comes from the code under review. A limit on it is a threshold that only the owner may lower, and the contract test pins it.
- **A new field on `ToolRun` (`database_digest`, `config_digest: str | None`).**: `ToolRun` already has `config_digest: str` and `ruleset_digest: str`, and the Tool Runs table and the SARIF export show them. A second field would duplicate them.
- **The digest of `ruff check --show-settings`.**: The output is plain text, it holds absolute paths of the checkout, and `--isolated` ignores the config file (see Feasibility). Stripping the paths would be a new hand-written parser of a text format that Ruff does not promise to keep.
- **`get_or_compute_snapshot_digest` for the record.**: It reads `data.cache_dir/osv_db`, which the scan does not read, and it uses modified times.
54 changes: 32 additions & 22 deletions openspec/changes/2130-the-toolrun-of-osv-scanner-and/proposal.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,47 @@

## Why

Issue #2130: the ToolRun of an osv-scanner run records no digest of its advisory database, and the ToolRun of a Ruff run records no digest of the Ruff configuration it read. Without these digests, a user cannot verify which database version or configuration produced a given scan result. The 36 MB PyPI database exceeds the file-size cap of the record's digest step, so a plain file hash is not possible.
Issue #2130: the ToolRun of an osv-scanner run records no digest of its advisory database, and the ToolRun of a Ruff run is said to record no digest of the Ruff configuration. The issue asks for two results:

## What Changes

- The ToolRun of an osv-scanner run records a `database_digest` field: a streamed SHA-256 over the advisory database files in sorted filename order.
- The ToolRun of a Ruff run records a `config_digest` field: the SHA-256 of the JSON output of `ruff --show-settings` for the target.
- The `review-tool-runs` capability gains two requirements that pin the digest behavior.
- The change fits one pull request.

## Capabilities
- The ToolRun of an osv-scanner run changes when its database changes, and stays the same when it does not.
- The ToolRun of a Ruff run changes when the Ruff configuration of the target changes.

### New Capabilities
The check on `origin/main` shows that the first result is missing. The second result exists for the config files that Ruff finds by name, and not for a base file named by `extend` (see Acceptance).

None.

### Modified Capabilities
## What Changes

- `review-tool-runs`: osv-scanner toolrun records database digest, ruff toolrun records configuration digest
- `OsvScanner` names the `all.zip` file of each ecosystem in its offline database as ruleset files, as `TrivyScanner` names its metadata file. The `ruleset_digest` of the ToolRun then follows the content of the database.
- The record digests a ruleset file below a read-only bind of the request (the data directory of the tool) as a stream, whatever its size. A file that is not below a bind, and every config file, keep the 50 MiB limit. `file_digest` in `src/devops_cli/ai/run_store.py` reads a file as a stream, with the same result.
- `docs/SELF_IMPROVEMENT.md` says what the osv-scanner ruleset digest covers.
- Ruff gets no code change: its `config_digest` already follows the config files that Ruff finds by name. The `extend` limit is written down and left to a person.
- The change has a spec delta in `review-tool-runs`: the ToolRun record in `review.md` and in the SARIF export is observable.
- The change fits one pull request.

## Impact
## Acceptance

- Code: `src/devops_cli/review/scanners/osv_scanner.py`, `src/devops_cli/review/scanners/ruff.py`
- Tests: `tests/test_review_tool_runs.py`
- Criterion 1, the osv-scanner digest follows the database: group 1 lets the record digest a large database file, and group 2 names the database files in the request.
- Criterion 2, the Ruff digest follows the Ruff configuration: met on `main` for the files that Ruff finds by name, and pending a person for a base file named by `extend`.
- Met: `RuffScanner.config_names` lists `.ruff.toml`, `ruff.toml` and `pyproject.toml`, and the test `test_a_changed_config_file_of_the_tree_changes_the_config_digest` pins it. Group 3 runs that test again.
- Pending a person: a Ruff config that uses `extend` has a base file that is part of the resolved configuration, and the digest does not cover it. A change of that base file leaves the `config_digest` the same. The plan adds no code for it, because it needs a reader of the `extend` chain in devops-cli, and the choice is a decision of a person (see `design.md`). Group 3 writes the limit into `docs/SELF_IMPROVEMENT.md`.

## Feasibility

- The issue states the ToolRun records no digest today and cites #2096 for the 36 MB database and the file-size cap. The capability `review-tool-runs` exists in the spec list, confirming the ToolRun record is a defined concept.
- Python's `hashlib` supports streaming SHA-256 over file objects, so the 36 MB database can be hashed without loading it into memory. This satisfies the file-size cap constraint.
- Ruff's `--show-settings` flag outputs the resolved configuration as JSON to stdout. A test can invoke it on a fixture directory and hash the output offline.
- First checks the implementation runs against the real system: `uv run ruff --show-settings --isolated` to confirm the flag exists and outputs JSON; `uv run devops review scan --help` to confirm the scan command and its flags; `ls` of the osv-scanner database directory to confirm the file layout for the streamed hash.
All commands ran on 2026-11-01 on `origin/main`.

- The osv-scanner record has no database digest today. `git grep -n ruleset_files -- src/devops_cli/security/osv_scanner.py` prints nothing, so `OsvScanner` keeps the default hook of `BaseSecurityScanner` (`ruleset_files` returns no file). `docs/SELF_IMPROVEMENT.md` says the same: "none: the database has no metadata file yet".
- The database is one file. `OSV_SCANNER_LOCAL_DB_CACHE_DIRECTORY=<scratch> osv-scanner scan source --offline --offline-vulnerabilities --download-offline-databases --lockfile uv.lock` (osv-scanner 2.6.0, a `uv.lock` that has one PyPI package) wrote `find <scratch> -type f` = `osv-scalibr/PyPI/all.zip`, 37604846 bytes. There is no index or metadata file next to it.
- The size claim of the issue is half right. The limit of the digest step is `DEFAULT_REPOMAP_MAX_FILE_SIZE_BYTES` = 52428800 (50 MiB, `src/devops_cli/config/defaults.py`). A HEAD request for `https://osv-vulnerabilities.storage.googleapis.com/<ecosystem>/all.zip` gave `Content-Length` 37604846 for PyPI (under the limit) and 219697992 for npm (over it). Go, Maven, crates.io, RubyGems, Packagist and NuGet are between 2.6 MB and 12.7 MB.
- Naming the database file as a ruleset file today stops the scan. A scratch script built a `ToolRequest` with a sparse `all.zip` of 52428801 bytes as `ruleset_files` and called `record_tool_run`. It raised `SandboxValidationError: The file .../npm/all.zip cannot be read as a regular file of at most 52428800 bytes`. The sandbox executor builds its record with `require_files=True`, so a review with an npm database would fail. This is why group 1 comes first.
- A stream needs no memory limit. `hashlib.file_digest` (standard library, Python 3.11 and later) digested the same 52428801-byte file in 0.07 s on Python 3.14.10 (`uv run python`).
- The Ruff digest exists. `git grep -n config_names -- src/devops_cli/security/ruff.py` shows `config_names = CONST_RUFF_CONFIG_FILE_NAMES` and `config_search_upward = True`, and `record_tool_run` digests `request.config_files`. `uv run devops ci test tests/security/test_review_tool_runs.py -k "changed_config_file_of_the_tree or names_every_config_file_it_may_read"` ran 3 tests, 3 passed. The `ruff` case edits `pyproject.toml` and checks that the digest changes, and that it stays the same for an unchanged file.
- The Ruff command of the first plan does not work. `ruff --show-settings a.py` (ruff 0.16.8) printed `error: unexpected argument '--show-settings' found`. `ruff check --show-settings a.py` prints plain text, not JSON, with absolute paths (`cache_dir`, `file_resolver.project_root`), and `--isolated` ignores `ruff.toml` (`linter.line_length = 88` where the file sets 100). So a digest of that output would depend on the checkout path. The plan does not use it.
- A Ruff `extend` is not covered. With `ruff.toml` = `extend = "base.toml"` and `base.toml` = `line-length = 77`, `ruff check --show-settings a.py` printed `linter.line_length = 77`. The digest covers `ruff.toml` but not `base.toml`.
- Nothing newer did this work. `git log origin/main --since=2026-10-24 -- src/devops_cli/security/osv_scanner.py src/devops_cli/security/tool_executor.py src/devops_cli/ai/run_store.py` shows no commit about a database digest. The active changes under `openspec/changes/` that name osv-scanner or `file_digest` are this one and 2116, which adds only a test fixture requirement. An open-issue search for "osv digest" finds only #2130.
- `get_or_compute_snapshot_digest` in `src/devops_cli/security/osv_scanner.py` is not the answer. It hashes the names and modified times of `data.cache_dir/osv_db`, a directory that the scan does not read (the scan reads `osv-scalibr` below `OSV_SCANNER_LOCAL_DB_CACHE_DIRECTORY`). It feeds the `AdvisoryAnchor` of findings. `openspec/specs/config-commands/spec.md` pins it, so the plan keeps it as it is.

## Existing Tool Considered

Python's `hashlib` (standard library) provides the streamed SHA-256. Ruff's `--show-settings` flag outputs the resolved configuration. No external library is needed. The change adds two small functions in devops-cli's own scanner adapter code and one field to the ToolRun record. The written cost is roughly 80 lines of code and tests, well within one step's budget. No internal code is deleted.
- `hashlib.file_digest` from the Python standard library streams a file through SHA-256. No new dependency and no hand-written chunk loop.
- The record already has the right place: `BaseSecurityScanner.ruleset_files`, `ToolDataDir`, and the `ruleset_digest` of `ToolRun`. `TrivyScanner.ruleset_files` is the model for the osv-scanner hook.
- The change must keep passing: `tests/ai/test_tool_executor_contract.py::test_a_file_that_cannot_be_digested_is_refused` (a file over the limit that is not below a bind stays refused), `tests/security/test_review_tool_runs.py::test_each_review_scan_names_the_config_files_and_the_ruleset_files_its_tool_reads`, and `tests/security/test_scanner_tool_executor.py::test_a_tool_that_reads_a_fetched_database_gets_it_read_only_by_its_variable`.
- Code removed: `_is_small_regular_file` in `src/devops_cli/security/tool_executor.py` becomes a check for a regular file with an optional limit. `file_digest` no longer reads a whole file into memory.
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
## ADDED Requirements

### Requirement: osv-scanner toolrun records database digest
The ToolRun of an osv-scanner run SHALL record a `database_digest` field that is a SHA-256 hex digest computed by streaming the advisory database files in sorted filename order. The digest SHALL change when the database content changes and SHALL stay the same when it does not.
### Requirement: The ruleset digest of an osv-scanner run follows its offline database
The record of an osv-scanner run SHALL name the file `all.zip` of each ecosystem in the `osv-scalibr` directory of its offline database as ruleset files. So the `ruleset_digest` of the record SHALL change when the content of a database file changes, and SHALL stay the same when it does not. A run with no database file SHALL name no ruleset file.

#### Scenario: digest changes when database changes
- **WHEN** the advisory database files differ between two osv-scanner runs
- **THEN** the `database_digest` in the two ToolRun records is different
#### Scenario: The same database
- **WHEN** osv-scanner scans a tree two times and its database holds `PyPI/all.zip` and `npm/all.zip` both times
- **THEN** the two records have the same `ruleset_digest`
- **AND** the request names both files as ruleset files

#### Scenario: digest stable for unchanged database
- **WHEN** the advisory database files are identical between two osv-scanner runs
- **THEN** the `database_digest` in the two ToolRun records is the same
#### Scenario: A changed database
- **WHEN** the bytes of `PyPI/all.zip` change between two scans
- **THEN** the `ruleset_digest` of the second record differs from the first

### Requirement: ruff toolrun records configuration digest
The ToolRun of a Ruff run SHALL record a `config_digest` field that is a SHA-256 hex digest of the resolved configuration that Ruff used for the target. The digest SHALL change when the resolved configuration changes and SHALL stay the same when it does not.
#### Scenario: No database
- **WHEN** the offline database directory of osv-scanner holds no `all.zip`
- **THEN** the request names no ruleset file
- **AND** the `ruleset_digest` is the digest of no files

#### Scenario: digest changes when configuration changes
- **WHEN** the Ruff configuration of the target differs between two Ruff runs
- **THEN** the `config_digest` in the two ToolRun records is different
### Requirement: A ruleset file of a data directory is digested at any size
The record SHALL digest a ruleset file that lies below a read-only bind of the request by reading it as a stream, whatever its size. A ruleset file that lies below no bind, and every config file, SHALL still be refused when it is larger than 50 MiB (`DEFAULT_REPOMAP_MAX_FILE_SIZE_BYTES`).

#### Scenario: digest stable for unchanged configuration
- **WHEN** the Ruff configuration of the target is identical between two Ruff runs
- **THEN** the `config_digest` in the two ToolRun records is the same
#### Scenario: A large database file below a bind
- **WHEN** a request names a ruleset file of 50 MiB and one byte below one of its read-only binds
- **THEN** the run starts and the `ruleset_digest` is the digest of the content of that file

#### Scenario: A large ruleset file below no bind
- **WHEN** a request names a ruleset file of 50 MiB and one byte that lies below no read-only bind
- **THEN** the executor refuses the request with a `SandboxValidationError` and the tool does not start

#### Scenario: A large config file below a bind
- **WHEN** a request names a config file of 50 MiB and one byte that lies below a read-only bind
- **THEN** the executor refuses the request with a `SandboxValidationError` and the tool does not start
Loading
Loading