Repository navigation
Expand file tree
/
Copy pathdetect.go
More file actions
463 lines (431 loc) · 18.1 KB
/
Copy pathdetect.go
File metadata and controls
463 lines (431 loc) · 18.1 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package redact
import (
"fmt"
"regexp"
"sort"
)
// Match represents a single detected sensitive pattern in the input string.
//
// MatchedText is a zero-copy substring slice of the original input string
// (input[Offset:Offset+Length]). It shares the backing array of the input —
// do not hold a Match longer than the input string is alive.
//
// Match is in-memory only — it is never serialised to disk or the network.
type Match struct {
// Rule is the rule ID that produced this match (e.g., "github_token", "email").
Rule string
// Offset is the byte offset of the match start in the input string.
Offset int
// Length is the byte length of the match.
Length int
// Category is the rule category that fired (e.g., CategorySecrets, CategoryPII).
Category Category
// MatchedText is input[Offset:Offset+Length] — zero-copy substring slice.
MatchedText string
}
// Detect scans input for all sensitive patterns active at the receiver's redaction level.
// It returns a slice of Match values sorted by Offset (ascending); matches that
// start at one offset are ordered longest first, then by rule order.
//
// A match of a rule that reads the name of a key or the construct around a
// value (key_name_secret, url_query_secret, cookie_header, curl_user_password)
// spans the value alone, not the name or the option. Where such a match overlaps a match of
// another rule, it is dropped or shortened so that the other match is applied.
//
// Detect does NOT modify the input string.
// The returned MatchedText fields share the backing array of input — callers that
// need to hold matches past the input's lifetime must copy MatchedText themselves.
//
// If no patterns match, Detect returns nil (not an empty slice).
//
// input must be valid UTF-8; see the package input contract.
func (r *DefaultRedactor) Detect(input string) []Match {
return r.detect(input, nil)
}
func (r *DefaultRedactor) detect(input string, call *observationCall) []Match {
if input == "" {
return nil
}
matches := r.scan(input, call)
// Store as the last-detect result (last-call-only, not accumulated).
r.mu.Lock()
r.lastMatches = matches
r.mu.Unlock()
return matches
}
// scan returns the matches of every rule active at the configured level. It
// does not record them as the last Detect result.
func (r *DefaultRedactor) scan(input string, call *observationCall) []Match {
if input == "" {
return nil
}
activeRules := make([]Rule, 0, len(Rules)+len(r.userRules))
builtins := 0
for index, rule := range Rules {
if r.isActiveRule(rule) {
activeRules = append(activeRules, rule)
builtins++
if call != nil {
call.record.Rules = append(call.record.Rules, RuleDetection{Rule: RuleIdentity{ID: rule.ID, Origin: RuleOriginBuiltin, Index: index}, Category: rule.Category})
}
}
}
for index, rule := range r.userRules {
if r.isActiveRule(rule) {
activeRules = append(activeRules, rule)
if call != nil {
origin, sourceIndex := RuleOriginConfigured, index
if index >= r.configuredRuleCount {
origin, sourceIndex = RuleOriginRuntimeXDG, index-r.configuredRuleCount
}
call.record.Rules = append(call.record.Rules, RuleDetection{Rule: RuleIdentity{ID: rule.ID, Origin: origin, Index: sourceIndex}, Category: rule.Category})
}
}
}
var accepted func(int)
if call != nil {
accepted = func(index int) { call.record.Rules[index].Count++; call.record.RegexDetected.Value++ }
}
return detectWithRulesInto(input, activeRules, builtins, r.patterns, accepted)
}
// valueSpan is a part of the input that a rule replaces: input[start:end].
// construct is where the construct the value was found in starts: the start of
// the name in "name=value".
type valueSpan struct{ start, end, construct int }
// valueFinder returns the parts of the input that a rule replaces for one match
// of its pattern, in ascending order and without overlap. matched is the
// pattern match and offset is its position in input, as for Rule.FilterFn. No
// span means the match holds nothing to redact.
//
// A span usually lies inside the match. It may end after it, when the pattern
// stops at a character that the value continues through.
//
// A finder reads the match with the standard library's regexp package,
// whichever engine found the match.
type valueFinder func(matched, input string, offset int) []valueSpan
// valueRule describes a rule that replaces only a part of what its pattern
// matches: the value after a key name, not the name. Detection reports one
// match for each span that find returns, in place of the pattern match, and
// Redact replaces each with placeholder, which is also the rule's
// Replacement.
//
// recovery, when set, replaces a whole pattern match and keeps the name and
// the quotes around the value. It serves the recovery path, which applies a
// pattern without its filter or finder (see applyRule). It stays here and
// not in Rule.Replacement, so a reader of the rule table sees the placeholder
// that is written.
type valueRule struct {
// id is the ID of the rule the entry belongs to. Constructing a redactor
// checks that the rule with that ID still has this pattern (see
// validateValueRules).
id string
find valueFinder
placeholder string
recovery string
// knowsFormat is set on a rule that recognises the text itself and uses
// find only to say where in the pattern match it starts. Its matches are
// left as they are when overlaps are settled, like those of any rule
// without a finder. The other value rules know only where a value sits,
// and resolveValueOverlaps makes them give way.
knowsFormat bool
}
// valueRules holds the value rules by their pattern. The entry lives beside
// the rule table and not in Rule, whose field layout is public.
var valueRules = map[*regexp.Regexp]valueRule{
keyNameSecretPattern: {id: keyNameSecretRuleID, find: keyNameSecretValues, placeholder: keyNameSecretPlaceholder, recovery: keyNameSecretReplacement},
urlQuerySecretPattern: {id: urlQuerySecretRuleID, find: urlQuerySecretValues, placeholder: keyNameSecretPlaceholder, recovery: "${before}" + keyNameSecretPlaceholder},
cookieHeaderPattern: {id: cookieHeaderRuleID, find: cookieHeaderValues, placeholder: cookieHeaderPlaceholder, recovery: "${before}" + cookieHeaderPlaceholder},
curlUserPasswordPattern: {id: curlUserPasswordRuleID, find: curlUserPasswordValues, placeholder: curlUserPasswordPlaceholder, recovery: curlUserPasswordReplacement},
// The rules built by prefixTokenRule add themselves.
}
// validateValueRules reports a rule whose ID belongs to a value rule but whose
// pattern is not the one that value rule is keyed by. Detection finds the
// value rule by the pattern, so such a rule would match and replace its whole
// construct, name included, without a word of warning.
func validateValueRules(rules []Rule) error {
for _, rule := range rules {
for _, value := range valueRules {
if value.id != rule.ID || valueRules[rule.Pattern].id == rule.ID {
continue
}
return &actionableError{
what: fmt.Sprintf("built-in redaction rule %q has a pattern its value finder does not know", rule.ID),
why: "the rule replaces only the value it finds inside its pattern match, and detection finds the value by the pattern",
where: "redact.validateValueRules",
when: "NewRedactor validated built-in rule metadata before scanning content",
means: "the rule would replace its whole pattern match, so no redactor was created",
fix: "keep the rule's compiled pattern, or register the new pattern with its value finder",
}
}
}
return nil
}
// hasValue adapts a valueFinder to Rule.FilterFn, so a caller that applies a
// rule by hand makes the same decision as detection. Every value rule carries
// this filter.
func hasValue(find valueFinder) func(matched, input string, offset int) bool {
return func(matched, input string, offset int) bool { return len(find(matched, input, offset)) > 0 }
}
// detectWithRules is the pure canonical regex detector. Reporting and
// last-call state remain the responsibility of DefaultRedactor.Detect. Every
// rule in rules is treated as built in.
func detectWithRules(input string, rules []Rule, patterns compiledPatterns) []Match {
return detectWithRulesInto(input, rules, len(rules), patterns, nil)
}
// detectWithRulesInto detects with rules, of which the first builtins are
// rules of this package and the rest are configured. Settling a value match
// may drop a match of a built-in rule, never one of a configured rule.
func detectWithRulesInto(input string, rules []Rule, builtins int, patterns compiledPatterns, accepted func(ruleIndex int)) []Match {
var found detectedMatches
valueMatches := false
for index, rule := range rules {
pattern := patterns.lookup(rule.Pattern)
if pattern == nil {
continue
}
values := valueRules[rule.Pattern]
for _, loc := range pattern.FindAllStringIndex(input, -1) {
matched := input[loc[0]:loc[1]]
if values.find != nil {
for _, span := range values.find(matched, input, loc[0]) {
found.add(Match{Rule: rule.ID, Offset: span.start, Length: span.end - span.start, Category: rule.Category, MatchedText: input[span.start:span.end]}, matchSource{rule: index, value: !values.knowsFormat, construct: span.construct, builtin: index < builtins})
valueMatches = valueMatches || !values.knowsFormat
}
continue
}
if rule.FilterFn == nil || rule.FilterFn(matched, input, loc[0]) {
found.add(Match{Rule: rule.ID, Offset: loc[0], Length: loc[1] - loc[0], Category: rule.Category, MatchedText: matched}, matchSource{rule: index, builtin: index < builtins})
}
}
}
if len(found.matches) == 0 {
return nil
}
// Order by offset. Two matches that start at the same offset are ordered
// longest first, so Redact, which keeps the first of two overlapping
// matches, never prefers a rule that covers less of the same text. Two
// matches of one span keep the order of the rule table.
sort.Stable(&found)
if valueMatches {
found.resolveValueOverlaps(input)
}
if accepted != nil {
for _, source := range found.sources {
accepted(source.rule)
}
}
if len(found.matches) == 0 {
return nil
}
return found.matches
}
// detectedMatches holds the accepted matches of one scan and where each came
// from.
type detectedMatches struct {
matches []Match
sources []matchSource
}
// matchSource is the index of a match's rule in the scanned rule list. For a
// value that resolveValueOverlaps settles (see valueRule.knowsFormat) it also
// holds where the construct that the value was found in starts: the start of
// the name in "name=value", which for the second setting of
// "password=abc12345,token=def67890" is the start of "token".
//
// builtin is set on a match of a rule of this package. Only such a match may
// be dropped in favour of a value match; a configured rule's match is kept
// and the value match gives way to it.
type matchSource struct {
rule int
value bool
construct int
builtin bool
}
func (d *detectedMatches) add(match Match, source matchSource) {
d.matches = append(d.matches, match)
d.sources = append(d.sources, source)
}
func (d *detectedMatches) Len() int { return len(d.matches) }
func (d *detectedMatches) Less(i, j int) bool {
if d.matches[i].Offset != d.matches[j].Offset {
return d.matches[i].Offset < d.matches[j].Offset
}
return d.matches[i].Length > d.matches[j].Length
}
func (d *detectedMatches) Swap(i, j int) {
d.matches[i], d.matches[j] = d.matches[j], d.matches[i]
d.sources[i], d.sources[j] = d.sources[j], d.sources[i]
}
// resolveValueOverlaps settles every overlap between a match of a value rule
// and the matches around it. A value rule knows only where a value sits, so a
// rule that knows what the text is goes first, and the value match gives way:
// it is dropped or shortened, so that it is never the reason another rule's
// match is not applied. The claim is about value matches only. Two matches of
// other rules that overlap each other are left to Redact, which keeps the
// first, as without the value rules.
//
// The matches are sorted, and Redact applies them in that order and skips a
// match that starts inside one it already applied. This pass walks them the
// same way and settles four cases:
//
// - A match of another rule covers the whole value. The value match is
// dropped: GITHUB_TOKEN=<GITHUB_PAT>, not GITHUB_TOKEN=<SECRET>.
// - A match of a built-in rule starts inside the construct the value was
// found in and stops part of the way through the value: generic_api_key
// reads the name in api_key="abc...+/x9" too and stops at the "+". That
// match is dropped and the value match replaces the whole value. What the
// dropped match covered outside the value is the name and the separator.
// A configured rule's match is never dropped; the next case applies.
// - Any other applied match covers the start of the value. The value match
// keeps the rest, which Redact would otherwise skip and leave in the
// output.
// - A later match starts inside the value and ends more than one character
// after it. The value match ends where that match starts, so both are
// applied: the value of "password=abc12345,card=4111 1111 1111 1111" and
// the card number.
//
// A match that lies inside the value is left to Redact, which skips it. So is
// one that ends a single character after the value: a pattern that takes the
// character after its token, as aws_secret_key does, covers no more of the
// input than the value.
//
// Apart from the second case no match of another rule is changed. So every
// part of the input that the other rules would replace without the value
// rules is still replaced, except the name and the separator that a built-in
// match dropped in the second case covered.
func (d *detectedMatches) resolveValueOverlaps(input string) {
end := func(i int) int { return d.matches[i].Offset + d.matches[i].Length }
dropped := make([]bool, len(d.matches))
for i, source := range d.sources {
if !source.value {
continue
}
for j := i - 1; j >= 0 && d.matches[j].Offset >= source.construct; j-- {
if !d.sources[j].value && d.sources[j].builtin && end(j) > d.matches[i].Offset && end(j) < end(i) {
dropped[j] = true
}
}
}
kept := 0
applied := 0 // end of the last match Redact applies
moved := false
for i, match := range d.matches {
if dropped[i] {
continue
}
start, stop := match.Offset, end(i)
if !d.sources[i].value {
if start >= applied {
applied = stop
}
} else {
start = max(start, applied)
for j := i + 1; j < len(d.matches) && d.matches[j].Offset < stop; j++ {
if dropped[j] || d.matches[j].Offset < start {
continue
}
if covers := d.matches[j].Offset == start && end(j) == stop && !d.sources[j].value; covers || end(j) > stop+1 {
stop = d.matches[j].Offset
break
}
}
if start >= stop {
continue
}
moved = moved || start != match.Offset
match.Offset, match.Length, match.MatchedText = start, stop-start, input[start:stop]
applied = stop
}
d.matches[kept], d.sources[kept] = match, d.sources[i]
kept++
}
d.matches, d.sources = d.matches[:kept], d.sources[:kept]
if moved {
// A value match that lost its start now begins after matches that lie
// inside the match before it. Restore the order Detect promises.
sort.Stable(d)
}
}
// Redact replaces the byte spans identified by matches in input with their rule
// replacements, then returns the resulting string. It does NOT re-scan — only
// the provided match spans are replaced.
//
// If matches is nil or empty, Redact returns input unchanged.
// Overlapping matches are skipped: if a span is already covered by a previously
// applied match, the overlapping match is silently ignored.
//
// The output string is built in a single pass from left to right.
//
// input must be valid UTF-8; see the package input contract.
func (r *DefaultRedactor) Redact(input string, matches []Match) string {
rules := make([]Rule, 0, len(Rules)+len(r.userRules))
rules = append(rules, Rules...)
rules = append(rules, r.userRules...)
output, _ := redactWithRules(input, matches, rules, r.patterns)
return output
}
// redactWithRules is the pure canonical overlap and replacement engine. The
// returned matches are exactly those whose non-overlapping spans were applied.
func redactWithRules(input string, matches []Match, rules []Rule, patterns compiledPatterns) (string, []Match) {
if len(matches) == 0 {
return input, nil
}
replacements := make(map[string]Rule, len(rules))
for _, rule := range rules {
replacements[rule.ID] = rule
}
var out []byte
cursor := 0
var applied []Match
for _, m := range matches {
// Skip matches that start before cursor (overlapping or out-of-order spans).
if m.Offset < cursor {
continue
}
// Validate span bounds.
end := m.Offset + m.Length
if end > len(input) {
continue
}
// Append non-match text between cursor and this match.
out = append(out, input[cursor:m.Offset]...)
// Append the replacement for this rule, falling back to a generic placeholder
// if the rule ID is not found (e.g., caller supplied synthetic Match values).
rule, ok := replacements[m.Rule]
replacement := rule.Replacement
if !ok {
replacement = "<REDACTED>"
}
appliedReplacement := replacement
if value, isValue := valueRules[rule.Pattern]; ok && isValue {
// The match is a value the rule found inside its pattern match.
appliedReplacement = value.placeholder
} else if replacement != "" && containsBackref(replacement) {
if ok && rule.Pattern != nil {
appliedReplacement = patterns.lookup(rule.Pattern).ReplaceAllString(input[m.Offset:end], replacement)
}
}
out = append(out, appliedReplacement...)
cursor = end
if appliedReplacement != input[m.Offset:end] {
applied = append(applied, m)
}
}
// Append any remaining text after the last match.
if cursor < len(input) {
out = append(out, input[cursor:]...)
}
return string(out), applied
}
// containsBackref reports whether a replacement string contains a back-reference
// such as "${1}", "${2}", "$1", "$2", etc.
func containsBackref(s string) bool {
for i := 0; i < len(s)-1; i++ {
if s[i] == '$' {
next := s[i+1]
if next == '{' || (next >= '0' && next <= '9') {
return true
}
}
}
return false
}