Skip to content

Commit a631beb

Browse files
committed
build(dependency-check): make the NVD API key optional
The pom handed dependency-check ${env.NVD_API_KEY} unconditionally. With no key in the environment that arrives as an empty string, which the NVD API rejects outright - "Invalid API Key, length of 0 too short" - so the scan fails the build having checked nothing, rather than degrading to anonymous access. The scan now defaults to skipped, and an nvd-api-key profile turns it back on when a key is really there. Activation is by presence: Maven reads an environment variable set to the empty string as absent, which is exactly what CI leaves behind for an unconfigured secret, so a fork or a repository that never had the key skips the scan instead of failing on a blank credential.
1 parent e566547 commit a631beb

1 file changed

Lines changed: 43 additions & 5 deletions

File tree

‎pom.xml‎

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,24 @@
6565
<java.minVersion>17</java.minVersion>
6666
<antlr4.version>4.13.2</antlr4.version>
6767
<dependencyCheck.version>13.0.0</dependencyCheck.version>
68-
<dependencyCheck.skip>false</dependencyCheck.skip>
68+
<!-- Off unless an NVD API key reaches the build; the nvd-api-key profile
69+
below turns it on. The scan genuinely cannot run without one: the NVD
70+
API rejects an empty key outright rather than falling back to
71+
anonymous access ("Invalid API Key, length of 0 too short"), and
72+
keyless access is throttled to 5 requests per rolling 30s, which is
73+
far too slow to populate the vulnerability cache from cold. Skipping
74+
is the honest default - handed no key the scan does not degrade, it
75+
fails the build without having checked anything. A key is free from
76+
https://nvd.nist.gov/developers/request-an-api-key; see the profile
77+
for how to supply it. -->
78+
<dependencyCheck.skip>true</dependencyCheck.skip>
6979
<dependencyCheck.threshold>7</dependencyCheck.threshold>
70-
<!-- Read the NVD API key from the environment so the secret is never
71-
passed on the command line (visible in the process list). Can still
72-
be overridden with -DdependencyCheck.NVDApiKey=... if needed. -->
73-
<dependencyCheck.NVDApiKey>${env.NVD_API_KEY}</dependencyCheck.NVDApiKey>
80+
<!-- Empty on purpose, and only ever read when the scan is skipped: the
81+
nvd-api-key profile below supplies the real value from the
82+
environment. An empty key is worse than no key at all - that is the
83+
NVD error quoted above - so nothing here ever hands the plugin a
84+
blank credential. -->
85+
<dependencyCheck.NVDApiKey></dependencyCheck.NVDApiKey>
7486
</properties>
7587
<dependencies>
7688
<dependency>
@@ -268,4 +280,30 @@
268280
</plugin>
269281
</plugins>
270282
</reporting>
283+
<profiles>
284+
<!-- Enables the dependency-check scan, reading the key from the
285+
environment so the secret is never passed on the command line, where
286+
the process list would show it.
287+
288+
Activation is by presence rather than value, and the empty case is
289+
the point: Maven activates a valueless <property> only when the
290+
property is non-empty, and CI expands an unset secret or variable to
291+
the empty string. So a fork, or a repository where the secret was
292+
never added, leaves the scan off instead of failing the build with a
293+
blank credential.
294+
295+
To run it locally: NVD_API_KEY=... ./mvnw verify. To force it on with
296+
the key supplied some other way (a settings.xml server entry, say):
297+
-DdependencyCheck.skip=false. -->
298+
<profile>
299+
<id>nvd-api-key</id>
300+
<activation>
301+
<property><name>env.NVD_API_KEY</name></property>
302+
</activation>
303+
<properties>
304+
<dependencyCheck.skip>false</dependencyCheck.skip>
305+
<dependencyCheck.NVDApiKey>${env.NVD_API_KEY}</dependencyCheck.NVDApiKey>
306+
</properties>
307+
</profile>
308+
</profiles>
271309
</project>

0 commit comments

Comments
 (0)