Snyk CLIでnpmプロジェクト内のDependencyの脆弱性をスキャンしてみた

2022.03.01

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部 IoT事業部の若槻です。

Snykは、コードやオープンソースのDependency、コンテナ、Infrastracture as Codeなどに対して、独自のセキュリティインテリジェンスを使用して脆弱性検知や自動修復を行うことができるクラウドベースのサービスです。

このSnykによる脆弱性スキャンは、GitHubなどのコードリポジトリサービスと連携して利用できるほか、Snyk CLIを使用すればマシン端末上を含む様々な環境でも利用が可能です。

今回は、Snyk CLIを使用してnpmプロジェクト内のDependencyをスキャンしてみました。

前提

Snykアカウント

まだの場合はSnykアカウントにサインアップしておきます。

ちなみにSnykは個人又は小規模なチームであれば無料で利用可能です。詳しくは下記を御覧ください。

環境

Snyk CLIを使用する端末の環境情報です。

$ node --version
v14.17.0
$ npm --version
8.5.0
$ sw_vers
ProductName:    macOS
ProductVersion: 11.6
BuildVersion:   20G165

スキャン対象

AWS CDKプロジェクトを作り、さらにその配下にReactプロジェクトを作ります。これらのnpmプロジェクトをスキャン対象とします。

$ mkdir aws-cdk-v2-app && cd aws-cdk-v2-app
$ cdk init --language typescript
$ npx create-react-app web

CDKプロジェクトaws-cdk-v2-appの配下にReactプロジェクトwebがある構成です。

やってみた

導入

Snyk CLIをnpmでグローバルインストールします。

$ npm install -g snyk

インストールが成功し、パスが通っていればsnykコマンドが実行できるようになります。

$ snyk --help
CLI commands help
  Snyk CLI scans and monitors your projects for security vulnerabilities and license issues.

  Visit the Snyk website https://snyk.io for more information. See the CLI documentation 
  https://docs.snyk.io/features/snyk-cli for details.

How to get started
  1. Authenticate by running snyk auth.
  2. Test your local project with snyk test.
  3. Get alerted for new vulnerabilities with snyk monitor.

Available commands
  To learn more about each Snyk CLI command, use the --help option, for example, snyk auth --help or 
  snyk container --help. You can also use the help command: snyk help [<COMMAND>].

  snyk auth
    Authenticate Snyk CLI with a Snyk account.

  snyk test
    Test a project for open source vulnerabilities and license issues.

  snyk monitor
    Snapshot and continuously monitor a project for open source vulnerabilities and license issues.

  snyk container
    Test container images for vulnerabilities.

  snyk iac
    Find security issues in Infrastructure as Code files.

  snyk code
    Find security issues using static code analysis.

  snyk log4shell
    Find Log4Shell vulnerability.

  snyk config
    Manage Snyk CLI configuration.

  snyk policy
    Display the .snyk policy for a package.

  snyk ignore
    Modify the .snyk policy to ignore stated issues.

Debug
  Use -d option to output the debug logs.

Configure the Snyk CLI
  You can use environment variables to configure the Snyk CLI and also set variables to configure the
  Snyk CLI to connect with the Snyk API. See Configure the Snyk CLI 
  https://docs.snyk.io/features/snyk-cli/configure-the-snyk-cli.

npm以外の導入方法は下記を御覧ください。

参考

認証

下記のコマンドを実行します。

$ snyk auth

するとブラウザで認証画面が開くので、[Authenticate]をクリックして認証します。

コマンドの実行結果にYour account has been authenticated. Snyk is now ready to be used.と表示されていれば認証成功です。

参考

脆弱性のスキャン

Snyk CLIでプロジェクトに対して脆弱性のスキャンをしてみます。オープンソースの脆弱性のスキャンはsnyk testコマンドで行います。

ワーキングディレクトリ直下のプロジェクトのみ

単にsnyk testを実行するとワーキングディレクトリ内のプロジェクトのみスキャンされます。

aws-cdk-v2-app $ snyk test

Testing /Users/wakatsuki.ryuta/projects/cm-rwakatsuki/aws-cdk-v2-app...

Organization:      cm-rwakatsuki
Package manager:   npm
Target file:       package-lock.json
Project name:      aws-cdk-v2-app
Open source:       no
Project path:      /Users/wakatsuki.ryuta/projects/cm-rwakatsuki/aws-cdk-v2-app
Licenses:          enabled

✔ Tested 23 dependencies for known issues, no vulnerable paths found.

Tip: Detected multiple supported manifests (1), use --all-projects to scan all of them at once.

Next steps:
- Run `snyk monitor` to be notified about new related vulnerabilities.
- Run `snyk test` as part of your CI/test.

aws-cdk-v2-appプロジェクト内には脆弱性が見つかりませんでした。

ワーキングディレクトリ配下のすべてのプロジェクト

--all-projectsオプションを使用すると、ワーキングディレクトリ配下のすべてのプロジェクトをスキャン対象となります。

$  snyk test --all-projects

Testing /Users/wakatsuki.ryuta/projects/cm-rwakatsuki/aws-cdk-v2-app...

Organization:      cm-rwakatsuki
Package manager:   npm
Target file:       package-lock.json
Project name:      aws-cdk-v2-app
Open source:       no
Project path:      /Users/wakatsuki.ryuta/projects/cm-rwakatsuki/aws-cdk-v2-app
Licenses:          enabled

✔ Tested 23 dependencies for known issues, no vulnerable paths found.

Next steps:
- Run `snyk monitor` to be notified about new related vulnerabilities.
- Run `snyk test` as part of your CI/test.

-------------------------------------------------------

Testing /Users/wakatsuki.ryuta/projects/cm-rwakatsuki/aws-cdk-v2-app...

Tested 1145 dependencies for known issues, found 2 issues, 2 vulnerable paths.


Issues with no direct upgrade or patch:
  ✗ Regular Expression Denial of Service (ReDoS) [Medium Severity][https://snyk.io/vuln/SNYK-JS-CSSWHAT-1298035] in css-what@3.4.2
    introduced by react-scripts@5.0.0 > @svgr/webpack@5.5.0 > @svgr/plugin-svgo@5.5.0 > svgo@1.3.2 > css-select@2.1.0 > css-what@3.4.2
  This issue was fixed in versions: 5.0.1
  ✗ Regular Expression Denial of Service (ReDoS) [High Severity][https://snyk.io/vuln/SNYK-JS-NTHCHECK-1586032] in nth-check@1.0.2
    introduced by react-scripts@5.0.0 > @svgr/webpack@5.5.0 > @svgr/plugin-svgo@5.5.0 > svgo@1.3.2 > css-select@2.1.0 > nth-check@1.0.2
  This issue was fixed in versions: 2.0.1



Organization:      cm-rwakatsuki
Package manager:   npm
Target file:       web/package-lock.json
Project name:      web
Open source:       no
Project path:      /Users/wakatsuki.ryuta/projects/cm-rwakatsuki/aws-cdk-v2-app
Licenses:          enabled


Tested 2 projects, 1 contained vulnerable paths.

スキャン結果から、aws-cdk-v2-appプロジェクトにはなし、webプロジェクトからはReDoSの脆弱性が2件見つかりました。1つ目がcss-what@3.4.2Medium Severity、2つ目がnth-check@1.0.2High Severityとなっています。

また記載のURLでは脆弱性の詳細な情報を確認することができます。すぐ対応するべきかどうかの判断に役立ちそうですね。

  • https://snyk.io/vuln/SNYK-JS-CSSWHAT-1298035
  • https://snyk.io/vuln/SNYK-JS-NTHCHECK-1586032

補足:npm auditの場合

比較のため、npm auditも実行してみるとnth-checkのみ脆弱性が検知されました。Snykでのスキャンでのみ脆弱性が検知できる場合があるようで、npmとは異なる独自の脆弱性データベースおよび検知基準であることが伺えます。

$ npm --prefix web audit       
# npm audit report

nth-check  <2.0.1
Severity: moderate
Inefficient Regular Expression Complexity in nth-check - https://github.com/advisories/GHSA-rp65-9cf3-cjxr
fix available via `npm audit fix --force`
Will install react-scripts@2.1.3, which is a breaking change
node_modules/svgo/node_modules/nth-check
  css-select  <=3.1.0
  Depends on vulnerable versions of nth-check
  node_modules/svgo/node_modules/css-select
    svgo  1.0.0 - 1.3.2
    Depends on vulnerable versions of css-select
    node_modules/svgo
      @svgr/plugin-svgo  <=5.5.0
      Depends on vulnerable versions of svgo
      node_modules/@svgr/plugin-svgo
        @svgr/webpack  4.0.0 - 5.5.0
        Depends on vulnerable versions of @svgr/plugin-svgo
        node_modules/@svgr/webpack
          react-scripts  >=2.1.4
          Depends on vulnerable versions of @svgr/webpack
          node_modules/react-scripts

6 moderate severity vulnerabilities

To address all issues (including breaking changes), run:
  npm audit fix --force

脆弱性の修復

スキャンで見つかった2つの脆弱性を修復を試みてみます。

脆弱性を自動で修復できるsnyk fixコマンドは現時点でまだベータなので、今回は別の方法を探ってみます。

先程のスキャン結果で、css-what@3.4.2This issue was fixed in versions: 5.0.1nth-check@1.0.2This issue was fixed in versions: 2.0.1とありました。npm lsでそれぞれの依存関係を見てみるといずれもreact-scripts@5.0.0のDependencyとなっています。

$  npm --prefix web ls css-what
web@0.1.0 /Users/wakatsuki.ryuta/projects/cm-rwakatsuki/aws-cdk-v2-app/web
└─┬ react-scripts@5.0.0
  ├─┬ @svgr/webpack@5.5.0
  │ └─┬ @svgr/plugin-svgo@5.5.0
  │   └─┬ svgo@1.3.2
  │     └─┬ css-select@2.1.0
  │       └── css-what@3.4.2
  └─┬ html-webpack-plugin@5.5.0
    └─┬ pretty-error@4.0.0
      └─┬ renderkid@3.0.0
        └─┬ css-select@4.2.1
          └── css-what@5.1.0

$  npm --prefix web ls nth-check    
web@0.1.0 /Users/wakatsuki.ryuta/projects/cm-rwakatsuki/aws-cdk-v2-app/web
└─┬ react-scripts@5.0.0
  ├─┬ @svgr/webpack@5.5.0
  │ └─┬ @svgr/plugin-svgo@5.5.0
  │   └─┬ svgo@1.3.2
  │     └─┬ css-select@2.1.0
  │       └── nth-check@1.0.2
  └─┬ html-webpack-plugin@5.5.0
    └─┬ pretty-error@4.0.0
      └─┬ renderkid@3.0.0
        └─┬ css-select@4.2.1
          └── nth-check@2.0.1

react-scriptsのアップデートにより対応したいところですが、現在の5.0.0公開されている最新バージョンであるため、その対応はできません。

一方で、先程のnpm auditの実行結果で下記のようにありました。npm audit fix --forceによりreact-scripts5.0.0から2.1.3へダウングレードすれば良いとありますが、流石にそれは出来ません。

nth-check  <2.0.1
Severity: moderate
Inefficient Regular Expression Complexity in nth-check - https://github.com/advisories/GHSA-rp65-9cf3-cjxr
fix available via `npm audit fix --force`
Will install react-scripts@2.1.3, which is a breaking change

というわけで今回は静観としました。snyk fixコマンドを使えばこのような場合の自動修復も簡単にできるのでしょうか。早期のGenerally Availableが望まれますね。

Exit Code

snyk test実行時のExit Codeは次のようになります。

0: success, no vulnerabilities found
1: action_needed, vulnerabilities found
2: failure, try to re-run command
3: failure, no supported projects detected

脆弱性が見つかった場合はExit Codeは1となります。

$ snyk test --all-projects

Testing /Users/wakatsuki.ryuta/projects/cm-rwakatsuki/aws-cdk-v2-app...

Organization:      cm-rwakatsuki
Package manager:   npm
Target file:       package-lock.json
Project name:      aws-cdk-v2-app
Open source:       no
Project path:      /Users/wakatsuki.ryuta/projects/cm-rwakatsuki/aws-cdk-v2-app
Licenses:          enabled

✔ Tested 23 dependencies for known issues, no vulnerable paths found.

Next steps:
- Run `snyk monitor` to be notified about new related vulnerabilities.
- Run `snyk test` as part of your CI/test.

-------------------------------------------------------

Testing /Users/wakatsuki.ryuta/projects/cm-rwakatsuki/aws-cdk-v2-app...

Tested 1145 dependencies for known issues, found 2 issues, 2 vulnerable paths.


Issues with no direct upgrade or patch:
  ✗ Regular Expression Denial of Service (ReDoS) [Medium Severity][https://snyk.io/vuln/SNYK-JS-CSSWHAT-1298035] in css-what@3.4.2
    introduced by react-scripts@5.0.0 > @svgr/webpack@5.5.0 > @svgr/plugin-svgo@5.5.0 > svgo@1.3.2 > css-select@2.1.0 > css-what@3.4.2
  This issue was fixed in versions: 5.0.1
  ✗ Regular Expression Denial of Service (ReDoS) [High Severity][https://snyk.io/vuln/SNYK-JS-NTHCHECK-1586032] in nth-check@1.0.2
    introduced by react-scripts@5.0.0 > @svgr/webpack@5.5.0 > @svgr/plugin-svgo@5.5.0 > svgo@1.3.2 > css-select@2.1.0 > nth-check@1.0.2
  This issue was fixed in versions: 2.0.1



Organization:      cm-rwakatsuki
Package manager:   npm
Target file:       web/package-lock.json
Project name:      web
Open source:       no
Project path:      /Users/wakatsuki.ryuta/projects/cm-rwakatsuki/aws-cdk-v2-app
Licenses:          enabled


Tested 2 projects, 1 contained vulnerable paths.



$ echo $?
1

これにより、CI/CDにsnyk testコマンドを組み込んで、脆弱性があればFailさせ、開発者に修正を促すというフローも作れそうですね。

おわりに

Snyk CLIを使用してnpmプロジェクト内のDependencyをスキャンしてみました。

npm auditでは拾いきれない脆弱性を検知できたり、検知された脆弱性に対して統一的な情報提供が行われているなど、活用すればより早期の対応や判断に役立てられると感じました。他にもいろいろな機能があるようなので順次触っていきたいと思います。

参考

以上