config

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: 19 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultRuleSpecificity = 100

Variables

View Source
var (
	//go:embed betterleaks.toml
	DefaultConfig string
)

Functions

This section is empty.

Types

type Allowlist

type Allowlist struct {
	// Short human readable description of the allowlist.
	Description string

	// MatchCondition determines whether all criteria must match. Defaults to "OR".
	MatchCondition AllowlistMatchCondition

	// Commits is a slice of commit SHAs that are allowed to be ignored.
	Commits []string

	// Paths is a slice of path regular expressions that are allowed to be ignored.
	Paths []*regexp.Regexp

	// Can be `match` or `line`.
	//
	// If `match` the _Regexes_ will be tested against the match of the _Rule.Regex_.
	//
	// If `line` the _Regexes_ will be tested against the entire line.
	//
	// If RegexTarget is empty, it will be tested against the found secret.
	RegexTarget string

	// Regexes is slice of content regular expressions that are allowed to be ignored.
	Regexes []*regexp.Regexp

	// StopWords is a slice of stop words that are allowed to be ignored.
	// This targets the _secret_, not the content of the regex match like the
	// Regexes slice.
	StopWords []string
	// contains filtered or unexported fields
}

Allowlist allows a rule to be ignored for specific regexes, paths, and/or commits

func (*Allowlist) CommitAllowed

func (a *Allowlist) CommitAllowed(c string) (bool, string)

CommitAllowed returns true if the commit is allowed to be ignored.

func (*Allowlist) ContainsStopWord

func (a *Allowlist) ContainsStopWord(s string) (bool, string)

func (*Allowlist) PathAllowed

func (a *Allowlist) PathAllowed(path string) bool

PathAllowed returns true if the path is allowed to be ignored.

func (*Allowlist) RegexAllowed

func (a *Allowlist) RegexAllowed(secret string) bool

RegexAllowed returns true if the regex is allowed to be ignored.

func (*Allowlist) Validate

func (a *Allowlist) Validate() error

type AllowlistMatchCondition

type AllowlistMatchCondition int
const (
	AllowlistMatchOr AllowlistMatchCondition = iota
	AllowlistMatchAnd
)

func (AllowlistMatchCondition) String

func (a AllowlistMatchCondition) String() string

type Component

type Component struct {
	RuleID string
	// Optional components are attached when found but do not gate the primary finding.
	Optional bool
	// Within uses the same directional L/C grammar as --match-context.
	Within string
}

Component references another rule that contributes a nearby match to a multipart finding.

type Config

type Config struct {
	Title       string
	Extend      Extend
	Path        string
	Description string
	Rules       map[string]Rule
	Keywords    map[string]struct{}
	// KeywordToRules maps each lowercase keyword to the rule IDs that use it.
	// This allows O(1) lookup from Aho-Corasick keyword matches to the rules
	// that need to be checked, instead of iterating all rules.
	KeywordToRules map[string][]string
	// NoKeywordRules contains rule IDs that have no keywords and must always be checked.
	NoKeywordRules []string
	// used to keep sarif results consistent
	OrderedRules []string

	// Deprecated: use filter/prefilter Expr expressions instead. This is a shim for backwards-compatibility.
	Allowlists []*Allowlist

	MinVersion            string
	BetterleaksMinVersion string

	// Prefilter is a global expression (attributes only) evaluated before any
	// per-match work. Returns true = skip this fragment entirely; false = keep.
	// Translated from global Allowlists path/commit checks.
	Prefilter string
	// Filter is a global expression (attributes + finding) evaluated per match.
	// Returns true = skip (discard) this finding; false = keep.
	// Translated from global Allowlists regex/stopword checks.
	Filter string
	// contains filtered or unexported fields
}

Config is a configuration struct that contains rules and an allowlist if present.

func Default

func Default() (*Config, error)

func LoadFile

func LoadFile(path string) (*Config, error)

func ParseTOML

func ParseTOML(data []byte, path string) (*Config, error)

func ParseTOMLString

func ParseTOMLString(content, path string) (*Config, error)

func (*Config) CompileFilters

func (c *Config) CompileFilters(tokenizer *tiktoken.Tiktoken) error

CompileFilters compiles only the global prefilter needed before scanning. Global finding filters and per-rule filters compile lazily on first candidate.

func (*Config) CompileValidation

func (c *Config) CompileValidation() (*exprruntime.Runtime, error)

CompileValidation returns a runtime for per-rule validation expressions. Individual validation programs are compiled lazily by the detector. Returns (nil, nil) when no rules have validation expressions.

func (*Config) FilterProgram

func (c *Config) FilterProgram() exprruntime.Program

FilterProgram returns the compiled global filter program, or nil if not set.

func (*Config) GetOrderedRules

func (c *Config) GetOrderedRules() []Rule

func (*Config) PrefilterProgram

func (c *Config) PrefilterProgram() exprruntime.Program

PrefilterProgram returns the compiled global prefilter program, or nil if not set.

func (*Config) SetFilterProgram

func (c *Config) SetFilterProgram(p exprruntime.Program)

SetFilterProgram stores a compiled global filter program.

func (*Config) SetPrefilterProgram

func (c *Config) SetPrefilterProgram(p exprruntime.Program)

SetPrefilterProgram stores a compiled global prefilter program.

func (*Config) TranslateLegacyFilters

func (c *Config) TranslateLegacyFilters() error

TranslateLegacyFilters converts deprecated Allowlists, Entropy, and TokenEfficiency fields into prefilter/filter expressions on the Config and its Rules. This is exported for use by the config generator, but is also called internally at the end of config translation (after all extends and targeted allowlists are populated). Translated expressions are logged at the debug level to help users migrate. Notes:

  • Expressions return true (skip) or false (keep).
  • Prefilters exist only at the global level (source/resource attributes-only, runs before regex).
  • Rules only have filters (attributes + finding, runs per match).

type Extend

type Extend struct {
	Path          string   `toml:"path"`
	URL           string   `toml:"url"`
	UseDefault    bool     `toml:"useDefault"`
	DisabledRules []string `toml:"disabledRules"`
}

Extend is a struct that allows users to define how they want their configuration extended by other configuration files.

type Rule

type Rule struct {
	// RuleID is a unique identifier for this rule
	RuleID string

	// Description is the description of the rule.
	Description string

	// Entropy is a float representing the minimum shannon
	// entropy a regex group must have to be considered a secret.
	Entropy float64

	// SecretGroup is an int used to extract secret from regex
	// match and used as the group that will have its entropy
	// checked if `entropy` is set.
	SecretGroup int

	// Regex is a golang regular expression used to detect secrets.
	Regex *regexp.Regexp

	// Path is a golang regular expression used to
	// filter secrets by path
	Path *regexp.Regexp

	// Tags is an array of strings used for metadata
	// and reporting purposes.
	Tags []string

	// Specificity controls precedence when overlapping findings compete.
	// Higher specificity findings suppress lower specificity findings.
	Specificity int

	// Confidence estimates how likely a match is to be a real secret.
	Confidence string

	// Keywords are used for pre-regex check filtering. Rules that contain
	// keywords will perform a quick string compare check to make sure the
	// keyword(s) are in the content being scanned.
	Keywords []string

	// Allowlists allows a rule to be ignored for specific commits, paths, regexes, and/or stopwords.
	Allowlists []*Allowlist

	// Components are other rules whose matches contribute to this rule.
	// Required components gate the rule; optional components are attached when found.
	Components []*Component

	SkipReport bool

	// TokenEfficiency enables the Token Efficiency filter for this rule.
	// When enabled, candidate secrets are evaluated using BPE tokenization
	// to measure how "rare" or non-natural-language a string is. Strings that
	// tokenize efficiently (i.e., common words/phrases) are filtered out.
	TokenEfficiency bool

	// ValidateExpr is the raw expression used for secret validation.
	ValidateExpr string

	// Filter is an expression evaluated against attributes + finding per regex match.
	// Returns true = skip (discard this finding); false = keep.
	// Deprecated legacy Allowlists, Entropy, and TokenEfficiency are translated into this field.
	Filter string
	// contains filtered or unexported fields
}

Rules contain information that define details on how to detect secrets

func (*Rule) FilterProgram

func (r *Rule) FilterProgram() exprruntime.Program

FilterProgram returns the compiled filter program for this rule, or nil.

func (*Rule) SetFilterProgram

func (r *Rule) SetFilterProgram(p exprruntime.Program)

SetFilterProgram stores a compiled filter program on the rule.

func (*Rule) SetValidationProgram

func (r *Rule) SetValidationProgram(p exprruntime.Program)

SetValidationProgram stores a compiled validation program on the rule.

func (*Rule) Validate

func (r *Rule) Validate() error

Validate guards against common misconfigurations.

func (*Rule) ValidationProgram

func (r *Rule) ValidationProgram() exprruntime.Program

ValidationProgram returns the compiled validation program for this rule, or nil.

Jump to

Keyboard shortcuts

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