secrets

package
v0.151.2 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 3 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 secrets provides built-in secret-pattern scanning for gk push.

Index

Constants

This section is empty.

Variables

View Source
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).

View Source
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

func CompilePatterns(raw []string) (compiled []*regexp.Regexp, bad []string)

CompilePatterns compiles user-supplied regex strings. Returns compiled list and the list of raw patterns that failed to compile.

func IsLowEntropySecret

func IsLowEntropySecret(value string) bool

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

func IsPlaceholderLine(line string) bool

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

func MaskLine(line string) string

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

func PayloadFileHeader(path string) string

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

func SecretValue(sm []string) string

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

func Scan(blob string, extra []*regexp.Regexp) []Finding

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.

func (Finding) Location

func (f Finding) Location() string

Location renders the finding's position for display: "file:line" when the owning file is known (the scan input carried PayloadFileHeader markers), otherwise "line N" using the raw blob position. Keeps push/ship output in sync from a single source.

type Pattern

type Pattern struct {
	Kind  string
	Regex *regexp.Regexp
}

Pattern is a named regex.

Jump to

Keyboard shortcuts

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