guard

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const PathPrefix = ".commitbrief/"

PathPrefix is the trigger condition: any diff file whose path starts with this string (i.e., lives under the .commitbrief/ directory) prompts the user. Root-level COMMITBRIEF.md and .commitbriefignore are intentionally excluded — they are team-shared by design (ADR-0007).

Variables

This section is empty.

Functions

func AllPatternNames added in v1.7.0

func AllPatternNames(extra []secretPattern) []string

AllPatternNames returns the labels of every pattern in the effective set (built-ins + extra user patterns), de-duped and sorted alphabetically. extra == nil yields exactly the built-in list, so SecretPatternNames delegates here.

func CompileUserPatterns added in v1.7.0

func CompileUserPatterns(specs []UserSecretPattern) ([]secretPattern, error)

CompileUserPatterns turns user-supplied {Name, Regex} specs into the internal compiled form so they can be merged with the built-ins (ADR-0024). It is purely additive — the built-ins always run; this only supplies extras. A spec with an empty name, an empty regex, or an invalid regex returns a nil slice and an error naming the offending pattern, so the review can fail fast with an actionable message instead of silently skipping the pattern. A nil/empty input returns (nil, nil) so the common "no user patterns" path stays allocation-free.

func InjectionLines added in v1.7.0

func InjectionLines(matches []InjectionMatch) []int

InjectionLines flattens a slice of InjectionMatch into a sorted, comma-free list of the 1-based line numbers that matched. Convenience for the CLI's single-line warning ("lines: 4, 9") so the formatting logic doesn't have to live in the i18n call site.

func SecretPatternNames added in v0.8.0

func SecretPatternNames() []string

SecretPatternNames returns the labels of every built-in pattern the scanner knows about, sorted alphabetically. Used by docs/tests as the authoritative list — keeps drift between the table here and the CHANGELOG/README description detectable. User patterns are not included (use AllPatternNames for the effective set).

func Triggers

func Triggers(d diff.Diff) []string

Types

type InjectionMatch added in v1.7.0

type InjectionMatch struct {
	Line     int      // 1-based line number within the scanned content
	Patterns []string // alphabetised category labels that matched this line
}

InjectionMatch describes a single line in a user-authored rules file (a non-default COMMITBRIEF.md or OUTPUT.md template) whose text looks like an attempt to override the system prompt. Like SecretMatch it records only the 1-based line number and the matched category labels — never the raw line — so the warning output can't itself become noise or a copy of whatever the user wrote. ADR-0025.

func ScanForInjection added in v1.7.0

func ScanForInjection(content string) []InjectionMatch

ScanForInjection walks arbitrary text (a user's rules content) and reports any line that matches one or more prompt-injection patterns, case-insensitively. It is intended for a NON-DEFAULT COMMITBRIEF.md and the user's OUTPUT.md template — the caller is responsible for skipping the trusted embedded defaults (ADR-0025).

Returns matches sorted by line number; empty/clean input returns nil so callers can rely on len(out)==0 as the "nothing to warn about" signal. Line numbers are 1-based and count every line in the input verbatim.

type Options

type Options struct {
	AssumeYes      bool
	NonInteractive bool
	Writer         io.Writer
	Reader         io.Reader

	// Interactive routes the confirm through ui.Confirm's arrow-key
	// Yes/No toggle (huh) instead of reading a line off Reader. CLI
	// callers set it from ui.IsStdinTTY(os.Stdin); tests leave it
	// false so the deterministic line path (Reader) still drives them.
	Interactive bool

	// Catalog plumbs i18n into the .commitbrief/* write-guard so the
	// user-visible warning, file lines, prompt, and abort messages
	// honour the active locale. Nil → English defaults (legacy
	// behaviour). Every CLI caller should pass app.Catalog so
	// Turkish users actually see Turkish here (UC-15).
	Catalog *i18n.Catalog
}

type Result

type Result int
const (
	Continue Result = iota
	Abort
)

func CheckDiffForLocalConfig

func CheckDiffForLocalConfig(d diff.Diff, opts Options) (Result, error)

func (Result) String

func (r Result) String() string

type SecretMatch added in v0.8.0

type SecretMatch struct {
	Line     int      // 1-based line number within the diff string
	Patterns []string // alphabetised pattern names that matched this line
}

SecretMatch describes a single line in the diff that looks like it might contain a credential the user shouldn't ship to an LLM. Only the line number and the matched-pattern names are recorded — never the matched substring itself, so the scanner's own output can't become a secondary leak vector via logs, stderr, or cache files.

func ScanForSecrets added in v0.8.0

func ScanForSecrets(diff string) []SecretMatch

ScanForSecrets walks the diff and reports any added line (prefixed with a single `+`, excluding the `+++ b/path` header) that matches one or more of the built-in credential patterns. Thin wrapper over ScanForSecretsWith(diff, nil) for callers with no user patterns.

Returns a slice of matches sorted by line number. An empty diff or a diff with no `+` lines returns nil — callers can rely on `len(out) == 0` as the "all clear" signal.

func ScanForSecretsWith added in v1.7.0

func ScanForSecretsWith(diff string, extra []secretPattern) []SecretMatch

ScanForSecretsWith is ScanForSecrets plus extra user-supplied patterns (ADR-0024). The built-ins always run; extra is appended (de-duped by name, built-in wins) so a user pattern can never silence a built-in. Removed and context lines are skipped — the goal is to catch *new* leaks, not to re-flag historical content already on disk.

func ScanText added in v0.9.1

func ScanText(content string) []SecretMatch

ScanText runs the built-in credential patterns against arbitrary text (no diff prefixes). Thin wrapper over ScanTextWith(content, nil). Used to scan rules content like COMMITBRIEF.md and the output template before they get embedded into the system prompt and shipped to the provider. UC-05 in PATCH_ROADMAP. Empty input returns nil so callers can rely on len(out)==0 as the "all clear" signal.

func ScanTextWith added in v1.7.0

func ScanTextWith(content string, extra []secretPattern) []SecretMatch

ScanTextWith is ScanText plus extra user-supplied patterns (ADR-0024). Same merge semantics as ScanForSecretsWith.

type UserSecretPattern added in v1.7.0

type UserSecretPattern struct {
	Name  string
	Regex string
}

UserSecretPattern is one user-supplied credential spec (ADR-0024): a human-readable Name and a Regex source string. It is the input shape for CompileUserPatterns and mirrors config.SecretPatternConfig without importing the config package (guard stays leaf-level). The compiled form (secretPattern) is unexported; callers pass the result of CompileUserPatterns to ScanForSecretsWith / ScanTextWith.

Jump to

Keyboard shortcuts

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