detect

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2026 License: MIT, MIT Imports: 39 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SlowWarningThreshold is the amount of time to wait before logging that a file is slow.
	// This is useful for identifying problematic files and tuning the allowlist.
	SlowWarningThreshold = 5 * time.Second
)

Variables

This section is empty.

Functions

func IsNew

func IsNew(finding report.Finding, redact uint, baseline []report.Finding) bool

func LoadBaseline

func LoadBaseline(baselinePath string) ([]report.Finding, error)

func RedactFindings

func RedactFindings(findings []report.Finding, percent uint)

func SortRuleTimings

func SortRuleTimings(timings []RuleTiming)

func WriteRuleTimingsCSV

func WriteRuleTimingsCSV(w io.Writer, timings []RuleTiming) error

func WriteRuleTimingsHuman

func WriteRuleTimingsHuman(w io.Writer, timings []RuleTiming) error

Types

type Detector

type Detector struct {
	// Config is the configuration for the detector
	Config *config.Config

	// MaxDecodeDepths limits how many recursive decoding passes are allowed
	MaxDecodeDepth int

	// MatchContext specifies how much context to extract around a match.
	MatchContext contextwindow.Spec

	// ValidationStatusFilter, when non-empty, restricts which findings are
	// printed in verbose mode. Parsed from --validation-status.
	ValidationStatusFilter map[string]struct{}

	// MinConfidence suppresses classified findings below this level.
	MinConfidence string

	// ValidationPool is the expression validation worker pool.
	ValidationPool *validate.Pool

	// ValidationCounts tracks how many findings were returned for each
	// ValidationStatus value. Populated by the Run/DetectSource consumer;
	// safe to read after the scan returns.
	ValidationCounts map[report.ValidationStatus]int

	// ValidationExtractEmpty controls whether empty values from extractors
	// are included in validation output.
	ValidationExtractEmpty bool

	// IgnoreGitleaksAllow is a flag to ignore gitleaks:allow comments.
	IgnoreGitleaksAllow bool

	TotalBytes atomic.Uint64

	// RuleTimings records per-rule diagnostic timings when diagnostics are enabled.
	RuleTimings *RuleTimingCollector

	// TODO remove this in v2
	// SkipFindingAppend skips populating the deprecated detector-level findings
	// slice while consuming results from Run.
	//
	// This keeps Run callers from retaining a second compatibility copy of the
	// same findings when they are already consuming results directly.
	//
	// DetectSource intentionally ignores this flag to preserve its historical
	// return contract.
	SkipFindingAppend bool

	// ----------------------------------------------------------------
	// DEPRECATED fields below, to be removed in the next major version
	//
	//
	// report-related settings.
	// Deprecated: detect should not handle reporting
	ReportPath string
	// Deprecated: detect should not handle reporting
	Reporter report.Reporter

	// Redact is a flag to redact findings. This is exported
	// so users using gitleaks as a library can set this flag
	// without calling `detector.Start(cmd *cobra.Command)`
	Redact uint

	// verbose is a flag to print findings
	Verbose bool

	// MaxArchiveDepth limits how deep the sources will explore nested archives
	MaxArchiveDepth int

	// files larger than this will be skipped
	MaxTargetMegaBytes int

	// followSymlinks is a flag to enable scanning symlink files
	FollowSymlinks bool

	// NoColor is a flag to disable color output
	NoColor bool

	// LegacyPrint uses the legacy key/value verbose format (typically with Verbose=true).
	LegacyPrint bool

	// Sema (https://github.com/fatih/semgroup) controls the concurrency
	// Deprecated: this is only used for git log workers and can be removed when the legacy git scan is removed in v2.
	Sema *semgroup.Group
	// contains filtered or unexported fields
}

Detector is the main detector struct

func NewDetector deprecated

func NewDetector(cfg *config.Config) *Detector

NewDetector creates a new detector with the given config

Deprecated: use NewDetectorContext instead.

func NewDetectorContext

func NewDetectorContext(ctx context.Context, cfg *config.Config, valOpts ValidationOptions) *Detector

NewDetectorContext creates a new Detector. It compiles global expressions and, when valOpts.Enabled is true, creates the validation worker pool. Per-rule expressions compile lazily on first use.

func NewDetectorDefaultConfig

func NewDetectorDefaultConfig() (*Detector, error)

NewDetectorDefaultConfig creates a new detector with the default config

func (*Detector) AddBaseline

func (d *Detector) AddBaseline(baselinePath string, source string) error

func (*Detector) AddFinding

func (d *Detector) AddFinding(finding report.Finding)

AddFinding adds a finding to the pipeline. Findings needing validation are submitted to the pool; all others go directly to findingsCh. Deprecated: only used in deprecated calls. New code calls routeFinding directly.

func (*Detector) AddGitleaksIgnore

func (d *Detector) AddGitleaksIgnore(gitleaksIgnorePath string) error

func (*Detector) Detect deprecated

func (d *Detector) Detect(fragment sources.Fragment) []report.Finding

Detect scans the given fragment and returns a list of findings

Deprecated: use DetectContext instead.

func (*Detector) DetectContext deprecated

func (d *Detector) DetectContext(ctx context.Context, fragment sources.Fragment) []report.Finding

DetectContext is the same as Detect but supports passing in a context to use for timeouts

Deprecated: use Run or DetectString

func (*Detector) DetectFragment

func (d *Detector) DetectFragment(ctx context.Context, fragment sources.Fragment) []report.Finding

DetectFragment scans one fragment with its source attributes.

func (*Detector) DetectFragmentRuleWindows

func (d *Detector) DetectFragmentRuleWindows(ctx context.Context, fragment sources.Fragment, ruleIDs []string, windows map[string][]RuleWindow) []report.Finding

DetectFragmentRuleWindows limits selected rules to byte ranges known to contain their matches while retaining the fragment's source attributes.

func (*Detector) DetectSource

func (d *Detector) DetectSource(ctx context.Context, source sources.Source) ([]report.Finding, error)

DetectSource scans the given source and returns a list of findings Deprecated: use Run instead for more flexible and efficient processing of findings.

func (*Detector) DetectString

func (d *Detector) DetectString(content string) []report.Finding

DetectString scans the given string and returns a list of findings

func (*Detector) DetectStringRuleWindows

func (d *Detector) DetectStringRuleWindows(content string, ruleIDs []string, windows map[string][]RuleWindow) []report.Finding

DetectStringRuleWindows limits selected rules to byte ranges known to contain their matches. Rules without windows scan the complete content.

func (*Detector) DetectStringRules

func (d *Detector) DetectStringRules(content string, ruleIDs []string) []report.Finding

DetectStringRules scans content with the named rules. Component rules are still resolved from the detector config when a selected rule references them.

func (*Detector) FilterByStatus

func (d *Detector) FilterByStatus(findings []report.Finding) []report.Finding

FilterByStatus returns findings whose ValidationStatus is in d.ValidationStatusFilter. If the filter is empty, all findings are returned. The pseudo-status "none" matches findings with no ValidationStatus set.

func (*Detector) Findings

func (d *Detector) Findings() []report.Finding

Findings returns the findings added to the detector. Deprecated: this is only used in deprecated calls. New code should access findings directly from the channel or ValidationPool. Report redaction is applied in cmd.findingSummaryAndExit via RedactFindings.

func (*Detector) Run

func (d *Detector) Run(ctx context.Context, source sources.Source) iter.Seq[Result]

Run executes the pipeline on the given source and yields results as they are found. It returns an iterator of Results, which can be consumed by the caller. We return an iterator to make the API clean. You can do things like:

	for result := range detector.Run(ctx, source) {
    	// do something
	}

The context can be used to cancel the scan. Internally uses a channel to send results from the scanning goroutine to the caller, allowing for concurrent processing of findings as they are discovered.

func (*Detector) SkipFunc

func (d *Detector) SkipFunc() sources.SkipFunc

SkipFunc returns a sources.SkipFunc callback that evaluates the config's prefilter program against fragment attributes. Returns nil when no prefilter is configured (sources treat nil as "skip nothing").

func (*Detector) Tokenizer

func (d *Detector) Tokenizer() *tiktoken.Tiktoken

Tokenizer returns the BPE tokenizer used for token efficiency filtering. May be nil if the tokenizer failed to initialize.

func (*Detector) ValidateFindings

func (d *Detector) ValidateFindings(ctx context.Context, findings []report.Finding) ([]report.Finding, error)

ValidateFindings applies optional credential validation through the shared pool.

type Location

type Location struct {
	// contains filtered or unexported fields
}

Location represents a location in a file

type Result

type Result struct {
	Finding report.Finding
	Err     error
}

type RuleTiming

type RuleTiming struct {
	RuleID string
	Total  time.Duration
	Hits   uint64
}

func (RuleTiming) Average

func (rt RuleTiming) Average() time.Duration

type RuleTimingCollector

type RuleTimingCollector struct {
	// contains filtered or unexported fields
}

func NewRuleTimingCollector

func NewRuleTimingCollector() *RuleTimingCollector

func (*RuleTimingCollector) Record

func (c *RuleTimingCollector) Record(ruleID string, duration time.Duration)

func (*RuleTimingCollector) Snapshot

func (c *RuleTimingCollector) Snapshot() []RuleTiming

type RuleWindow

type RuleWindow struct {
	Start     int
	End       int
	StartLine int
}

type TiktokenLoader

type TiktokenLoader struct{}

TikTokenLoader implements the tiktoken.BpeLoader interface

func (*TiktokenLoader) LoadTiktokenBpe

func (l *TiktokenLoader) LoadTiktokenBpe(file string) (map[string]int, error)

LoadTiktokenBpe parses the embedded BPE file into the expected map.

type ValidationOptions

type ValidationOptions struct {
	Enabled                 bool
	Debug                   bool
	Workers                 int
	Timeout                 time.Duration
	ExtractEmpty            bool
	StatusFilter            string // comma-separated list of statuses to include
	MaxRequestsPerTarget    int
	RequestsPerSecond       float64
	RequestsPerSecondByRule map[string]float64
	// ValidationEnvVars lists environment variable names the validation Expr
	// env(...) binding may read (see --validation-env-vars). Parsed into
	// exprruntime.Runtime.AllowedEnv when the validation env is created.
	ValidationEnvVars []string
}

ValidationOptions controls secret validation behavior. Zero value means validation is disabled.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL