CVE reachability analysis: does your code use it?
By the CapyShield team · · 5 min read
In short
CVE reachability analysis checks whether your running code actually uses the vulnerable dependency a scanner reported, from whether the package is imported in production down to whether the vulnerable function is called. Package-level analysis works from lockfiles and imports and is the dependable first pass; function-level tools such as govulncheck and OSV-Scanner's call analysis are more precise for the languages they support. Record the outcome as VEX so the scanner stops re-reporting findings that don't apply.
Reachability analysis answers one question for each CVE a scanner reports: does my code actually use the vulnerable dependency? Most of the noise in a dependency scan comes from packages that are installed but never loaded in production. This post explains the levels of reachability, what each one can and can't prove, and how to turn the answer into something your scanner and your auditors understand.
What is CVE reachability analysis?
It is the process of checking whether there is a path from your running code to the vulnerable code a CVE describes. A scanner only checks the first condition below. Reachability analysis checks the rest, from coarse to precise:
- Present: the vulnerable package version is in your lockfile or image. This is what every scanner reports.
- Loaded: production code imports the package, directly or through another package that does.
- Called: the specific vulnerable function is on a call path from your code.
- Exploitable: an attacker can control the input that reaches that function.
Each level removes more false positives and costs more effort to prove. Level 2 can be decided from manifests and import statements. Level 3 needs a call graph. Level 4 usually needs a person who knows the application.
Why do scanners report CVEs your code never uses?
Because they match versions, not usage. Trivy, Grype and OSV-Scanner read your lockfiles or image, list the package versions, and compare them with advisory databases. That is fast and reliable for its purpose, but it means a scanner reports the same CVE whether the package is:
- a test runner or build tool that never ships to production,
- a direct dependency someone added and stopped importing,
- a transitive dependency pulled in by a feature you don't use, or
- a library your server calls on every request.
Only the last one is urgent. Treating them all the same either burns engineering time on upgrades that change nothing, or trains people to ignore the report, including the findings that matter.
What is the difference between package-level and function-level reachability?
Package-level reachability asks whether production code loads the package at all. It works from lockfiles, manifests and import statements, so it is fast, language-agnostic in principle and does not need to build your code. Its blind spot is that it treats the whole package as one unit.
Function-level reachability asks whether the vulnerable symbol is called. Take CVE-2021-23337: lodash before 4.17.21 is vulnerable to command injection through its template function. A service that imports lodash only for debounce is reachable at package level and not at function level. Two tools do function-level analysis as part of a normal scan:
- govulncheck, the Go team's tool, reports vulnerabilities based on which functions in your code transitively call vulnerable functions.
- OSV-Scanner runs call analysis for Go by default using the govulncheck library, and has experimental call analysis for Rust, enabled with
--call-analysis. Its docs warn that Rust call analysis compiles the project and runs dependencies' build scripts.
Function-level analysis is more precise but needs per-language tooling, a buildable project and advisories that name the vulnerable symbols. For JavaScript and Python, where dynamic dispatch and runtime imports are common, package-level analysis is the dependable first pass and function-level review is a manual follow-up for the findings that remain.
How do you check reachability by hand?
For a single finding in a Node.js project, three questions settle most cases:
# 1. Why is it installed? Shows every dependency path to the package.
npm ls lodash
# 2. Does production code import it? Exclude tests.
grep -rnE "(from|require\()\s*['\"]lodash" src/ --include='*.js' --include='*.ts'
# 3. Does it call the vulnerable function?
grep -rn "template(" src/If the package is only under devDependencies, or no production file imports it, the CVE is not in your execute path. If it is imported but the vulnerable function never appears, you likely have a function-level not_affected, and the advisory's description of the vulnerable code is your evidence. If it is called, check whether user input reaches it. This takes a few minutes per finding, which is why it pays to automate the package-level step and save manual review for what's left.
How does CapyShield decide reachability?
capyshield vex automates the package-level step for Node.js and Python. It reads a Trivy, Grype or OSV-Scanner JSON report and derives four facts for each package from your lockfiles, manifests and production imports: in_lockfile, dependency_scope (direct, transitive, dev_only or not_found), imported_in_prod and matched_files_count. Test files and exclude_paths don't count as production.
capyshield vex --input trivy-report.json --repo . \
--output release.openvex.json --summary vex.mdEach finding is then decided in a fixed order, and the first match wins:
- A local rule in
.capyshield/vex-rules.yaml, then team rules from CapyShield Cloud with--sync. - A
dev_onlypackage, or a direct dependency production code never imports:not_affectedwithvulnerable_code_not_in_execute_path. - A package imported in production:
affected, with an upgrade action statement. - Anything else, typically a transitive package nobody imports directly:
under_investigation.
The last rule is deliberate. Whether a transitive package is loaded depends on what its parent does with it, which import analysis can't see, so CapyShield reports it as unknown rather than guessing. It does not build call graphs either: reachability is static and package-level. On the demo project in the CLI's README, a Trivy report of 22 CVEs came out as 6 affected, 2 not_affected and 14 under_investigation. The facts behind every decision are written into the OpenVEX status_notes, so a reviewer can check them.
What should you do with each result?
affected: upgrade. These are the findings to prioritize, and--fail-on high,criticalmakes CI exit with code3when one of them matches those severities.not_affected: ship the OpenVEX file with the release and feed it to your scanner, so the finding stops coming back.under_investigation: do the function-level and input review by hand, then record the outcome as a local rule with a standard justification, so the decision is versioned with the code:
# .capyshield/vex-rules.yaml
rules:
- vulnerability: CVE-2020-14343
package: PyYAML
status: not_affected
justification: vulnerable_code_cannot_be_controlled_by_adversary
impact_statement: "Only yaml.safe_load on trusted config files."Reachability changes as code changes: a package nobody imports today can be imported tomorrow. Run the analysis on every build rather than once, and the VEX file stays true for the release it ships with. The docs show the full CI setup.
Frequently asked questions
- What is CVE reachability analysis?
- It is checking whether there is a path from your running code to the vulnerable code a CVE describes, rather than only whether the vulnerable package version is installed. It removes findings for packages that are never loaded or called in production.
- What is the difference between package-level and function-level reachability?
- Package-level reachability asks whether production code imports the vulnerable package at all. Function-level reachability asks whether the specific vulnerable function is on a call path, which is more precise but needs a call graph and per-language tooling.
- Why do vulnerability scanners report CVEs my code doesn't use?
- Scanners match installed package versions against advisory databases and don't know how your code uses them. Dev-only tools, unused direct dependencies and unloaded transitive packages are all reported the same way as libraries called on every request.
- How does CapyShield handle transitive dependencies in reachability analysis?
- A transitive package that production code doesn't import directly is reported as under_investigation rather than guessed, because whether it loads depends on its parent package. You can record a reviewed decision as a local rule in .capyshield/vex-rules.yaml.