Skip to main content

Snyk Code CLIのパブリックベータ版が利用可能に

著者

Frank Fischer

2021年9月21日

0 分で読めます

Snykは、DevOpsパイプライン全体で開発者が活用できる静的アプリケーションセキュリティテスト(SAST)ツールの実現を目指しています。Snyk Codeは高精度なリアルタイムスキャンを、開発者がすでに使用しているツールやワークフローから実行できます。たとえば、IntelliJ、PyCharm、WebStorm、Visual Studio Code向けのIDEプラグインを使えば、コードがバージョン管理に登録される前に、簡単にコーディング、スキャン、修正を行えます。また、WebベースのUIを使って、レガシーコードのスキャンやコードレビューを実施することもできます。 

さらに、Snyk Codeがパブリックベータ版としてSnykコマンドラインインターフェース(Snyk CLI)に対応したことをお知らせします。これにより、Snyk CodeをCI/CDプロセスに簡単に組み込み、スキャンの実行や結果の処理を柔軟に自動化できます。

Snykは、Snykプラットフォームのあらゆる機能を操作するためのCLIを提供しています。Snyk Codeによって、既存のCLIの機能が拡張されます。この記事では、まず使い始めるための手順をご紹介します。詳しく知りたい方は、Snyk Code CLIのドキュメントをご覧ください。

念のためお知らせすると、CLIのサポートは現在ベータ版です。ぜひお試しいただき、より良いものにするためにフィードバックをお寄せください。

Snyk CLIのインストールと設定

Snyk CLIはNode.jsアプリケーションのため、Node.jsをサポートするあらゆるシステムで実行できます。インストールと設定も簡単です。

npmを使って、次のコマンドでインストールできます。npm install -g snyk

その他のインストール方法やnpmを使ったインストールについて詳しくは、Snyk Code CLIのドキュメントをご覧ください。

インストール後、次のコマンドを実行して認証します。snyk auth。

認証プロセスが開始され、CLIで今後使用する認証トークンが生成されて保存されます。

プロのヒント:CLIは実行のたびにアップデートを確認します。アップデートがある場合は、必要なコマンドとともに通知します。常に最新バージョンを使うには、npm update -g snykを実行して手動で更新することもできます。

プロジェクトまたはフォルダーをスキャンする

CLIでSnyk Codeのスキャンを実行するには、次のようにします。snyk code test [my-folder-path]

パスを指定しない場合、CLIは現在のフォルダーをスキャンします。ディレクトリツリーをたどって、その内容をスキャンします。ほかにも多くのコマンドやパラメーターを利用できます。詳しくはドキュメントをご覧ください。

既存のCLIユーザーの方は、このベータ版ではSnyk Codeのmonitorコマンドがサポートされていない点にご注意ください。

注意:CLIはスキャン対象のサポートされているソースコードファイルをまとめてSnykにアップロードします。転送中および保存中のデータは暗号化されますが、ソースコードはお使いの環境の外部に送信されます。リポジトリでCLIを実行する前に、この点をご確認ください。

結果の出力形式

CLIでは、結果を3つの方法で出力できます。まず、CLIを使い始めるのにおすすめなのが、コマンドをそのまま実行する方法です。人が読みやすい形式でレポートが出力されます。

Testing . ...

 ✗ [Medium] Cross-Site Request Forgery (CSRF) 
     Path: app.js, line 16 
     Info: Consider using csurf middleware for your Express app to protect against CSRF attacks.

 ✗ [Medium] Use of Hard-coded Credentials 
     Path: index.js, line 10 
     Info: Do not hardcode credentials in code. Found hardcoded credential used in mysql.createConnection.

 ✗ [Medium] Allocation of Resources Without Limits or Throttling 
     Path: app.js, line 46 
     Info: This endpoint handler performs a file system operation and does not use a rate-limiting mechanism. It may enable the attackers to perform Denial-of-service attacks. Consider using a rate-limiting middleware such as express-limit.

 ✗ [Medium] Allocation of Resources Without Limits or Throttling 
     Path: index.js, line 15 
     Info: This endpoint handler performs a file system operation and does not use a rate-limiting mechanism. It may enable the attackers to perform Denial-of-service attacks. Consider using a rate-limiting middleware such as express-limit.

 ✗ [Medium] Allocation of Resources Without Limits or Throttling 
     Path: index.js, line 21 
     Info: This endpoint handler performs a file system operation and does not use a rate-limiting mechanism. It may enable the attackers to perform Denial-of-service attacks. Consider using a rate-limiting middleware such as express-limit.

 ✗ [Medium] Allocation of Resources Without Limits or Throttling 
     Path: createDummyData.js, line 16 
     Info: This endpoint handler performs a file system operation and does not use a rate-limiting mechanism. It may enable the attackers to perform Denial-of-service attacks. Consider using a rate-limiting middleware such as express-limit.

 ✗ [Medium] Use of Hard-coded Credentials 
     Path: index.js, line 11 
     Info: Do not hardcode passwords in code. Found hardcoded password used in mysql.createConnection.

 ✗ [High] Path Traversal 
     Path: index.js, line 25 
     Info: Unsanitized input from an HTTP parameter flows into fs.readFile, where it is used as a path. This may result in a Path Traversal vulnerability and allow an attacker to read arbitrary files.

 ✗ [High] SQL Injection 
     Path: index.js, line 39 
     Info: Unsanitized input from an HTTP parameter flows into get, where it is used in an SQL query. This may result in an SQL Injection vulnerability.

✔ Test completed

Organization:      undefined
Test type:         Static code analysis
Project path:      .

9 Code issues found
2 [High]  7 [Medium] 

重要度は色分けされ、脆弱性の説明と推奨される修正方法も表示されます。

プロのヒント:Snyk Codeは非常に高速です。適当なプロジェクト(クローンしたオープンソースリポジトリでも構いません)を選び、スキャンを実行して動作を確認してみましょう。

次に、CLIではsnyk code test --sarifを実行して、SARIFというデータ形式で結果を出力できます。

{
  "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
  "version": "2.1.0",
  "runs": [
    {
      "tool": {
        "driver": {
          "name": "SnykCode",
          "semanticVersion": "1.0.0",
          "version": "1.0.0",
          "rules": [
            {
              "id": "javascript/UseCsurfForExpress",
              "name": "UseCsurfForExpress",
              "shortDescription": {
                "text": "Cross-Site Request Forgery (CSRF)"
              },
              "defaultConfiguration": {
                "level": "warning"
              },
              "help": {
                "markdown": "\n## Details\nCross-site request forgery is an attack in which a malicious third party takes advantage of a user's authenticated credentials (such as a browser cookie) to impersonate that trusted user and perform unauthorized actions. The web application server cannot tell the difference between legitimate and malicious requests. This type of attack generally begins by tricking the user with a social engineering attack, such as a link or popup that the user inadvertently clicks, causing an unauthorized request to be sent to the web server. Consequences vary: At a standard user level, hackers can change passwords, transfer funds, make purchases, or connect with contacts; from an administrator account, hackers can then make changes to or even take down the app itself.\n\n### Best practices for prevention\n* Use development frameworks that defend against CSRF, using a nonce, hash, or some other security device to the URL and/or to forms.\n* Implement secure, unique, hidden tokens that are checked by the server each time to validate state-change requests.\n* Never assume that authentication tokens and session identifiers mean a request is legitimate.\n* Understand and implement other safe-cookie techniques, such as double submit cookies.\n* Terminate user sessions when not in use, including automatic timeout.\n* Ensure rigorous coding practices and defenses against other commonly exploited CWEs, since cross-site scripting (XSS), for example, can be used to bypass defenses against CSRF.\n\n## References\n\n* [Express Documentation - Implement CSRF Protection](https://expressjs.com/en/resources/middleware/csurf.html)\n* [Node Security Checklist - CSRF](https://blog.risingstack.com/node-js-security-checklist/#csrf)",
                "text": ""
              },
              "properties": {
                "tags": [
                  "javascript",
                  "maintenance",
                  "express",
                  "server",
                  "CSRF"
                ],
                "categories": [
                  "Security"
                ],
                "exampleCommitFixes": [
                  {
                    "commitURL": "https://github.com/eclipse/orion.client/commit/ad8f3bce33a1ea9d1e2144e6c42f075ad25829d6?diff=split#diff-16594450dc1f06f7d9cf4a47859cfa52L175",
                    "lines": [
                      {
                        "line": "}",
                        "lineNumber": 172,
                        "lineChange": "none"
                      },
...

この出力には、Snyk Codeのフロントエンドで確認できるすべての情報に加え、さらに詳しい情報が含まれています。修正例や優先度スコアも確認できます。

プロのヒント:コーヒーを片手に、SARIFファイルをじっくり見てみましょう。JSON形式なので、理解するのはそれほど難しくありません。豊富な情報が含まれているので、プロセスでの活用方法や統計の収集方法など、さまざまなアイデアが浮かぶはずです。

最後に、CLIは終了コードを出力します。たとえば、CI/CDプロセスの制御に利用できます。終了コードは次のとおりです。

  • 3:失敗。サポート対象のプロジェクトが見つからない

  • 2:失敗。コマンドを再実行する

  • 1:対応が必要。脆弱性が見つかった

  • 0:成功。脆弱性は見つからなかった

コマンドに--severity-threshold=<low|medium|high>パラメーターを指定すると、重要度で結果を絞り込めます。

ベータ版のSnyk Code CLIを試す

CLIは強力なツールです。パブリックベータへの参加を皆さんに呼びかけています。最新のSnyk CLIをインストールし、組織でSnyk Codeを有効にして、ぜひお試しください。ベータ版ですので、実際に使ってフィードバックをお寄せいただけると幸いです。

Snykを初めてご利用になる方は、まずSnyk Codeが有効になったSnykアカウントを作成しましょう。今すぐ無料で登録できます。

Capture the Flagを始めよう

オンデマンドのバーチャル入門ワークショップを見て、Capture the Flagの課題の解き方を学びましょう。

続きを読む

feature insights context
Blog

自律型攻撃はすでに始まっている。防御もそのスピードに追いつかなければならない。

自律型攻撃者によって、防御に使える時間は短くなっています。継続的な検出、修復、検証、予防で、セキュリティチームが攻撃に歩調を合わせる方法をご紹介します。

illustration hero ai
Blog

AIハリケーンが到来

AIはソフトウェア開発とサイバー攻撃の双方を加速させています。リーダーは、エージェントとコードを開発の初期段階から保護し、実行時に制御を徹底するとともに、防御策を独立して検証しなければなりません。

feature insights context
Blog

予防は、本質的に解決済みの問題なのでしょうか?

エージェントが生成するコードの予防策はアーキテクチャ上解決されていますが、開発を遅らせることなくセキュリティを守る制御を選ぶことが、依然として課題です。