package websource import ( "net/url" "regexp" ) // secretPatterns are the credential shapes the runner's redaction knows // (runnerproto.RedactText and RedactDetail: bearer and basic values, // credential-named assignments, sk-, ghp_ and github_pat_ prefixes, JSON // web tokens), plus other common key prefixes and private key headers. var secretPatterns = []*regexp.Regexp{ regexp.MustCompile(`(?i)\b(?:bearer|basic)(?:\s|%20|\+)+[A-Za-z0-9._~+/=-]{8,}`), regexp.MustCompile(`(?i)(?:api[_-]?key|access[_-]?key|password|passwd|secret|token|auth)\s*[=:]\s*[^\s,"'<>&/]+`), regexp.MustCompile(`\b(?:sk|rk|pk)-[A-Za-z0-9_-]{8,}`), regexp.MustCompile(`\b(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{8,}`), regexp.MustCompile(`\bgh[pousr]_[A-Za-z0-9]{16,}`), regexp.MustCompile(`\bgithub_pat_[A-Za-z0-9_]{16,}`), regexp.MustCompile(`\bglpat-[A-Za-z0-9_-]{16,}`), regexp.MustCompile(`\bxox[abposr]-[A-Za-z0-9-]{10,}`), regexp.MustCompile(`\b(?:AKIA|ASIA)[0-9A-Z]{16}\b`), regexp.MustCompile(`\bAIza[0-9A-Za-z_-]{30,}`), regexp.MustCompile(`\beyJ[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]*`), regexp.MustCompile(`-----BEGIN|PRIVATE KEY`), // RedactDetail's long opaque run. regexp.MustCompile(`[A-Za-z0-9_+]{41,}`), } // opaqueRun is a run that could hold an encoded secret: base64 or hex // characters, broken by the punctuation that separates words in a path. var opaqueRun = regexp.MustCompile(`[A-Za-z0-9+=]{24,}`) // LooksSecret reports text that matches a credential pattern, or holds a // high-entropy token: 24 or more letters and digits in a run, with both // letters and digits and at least 12 different characters. It checks s // and, when s is percent-encoded, its decoded form too. func LooksSecret(s string) bool { if looksSecret(s) { return true } if d, err := url.PathUnescape(s); err == nil && d != s { return looksSecret(d) } return false } func looksSecret(s string) bool { for _, p := range secretPatterns { if p.MatchString(s) { return true } } for _, run := range opaqueRun.FindAllString(s, -1) { var letter, digit bool seen := map[rune]bool{} for _, c := range run { seen[c] = true letter = letter || c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' digit = digit || c >= '0' && c <= '9' } if letter && digit && len(seen) >= 12 { return true } } return false }