Documentation
¶
Overview ¶
Package efficacy measures whether a SAST rule does what it says: it reports the defect it was written for, in the shapes that defect takes, and stays quiet on the code that merely resembles it.
A case is a directory of real files under testdata/efficacy/<rule>/<case>/. A line that begins with the marker "~>" is a line the rule must report; the marker is removed before the files are scanned, so the scanner sees exactly the code a developer would have written. Every other line in the case must not be reported. A case with no marker is a pure false-positive trap.
Cases are evaluated through the production engine (sast.NewEngine and Evaluate), so the input builder, the language filter, content loading and the per-finding file scope all apply exactly as they do in a scan.
Index ¶
- Constants
- func Describe(r Result) string
- func Entropy(s string) float64
- func EvalSharded(rules *Rules, stems []string, in *sast.ScanInput) (map[string][]Hit, error)
- func ExpandSamples(src, file string) (string, error)
- func RandomTokenEntropy(pattern string, q float64, n int) (float64, error)
- func RuleID(src string) string
- func Sample(pattern string, variant int) (full, group string, err error)
- func SampleMode(pattern string, variant int, mode genMode) (full, group string, err error)
- func SaveSnapshot(path string, ps []Project) error
- func StripMarkers(src string) (string, []int)
- type Case
- type CatalogEntry
- type Hit
- type Loc
- type Profile
- type ProfileOptions
- type Project
- type Result
- type Rules
- type SecretCheck
- type Summary
Constants ¶
const ( VerdictOK = "ok" VerdictFail = "fail" // realistic tokens of the rule's own shape are missed, or a gate is ineffective VerdictUnmeasured = "unmeasured" // no pattern-valid sample carries a keyword: needs a realistic example )
Verdicts for a catalog entry.
const Marker = "~>"
Marker is the line prefix that marks a line the rule must report.
Variables ¶
This section is empty.
Functions ¶
func EvalSharded ¶
EvalSharded evaluates many rules against one input, compiling them in parallel shards as the engine does, and returns each rule's hits.
func ExpandSamples ¶
ExpandSamples replaces every @@sample:REGEX@@ in src.
func RandomTokenEntropy ¶
RandomTokenEntropy returns the q-quantile of the entropy of n random tokens of the pattern's capture group: the threshold below which a real credential of this shape almost always falls.
func Sample ¶
Sample produces a string that matches pattern, built from the pattern's own syntax tree. It exists so a secret-detection rule can be proven to fire on the token shape it describes without a realistic credential ever being committed (provider push protection would rightly block that, and a test corpus of live-looking keys is its own leak).
variant selects among alternatives and repetition lengths, so successive variants exercise different branches. Character runs are drawn without replacement from the class, which maximises entropy: an entropy-gated rule should accept a sample, and when it does not, the threshold is unreachable for that alphabet.
The result is checked against the compiled pattern; an error means no variant up to the limit could satisfy the pattern's assertions (\b, ^, $).
func SampleMode ¶
SampleMode is Sample with a fill mode for character runs.
func SaveSnapshot ¶
SaveSnapshot and LoadSnapshot persist a corpus so repeated measurements do not walk and read thousands of files each time.
func StripMarkers ¶
StripMarkers removes "~>" markers and returns the cleaned text with the 1-based lines that carried one.
Types ¶
type Case ¶
type Case struct {
Rule string `json:"rule"` // rule file stem, e.g. "vnx-go-014"
Name string `json:"name"` // case directory name
Dir string `json:"dir"`
Files map[string]string `json:"-"` // relative path → content, markers removed
Want []Loc `json:"want"`
}
Case is one fixture directory for one rule.
type CatalogEntry ¶
type CatalogEntry struct {
ID int `json:"id"`
Category string `json:"category"`
Name string `json:"name"`
Strategy string `json:"strategy"`
Pattern string `json:"token_pattern"`
Keywords []string `json:"keywords"`
EntropyMin float64 `json:"entropy_min"`
Deprecated bool `json:"deprecated"`
}
CatalogEntry is the part of a secretsgen catalog entry the checks need.
func LoadCatalog ¶
func LoadCatalog(path string) ([]CatalogEntry, error)
LoadCatalog reads secretsgen/catalog.json.
func (CatalogEntry) Stem ¶
func (e CatalogEntry) Stem() string
Stem is the generated rule's file stem.
type Hit ¶
type Hit struct {
Project string `json:"project"`
File string `json:"file"`
Line int `json:"line"`
Snippet string `json:"snippet"`
}
Hit is one finding on a corpus project.
type Profile ¶
type Profile struct {
Rule string `json:"rule"`
ID string `json:"id"`
Compile time.Duration `json:"compile_ns"`
Eval time.Duration `json:"eval_ns"` // sum over projects of the fastest repetition
Findings int `json:"findings"`
Files int `json:"files"` // distinct files with a finding
ByProject map[string]int `json:"by_project,omitempty"`
Hits []Hit `json:"hits,omitempty"`
Err string `json:"error,omitempty"`
}
Profile is the measurement of one rule over a corpus.
func ProfileRules ¶
func ProfileRules(rules *Rules, stems []string, projects []Project, opts ProfileOptions) ([]Profile, error)
ProfileRules compiles and evaluates each rule on its own against every project and reports its cost and its findings.
type ProfileOptions ¶
type ProfileOptions struct {
Reps int // repetitions per project; the fastest is kept (default 1)
Workers int // concurrent rules (default GOMAXPROCS/2)
KeepHits int // hits to keep per rule (0 keeps none)
}
ProfileOptions controls Profile.
type Project ¶
type Project struct {
Name string `json:"name"`
Root string `json:"root"`
Input *sast.ScanInput `json:"input"`
}
Project is one scanned tree in a corpus snapshot. Projects are kept apart rather than merged under a prefix so that root-anchored rules (a path that starts with ".github/", a Dockerfile at the top) see the layout they would see in a real scan.
func LoadSnapshot ¶
func Snapshot ¶
Snapshot builds the scan input for each corpus directory the way the sast command does: gitignore respected, text up to 1 MiB, binaries skipped. With secrets it is built the way the secrets command does instead: binaries read for their strings and the git history merged in, up to the command's default limits, which is most of what a secrets rule evaluates.
type Result ¶
type Result struct {
Rule string `json:"rule"`
Case string `json:"case"`
Want []Loc `json:"want"`
Got []Loc `json:"got"`
Missing []Loc `json:"missing,omitempty"` // expected, not reported: a false negative
Extra []Loc `json:"extra,omitempty"` // reported, not expected: a false positive
Err string `json:"error,omitempty"`
Elapsed time.Duration `json:"elapsed_ns"`
}
Result is the outcome of one case.
type Rules ¶
Rules is a rule directory read from disk: the shared library modules every rule may import, and the rule modules by file stem.
func LoadRules ¶
LoadRules reads a rule directory. Reading from disk (not the embedded FS) means an edited rule is measured without rebuilding anything.
type SecretCheck ¶
type SecretCheck struct {
ID int `json:"id"`
Rule string `json:"rule"`
Name string `json:"name"`
Verdict string `json:"verdict"`
Eligible int `json:"eligible"` // random samples that carry a keyword and are not placeholders
Detected int `json:"detected"`
Rate float64 `json:"rate"`
KeywordGap int `json:"keyword_gap"` // pattern-valid samples the keyword prefilter skips
MedianEntropy float64 `json:"median_entropy,omitempty"`
Problems []string `json:"problems,omitempty"`
Warnings []string `json:"warnings,omitempty"`
FiredOnBy []string `json:"fired_on_by,omitempty"` // other rules that report this entry's tokens
FiresOnOthers int `json:"fires_on_others"` // other entries' tokens this rule reports
Skipped string `json:"skipped,omitempty"` // the generator could not produce a sample
}
SecretCheck is the verdict for one catalog entry.
func CheckSecretsCatalog ¶
func CheckSecretsCatalog(rules *Rules, entries []CatalogEntry) ([]SecretCheck, error)
CheckSecretsCatalog proves each generated secret rule reports realistic random tokens of its own shape, that its placeholder and entropy gates reject what they are meant to, and records which other rules report the same token.
type Summary ¶
type Summary struct {
Rule string `json:"rule"`
Cases int `json:"cases"`
PositiveCases int `json:"positive_cases"`
NegativeCases int `json:"negative_cases"`
Expected int `json:"expected"` // lines the cases mark
TruePos int `json:"tp"`
FalseNeg int `json:"fn"`
FalsePos int `json:"fp"`
Failed int `json:"failed_cases"`
}
Summary aggregates results per rule.