5
3

More than 1 year has passed since last update.

npm audit のvulnerabilityを手動で直す

Last updated at Posted at 2022-09-28

始める前に

この記事中の「マニュアル(手動)で修復する」が発生したらこの記事を進めてください.

環境

Debian 11.5
Node 14.20.1
Nuxt 2.15.8
yarn 1.22.19

脆弱性の確認

npm auditで脆弱性(vulnerability)のあるパッケージをリストする.

$ npm audit

実行結果(例)

                       === npm audit security report ===                        
                                                                                
┌──────────────────────────────────────────────────────────────────────────────┐
│                                Manual Review                                 │
│            Some vulnerabilities require your attention to resolve            │
│                                                                              │
│         Visit https://go.npm.me/audit-guide for additional guidance          │
└──────────────────────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ High          │ Inefficient Regular Expression Complexity in nth-check       │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package       │ nth-check                                                    │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in    │ >=2.0.1                                                      │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ nuxt                                                         │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path          │ nuxt > @nuxt/builder > @nuxt/webpack > cssnano >             │
│               │ cssnano-preset-default > postcss-svgo > svgo > css-select >  │
│               │ nth-check                                                    │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info     │ https://github.com/advisories/GHSA-rp65-9cf3-cjxr            │
└───────────────┴──────────────────────────────────────────────────────────────┘

この例では,css-select内のnth-checkで脆弱性が見つかったといわれています.

対処法

npm-force-resolutionsのインストール

$ npm -g install npm-force-resolutions -D

package.jsonの編集

package.jsonにresolutionを追加します.
中身には脆弱性のあったパッケージを追加します.

package.json
"resolutions": {
    "nth-check": "^2.0.1"
}

npm-force-resolutionsで強制的にバージョンを上げる

$ npx npm-force-resolutions

node_moduleを再インストール

$ rm -rf node_module
$ yarn install 
    or 
$ npm ci

脆弱性の再確認

$ npm audit 
    or
$ yarn audit
                                                                                
                       === npm audit security report ===                        
                                                                                
found 0 vulnerabilities
 in 1610 scanned packages

やったぜ

参考文献

npm パッケージのバージョンアップと脆弱性対応

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3