背景
使用しているnpmライブラリにいつの間にか脆弱性が含まれてしまい、それを知らないうちに放置していた。
⇒
定期的に脆弱性チェックを行うことで、脆弱性が含まれたことを検知したい。
対応
パイプラインからnpm auditを行う
下記のcommand: 'custom'
で指定したディレクトリに対してnpm audit
を実施する
steps:
# Nodeインストール
- task: NodeTool@0
inputs:
versionSpec: '14.16.0'
displayName: 'Install Node.js'
# npm ci
- script: |
npm ci
displayName: 'npm ci'
workingDirectory: '$(System.DefaultWorkingDirectory)'
# 脆弱性high以上が含まれるかチェック
- task: Npm@1
inputs:
command: 'custom'
workingDir: '$(System.DefaultWorkingDirectory)'
customCommand: 'audit --registry=https://registry.npmjs.org/ --audit-level=high'
displayName: 'npm audit'
※--registry=https://registry.npmjs.org/
このオプションについては、Azure公式Docで必要と記載があるのでつけてます。(registryを明示してあげないといけないっぽい?/デフォルトのregistry指定では動作しない?)
パイプラインを定期的に実行する
下記記載により、パイプラインの定期実行を構成する。
schedules:
- cron: '0 10 * * *'
displayName: '毎日19:00起動'
branches:
include:
- develop
上記設定後、AzureDevOps > Pipelinesの該当パイプラインの設定から「Scheduled runs」を押下すると、設定を確認することができます。
npm audit
でhigh以上のものを検知すると、パイプライン自体が失敗するようになっています。