leaks

package
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: GPL-3.0 Imports: 13 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 leaks scans a repository for credential-shaped content (ADR-0036).

It is the standalone counterpart to the pre-send scanner in internal/guard. That one is a gate: it sees the added lines of the one diff about to be sent to a provider, and its job is to stop the send. This one is an audit: it reads whole files in the working tree and the added lines of arbitrary historical commits, and its job is to tell you what is there.

The pattern set is not duplicated — guard owns it, including the ADR-0024 user extensions, and this package drives it over two new inputs. guard stays a leaf (it must not import internal/git); the orchestration lives here.

The redaction invariant

A Finding records a file, a line number and the pattern names that matched. It NEVER records the matched text. That is the ADR-0007 invariant, restated in engineering/standards/security.md, and it matters more here than in guard because this package reads whole files and writes a report the user is likely to paste somewhere. There is a test asserting the secret never reaches the output; keep it.

Index

Constants

View Source
const DefaultMaxFileBytes = 5 << 20 // 5 MiB

DefaultMaxFileBytes caps how large a file may be before the scanner passes over it. The patterns all target short, structured tokens; a file this size is a database dump or a bundled asset, and running eight regexes over every line of it costs far more than it can plausibly find.

Variables

This section is empty.

Functions

func IsHighNotCritical

func IsHighNotCritical(pattern string, userPatterns []guard.UserSecretPattern) bool

IsHighNotCritical reports whether a pattern name should be surfaced as `high` rather than `critical`. User patterns (ADR-0024) are also `high`: they are somebody's house format and this package cannot assert how bad a hit really is.

Types

type Finding

type Finding struct {
	// File is the repo-relative, slash-normalized path.
	File string
	// Line is 1-based: the line in the file for a worktree hit, or the line in
	// the commit's post-image for a history hit.
	Line int
	// Patterns are the alphabetised names of every pattern that matched.
	Patterns []string

	// Commit attribution, empty for a worktree hit. Knowing who introduced a
	// key and when is what decides whether it still needs rotating.
	Commit string
	Short  string
	Author string
	Date   time.Time
}

Finding is one credential-shaped hit.

func (Finding) FromHistory

func (f Finding) FromHistory() bool

FromHistory reports whether this hit came from a commit rather than the current working tree.

type Options

type Options struct {
	// Patterns is the compiled user-pattern set from guard.CompileUserPatterns.
	// The built-ins always run regardless; this is additive only (ADR-0024).
	Patterns []guard.UserSecretPattern

	// Matcher is the composed ignore layer set (built-ins + .commitbriefignore).
	// nil means no ignore filtering.
	Matcher *ignore.Matcher

	// Files/Dirs narrow to these paths; ExcludeFiles/ExcludeDirs remove from
	// the result. Same semantics as the review's --file/--dir/--exclude-*.
	Files        []string
	Dirs         []string
	ExcludeFiles []string
	ExcludeDirs  []string

	// MaxFileBytes caps how large a file may be before it is skipped; 0 uses
	// DefaultMaxFileBytes.
	MaxFileBytes int64
}

Options are the inputs shared by both scan halves.

type Result

type Result struct {
	Findings []Finding

	// FilesScanned counts files actually read (worktree) or file entries
	// examined (history).
	FilesScanned int
	// CommitsScanned is 0 for the worktree half.
	CommitsScanned int
	// Skipped counts files passed over as binary or oversized. Reported, never
	// silent — a scanner that quietly ignores half a repo is worse than none.
	Skipped int
	// Truncated is set when the commit walk hit its cap.
	Truncated bool
}

Result is one half's outcome.

func ScanHistory

func ScanHistory(ctx context.Context, repoRoot string, sel git.Selection, opts Options) (Result, error)

ScanHistory reports credential-shaped content in the ADDED lines of the selected commits.

Added lines only, deliberately: a secret that was committed and later removed is still in the history — reachable by anyone who clones the repo — and finding exactly that is the point of scanning history at all. Scanning context lines instead would re-report the same key once per commit that touched the file near it.

Each commit's patch is fetched and scanned separately rather than as one concatenated blob, because a finding is only actionable with attribution: which commit introduced it, by whom, and when decides whether the key still needs rotating.

func ScanWorktree

func ScanWorktree(ctx context.Context, repoRoot string, opts Options) (Result, error)

ScanWorktree reads every tracked file in the working tree and reports the credential-shaped lines in it.

Enumeration is `git ls-files -z`, not filepath.WalkDir, for three reasons: it yields exactly the tracked set (an untracked, gitignored .env is where a secret is *supposed* to live, so flagging it would be noise); it respects .gitignore for free; and it sidesteps ignore.Matcher having no isDir=true entry point, which would make directory pruning during a walk subtly wrong.

Unlike guard's diff scan this reads whole files — every line, not just added ones — because the question here is "what is in my tree right now", not "what am I about to send".

func (Result) Merge

func (r Result) Merge(other Result) Result

Merge folds another Result into this one, concatenating findings and summing the counters. Used to combine the worktree and history halves into one report.

func (*Result) Sort

func (r *Result) Sort()

Sort orders findings deterministically: worktree hits before history hits, then by path, then by line, then by commit. Two runs over an unchanged repo must produce byte-identical reports, or diffing two scans is useless.

Jump to

Keyboard shortcuts

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