Documentation
¶
Overview ¶
Package secrets provides built-in secret-pattern scanning for gk push.
Index ¶
- Variables
- func CompilePatterns(raw []string) (compiled []*regexp.Regexp, bad []string)
- func IsLowEntropySecret(value string) bool
- func IsPlaceholderLine(line string) bool
- func MaskLine(line string) string
- func PayloadFileHeader(path string) string
- func SecretValue(sm []string) string
- type Finding
- type Pattern
Constants ¶
This section is empty.
Variables ¶
var BuiltinPatterns = []Pattern{ {Kind: "aws-access-key", Regex: regexp.MustCompile(`\bAKIA[0-9A-Z]{16}\b`)}, {Kind: "aws-secret-key", Regex: regexp.MustCompile(`(?i)aws[_-]?(?:secret|sk)[^"']{0,5}["']?([A-Za-z0-9/+=]{40})`)}, {Kind: "github-token", Regex: regexp.MustCompile(`\bghp_[A-Za-z0-9]{36}\b`)}, {Kind: "github-fine", Regex: regexp.MustCompile(`\bgithub_pat_[A-Za-z0-9_]{82}\b`)}, {Kind: "slack-token", Regex: regexp.MustCompile(`\bxox[abpr]-[A-Za-z0-9-]{10,}\b`)}, {Kind: "openai-key", Regex: regexp.MustCompile(`\bsk-[A-Za-z0-9]{20,}\b`)}, {Kind: "private-key", Regex: regexp.MustCompile(`-----BEGIN (RSA |EC |OPENSSH |DSA |ENCRYPTED |PGP )?PRIVATE KEY-----`)}, {Kind: "generic-secret", Regex: regexp.MustCompile(`(?i)(api[_-]?key|secret|token|password)\s*[:=]\s*["']([A-Za-z0-9/_\-+=]{24,})["']`)}, }
BuiltinPatterns is the default scan set. Keep it small and high-signal. Each entry must be broadly unambiguous (avoid matching normal code).
var PayloadFileHeaderRE = regexp.MustCompile(`^>>> gk-file (.+) <<<$`)
PayloadFileHeaderRE matches a single header line. Anchored on both ends so a stray ">>> gk-file foo <<<" appearing inside real content (e.g. quoted in a doc) cannot trigger a false boundary.
Functions ¶
func CompilePatterns ¶
CompilePatterns compiles user-supplied regex strings. Returns compiled list and the list of raw patterns that failed to compile.
func IsLowEntropySecret ¶
isLowEntropySecret reports whether a matched value is too monotonous to be a real credential. Genuine tokens are near-random over a large alphabet (base62 for github-token/openai-key); fixtures repeat a tiny one — "ghp_0000…0000", "sk-aaaa…aaaa", "1234…". Only values of 20+ characters are judged, so the distinct-rune floor never trips a real key: any real base62 token that long clears 8 distinct characters with overwhelming probability, while reproduction-by-hand fakes do not.
func IsPlaceholderLine ¶
IsPlaceholderLine returns true when the line contains an example/placeholder value (example/dummy/test/changeme/…). Applied to every kind except private-key.
Judged on the whole LINE, not the value alone, because the giveaway usually sits beside the credential — a comment, a variable name, an assert. That breadth is why callers that BLOCK on a hit (the push/ship scanner) drop the finding outright, while callers that merely REDACT (the aicommit privacy gate) keep redacting and only exempt the value from their abort threshold.
func MaskLine ¶
MaskLine returns line with every built-in secret match masked in place. The credential value is replaced by its mask() form while the surrounding code (variable names, comments) stays intact, so verbose context output around a hit reads naturally yet never exposes an adjacent secret. For keyword=value kinds (generic-secret) only the value is masked, not the keyword.
func PayloadFileHeader ¶
PayloadFileHeader formats the file boundary marker that the secret-scan pipeline emits between files in its aggregated blob. The marker uses ">>> gk-file <path> <<<" so it cannot collide with markdown H3 headers (`### foo`) — an earlier `### <path>` convention caused README section titles like "### 첫 호출" to be mistaken for filenames in finding output.
PayloadFileHeaderRE is the matching parser. Both must stay in sync; callers should never hand-roll either.
func SecretValue ¶
SecretValue returns the substring worth masking for display. Patterns like generic-secret (group 1 = keyword, group 2 = value) or aws-secret-key (group 1 = value) capture the credential separately from surrounding boilerplate; we mask the last non-empty capture group so the sample shows the value's prefix (e.g. "dev-****") instead of the keyword ("secr****"), which is what a reader needs to judge a false positive. Patterns with no capture groups fall back to the full match.
Exported so the aicommit privacy gate identifies the credential inside its own (differently-grouped) patterns the same way, rather than weighing the whole `key = "value"` match as if it were the secret.
Types ¶
type Finding ¶
type Finding struct {
Kind string // short label: "aws-access-key", "github-token", "private-key", ...
Line int // 1-based line number within the input blob (kept verbatim for
// callers — e.g. aicommit — that remap blob lines themselves)
File string // owning file path, recovered from PayloadFileHeader markers; "" when none present
FileLine int // 1-based line within File; equals Line when no file header precedes the hit
Sample string // a masked sample suitable for display
// LineText, ContextBefore, and ContextAfter back verbose ("show the
// surrounding source") output. They are populated only by diff-aware
// callers (the push/ship scanner) and are all secret-masked, so printing
// them never leaks an adjacent credential. Empty when not collected or at
// a hunk edge.
LineText string `json:"line_text,omitempty"`
ContextBefore string `json:"context_before,omitempty"`
ContextAfter string `json:"context_after,omitempty"`
}
Finding is a single secret-pattern hit.
func Scan ¶
Scan scans blob for secret findings. extra lets callers append user-provided compiled regexes from config. Returns findings in line order; multiple findings per line are included.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package gitleaksrules scans unified diffs with the gitleaks default ruleset embedded in the gk binary, so secret scanning works without the gitleaks binary installed.
|
Package gitleaksrules scans unified diffs with the gitleaks default ruleset embedded in the gk binary, so secret scanning works without the gitleaks binary installed. |