detect

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package detect is the third pipeline stage: it matches registry patterns, entropy and keyword context, computes the fingerprint id and confidence, and emits candidate.Candidate envelopes.

Index

Constants

View Source
const Patterns = "registry"

Patterns is the name of the registry pattern adapter.

Variables

View Source
var ErrNoRegistry = errors.New("detect: no registry")

ErrNoRegistry is returned when Options.Registry is nil.

Functions

func Fingerprint

func Fingerprint(provider string, parts ...string) string

Fingerprint returns the stable finding id of a credential:

"th_" + hex(sha256(provider + "\x00" + part1 + "\x00" + part2 ...))[:16]

parts are the credential's ordered parts as the registry declares them (one part for a single key). The id depends only on the provider and the parts, never on a path, line or commit, so the same key has the same id everywhere and on every OS.

func HasTestMarker

func HasTestMarker(raw string) bool

HasTestMarker reports whether raw carries the test key marker, plain or hex encoded (either case).

func Register

func Register(name string, f Factory)

Register adds a detect adapter. Adapters call it from init().

Types

type Batch

type Batch interface {
	Detect(ctx context.Context, c candidate.Chunk, emit func(candidate.Candidate) bool, event func(model.Event))
	End(ctx context.Context, emit func(candidate.Candidate) bool, event func(model.Event))
}

Batch detects in the chunks of one source chunk. Detect handles one chunk (the source chunk or a chunk decoded from it) and records single-part credentials at once; parts of multi-part credentials are buffered until End pairs them and records the result. End must be called once, after the last Detect, with the same emit; it stops early when ctx is done.

type Detector

type Detector interface {
	Detect(ctx context.Context, c candidate.Chunk, emit func(candidate.Candidate) bool, event func(model.Event))
	Begin() Batch
	Merged(id string) (model.Finding, bool)
	// MergedCtx returns the suppression context of the finding forwarded under the
	// first-sight id, merged over every sighting: the test marker, the first-sight id
	// and the inline allow facts of every location Merged returns.
	MergedCtx(id string) (candidate.SuppressCtx, bool)
}

Detector is the detect stage interface. Detect finds credentials in one chunk. The first sighting of a credential is emitted as a Candidate (emit returns false when the scan is stopping) with a finding-detected event; later sightings only add locations to the dedupe record, with a location-added event each. After the engine barrier, Merged(first-sight id) returns the detect-owned fields of a finding with every location, computed from the best winner over every sighting, and MergedCtx its suppression context with the inline allow facts of every location. Detect only records facts; it never sets Suppressed.

Begin starts a Batch: one source chunk plus every chunk decoded from it. Parts of a multi-part credential are paired only within one Batch, at End; Detect equals Begin, Batch.Detect and Batch.End on one chunk. Detect, Begin and Merged are safe for concurrent use; one Batch is used by one goroutine. event is called with an internal lock held and must not block.

func NewPatterns

func NewPatterns(opts Options) (Detector, error)

NewPatterns returns a detector running every pattern of opts.Registry.

type Factory

type Factory func(Options) (Detector, error)

Factory builds a Detector.

func Lookup

func Lookup(name string) (Factory, bool)

Lookup returns the detect adapter registered under name.

type Options

type Options struct {
	// Registry supplies the patterns. Required.
	Registry *registry.Registry
}

Options configures a detector.

type Pattern

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

Pattern is the registry pattern detector.

func (*Pattern) Begin

func (p *Pattern) Begin() Batch

Begin starts a Batch for one source chunk and the chunks decoded from it.

func (*Pattern) Detect

func (p *Pattern) Detect(ctx context.Context, c candidate.Chunk, emit func(candidate.Candidate) bool, event func(model.Event))

Detect runs every pattern and the generic detector over c alone: it is Begin, Batch.Detect and Batch.End on one chunk.

func (*Pattern) Merged

func (p *Pattern) Merged(id string) (model.Finding, bool)

Merged returns the detect-owned fields of the finding forwarded under the first-sight id, with every location sorted. Its ID, Provider, Detector, Confidence and AlsoMatchedBy come from the best winner over every sighting, so they do not depend on the order chunks arrived in; the ID may differ from the first-sight id.

func (*Pattern) MergedCtx added in v0.2.0

func (p *Pattern) MergedCtx(id string) (candidate.SuppressCtx, bool)

MergedCtx returns the suppression context of the finding forwarded under the first-sight id: the test marker, the first-sight id and the inline allow fact of every location Merged returns that has one, in the same order. InlineAllow is set when at least one location is covered for the first-sight or the final id.

Jump to

Keyboard shortcuts

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