npm-audit.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <html><head>
  2. <title>npm-audit</title>
  3. <style>
  4. body {
  5. background-color: #ffffff;
  6. color: #24292e;
  7. margin: 0;
  8. line-height: 1.5;
  9. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  10. }
  11. #rainbar {
  12. height: 10px;
  13. background-image: linear-gradient(139deg, #fb8817, #ff4b01, #c12127, #e02aff);
  14. }
  15. a {
  16. text-decoration: none;
  17. color: #0366d6;
  18. }
  19. a:hover {
  20. text-decoration: underline;
  21. }
  22. pre {
  23. margin: 1em 0px;
  24. padding: 1em;
  25. border: solid 1px #e1e4e8;
  26. border-radius: 6px;
  27. display: block;
  28. overflow: auto;
  29. white-space: pre;
  30. background-color: #f6f8fa;
  31. color: #393a34;
  32. }
  33. code {
  34. font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
  35. font-size: 85%;
  36. padding: 0.2em 0.4em;
  37. background-color: #f6f8fa;
  38. color: #393a34;
  39. }
  40. pre > code {
  41. padding: 0;
  42. background-color: inherit;
  43. color: inherit;
  44. }
  45. h1, h2, h3 {
  46. font-weight: 600;
  47. }
  48. #logobar {
  49. background-color: #333333;
  50. margin: 0 auto;
  51. padding: 1em 4em;
  52. }
  53. #logobar .logo {
  54. float: left;
  55. }
  56. #logobar .title {
  57. font-weight: 600;
  58. color: #dddddd;
  59. float: left;
  60. margin: 5px 0 0 1em;
  61. }
  62. #logobar:after {
  63. content: "";
  64. display: block;
  65. clear: both;
  66. }
  67. #content {
  68. margin: 0 auto;
  69. padding: 0 4em;
  70. }
  71. #table_of_contents > h2 {
  72. font-size: 1.17em;
  73. }
  74. #table_of_contents ul:first-child {
  75. border: solid 1px #e1e4e8;
  76. border-radius: 6px;
  77. padding: 1em;
  78. background-color: #f6f8fa;
  79. color: #393a34;
  80. }
  81. #table_of_contents ul {
  82. list-style-type: none;
  83. padding-left: 1.5em;
  84. }
  85. #table_of_contents li {
  86. font-size: 0.9em;
  87. }
  88. #table_of_contents li a {
  89. color: #000000;
  90. }
  91. header.title {
  92. border-bottom: solid 1px #e1e4e8;
  93. }
  94. header.title > h1 {
  95. margin-bottom: 0.25em;
  96. }
  97. header.title > .description {
  98. display: block;
  99. margin-bottom: 0.5em;
  100. line-height: 1;
  101. }
  102. footer#edit {
  103. border-top: solid 1px #e1e4e8;
  104. margin: 3em 0 4em 0;
  105. padding-top: 2em;
  106. }
  107. </style>
  108. </head>
  109. <body>
  110. <div id="banner">
  111. <div id="rainbar"></div>
  112. <div id="logobar">
  113. <svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700">
  114. <polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon>
  115. <polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon>
  116. </svg>
  117. <div class="title">
  118. npm command-line interface
  119. </div>
  120. </div>
  121. </div>
  122. <section id="content">
  123. <header class="title">
  124. <h1 id="npm-audit">npm-audit</h1>
  125. <span class="description">Run a security audit</span>
  126. </header>
  127. <section id="table_of_contents">
  128. <h2 id="table-of-contents">Table of contents</h2>
  129. <div id="_table_of_contents"><ul><li><a href="#synopsis">Synopsis</a></li><li><a href="#description">Description</a></li><li><a href="#audit-endpoints">Audit Endpoints</a></li><ul><li><a href="#bulk-advisory-endpoint">Bulk Advisory Endpoint</a></li><li><a href="#quick-audit-endpoint">Quick Audit Endpoint</a></li><li><a href="#scrubbing">Scrubbing</a></li><li><a href="#calculating-meta-vulnerabilities-and-remediations">Calculating Meta-Vulnerabilities and Remediations</a></li></ul><li><a href="#exit-code">Exit Code</a></li><li><a href="#examples">Examples</a></li><li><a href="#configuration">Configuration</a></li><ul><li><a href="#audit-level"><code>audit-level</code></a></li><li><a href="#dry-run"><code>dry-run</code></a></li><li><a href="#force"><code>force</code></a></li><li><a href="#json"><code>json</code></a></li><li><a href="#package-lock-only"><code>package-lock-only</code></a></li><li><a href="#omit"><code>omit</code></a></li><li><a href="#workspace"><code>workspace</code></a></li><li><a href="#workspaces"><code>workspaces</code></a></li><li><a href="#include-workspace-root"><code>include-workspace-root</code></a></li></ul><li><a href="#see-also">See Also</a></li></ul></div>
  130. </section>
  131. <div id="_content"><h3 id="synopsis">Synopsis</h3>
  132. <pre lang="bash"><code>npm audit [--json] [--production] [--audit-level=(low|moderate|high|critical)]
  133. npm audit fix [--force|--package-lock-only|--dry-run|--production|--only=(dev|prod)]
  134. common options: [--production] [--only=(dev|prod)]
  135. </code></pre>
  136. <h3 id="description">Description</h3>
  137. <p>The audit command submits a description of the dependencies configured in
  138. your project to your default registry and asks for a report of known
  139. vulnerabilities. If any vulnerabilities are found, then the impact and
  140. appropriate remediation will be calculated. If the <code>fix</code> argument is
  141. provided, then remediations will be applied to the package tree.</p>
  142. <p>The command will exit with a 0 exit code if no vulnerabilities were found.</p>
  143. <p>Note that some vulnerabilities cannot be fixed automatically and will
  144. require manual intervention or review. Also note that since <code>npm audit fix</code> runs a full-fledged <code>npm install</code> under the hood, all configs that
  145. apply to the installer will also apply to <code>npm install</code> – so things like
  146. <code>npm audit fix --package-lock-only</code> will work as expected.</p>
  147. <p>By default, the audit command will exit with a non-zero code if any
  148. vulnerability is found. It may be useful in CI environments to include the
  149. <code>--audit-level</code> parameter to specify the minimum vulnerability level that
  150. will cause the command to fail. This option does not filter the report
  151. output, it simply changes the command’s failure threshold.</p>
  152. <h3 id="audit-endpoints">Audit Endpoints</h3>
  153. <p>There are two audit endpoints that npm may use to fetch vulnerability
  154. information: the <code>Bulk Advisory</code> endpoint and the <code>Quick Audit</code> endpoint.</p>
  155. <h4 id="bulk-advisory-endpoint">Bulk Advisory Endpoint</h4>
  156. <p>As of version 7, npm uses the much faster <code>Bulk Advisory</code> endpoint to
  157. optimize the speed of calculating audit results.</p>
  158. <p>npm will generate a JSON payload with the name and list of versions of each
  159. package in the tree, and POST it to the default configured registry at
  160. the path <code>/-/npm/v1/security/advisories/bulk</code>.</p>
  161. <p>Any packages in the tree that do not have a <code>version</code> field in their
  162. package.json file will be ignored. If any <code>--omit</code> options are specified
  163. (either via the <code>--omit</code> config, or one of the shorthands such as
  164. <code>--production</code>, <code>--only=dev</code>, and so on), then packages will be omitted
  165. from the submitted payload as appropriate.</p>
  166. <p>If the registry responds with an error, or with an invalid response, then
  167. npm will attempt to load advisory data from the <code>Quick Audit</code> endpoint.</p>
  168. <p>The expected result will contain a set of advisory objects for each
  169. dependency that matches the advisory range. Each advisory object contains
  170. a <code>name</code>, <code>url</code>, <code>id</code>, <code>severity</code>, <code>vulnerable_versions</code>, and <code>title</code>.</p>
  171. <p>npm then uses these advisory objects to calculate vulnerabilities and
  172. meta-vulnerabilities of the dependencies within the tree.</p>
  173. <h4 id="quick-audit-endpoint">Quick Audit Endpoint</h4>
  174. <p>If the <code>Bulk Advisory</code> endpoint returns an error, or invalid data, npm will
  175. attempt to load advisory data from the <code>Quick Audit</code> endpoint, which is
  176. considerably slower in most cases.</p>
  177. <p>The full package tree as found in <code>package-lock.json</code> is submitted, along
  178. with the following pieces of additional metadata:</p>
  179. <ul>
  180. <li><code>npm_version</code></li>
  181. <li><code>node_version</code></li>
  182. <li><code>platform</code></li>
  183. <li><code>arch</code></li>
  184. <li><code>node_env</code></li>
  185. </ul>
  186. <p>All packages in the tree are submitted to the Quick Audit endpoint.
  187. Omitted dependency types are skipped when generating the report.</p>
  188. <h4 id="scrubbing">Scrubbing</h4>
  189. <p>Out of an abundance of caution, npm versions 5 and 6 would “scrub” any
  190. packages from the submitted report if their name contained a <code>/</code> character,
  191. so as to avoid leaking the names of potentially private packages or git
  192. URLs.</p>
  193. <p>However, in practice, this resulted in audits often failing to properly
  194. detect meta-vulnerabilities, because the tree would appear to be invalid
  195. due to missing dependencies, and prevented the detection of vulnerabilities
  196. in package trees that used git dependencies or private modules.</p>
  197. <p>This scrubbing has been removed from npm as of version 7.</p>
  198. <h4 id="calculating-meta-vulnerabilities-and-remediations">Calculating Meta-Vulnerabilities and Remediations</h4>
  199. <p>npm uses the
  200. <a href="http://npm.im/@npmcli/metavuln-calculator"><code>@npmcli/metavuln-calculator</code></a>
  201. module to turn a set of security advisories into a set of “vulnerability”
  202. objects. A “meta-vulnerability” is a dependency that is vulnerable by
  203. virtue of dependence on vulnerable versions of a vulnerable package.</p>
  204. <p>For example, if the package <code>foo</code> is vulnerable in the range <code>&gt;=1.0.2 &lt;2.0.0</code>, and the package <code>bar</code> depends on <code>foo@^1.1.0</code>, then that version
  205. of <code>bar</code> can only be installed by installing a vulnerable version of <code>foo</code>.
  206. In this case, <code>bar</code> is a “metavulnerability”.</p>
  207. <p>Once metavulnerabilities for a given package are calculated, they are
  208. cached in the <code>~/.npm</code> folder and only re-evaluated if the advisory range
  209. changes, or a new version of the package is published (in which case, the
  210. new version is checked for metavulnerable status as well).</p>
  211. <p>If the chain of metavulnerabilities extends all the way to the root
  212. project, and it cannot be updated without changing its dependency ranges,
  213. then <code>npm audit fix</code> will require the <code>--force</code> option to apply the
  214. remediation. If remediations do not require changes to the dependency
  215. ranges, then all vulnerable packages will be updated to a version that does
  216. not have an advisory or metavulnerability posted against it.</p>
  217. <h3 id="exit-code">Exit Code</h3>
  218. <p>The <code>npm audit</code> command will exit with a 0 exit code if no vulnerabilities
  219. were found. The <code>npm audit fix</code> command will exit with 0 exit code if no
  220. vulnerabilities are found <em>or</em> if the remediation is able to successfully
  221. fix all vulnerabilities.</p>
  222. <p>If vulnerabilities were found the exit code will depend on the
  223. <code>audit-level</code> configuration setting.</p>
  224. <h3 id="examples">Examples</h3>
  225. <p>Scan your project for vulnerabilities and automatically install any compatible
  226. updates to vulnerable dependencies:</p>
  227. <pre lang="bash"><code>$ npm audit fix
  228. </code></pre>
  229. <p>Run <code>audit fix</code> without modifying <code>node_modules</code>, but still updating the
  230. pkglock:</p>
  231. <pre lang="bash"><code>$ npm audit fix --package-lock-only
  232. </code></pre>
  233. <p>Skip updating <code>devDependencies</code>:</p>
  234. <pre lang="bash"><code>$ npm audit fix --only=prod
  235. </code></pre>
  236. <p>Have <code>audit fix</code> install SemVer-major updates to toplevel dependencies, not
  237. just SemVer-compatible ones:</p>
  238. <pre lang="bash"><code>$ npm audit fix --force
  239. </code></pre>
  240. <p>Do a dry run to get an idea of what <code>audit fix</code> will do, and <em>also</em> output
  241. install information in JSON format:</p>
  242. <pre lang="bash"><code>$ npm audit fix --dry-run --json
  243. </code></pre>
  244. <p>Scan your project for vulnerabilities and just show the details, without
  245. fixing anything:</p>
  246. <pre lang="bash"><code>$ npm audit
  247. </code></pre>
  248. <p>Get the detailed audit report in JSON format:</p>
  249. <pre lang="bash"><code>$ npm audit --json
  250. </code></pre>
  251. <p>Fail an audit only if the results include a vulnerability with a level of moderate or higher:</p>
  252. <pre lang="bash"><code>$ npm audit --audit-level=moderate
  253. </code></pre>
  254. <h3 id="configuration">Configuration</h3>
  255. <!-- raw HTML omitted -->
  256. <!-- raw HTML omitted -->
  257. <!-- raw HTML omitted -->
  258. <h4 id="audit-level"><code>audit-level</code></h4>
  259. <ul>
  260. <li>Default: null</li>
  261. <li>Type: null, “info”, “low”, “moderate”, “high”, “critical”, or “none”</li>
  262. </ul>
  263. <p>The minimum level of vulnerability for <code>npm audit</code> to exit with a non-zero
  264. exit code.</p>
  265. <!-- raw HTML omitted -->
  266. <!-- raw HTML omitted -->
  267. <h4 id="dry-run"><code>dry-run</code></h4>
  268. <ul>
  269. <li>Default: false</li>
  270. <li>Type: Boolean</li>
  271. </ul>
  272. <p>Indicates that you don’t want npm to make any changes and that it should
  273. only report what it would have done. This can be passed into any of the
  274. commands that modify your local installation, eg, <code>install</code>, <code>update</code>,
  275. <code>dedupe</code>, <code>uninstall</code>, as well as <code>pack</code> and <code>publish</code>.</p>
  276. <p>Note: This is NOT honored by other network related commands, eg <code>dist-tags</code>,
  277. <code>owner</code>, etc.</p>
  278. <!-- raw HTML omitted -->
  279. <!-- raw HTML omitted -->
  280. <h4 id="force"><code>force</code></h4>
  281. <ul>
  282. <li>Default: false</li>
  283. <li>Type: Boolean</li>
  284. </ul>
  285. <p>Removes various protections against unfortunate side effects, common
  286. mistakes, unnecessary performance degradation, and malicious input.</p>
  287. <ul>
  288. <li>Allow clobbering non-npm files in global installs.</li>
  289. <li>Allow the <code>npm version</code> command to work on an unclean git repository.</li>
  290. <li>Allow deleting the cache folder with <code>npm cache clean</code>.</li>
  291. <li>Allow installing packages that have an <code>engines</code> declaration requiring a
  292. different version of npm.</li>
  293. <li>Allow installing packages that have an <code>engines</code> declaration requiring a
  294. different version of <code>node</code>, even if <code>--engine-strict</code> is enabled.</li>
  295. <li>Allow <code>npm audit fix</code> to install modules outside your stated dependency
  296. range (including SemVer-major changes).</li>
  297. <li>Allow unpublishing all versions of a published package.</li>
  298. <li>Allow conflicting peerDependencies to be installed in the root project.</li>
  299. <li>Implicitly set <code>--yes</code> during <code>npm init</code>.</li>
  300. <li>Allow clobbering existing values in <code>npm pkg</code></li>
  301. </ul>
  302. <p>If you don’t have a clear idea of what you want to do, it is strongly
  303. recommended that you do not use this option!</p>
  304. <!-- raw HTML omitted -->
  305. <!-- raw HTML omitted -->
  306. <h4 id="json"><code>json</code></h4>
  307. <ul>
  308. <li>Default: false</li>
  309. <li>Type: Boolean</li>
  310. </ul>
  311. <p>Whether or not to output JSON data, rather than the normal output.</p>
  312. <ul>
  313. <li>In <code>npm pkg set</code> it enables parsing set values with JSON.parse() before
  314. saving them to your <code>package.json</code>.</li>
  315. </ul>
  316. <p>Not supported by all npm commands.</p>
  317. <!-- raw HTML omitted -->
  318. <!-- raw HTML omitted -->
  319. <h4 id="package-lock-only"><code>package-lock-only</code></h4>
  320. <ul>
  321. <li>Default: false</li>
  322. <li>Type: Boolean</li>
  323. </ul>
  324. <p>If set to true, the current operation will only use the <code>package-lock.json</code>,
  325. ignoring <code>node_modules</code>.</p>
  326. <p>For <code>update</code> this means only the <code>package-lock.json</code> will be updated,
  327. instead of checking <code>node_modules</code> and downloading dependencies.</p>
  328. <p>For <code>list</code> this means the output will be based on the tree described by the
  329. <code>package-lock.json</code>, rather than the contents of <code>node_modules</code>.</p>
  330. <!-- raw HTML omitted -->
  331. <!-- raw HTML omitted -->
  332. <h4 id="omit"><code>omit</code></h4>
  333. <ul>
  334. <li>Default: ‘dev’ if the <code>NODE_ENV</code> environment variable is set to
  335. ‘production’, otherwise empty.</li>
  336. <li>Type: “dev”, “optional”, or “peer” (can be set multiple times)</li>
  337. </ul>
  338. <p>Dependency types to omit from the installation tree on disk.</p>
  339. <p>Note that these dependencies <em>are</em> still resolved and added to the
  340. <code>package-lock.json</code> or <code>npm-shrinkwrap.json</code> file. They are just not
  341. physically installed on disk.</p>
  342. <p>If a package type appears in both the <code>--include</code> and <code>--omit</code> lists, then
  343. it will be included.</p>
  344. <p>If the resulting omit list includes <code>'dev'</code>, then the <code>NODE_ENV</code> environment
  345. variable will be set to <code>'production'</code> for all lifecycle scripts.</p>
  346. <!-- raw HTML omitted -->
  347. <!-- raw HTML omitted -->
  348. <h4 id="workspace"><code>workspace</code></h4>
  349. <ul>
  350. <li>Default:</li>
  351. <li>Type: String (can be set multiple times)</li>
  352. </ul>
  353. <p>Enable running a command in the context of the configured workspaces of the
  354. current project while filtering by running only the workspaces defined by
  355. this configuration option.</p>
  356. <p>Valid values for the <code>workspace</code> config are either:</p>
  357. <ul>
  358. <li>Workspace names</li>
  359. <li>Path to a workspace directory</li>
  360. <li>Path to a parent workspace directory (will result in selecting all
  361. workspaces within that folder)</li>
  362. </ul>
  363. <p>When set for the <code>npm init</code> command, this may be set to the folder of a
  364. workspace which does not yet exist, to create the folder and set it up as a
  365. brand new workspace within the project.</p>
  366. <p>This value is not exported to the environment for child processes.</p>
  367. <!-- raw HTML omitted -->
  368. <!-- raw HTML omitted -->
  369. <h4 id="workspaces"><code>workspaces</code></h4>
  370. <ul>
  371. <li>Default: null</li>
  372. <li>Type: null or Boolean</li>
  373. </ul>
  374. <p>Set to true to run the command in the context of <strong>all</strong> configured
  375. workspaces.</p>
  376. <p>Explicitly setting this to false will cause commands like <code>install</code> to
  377. ignore workspaces altogether. When not set explicitly:</p>
  378. <ul>
  379. <li>Commands that operate on the <code>node_modules</code> tree (install, update, etc.)
  380. will link workspaces into the <code>node_modules</code> folder. - Commands that do
  381. other things (test, exec, publish, etc.) will operate on the root project,
  382. <em>unless</em> one or more workspaces are specified in the <code>workspace</code> config.</li>
  383. </ul>
  384. <p>This value is not exported to the environment for child processes.</p>
  385. <!-- raw HTML omitted -->
  386. <!-- raw HTML omitted -->
  387. <h4 id="include-workspace-root"><code>include-workspace-root</code></h4>
  388. <ul>
  389. <li>Default: false</li>
  390. <li>Type: Boolean</li>
  391. </ul>
  392. <p>Include the workspace root when workspaces are enabled for a command.</p>
  393. <p>When false, specifying individual workspaces via the <code>workspace</code> config, or
  394. all workspaces via the <code>workspaces</code> flag, will cause npm to operate only on
  395. the specified workspaces, and not on the root project.</p>
  396. <!-- raw HTML omitted -->
  397. <!-- raw HTML omitted -->
  398. <!-- raw HTML omitted -->
  399. <h3 id="see-also">See Also</h3>
  400. <ul>
  401. <li><a href="../commands/npm-install.html">npm install</a></li>
  402. <li><a href="../using-npm/config.html">config</a></li>
  403. </ul>
  404. </div>
  405. <footer id="edit">
  406. <a href="https://github.com/npm/cli/edit/latest/docs/content/commands/npm-audit.md">
  407. <svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
  408. <path fill-rule="evenodd" d="M11.013 1.427a1.75 1.75 0 012.474 0l1.086 1.086a1.75 1.75 0 010 2.474l-8.61 8.61c-.21.21-.47.364-.756.445l-3.251.93a.75.75 0 01-.927-.928l.929-3.25a1.75 1.75 0 01.445-.758l8.61-8.61zm1.414 1.06a.25.25 0 00-.354 0L10.811 3.75l1.439 1.44 1.263-1.263a.25.25 0 000-.354l-1.086-1.086zM11.189 6.25L9.75 4.81l-6.286 6.287a.25.25 0 00-.064.108l-.558 1.953 1.953-.558a.249.249 0 00.108-.064l6.286-6.286z"></path>
  409. </svg>
  410. Edit this page on GitHub
  411. </a>
  412. </footer>
  413. </section>
  414. </body></html>