Secrets scanning in the Snyk CLI
Use Snyk Secrets for the CLI to identify and manage sensitive information (API keys, passwords, tokens) in your source code. You can:
Prerequisites
To use this feature, you must:
Install the latest version of the Snyk CLI. For installation instructions, visit Install the Snyk CLI.
Authenticate your machine with the following command:
snyk authRun a secrets scan
To scan your current directory for hard-coded secrets, run:
snyk secrets testTo map results to your Organization, include the --org option.
The output includes the following information:
Severity: Snyk assigns a severity level to each finding based on the type and risk of the exposed secret.
Finding ID: A unique identifier for the specific leak. You need this ID to ignore findings.
Path and line number: The locations of the discovered secret in your codebase.
Ignore findings
If a secret is a known placeholder, a revoked key, or a won't fix scenario, use the ignore command to suppress the finding.
To ignore a finding:
Copy the Finding ID from the
snyk secrets testoutput.Run the
ignorecommand:Follow the prompts:
Finding ID: Paste the ID you copied.
Ignore Type: Select wont-fix, not-vulnerable, or temporary-ignore.
Expiration: Set an optional expiry date (YYYY-MM-DD).
Reason: Provide context (for example,
Key has been rotated and is no longer active).
Review ignored secrets
Use the --include-ignores option to run a scan and view suppressed items. This helps you audit your codebase and ensures you do not hide critical leaks.
Exclude files and directories from a scan
Exclude the paths that you do not want Snyk Secrets to scan, such as test fixtures, vendored dependencies, or sample credentials. Snyk applies the exclusions before the scan, so excluded files are not uploaded to Snyk and produce no findings.
You can exclude paths in the following ways:
Commit a
.snykfile to your repository. Snyk recommends this approach, because everyone who scans the repository shares the exclusions, and Snyk also applies them when the repository is scanned through an SCM integration. Visit Exclude paths using the.snykfile.Use the
--excludeoption for a single scan. Visit Exclude paths for a single scan.
Snyk Secrets also skips the paths listed in your .gitignore files, as well as file types that cannot contain readable secrets, such as binaries, archives, media files, fonts, and dependency lockfiles.
Exclude paths using the .snyk file
The .snyk file is a YAML policy file that you commit to your repository. The snyk secrets test command applies the patterns from the following exclude sections of every .snyk file it finds in the scanned directory tree:
global: Applies to Snyk Secrets and to the other Snyk products that support theglobalsection.secrets: Applies to Snyk Secrets only.
The following example excludes the vendor directory from all the supported Snyk products, and excludes the PEM test fixtures and the examples directory from Snyk Secrets only:
Exclude paths for a single scan
Use the --exclude option to exclude directory names and file names for one scan, without committing anything to the repository. Provide a comma-separated list of names, without paths:
This excludes every directory or file with a matching name, at any depth, for example ./fixtures and ./src/fixtures.
Scan with a pre-commit hook
Run Snyk Secrets as a pre-commit hook to catch secrets before they reach a commit. The hook keeps secret detection in the developer workflow and prevents new secrets from entering the repository.
Authenticate the hook
The secrets test command must run as an authenticated CLI command. Each developer authenticates their own CLI locally.
Authenticate in one of two ways:
Run
snyk authto use your local Snyk credentials.Export
SNYK_TOKENin your shell environment.
Do not embed a token in the hook command
Do not embed a token in the hook command, for example SNYK_TOKEN=... snyk secrets test. The repository stores and shares hook configuration, so anyone with repository access can copy an embedded token. The token can leak into logs and get flagged by future scans. Keep credentials out of the repository and have the CLI read them from local auth state or SNYK_TOKEN.
Pass staged file paths
Pass the staged files to Snyk instead of scanning the whole project. Scanning the full repository on every commit is slower, can block unrelated commits on pre-existing findings, and makes it harder to tell whether a failure came from your change or from older files.
Set up with the pre-commit framework
Add a local hook to .pre-commit-config.yaml:
With pass_filenames: true, pre-commit appends the staged files to the command. The shell wrapper preserves the blocking behavior by exiting non-zero when any scan fails.
Use other hook managers
The same pattern works with other hook managers: collect the staged files and pass them to secrets test.
Last updated
Was this helpful?

