.TH "NPM\-AUDIT" "1" "November 2022" "9.1.1" .SH "NAME" \fBnpm-audit\fR .SH Synopsis .SH Description .P The audit command submits a description of the dependencies configured in .br your project to your default registry and asks for a report of known .br vulnerabilities\. If any vulnerabilities are found, then the impact and .br appropriate remediation will be calculated\. If the \fBfix\fP argument is .br provided, then remediations will be applied to the package tree\. .P The command will exit with a 0 exit code if no vulnerabilities were found\. .P Note that some vulnerabilities cannot be fixed automatically and will .br require manual intervention or review\. Also note that since \fBnpm audit fix\fP runs a full\-fledged \fBnpm install\fP under the hood, all configs that .br apply to the installer will also apply to \fBnpm install\fP \-\- so things like .br \fBnpm audit fix \-\-package\-lock\-only\fP will work as expected\. .P By default, the audit command will exit with a non\-zero code if any .br vulnerability is found\. It may be useful in CI environments to include the .br \fB\-\-audit\-level\fP parameter to specify the minimum vulnerability level that .br will cause the command to fail\. This option does not filter the report .br output, it simply changes the command's failure threshold\. .SH Audit Signatures .P To ensure the integrity of packages you download from the public npm registry, or any registry that supports signatures, you can verify the registry signatures of downloaded packages using the npm CLI\. .P Registry signatures can be verified using the following \fBaudit\fP command: .RS 2 .nf $ npm audit signatures .fi .RE .P The npm CLI supports registry signatures and signing keys provided by any registry if the following conventions are followed: .RS 1 .IP \(bu 2 Signatures are provided in the package's \fBpackument\fP in each published version within the \fBdist\fP object: .RE .RS 2 .nf "dist":{ "\.\.omitted\.\.": "\.\.omitted\.\.", "signatures": [{ "keyid": "SHA256:{{SHA256_PUBLIC_KEY}}", "sig": "a312b9c3cb4a1b693e8ebac5ee1ca9cc01f2661c14391917dcb111517f72370809\.\.\." }] } .fi .RE .P See this example of a signed package from the public npm registry\. .P The \fBsig\fP is generated using the following template: \fB${package\.name}@${package\.version}:${package\.dist\.integrity}\fP and the \fBkeyid\fP has to match one of the public signing keys below\. .RS 1 .IP \(bu 2 Public signing keys are provided at \fBregistry\-host\.tld/\-/npm/v1/keys\fP in the following format: .RE .RS 2 .nf { "keys": [{ "expires": null, "keyid": "SHA256:{{SHA256_PUBLIC_KEY}}", "keytype": "ecdsa\-sha2\-nistp256", "scheme": "ecdsa\-sha2\-nistp256", "key": "{{B64_PUBLIC_KEY}}" }] } .fi .RE .P Keys response: .RS 1 .IP \(bu 2 \fBexpires\fP: null or a simplified extended ISO 8601 format: \fBYYYY\-MM\-DDTHH:mm:ss\.sssZ\fP .IP \(bu 2 \fBkeydid\fP: sha256 fingerprint of the public key .IP \(bu 2 \fBkeytype\fP: only \fBecdsa\-sha2\-nistp256\fP is currently supported by the npm CLI .IP \(bu 2 \fBscheme\fP: only \fBecdsa\-sha2\-nistp256\fP is currently supported by the npm CLI .IP \(bu 2 \fBkey\fP: base64 encoded public key .RE .P See this example key's response from the public npm registry\|\. .SH Audit Endpoints .P There are two audit endpoints that npm may use to fetch vulnerability .br information: the \fBBulk Advisory\fP endpoint and the \fBQuick Audit\fP endpoint\. .SS Bulk Advisory Endpoint .P As of version 7, npm uses the much faster \fBBulk Advisory\fP endpoint to .br optimize the speed of calculating audit results\. .P npm will generate a JSON payload with the name and list of versions of each .br package in the tree, and POST it to the default configured registry at .br the path \fB/\-/npm/v1/security/advisories/bulk\fP\|\. .P Any packages in the tree that do not have a \fBversion\fP field in their .br package\.json file will be ignored\. If any \fB\-\-omit\fP options are specified .br (either via the \fB\-\-omit\fP config, or one of the .br shorthands such as \fB\-\-production\fP, \fB\-\-only=dev\fP, and so on), then packages will .br be omitted from the submitted payload as appropriate\. .P If the registry responds with an error, or with an invalid response, then .br npm will attempt to load advisory data from the \fBQuick Audit\fP endpoint\. .P The expected result will contain a set of advisory objects for each .br dependency that matches the advisory range\. Each advisory object contains .br a \fBname\fP, \fBurl\fP, \fBid\fP, \fBseverity\fP, \fBvulnerable_versions\fP, and \fBtitle\fP\|\. .P npm then uses these advisory objects to calculate vulnerabilities and .br meta\-vulnerabilities of the dependencies within the tree\. .SS Quick Audit Endpoint .P If the \fBBulk Advisory\fP endpoint returns an error, or invalid data, npm will .br attempt to load advisory data from the \fBQuick Audit\fP endpoint, which is .br considerably slower in most cases\. .P The full package tree as found in \fBpackage\-lock\.json\fP is submitted, along .br with the following pieces of additional metadata: .RS 1 .IP \(bu 2 \fBnpm_version\fP .IP \(bu 2 \fBnode_version\fP .IP \(bu 2 \fBplatform\fP .IP \(bu 2 \fBarch\fP .IP \(bu 2 \fBnode_env\fP .RE .P All packages in the tree are submitted to the Quick Audit endpoint\. .br Omitted dependency types are skipped when generating the report\. .SS Scrubbing .P Out of an abundance of caution, npm versions 5 and 6 would "scrub" any .br packages from the submitted report if their name contained a \fB/\fP character, .br so as to avoid leaking the names of potentially private packages or git .br URLs\. .P However, in practice, this resulted in audits often failing to properly .br detect meta\-vulnerabilities, because the tree would appear to be invalid .br due to missing dependencies, and prevented the detection of vulnerabilities .br in package trees that used git dependencies or private modules\. .P This scrubbing has been removed from npm as of version 7\. .SS Calculating Meta\-Vulnerabilities and Remediations .P npm uses the .br \fB@npmcli/metavuln\-calculator\fP .br module to turn a set of security advisories into a set of "vulnerability" .br objects\. A "meta\-vulnerability" is a dependency that is vulnerable by .br virtue of dependence on vulnerable versions of a vulnerable package\. .P For example, if the package \fBfoo\fP is vulnerable in the range \fB>=1\.0\.2 <2\.0\.0\fP, and the package \fBbar\fP depends on \fBfoo@^1\.1\.0\fP, then that version .br of \fBbar\fP can only be installed by installing a vulnerable version of \fBfoo\fP\|\. .br In this case, \fBbar\fP is a "metavulnerability"\. .P Once metavulnerabilities for a given package are calculated, they are .br cached in the \fB~/\.npm\fP folder and only re\-evaluated if the advisory range .br changes, or a new version of the package is published (in which case, the .br new version is checked for metavulnerable status as well)\. .P If the chain of metavulnerabilities extends all the way to the root .br project, and it cannot be updated without changing its dependency ranges, .br then \fBnpm audit fix\fP will require the \fB\-\-force\fP option to apply the .br remediation\. If remediations do not require changes to the dependency .br ranges, then all vulnerable packages will be updated to a version that does .br not have an advisory or metavulnerability posted against it\. .SH Exit Code .P The \fBnpm audit\fP command will exit with a 0 exit code if no vulnerabilities .br were found\. The \fBnpm audit fix\fP command will exit with 0 exit code if no .br vulnerabilities are found \fIor\fR if the remediation is able to successfully .br fix all vulnerabilities\. .P If vulnerabilities were found the exit code will depend on the .br \fBaudit\-level\fP config\. .SH Examples .P Scan your project for vulnerabilities and automatically install any compatible .br updates to vulnerable dependencies: .RS 2 .nf $ npm audit fix .fi .RE .P Run \fBaudit fix\fP without modifying \fBnode_modules\fP, but still updating the .br pkglock: .RS 2 .nf $ npm audit fix \-\-package\-lock\-only .fi .RE .P Skip updating \fBdevDependencies\fP: .RS 2 .nf $ npm audit fix \-\-only=prod .fi .RE .P Have \fBaudit fix\fP install SemVer\-major updates to toplevel dependencies, not .br just SemVer\-compatible ones: .RS 2 .nf $ npm audit fix \-\-force .fi .RE .P Do a dry run to get an idea of what \fBaudit fix\fP will do, and \fIalso\fR output .br install information in JSON format: .RS 2 .nf $ npm audit fix \-\-dry\-run \-\-json .fi .RE .P Scan your project for vulnerabilities and just show the details, without .br fixing anything: .RS 2 .nf $ npm audit .fi .RE .P Get the detailed audit report in JSON format: .RS 2 .nf $ npm audit \-\-json .fi .RE .P Fail an audit only if the results include a vulnerability with a level of moderate or higher: .RS 2 .nf $ npm audit \-\-audit\-level=moderate .fi .RE .SH Configuration .SH See Also .RS 1 .IP \(bu 2 npm install .IP \(bu 2 config .RE