Scan Maven and Composer Dependencies with OSV-Scanner
Scan a Maven project and a PHP composer.lock with one OSV-Scanner command: transitive resolution, exit codes and a re-check after the fix, with real output.
osv-scanner walks a folder, finds dependency files such as pom.xml and composer.lock, and matches the versions in them against the OSV vulnerability data. With Java and PHP on the same server, one command covers both. For PHP alone there is also composer audit. Official docs: supported languages and lockfiles and usage.
[1] Download the binary from the release page into a work folder. Do not install it system-wide, and compare its hash with the SHA256SUMS published alongside it.
$ curl -fsSL -o osv-scanner https://github.com/google/osv-scanner/releases/download/v2.7.0/osv-scanner_linux_amd64$ curl -fsSL https://github.com/google/osv-scanner/releases/download/v2.7.0/osv-scanner_SHA256SUMS | grep linux_amd64$ sha256sum osv-scanner$ chmod +x osv-scanner$ ./osv-scanner --version$ osv-scanner --versionosv-scanner version: 2.7.0osv-scalibr version: 0.5.3[2] Scan the project folder with scan source -r; -r descends into subfolders. The test folder holds a pom.xml declaring log4j-core 2.14.1 and a composer.lock pinning guzzlehttp/guzzle 7.4.0.
$ osv-scanner scan source -r .$ osv-scanner scan source -r .Scanning dir .Scanned /srv/app/java-app/pom.xml file and found 1 packageScanned /srv/app/php-app/composer.lock file and found 11 packagesWarning: enricher transitivedependency/pomxml may be risky when run on untrusted artifacts. Please ensure you trust the source code and artifacts. Total 4 packages affected by 23 known vulnerabilities (2 Critical, 7 High, 13 Medium, 1 Low, 0 Unknown) from 2 ecosystems.23 vulnerabilities can be fixed.The Scanned lines tell you which files were read. If a file you expected is not listed, it was not checked.
[3] Read the table. The columns are OSV URL, CVSS, ecosystem, package, current version, FIXED VERSION and source file. A few rows:
| OSV URL | CVSS | ECOSYSTEM | PACKAGE | VERSION | FIXED VERSION | SOURCE || https://osv.dev/GHSA-jfh8-c2jp-5v3q | 10.0 | Maven | org.apache.logging.log4j:log4j-core | 2.14.1 | 2.15.0 | java-app/pom.xml || https://osv.dev/GHSA-3pxv-7cmr-fjr4 | 6.9 | Maven | org.apache.logging.log4j:log4j-core | 2.14.1 | 2.25.4 | java-app/pom.xml || https://osv.dev/GHSA-qv9r-c865-cp47 | 6.3 | Maven | org.apache.logging.log4j:log4j-api | 2.14.1 | 2.25.5 | java-app/pom.xml || https://osv.dev/GHSA-v5mv-p594-2x33 | 7.2 | Packagist | guzzlehttp/guzzle | 7.4.0 | 7.15.2 | php-app/composer.lock || https://osv.dev/GHSA-3rg7-wf37-54rm | 7.3 | Packagist | symfony/http-foundation (dev) | v5.4.0 | 5.4.50 | php-app/composer.lock |- When one package lists several
FIXED VERSIONvalues, move to the highest to clear them all. Check theFIXED VERSIONof transitive dependencies too. The highest value forlog4j-coreis 2.25.4, but the transitivelog4j-apineeds 2.25.5, so this project must go to 2.25.5 (step 6). - The scan said "1 package" for
pom.xml, yetlog4j-apiappears in the table. That is a transitive dependency, resolved in step 4. (dev)marks a development dependency.
[4] Transitive dependencies are resolved by default. According to the docs, transitive scanning of pom.xml is on by default, can be turned off with --no-resolve, and is disabled in offline mode. Turning it off drops log4j-api from the scan. The same page says test-scope dependencies are not yet part of the computed dependency graph for pom.xml, so treat test-scope dependencies as not covered and check them separately.
$ osv-scanner scan source -r . --no-resolve$ osv-scanner scan source -r . --no-resolveScanned /srv/app/java-app/pom.xml file and found 1 packageScanned /srv/app/php-app/composer.lock file and found 11 packagesTotal 3 packages affected by 22 known vulnerabilities (2 Critical, 7 High, 12 Medium, 1 Low, 0 Unknown) from 2 ecosystems.Packages went from 4 to 3 and vulnerabilities from 23 to 22; the missing one is log4j-api. By default the resolution data comes from deps.dev; add --data-source=native to use Maven Central, and --maven-registry=<url> for a private registry (per the docs, not tested). As the warning in step 2 says, resolve pom.xml files only from sources you trust.
[5] Fail the CI job on the exit code. It was 1 when vulnerabilities were found.
$ osv-scanner scan source -r . ; echo $?$ echo $?1$ osv-scanner scan source -r emptyScanning dir emptyNo package sources found, --help for usage information.$ echo $?128[6] Fix it. For Maven, edit the <version> of the <dependency> in pom.xml by hand. Following only the highest value for log4j-core in the step 3 table (2.25.4) leaves the transitive log4j-api behind; below is the scan of a pom with log4j-core 2.25.4.
$ osv-scanner scan source -r c4Scanning dir c4Scanned /srv/app/c4/pom.xml file and found 1 package Total 1 package affected by 1 known vulnerability (0 Critical, 0 High, 1 Medium, 0 Low, 0 Unknown) from 1 ecosystem.1 vulnerability can be fixed.| https://osv.dev/GHSA-qv9r-c865-cp47 | 6.3 | Maven | org.apache.logging.log4j:log4j-api | 2.25.4 | 2.25.5 | c4/pom.xml |$ echo $?1Raising log4j-core to 2.25.5 also cleared the transitive log4j-api.
$ osv-scanner scan source -r fixed$ osv-scanner scan source -r fixedScanning dir fixedScanned /srv/app/fixed/pom.xml file and found 1 package No issues found$ echo $?0For PHP, widen the constraint in composer.json, run composer update to regenerate composer.lock, then repeat the scan. The procedure is in the composer audit manual.
[7] To scan a single file, pass -L. It also works for a lock file whose name is not detected automatically.
$ osv-scanner scan source -L php-app/composer.lock$ osv-scanner scan source -L php-app/composer.lockScanned /srv/app/php-app/composer.lock file and found 11 packagesTotal 2 packages affected by 15 known vulnerabilities (0 Critical, 6 High, 8 Medium, 1 Low, 0 Unknown) from 1 ecosystem.Where the check gets confusing
- Counts differ between tools. The same
composer.lockgave 16 advisories incomposer auditand 15 in the OSV-Scanner summary (guzzleandsymfonyonly). Data sources and grouping differ, so compare packages and fixed versions, not totals. The table also had one row with an empty package column (GHSA-w248-…); the cause was not determined. - The number in
Scanned … found 1 packagecounts declared packages, not transitive ones. A longer package list in the table is normal. mvnandcomposerdo not need to be installed; no installed build tools are needed, but transitive resolution and the vulnerability lookup use the network (by default the OSV service and the deps.dev API); offline mode turns transitive resolution off. Whether properties or parent poms make the real build resolve different versions was not tested.