From 160a6d97dae339f9e43fce126b6252c44900cd6d Mon Sep 17 00:00:00 2001 From: blerfx2 Date: Mon, 19 Oct 2026 09:46:10 -0600 Subject: [PATCH] resolve sbom: Emit hyphenated vulnerabilities.json keys and add Claude provider resolve sbom wrote snake_case keys (cve_id, affected_function), but resolve reach, resolve remediate, and the vulnerabilities.json docs use hyphenated keys (cve-id, affected-function), so SBOM output could not be fed to those tools directly. Serialize Vulnerability with hyphenated aliases; snake_case input is still accepted. Add `-L claude`, which extracts the affected file/function via the Claude Code CLI (`claude -p`). Auth comes from ANTHROPIC_API_KEY or a prior `claude` login. The prompt is sent on stdin because --allowedTools is variadic and would swallow a positional prompt. Update the example output and SBOM docs to match. Co-Authored-By: Claude Opus 5.5 --- docs/components/resolve-cli/sbom.md | 10 ++++- docs/examples/sbom.md | 18 ++++----- resolve-cli/src/resolve/sbom/example_out.json | 2 +- resolve-cli/src/resolve/sbom/llm.py | 38 +++++++++++++++++++ resolve-cli/src/resolve/sbom/main.py | 5 ++- .../src/resolve/sbom/schema/vuln_spec.py | 9 ++++- 6 files changed, 67 insertions(+), 15 deletions(-) diff --git a/docs/components/resolve-cli/sbom.md b/docs/components/resolve-cli/sbom.md index 274747850..553994923 100644 --- a/docs/components/resolve-cli/sbom.md +++ b/docs/components/resolve-cli/sbom.md @@ -20,7 +20,7 @@ Finally, an LLM is used to extract the vulnerable file and function from the nat ### Options - `-o`, `--out` -- path for the output `vulnerabilities.json` (defaults to `.vuln.json`) - - `-L`, `--llm-provider` `[{gemini,ollama,opencode}]` -- LLM backend used to extract the affected file and function from each CVE description. Omit it to skip extraction (those fields are reported as `UNKNOWN`); passing `-L` with no value uses `opencode`. + - `-L`, `--llm-provider` `[{claude,gemini,ollama,opencode}]` -- LLM backend used to extract the affected file and function from each CVE description. Omit it to skip extraction (those fields are reported as `UNKNOWN`); passing `-L` with no value uses `opencode`. - `--id` `[ID ...]` -- only report the given vulnerability id(s) - `--min-score` -- minimum CVSS v3 base score for a CVE to be included. When set, CVEs that have no v3 score are also excluded (by default they are included). @@ -41,7 +41,13 @@ See the [SBOM example](../../examples/sbom.md) for an end-to-end walkthrough. ## LLM Providers -Affected-file/function extraction is optional; if you do not pass `-L`, those fields are left as `UNKNOWN` and no LLM is contacted. When enabled, the supported providers are Gemini, Ollama, and Opencode. +Affected-file/function extraction is optional; if you do not pass `-L`, those fields are left as `UNKNOWN` and no LLM is contacted. When enabled, the supported providers are Claude, Gemini, Ollama, and Opencode. + +### Claude + +To use Claude, the `claude` CLI ([Claude Code](https://docs.anthropic.com/en/docs/claude-code)) must be installed and authenticated, either through a prior `claude` login or by setting the `ANTHROPIC_API_KEY` environmental variable. + +This method uses the Claude Code CLI's default model. ### Gemini diff --git a/docs/examples/sbom.md b/docs/examples/sbom.md index 5c503fc64..cc1166a5e 100644 --- a/docs/examples/sbom.md +++ b/docs/examples/sbom.md @@ -44,7 +44,7 @@ resolve sbom example.spdx.json -o vulnerabilities.json -L ollama ``` !!! note - The lookup queries [NIST's NVD](https://nvd.nist.gov/) and [MITRE's CWE database](https://cwe.mitre.org/) over the network, so a run needs internet access, plus the chosen LLM provider (`gemini`, `ollama`, or `opencode`) installed and authenticated. Without `-L`, the CVEs are still reported, but `affected_file` and `affected_function` come back as `UNKNOWN`. + The lookup queries [NIST's NVD](https://nvd.nist.gov/) and [MITRE's CWE database](https://cwe.mitre.org/) over the network, so a run needs internet access, plus the chosen LLM provider (`gemini`, `ollama`, or `opencode`) installed and authenticated. Without `-L`, the CVEs are still reported, but `affected-file` and `affected-function` come back as `UNKNOWN`. Under the hood the tool runs four steps (see the [component docs](../components/resolve-cli/sbom.md) for detail): @@ -61,14 +61,14 @@ The tool writes a [`vulnerabilities.json`](../concepts/vulnerabilities-json.md) { "vulnerabilities": [ { - "cve_id": "CVE-2026-34085", - "cve_description": "fontconfig before 2.17.1 has an off-by-one error in allocation during sfnt capability handling, leading to a one-byte out-of-bounds write, and potentially a crash or code execution. This is in FcFontCapabilities in fcfreetype.c.", - "package_name": "fontconfig:fontconfig_project::fontconfig", - "package_version": "2.2.1", - "cwe_id": "193", - "cwe_name": "Off-by-one Error", - "affected_file": "fcfreetype.c", - "affected_function": "FcFontCapabilities" + "cve-id": "CVE-2026-34085", + "cve-description": "fontconfig before 2.17.1 has an off-by-one error in allocation during sfnt capability handling, leading to a one-byte out-of-bounds write, and potentially a crash or code execution. This is in FcFontCapabilities in fcfreetype.c.", + "package-name": "fontconfig:fontconfig_project::fontconfig", + "package-version": "2.2.1", + "cwe-id": "193", + "cwe-name": "Off-by-one Error", + "affected-file": "fcfreetype.c", + "affected-function": "FcFontCapabilities" } ] } diff --git a/resolve-cli/src/resolve/sbom/example_out.json b/resolve-cli/src/resolve/sbom/example_out.json index 8e76676d4..6cc4cd1c8 100644 --- a/resolve-cli/src/resolve/sbom/example_out.json +++ b/resolve-cli/src/resolve/sbom/example_out.json @@ -1 +1 @@ -{"vulnerabilities":[{"cve_id":"CVE-2026-34085","cve_description":"fontconfig before 2.17.1 has an off-by-one error in allocation during sfnt capability handling, leading to a one-byte out-of-bounds write, and potentially a crash or code execution. This is in FcFontCapabilities in fcfreetype.c.","package_name":"fontconfig:fontconfig_project::fontconfig","package_version":"2.2.1","cwe_id":"193","cwe_name":"Off-by-one Error","affected_file":"fcfreetype.c","affected_function":"FcFontCapabilities"}]} \ No newline at end of file +{"vulnerabilities":[{"cve-id":"CVE-2026-34085","cve-description":"fontconfig before 2.17.1 has an off-by-one error in allocation during sfnt capability handling, leading to a one-byte out-of-bounds write, and potentially a crash or code execution. This is in FcFontCapabilities in fcfreetype.c.","package-name":"fontconfig:fontconfig_project::fontconfig","package-version":"2.2.1","cwe-id":"193","cwe-name":"Off-by-one Error","affected-file":"fcfreetype.c","affected-function":"FcFontCapabilities"}]} \ No newline at end of file diff --git a/resolve-cli/src/resolve/sbom/llm.py b/resolve-cli/src/resolve/sbom/llm.py index a1c72c7f4..3683b0eb5 100644 --- a/resolve-cli/src/resolve/sbom/llm.py +++ b/resolve-cli/src/resolve/sbom/llm.py @@ -79,6 +79,44 @@ def _query(self, payload: str) -> str: return process.stdout.splitlines()[-1] +class Claude(LLM): + """Shells out to the Claude Code CLI. Auth comes from ANTHROPIC_API_KEY or a prior `claude` login.""" + + def __init__(self, model: str | None = None): + import shutil + + if shutil.which("claude") is None: + raise APIConnectionError() from FileNotFoundError("`claude` CLI not found on PATH") + self.model = model + + def _query(self, payload: str) -> str: + import subprocess + + # Web tools are pre-approved (as for Opencode) so the CLI never stops to + # ask for permission; everything else stays denied. + agent_command = ["claude", "-p", "--allowedTools", "WebFetch", "WebSearch"] + if self.model is not None: + agent_command += ["--model", self.model] + + # The prompt goes over stdin: --allowedTools is variadic and would + # swallow a trailing positional prompt. + process = subprocess.run( + agent_command, + input=payload, + text=True, + stderr=subprocess.PIPE, + stdout=subprocess.PIPE, + ) + if process.returncode != 0: + print(process.stderr) + raise LLMError(process.stderr or process.stdout) + + lines = process.stdout.strip().splitlines() + if not lines: + raise LLMError("Empty response") + return lines[-1].strip().strip("`") + + class Gemini(LLM): def __init__(self, model: str = 'gemini-2.5-flash', **kwargs) -> None: from google import genai diff --git a/resolve-cli/src/resolve/sbom/main.py b/resolve-cli/src/resolve/sbom/main.py index 47c43c479..19c8526a0 100644 --- a/resolve-cli/src/resolve/sbom/main.py +++ b/resolve-cli/src/resolve/sbom/main.py @@ -37,7 +37,7 @@ def parse_args(argv: list[str] | None = None) -> argparse.Namespace: "-L", "--llm-provider", nargs="?", - choices=["gemini", "ollama", "opencode"], + choices=["claude", "gemini", "ollama", "opencode"], const="opencode", default=None, ) @@ -81,7 +81,7 @@ def output_json(vulns: list[Vulnerability], output_path: Path): doc = VulnerabilityDocument(vulnerabilities=vulns) with open(output_path, 'w') as f: - f.write(doc.model_dump_json(indent=2)) + f.write(doc.model_dump_json(indent=2, by_alias=True)) def cve2vuln( @@ -150,6 +150,7 @@ def main(argv: list[str] | None = None) -> int: asyncio.run(dep_lookup(deps, **kwargs)) provider = { + "claude": llm.Claude, "gemini": llm.Gemini, "ollama": llm.Ollama, "opencode": llm.Opencode, diff --git a/resolve-cli/src/resolve/sbom/schema/vuln_spec.py b/resolve-cli/src/resolve/sbom/schema/vuln_spec.py index c018238ef..244b8d363 100644 --- a/resolve-cli/src/resolve/sbom/schema/vuln_spec.py +++ b/resolve-cli/src/resolve/sbom/schema/vuln_spec.py @@ -4,7 +4,7 @@ from typing import Annotated -from pydantic import BaseModel, Field +from pydantic import BaseModel, ConfigDict, Field from resolve.sbom.schema.nist_api import CveId @@ -14,6 +14,13 @@ class VulnerabilityDocument(BaseModel): class Vulnerability(BaseModel): + # vulnerabilities.json uses hyphenated keys (e.g. "cve-id"), which is what + # reach, remediate, and CVEAssert read. Serialize with by_alias=True. + model_config = ConfigDict( + alias_generator=lambda name: name.replace("_", "-"), + populate_by_name=True, + ) + cve_id: CveId cve_description: str package_name: str