【Node.js】npmパッケージの脆弱性対応
CIで脆弱性チェックがエラーとなり、npm audit
以外の方法で応急処置をしたのでメモ。
修正後は要動作確認。
npm audit
まずは npm audit
等で検出されたパッケージのレポジトリを調べ、必要であれば package.json のバージョンを修正後にnpm audit fix
を実行。
node_modules や package-lock.jsonは削除した方が間違いが無さそう。
$ npm audit fix
npm-force-resolutions
npm audit fix
で解決しなかった場合に、どうにかして脆弱性のあるライブラリのバージョンを修正したい場合にnpm-force-resolutions
が使用できます。
package.jsonのscripts.preinstallにnpx npm-force-resolutions
、resolutions
にバージョンを修正したいパッケージ情報を記載します。
依存関係を無理やり変更させる為、使用する場合はバージョンアップ後の動作確認を良く行ってください。
{ "name": "xxxxx", "version": "0.0.1", "description": "", "scripts": { "preinstall": "npx npm-force-resolutions", # ここ "build": "..." }, "dependencies": { "vue": "^2.6.14", ... }, "dev-dependencies": { "vue-jest": "^4.0.1", ... }, "resolutions": { "ansi-regex": "5.0.1" # ここ } }
Dockerfileからbuildする際にエラーとなった為、.npmrcにunsafe-perm=true
を追記して解決しました。
unsafe-perm=true
で、パッケージスクリプト(preinstall)の実行時のUID/GID切り替えを抑制します。
# .npmrc registry=https://xxx.xxx.xxx/... unsafe-perm=true # ここ