REGEXVAULT
Security/Secrets & Config
ReDoS Checked

Generic Secret Assignment in Code Regex for Go

/(?:password|passwd|secret|token|api[_-]?key|auth[_-]?key|access[_-]?key|client[_-]?secret|private[_-]?key|encryption[_-]?key)(?:[\s]*[:=][\s]*)["']([^"'\s]{8,})["']/i

All five implementations are free. Switch engines without signing in.

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching generic secret assignment in code, ported and verified for Go. In security-sensitive code, using an unverified regex can open the door to both false positives and denial-of-service attacks. The snippet below is ready to drop into your Go project — whether you're validating in a Gin handler, a gRPC service, or a command-line tool.

Go Implementation

Go
// Generic Secret Assignment in Code
// ReDoS-checked | RegexVault — Security > Secrets & Config

package validation

import "regexp"

var genericSecretAssignmentInCodeRe = regexp.MustCompile(`(?:password|passwd|secret|token|api[_-]?key|auth[_-]?key|access[_-]?key|client[_-]?secret|private[_-]?key|encryption[_-]?key)(?:[\s]*[:=][\s]*)["']([^"'\s]{8,})["']`)

func ValidateGenericSecretAssignmentInCode(s string) bool {
    return genericSecretAssignmentInCodeRe.MatchString(s)
}

// Example
// fmt.Println(ValidateGenericSecretAssignmentInCode("password = 'mySecretPassword123'")) // true

Test Cases

Matches (Valid)
Rejects (Invalid)
password = 'mySecretPassword123'password = ''
api_key = "AIzaSyD-9tSrke72I6e0qOZV"api_key = variable_name
token: 'eyJhbGciOiJIUzI1NiJ9'// password might be here

When to use this pattern

This pattern is drawn from the Security > Secrets & Config category and has been checked for ReDoS risk. That matters for Go developers because Go's RE2 engine is inherently safe from catastrophic backtracking, but this pattern has been additionally verified for correctness. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.

Common Pitfalls

Effective secret scanning requires both pattern matching AND entropy analysis. Simple dictionary words that match the pattern are likely placeholders. Tools like TruffleHog and GitLeaks combine regex with Shannon entropy scoring.

Technical Notes

Detection pattern for secret scanning in codebases and config files. The value capture group (1) should be examined for entropy and length. False positive rate is non-trivial — values like 'your_password_here' or 'changeme' are common placeholders. Use entropy scoring alongside pattern matching.

Have a pattern that belongs in the vault?

Submit it for review — community-verified patterns get credited to your GitHub handle. Submissions join the review queue after automated validation.

Submit a Pattern