Which audit-level threshold prevents CI failure for low-severity vulnerabilities?
0 reputation · 11 Oct 2020, 07:18 UTC
0 reputation · 11 Oct 2020, 07:18 UTC
When integrating npm audit into a Continuous Integration (CI) pipeline, the goal is to block builds based on critical security risks while ignoring noise from low-impact vulnerabilities that do not affect the specific application runtime.
The --audit-level flag is used to filter the reporting threshold. However, there is uncertainty regarding how the exit codes behave when multiple severity levels are present in the dependency tree. If a project contains both 'low' and 'critical' vulnerabilities, it is necessary to ensure the pipeline only fails when the threshold is met or exceeded.
--audit-level=high suppress the non-zero exit code for 'low' and 'moderate' vulnerabilities?npm audit fix?35275 reputation · 11 Oct 2020, 08:23 UTC
In an npm CI job, the --audit-level flag controls the minimum severity that will cause a non‑zero exit code. Setting it to high (or critical) means that only vulnerabilities classified as high or critical will break the build. low and moderate findings are still reported in the audit output, but they do not set the exit code to 1.
If a vulnerability is found in a nested dependency that npm audit fix cannot resolve, the audit command will still report it. The exit code depends solely on the severity threshold:
0, build continues.1, build fails.low or moderate vulnerability in a deep dependency will be logged but will not abort the pipeline.
npm audit --audit-level=high locally and inspect the exit code:
npm audit --audit-level=high
echo $? # should be 0 if only low/moderate are present
.github/workflows/ci.yml), add the flag to the audit step:
- name: Security audit
run: npm audit --audit-level=high
continue-on-error: false
package.json and push. The job should finish successfully while the audit report shows the issue.
These steps confirm that the threshold behaves as described: only high or critical findings trigger a CI failure.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 11 Oct 2020, 14:02 UTC
When npm audit --audit-level is used, the flag only changes the process exit code; the vulnerability listing (including JSON output) still contains every finding regardless of the threshold.
For example, running npm audit --audit-level=high --json will return a JSON array that may include low and moderate advisories, but the command’s exit code will be 0 unless a high or critical issue is present.
This distinction is useful in CI when you want to gate builds on serious issues while preserving the full audit report for later review or ticketing.
npm audit --audit-level=high --json > audit-report.json
echo $? # 0 if only low/moderate exist