Scans directories for leaked credentials using vendored Gitleaks rules.
See scan/2 for the library API and Mix.Tasks.SecretScan for the CI task.
Summary
Types
A detected credential.
How repeated occurrences are reported.
How much of a matched credential appears in the preview.
Rules retained first when the result limit is reached.
Rules to scan.
A named ruleset.
An option accepted by scan/2.
Functions
Returns the Gitleaks rule IDs in a named ruleset.
Recursively scans regular files below directory.
Types
@type finding() :: %{ rule: String.t(), file_path: String.t(), line: pos_integer(), byte_offset: non_neg_integer(), fingerprint: binary(), preview: String.t() }
A detected credential.
Paths are relative to the scanned directory, lines are one-based, and byte offsets are zero-based. The fingerprint is a raw HMAC-SHA256 binary. The matched credential itself is never included.
@type occurrence_mode() :: :all | :first_per_file
How repeated occurrences are reported.
@type preview_mode() :: :masked | :redacted
How much of a matched credential appears in the preview.
@type priority_rule_selection() :: :all | :hexpm | :none | [String.t()]
Rules retained first when the result limit is reached.
Rules to scan.
@type ruleset() :: :all | :hexpm
A named ruleset.
@type scan_option() :: {:ignore, [String.t()]} | {:rules, rule_selection()} | {:priority_rules, priority_rule_selection()} | {:occurrences, occurrence_mode()} | {:preview, preview_mode()} | {:fingerprint_key, binary()} | {:file_timeout, pos_integer()} | {:scan_timeout, pos_integer()} | {:max_concurrency, pos_integer()} | {:max_findings, pos_integer()} | {:max_locations, pos_integer()} | {:max_path_length, pos_integer() | :infinity}
An option accepted by scan/2.
Functions
Returns the Gitleaks rule IDs in a named ruleset.
Pass :all for every vendored rule or :hexpm for the narrower ruleset
used for Hex.pm package notifications.
@spec scan(Path.t(), [scan_option()]) :: {[finding()], boolean()}
Recursively scans regular files below directory.
Files are streamed in overlapping windows, so scanning doesn't load a whole file into memory. Symbolic links and other non-regular files aren't followed.
Returns {findings, incomplete?}. The second element is true when a file
couldn't be read, a timeout expired, or a result limit dropped findings.
Invalid options and a missing directory raise ArgumentError.
Options
:ignore- path globs relative todirectory.*matches within one path segment,?matches one non-separator character, and**crosses directory separators. Defaults to[].:rules- rules to run. Accepts:all,:hexpm, or a list of Gitleaks rule IDs.:allis the default.:hexpmis the narrower ruleset used for Hex.pm package notifications. Unknown IDs raiseArgumentError. Seerule_ids/1.:priority_rules- rules retained first if:max_findingsis reached. Accepts:all,:hexpm,:none, or a list of rule IDs. Defaults to:hexpm. This option doesn't change which rules run.:occurrences-:allreports every occurrence.:first_per_filereports the first occurrence of each credential in each file. Defaults to:all.:preview-:maskedshows only a fixed-width mask.:redactedmay retain a short prefix and suffix. Defaults to:masked.:fingerprint_key- binary HMAC key used to fingerprint credentials. Supply the same key to compare findings across scans. The default is a new random key for each scan.:file_timeout- maximum time spent scanning one file, in milliseconds. Defaults to10_000.:scan_timeout- maximum time for the whole scan, including directory traversal, in milliseconds. Defaults to30_000.:max_concurrency- maximum number of files scanned concurrently. Defaults toSystem.schedulers_online/0.:max_findings- maximum number of findings returned. Defaults to100. Dropped findings setincomplete?totrue.:max_locations- maximum locations returned for one credential. Defaults to10. Dropped locations setincomplete?totrue.:max_path_length- maximum length of a returned path, or:infinity. Defaults to:infinity.
Example
{findings, incomplete?} =
SecretScan.scan("/workspace/my_app",
rules: :hexpm,
ignore: ["test/fixtures/**"],
occurrences: :first_per_file
)