-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecrets.go
More file actions
297 lines (244 loc) · 9.38 KB
/
secrets.go
File metadata and controls
297 lines (244 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package tok
import (
"regexp"
"sort"
"strings"
"sync"
)
// SecretMatch represents a detected secret within text.
type SecretMatch struct {
Type string // The category/type of the secret (e.g., "AWS Access Key", "GitHub Token")
Value string // The matched secret value
Masked string // A masked version for safe display
StartPos int // Start byte position in the original text
EndPos int // End byte position in the original text
}
// secretPattern holds a compiled regex and its associated type name.
type secretPattern struct {
Type string
Pattern *regexp.Regexp
}
// SecretDetector detects and redacts secrets from text using compiled regex patterns.
type SecretDetector struct {
patterns []secretPattern
}
var (
defaultDetector *SecretDetector
defaultDetectorOnce sync.Once
)
// DefaultSecretDetector returns the singleton SecretDetector instance with all built-in patterns.
func DefaultSecretDetector() *SecretDetector {
defaultDetectorOnce.Do(func() {
defaultDetector = NewSecretDetector()
})
return defaultDetector
}
// NewSecretDetector creates a SecretDetector with all built-in patterns compiled and ready.
func NewSecretDetector() *SecretDetector {
sd := &SecretDetector{}
sd.patterns = compilePatterns()
return sd
}
// compilePatterns builds and returns all secret detection patterns.
// Patterns are inspired by trufflehog's detector architecture but adapted for
// fast, local-only redaction (no network verification).
func compilePatterns() []secretPattern {
raw := []struct {
Type string
Pattern string
}{
// AWS Access Key ID (starts with AKIA, ASIA for temporary)
{"AWS Access Key", `\b((?:AKIA|ASIA)[A-Z0-9]{16})\b`},
// AWS Secret Access Key (40 char base64-like after known prefixes in context)
{"AWS Secret Key", `(?i)(?:aws_secret_access_key|aws_secret_key|secret_access_key)\s*[:=]\s*['"]?([A-Za-z0-9/+=]{40})['"]?`},
// GitHub tokens (ghp_, gho_, ghu_, ghs_, ghr_, github_pat_)
{"GitHub Token", `\b((?:ghp|gho|ghu|ghs|ghr|github_pat)_[a-zA-Z0-9_]{36,255})\b`},
// GitLab Personal Access Token
{"GitLab Token", `\b(glpat-[a-zA-Z0-9\-=_]{20,22})\b`},
// Slack Bot Token
{"Slack Bot Token", `(xoxb-[0-9]{10,13}-[0-9]{10,13}[a-zA-Z0-9\-]*)`},
// Slack User Token
{"Slack User Token", `(xoxp-[0-9]{10,13}-[0-9]{10,13}[a-zA-Z0-9\-]*)`},
// Slack App Token
{"Slack App Token", `(xapp-[0-9]-[A-Z0-9]+-[0-9]+-[a-zA-Z0-9]+)`},
// JWT Token (three base64url segments separated by dots)
{"JWT Token", `\b(eyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,})\b`},
// Private Keys (PEM format)
{"RSA Private Key", `(-----BEGIN RSA PRIVATE KEY-----[\s\S]*?-----END RSA PRIVATE KEY-----)`},
{"EC Private Key", `(-----BEGIN EC PRIVATE KEY-----[\s\S]*?-----END EC PRIVATE KEY-----)`},
{"OpenSSH Private Key", `(-----BEGIN OPENSSH PRIVATE KEY-----[\s\S]*?-----END OPENSSH PRIVATE KEY-----)`},
{"Private Key", `(-----BEGIN PRIVATE KEY-----[\s\S]*?-----END PRIVATE KEY-----)`},
// Database Connection Strings
{"Database Connection String", `(?i)((?:postgres|mysql|mongodb|redis|amqp|mssql)://[^\s'"` + "`" + `]{10,})`},
// Stripe Keys
{"Stripe Secret Key", `\b([rs]k_live_[a-zA-Z0-9]{20,247})\b`},
{"Stripe Publishable Key", `\b(pk_live_[a-zA-Z0-9]{20,247})\b`},
// SendGrid API Key
{"SendGrid API Key", `\b(SG\.[a-zA-Z0-9_-]{22}\.[a-zA-Z0-9_-]{43})\b`},
// Twilio Account SID and Auth Token
{"Twilio Account SID", `\b(AC[a-f0-9]{32})\b`},
{"Twilio Auth Token", `(?i)(?:twilio_auth_token|twilio_token|auth_token)\s*[:=]\s*['"]?([a-f0-9]{32})['"]?`},
// Heroku API Key
{"Heroku API Key", `(?i)(?:heroku_api_key|heroku_key|HEROKU_API_KEY)\s*[:=]\s*['"]?([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})['"]?`},
// DigitalOcean
{"DigitalOcean Token", `\b(dop_v1_[a-f0-9]{64})\b`},
{"DigitalOcean OAuth Token", `\b(doo_v1_[a-f0-9]{64})\b`},
{"DigitalOcean Refresh Token", `\b(dor_v1_[a-f0-9]{64})\b`},
// Anthropic API Key
{"Anthropic API Key", `\b(sk-ant-(?:admin01|api03)-[\w\-]{80,100})\b`},
// OpenAI API Key
{"OpenAI API Key", `\b(sk-(?:proj-)?[A-Za-z0-9_-]{32,}T3BlbkFJ[A-Za-z0-9_-]+)\b`},
// Google API Key
{"Google API Key", `\b(AIza[A-Za-z0-9_-]{35})\b`},
// Azure Storage/Subscription Key
{"Azure Key", `(?i)(?:azure|subscription)[_\-]?(?:key|secret|password)\s*[:=]\s*['"]?([A-Za-z0-9+/=]{32,88})['"]?`},
// npm Token
{"npm Token", `\b(npm_[a-zA-Z0-9]{36})\b`},
// PyPI Token
{"PyPI Token", `\b(pypi-[A-Za-z0-9_-]{50,})\b`},
// Docker Registry Token (config auth)
{"Docker Registry Token", `(?i)"auth"\s*:\s*"([A-Za-z0-9+/=]{20,})"`},
// Generic API key patterns (commonly assigned to variables named "api_key", "apikey", etc.)
{"Generic API Key", `(?i)(?:api_key|apikey|api-key|access_key|secret_key|auth_token|access_token)\s*[:=]\s*['"]([A-Za-z0-9_\-./+=]{20,64})['"]`},
// Generic Secret assignment (commonly "secret", "password", "passwd" in config)
{"Generic Secret", `(?i)(?:secret|password|passwd|pwd)\s*[:=]\s*['"]([^\s'"]{8,64})['"]`},
}
patterns := make([]secretPattern, 0, len(raw))
for _, r := range raw {
compiled := regexp.MustCompile(r.Pattern)
patterns = append(patterns, secretPattern{
Type: r.Type,
Pattern: compiled,
})
}
return patterns
}
// DetectSecrets scans the input text and returns all detected secret matches.
// Matches are returned sorted by StartPos in ascending order.
func (sd *SecretDetector) DetectSecrets(text string) []SecretMatch {
if text == "" {
return nil
}
var matches []SecretMatch
seen := make(map[string]bool) // Deduplicate overlapping matches for same value
for _, sp := range sd.patterns {
locs := sp.Pattern.FindAllStringSubmatchIndex(text, -1)
for _, loc := range locs {
// Use the first capturing group if available, otherwise the full match
startIdx, endIdx := loc[0], loc[1]
if len(loc) >= 4 && loc[2] >= 0 {
startIdx, endIdx = loc[2], loc[3]
}
value := text[startIdx:endIdx]
// Deduplicate: same value at same position
key := value + ":" + sp.Type
if seen[key] {
continue
}
seen[key] = true
matches = append(matches, SecretMatch{
Type: sp.Type,
Value: value,
Masked: maskSecret(value, sp.Type),
StartPos: startIdx,
EndPos: endIdx,
})
}
}
// Sort by position for stable output and correct redaction ordering
sort.Slice(matches, func(i, j int) bool {
return matches[i].StartPos < matches[j].StartPos
})
return matches
}
// RedactSecrets replaces all detected secrets in the text with [REDACTED:<type>] placeholders.
func (sd *SecretDetector) RedactSecrets(text string) string {
if text == "" {
return ""
}
matches := sd.DetectSecrets(text)
if len(matches) == 0 {
return text
}
// Build the result by replacing matched regions, processing from end to start
// to preserve byte offsets.
result := text
// Process in reverse order so byte positions remain valid
for i := len(matches) - 1; i >= 0; i-- {
m := matches[i]
replacement := "[REDACTED:" + m.Type + "]"
result = result[:m.StartPos] + replacement + result[m.EndPos:]
}
return result
}
// DetectAndRedactWithEntropy performs secret detection using both patterns and entropy analysis.
// It applies entropy-based detection for segments that match a generic high-entropy pattern
// but were not caught by specific detectors.
func (sd *SecretDetector) DetectAndRedactWithEntropy(text string, entropyThreshold float64) string {
// First, apply pattern-based redaction
result := sd.RedactSecrets(text)
// Then scan for high-entropy strings that might be unknown secret formats.
// Look for quoted strings or assignment values that have high entropy.
highEntropyPat := regexp.MustCompile(`(?i)(?:key|token|secret|password|credential|auth)\s*[:=]\s*['"]?([A-Za-z0-9+/=_\-]{16,})['"]?`)
locs := highEntropyPat.FindAllStringSubmatchIndex(result, -1)
// Process in reverse to maintain positions
for i := len(locs) - 1; i >= 0; i-- {
loc := locs[i]
if len(loc) < 4 || loc[2] < 0 {
continue
}
value := result[loc[2]:loc[3]]
// Skip if already redacted
if strings.Contains(value, "[REDACTED:") {
continue
}
if IsHighEntropy(value, entropyThreshold) {
replacement := "[REDACTED:High Entropy]"
result = result[:loc[2]] + replacement + result[loc[3]:]
}
}
return result
}
// maskSecret creates a masked version of the secret, preserving a prefix for identification
// while hiding the sensitive portion.
func maskSecret(value string, secretType string) string {
if len(value) == 0 {
return ""
}
// For private keys, just show the header
if strings.HasPrefix(value, "-----BEGIN") {
lines := strings.SplitN(value, "\n", 2)
if len(lines) > 0 {
return lines[0] + "...[REDACTED]"
}
}
// For tokens with a known prefix, show the prefix
prefixes := []string{
"ghp_", "gho_", "ghu_", "ghs_", "ghr_", "github_pat_",
"glpat-",
"xoxb-", "xoxp-", "xapp-",
"sk_live_", "rk_live_", "pk_live_",
"SG.",
"dop_v1_", "doo_v1_", "dor_v1_",
"sk-ant-", "sk-proj-", "sk-",
"AIza",
"npm_",
"pypi-",
"AKIA", "ASIA",
"AC",
}
for _, prefix := range prefixes {
if strings.HasPrefix(value, prefix) {
showLen := len(prefix) + 4
if showLen > len(value) {
showLen = len(value)
}
return value[:showLen] + "..." + strings.Repeat("*", 8)
}
}
// Generic masking: show first 4 and last 4 characters
if len(value) > 12 {
return value[:4] + "..." + strings.Repeat("*", 8) + "..." + value[len(value)-4:]
}
return strings.Repeat("*", len(value))
}