Next.js プロジェクトのライブラリ脆弱性チェック
コマンドラインでの脆弱性チェック
npm
# 基本スキャン
npm audit
# 自動修正(互換性維持)
npm audit fix
# JSON形式レポート
npm audit --json > report.json
# 本番依存のみチェック
npm audit --production
yarn
# 基本スキャン
yarn audit
# 重要度フィルタ
yarn audit --level=high
専門ツールの活用
Snyk
# インストール・設定
npm install -g snyk
snyk auth
# チェックと監視
snyk test
snyk monitor
GitHub Dependabot の設定
.github/dependabot.yml:
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
open-pull-requests-limit: 10
CI/CD パイプラインへの統合
GitHub Actions
.github/workflows/security-audit.yml:
name: Security Audit
on:
push:
branches: [ main ]
schedule:
- cron: '0 0 * * 0' # 毎週日曜実行
jobs:
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm ci
- run: npm audit --audit-level=high
package.json への便利なスクリプト追加
{
"scripts": {
"security-check": "npm audit --json > security-report.json",
"security-fix": "npm audit fix && npm audit fix --force --production"
}
}
脆弱性対応の優先順位
- Critical/High: 即時対応
- Medium: 計画的対応
- Low: 定期更新時に対応
具体的な対応手順
# 1. 詳細確認
npm audit
# 2. パッケージ更新
npm update vulnerable-package@^2.0.0
# 3. テスト実行
npm test
# 4. 再チェック
npm audit
依存関係の競合解決
// package.json
{
"overrides": {
"<vulnerable-package>": "<secure-version>"
}
}
参考資料
Next.js 14.x および Node.js 18.x 以上を基準としています。