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 AWSCredential
- func (c *AWSCredential) GoString() string
- func (c *AWSCredential) KeyID() string
- func (c *AWSCredential) MarshalJSON() ([]byte, error)
- func (c *AWSCredential) MaskedKeyID() string
- func (c *AWSCredential) SecretCandidates() []string
- func (c *AWSCredential) SessionToken() string
- func (c *AWSCredential) 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 AWSCredential ¶ added in v0.155.0
type AWSCredential struct {
// contains filtered or unexported fields
}
AWSCredential is an AWS access key ID with the secret access key candidates and session token found next to it. It holds raw credential values for live verification, so every formatter and encoder sees only the masked key ID: a finding that carries one can be printed or marshaled without leaking.
func NewAWSCredential ¶ added in v0.155.0
func NewAWSCredential(keyID string, secretCandidates []string, token string) *AWSCredential
NewAWSCredential builds a credential directly; scanners use PairAWS.
func PairAWS ¶ added in v0.155.0
func PairAWS(lines []string, idx int, keyID string) *AWSCredential
PairAWS collects the secret access key candidates and the session token around lines[idx], which holds keyID. It searches the raw lines instead of other findings because the generic-api-key finding for a secret on the key ID's own line is dropped as shadowed. Candidates on lines that mention "secret" come first, then nearer lines.
func (*AWSCredential) GoString ¶ added in v0.155.0
func (c *AWSCredential) GoString() string
func (*AWSCredential) KeyID ¶ added in v0.155.0
func (c *AWSCredential) KeyID() string
KeyID returns the raw access key ID.
func (*AWSCredential) MarshalJSON ¶ added in v0.155.0
func (c *AWSCredential) MarshalJSON() ([]byte, error)
MarshalJSON keeps raw values out of any encoded finding.
func (*AWSCredential) MaskedKeyID ¶ added in v0.155.0
func (c *AWSCredential) MaskedKeyID() string
MaskedKeyID shows the key ID prefix and its last four characters.
func (*AWSCredential) SecretCandidates ¶ added in v0.155.0
func (c *AWSCredential) SecretCandidates() []string
SecretCandidates returns the raw secret access key candidates, nearest first.
func (*AWSCredential) SessionToken ¶ added in v0.155.0
func (c *AWSCredential) SessionToken() string
SessionToken returns the raw session token, or "" when none was found.
func (*AWSCredential) String ¶ added in v0.155.0
func (c *AWSCredential) String() string
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"`
// AWS is set on an embedded-engine aws-access-token finding so
// `gk guard check --verify` can test the key; it formats masked.
AWS *AWSCredential `json:"-"`
}
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 awsverify checks whether AWS access keys found by a secret scan are live, with signed STS GetCallerIdentity requests (at most three per key).
|
Package awsverify checks whether AWS access keys found by a secret scan are live, with signed STS GetCallerIdentity requests (at most three per key). |
|
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. |