@openreachtech/mentsu-npm-audit-gate
v0.3.0
Published
Gate that checks the result of npm audit
Keywords
Readme
@openreachtech/mentsu-npm-audit-gate
Gate that checks the result of npm audit.
Concept
npm audit reports every advisory it finds, and a project sometimes has to live with one that has no patched release. This package runs npm audit, lets through only the advisories you list, and fails on everything else, at any severity.
Each entry of the list carries its reason and the condition for removing it. An entry whose advisory is no longer reported fails the audit too, so the list cannot outlive the reasons it was written for.
An entry also fails the audit once its advisory has a patched release, unless the entry states in patchedOnlyIn why that release cannot be taken yet. The package asks the GitHub Advisory Database for it.
Installation
Requires Node.js 24.19.0 or later.
npm install @openreachtech/mentsu-npm-audit-gateIt is an ES module ("type": "module"); import it with ESM import syntax.
Usage
Write a script that hands the list to NpmAuditGate, and run it in the project whose npm audit it should judge.
To check the entries of the list, import the type file once in your project. Then annotate the list with NpmAuditType.IgnoredAdvisory.
// types/env/npm-audit-gate.d.ts
import '@openreachtech/mentsu-npm-audit-gate/types/index.d.ts'import {
NpmAuditGate,
} from '@openreachtech/mentsu-npm-audit-gate'
/**
* @type {Array<NpmAuditType.IgnoredAdvisory>}
*/
const IGNORED_ADVISORIES = [
{
id: 'GHSA-aaaa-bbbb-cccc',
package: 'alpha-package',
path: 'beta-package > alpha-package',
reason: 'No patched release exists, and the package is reached through a development dependency only',
removeWhen: 'alpha-package publishes a release outside the affected range',
},
]
const alphaGate = NpmAuditGate.create({
ignoredAdvisories: IGNORED_ADVISORIES,
})
process.exitCode = await alphaGate.auditAdvisories()auditAdvisories() is asynchronous, because it looks up the patched releases of the advisories the list lets through. Await it before you read the exit code.
The process ends with exit code 1 when an advisory the list does not name is reported, when an entry of the list is no longer reported, when an entry of the list has a patched release and no patchedOnlyIn, when the lookup of a patched release fails, or when npm audit answers with no report. Otherwise it ends with 0.
Advisories that already have a patched release
For each entry that npm audit still reports and that has no patchedOnlyIn, the gate calls GET https://api.github.com/advisories/<GHSA id> and reads first_patched_version of each vulnerabilities item that names the package of the entry. A patch that exists only in a newer major release counts as a patch. The gate prints a failure line such as the following and ends with exit code 1.
⏫ patched upstream: GHSA-aaaa-bbbb-cccc (alpha-package) has a patched release 2.0.1; upgrade it, or state why not in patchedOnlyInUpgrade the package, or state in the optional patchedOnlyIn field why you cannot take the patched release yet. An entry with patchedOnlyIn triggers no lookup, and its ignored lines gain patched only in: <value>.
{
id: 'GHSA-aaaa-bbbb-cccc',
package: 'alpha-package',
path: 'beta-package > alpha-package',
reason: 'The patched release changes an API that beta-package still calls',
removeWhen: 'beta-package supports alpha-package 3',
patchedOnlyIn: 'alpha-package 3, a new major that beta-package does not support yet',
}A lookup that fails (a network error, a non-2xx answer such as 403 for a rate limit or 404, or a body that is not an advisory) is a failure of its own, and the gate never reads it as "not patched". When no entry needs a lookup, the gate makes no network call.
GitHub limits the rate of requests, and a request without a token falls under the lowest limit (see Rate limits for the REST API). Set GITHUB_TOKEN to send it as Authorization: Bearer <token> and raise the limit. A GitHub Actions step passes the token of the workflow.
- run: npm run audit:npm
env:
GITHUB_TOKEN: ${{ github.token }}An empty GITHUB_TOKEN is treated as unset.
The other types of NpmAuditType (one for each class, named after it, and its Ctor) read the JSDoc of this package, so a project that uses them needs allowJs: true and maxNodeModuleJsDepth of 3 or more in its jsconfig.json.
Running the script shipped with the package
The package ships the script that runs the audit, so a repository does not need to carry a copy of it. Call it by its path from a script of package.json.
Pass the file that lists the ignored advisories as the only argument. Its default export is the array.
// scripts/stopgap-audit/IGNORED_ADVISORIES.js
export default [
{
id: 'GHSA-aaaa-bbbb-cccc',
package: 'alpha-package',
path: 'beta-package > alpha-package',
reason: 'No patched release exists, and the package is reached through a development dependency only',
removeWhen: 'alpha-package publishes a release outside the affected range',
},
]{
"scripts": {
"audit:npm": "node node_modules/@openreachtech/mentsu-npm-audit-gate/scripts/npm-audit-gate.js scripts/stopgap-audit/IGNORED_ADVISORIES.js"
}
}The file is resolved against the working directory, and the script fails when it is missing.
It ends with the same exit codes as NpmAuditGate, and with 1 as well when the list cannot be loaded. It reads GITHUB_TOKEN of its environment, as described above.
The package declares no command name on purpose. A command name is resolved through node_modules/.bin/, which every installed package shares, and another package that declares the same name takes it over without any warning. The path names this package alone.
API
Contribution
Bug reports, feature requests, and code contributions are welcome.
Feel free to contact us through GitHub Issues.
git clone https://github.com/openreachtech/mentsu-npm-audit-gate.git
cd mentsu-npm-audit-gate
npm install
npm run lint
npm testLicense
This project is released under the Apache License 2.0.
For more details, please see in the LICENSE file.
Developer
Copyright
© 2026 Open Reach Tech Inc.
