Snyk CLI Documentation

repository·main·Indexed 26 days ago

https://github.com/snyk/cli

A developer-first security tool used to scan and monitor software projects for vulnerabilities across Open Source, Code, Containers, and Infrastructure as Code (IaC). The CLI provides core commands such as snyk test, snyk monitor, and snyk auth, as well as specialized scanning for containers, IaC, and AI-BOMs. It includes the @snyk/protect library for applying vulnerability patches and supports configuration via environment variables.

Tokens
42.5K
Snippets
103
Records
348
Agent score
88%

What's inside Snyk CLI

  1. How @snyk/protect works

    main

    When a patch is available for a vulnerability, Snyk modifies your project to apply the patch automatically during installation. The process involves:

    1. Adding a patch entry to your .snyk file.
    2. Adding @snyk/protect to your package.json dependencies.
    3. Adding @snyk/protect to your package.json prepare script.

    Once configured, running npm install will trigger @snyk/protect to download and apply the patches defined in your .snyk file to your installed dependencies.

     {
       "name": "my-project",
       "scripts": {
    +    "prepare": "npm run snyk-protect",
    +    "snyk-protect": "snyk-protect"
       },
       "dependencies": {
    +    "@snyk/protect": "^1.657.0"
       }
     }
  2. Available Snyk CLI commands

    main

    The Snyk CLI provides a variety of commands for different security testing needs. You can view detailed information for any command by appending the --help flag (e.g., snyk auth --help).

    Core Commands

    • snyk auth: Authenticate the CLI with your Snyk account.
    • snyk test: Test a project for open-source vulnerabilities and license issues. Use snyk test --unmanaged for C/C++ dependency scanning.
    • snyk monitor: Snapshot a project and enable continuous monitoring for vulnerabilities and license issues.

    Specialized Scanning

    • snyk container: Test and monitor container images and generate container SBOMs.
    • snyk iac: Scan Infrastructure as Code (IaC) files for security issues and manage unmanaged resources.
    • snyk code: Perform Static Code Analysis using snyk code test.
    • snyk sbom: Generate or test an SBOM document for supported ecosystems.
    • snyk aibom: Generate an AI-BOM for Python, Java, JavaScript, or Go projects to identify AI models, datasets, and tools.
    • snyk aibom test: Generate an AI-BOM and test it against tenant policies.
    • snyk log4shell: Specifically search for Log4Shell vulnerabilities.

    Configuration and Policy Management

    • snyk config: Manage Snyk CLI configuration.
    • snyk policy: Display the .snyk policy for a package.
    • snyk ignore: Modify the .snyk policy to ignore specific issues.
  3. Generate a Container SBOM

    main

    Use the snyk container sbom command to generate a Software Bill of Materials (SBOM) for a container image. This includes both operating system and application dependencies.

    Prerequisites:

    • This feature is in Early Access and requires a Snyk Enterprise plan.
    • Minimum CLI version: 1.1226.0.
    • Requires an internet connection.

    Supported Formats:

    • CycloneDX v1.4 (JSON or XML)
    • CycloneDX v1.5 (JSON or XML)
    • CycloneDX v1.6 (JSON or XML)
    • SPDX v2.3 (JSON)
  4. Manage Snyk CLI configuration with `snyk config`

    main

    The snyk config command manages your local Snyk CLI configuration file. This file is a JSON object located at $XDG_CONFIG_HOME/configstore/snyk.json or ~/.config/configstore/snyk.json.

    Note: This command manages CLI settings and does not manage the .snyk project policy file. For project policies, use snyk policy or snyk ignore.

    snyk config <SUBCOMMAND> [<OPTIONS>]
  5. Get started with the Snyk CLI

    main

    To begin using the Snyk CLI to scan and monitor your projects for security vulnerabilities and license issues, follow these three steps:

    1. Authenticate: Connect the CLI to your Snyk account using snyk auth.
    2. Test: Scan your local project for vulnerabilities using snyk test.
    3. Monitor: Set up continuous monitoring and alerts for new vulnerabilities using snyk monitor.
    snyk auth
    snyk test
    snyk monitor
  6. Run an AI Red Teaming scan with snyk redteam

    main

    The snyk redteam command performs an experimental adversarial scan against AI models and applications. It probes for vulnerabilities like model jailbreaking, prompt injections, and sensitive information disclosure.

    Warning: This feature is experimental, potentially disruptive, and subject to breaking changes. Always use test data, test credentials, and target dev/staging environments rather than production. Avoid testing on applications that trigger costly or disruptive actions (e.g., sending emails or invoking expensive APIs).

    Prerequisites:

    • An internet connection.
    • A preview version of the Snyk CLI.
    • Authentication via snyk auth.
    $ snyk redteam --experimental [<OPTION>]
  7. Follow the Snyk Responsible Disclosure Policy

    main

    When reporting vulnerabilities to Snyk, you are expected to adhere to the following responsible disclosure policy:

    • Allow investigation time: Give Snyk reasonable time to investigate and mitigate the issue before making any information public or sharing it with others.
    • Respect user privacy: Do not interact with individual accounts (including accessing or modifying data) without the account owner's consent.
    • Avoid disruption: Make a good faith effort to avoid privacy violations and service disruptions, such as data destruction or service degradation.
    • No exploitation: Do not exploit the discovered security issue for any reason, including demonstrating additional risk through attempted compromises or probing for further issues.
    • Legal compliance: Do not violate any applicable laws or regulations.
  8. Build the Snyk CLI for Docker Desktop on macOS

    main

    To build the Snyk CLI distribution specifically for Docker Desktop on macOS, you must run the build script from the root of the workspace (one directory above the docker-desktop directory). The build process generates a tarball containing a signed NodeJS executable that allows Docker Desktop to execute the Snyk CLI JavaScript build.

    Build Command: Run the following from the workspace root:

    ./docker-desktop/build.sh darwin x64

    Output Location: The resulting tarball will be located at: ./binary-releases/snyk-for-docker-desktop-darwin-x64.tar.gz

  9. Authenticate in CI/CD environments

    main
    In CI/CD environments where interactive browser authentication is not possible, do not use snyk auth. Instead, set the SNYK_TOKEN environment variable with your Snyk API token. Once this variable is set, you can run CLI commands directly without an explicit auth step.