Skip to content

Commit 42b8bad

Browse files
authored
chore: redact the username when scrubbing logs (#327)
1 parent d3372b4 commit 42b8bad

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

pkg/logging/scrubbingLogWriter.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package logging
1919
import (
2020
"fmt"
2121
"io"
22+
"os/user"
2223
"regexp"
2324
"strings"
2425
"sync"
@@ -188,6 +189,13 @@ func addMandatoryMasking(dict ScrubbingDict) ScrubbingDict {
188189
groupToRedact: 2,
189190
regex: regexp.MustCompile(s),
190191
}
192+
193+
u, err := user.Current()
194+
if err == nil {
195+
s = fmt.Sprintf(`\b%s\b`, regexp.QuoteMeta(u.Username))
196+
addTermToDict(s, 0, dict)
197+
}
198+
191199
return dict
192200
}
193201

pkg/logging/scrubbingLogWriter_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package logging
1919
import (
2020
"bytes"
2121
"fmt"
22+
"os/user"
2223
"regexp"
2324
"testing"
2425

@@ -194,6 +195,8 @@ func TestScrubFunction(t *testing.T) {
194195

195196
func TestAddDefaults(t *testing.T) {
196197
dict := addMandatoryMasking(ScrubbingDict{})
198+
u, uErr := user.Current()
199+
assert.NoError(t, uErr)
197200

198201
tests := []struct {
199202
name string
@@ -255,6 +258,11 @@ func TestAddDefaults(t *testing.T) {
255258
input: "SNYK_TOKEN=01234567-0123-0123-0123-012345678901",
256259
expected: "SNYK_TOKEN=***",
257260
},
261+
{
262+
name: "username",
263+
input: fmt.Sprintf("User %s.%s is repeatedly mentioned, but not partially.", u.Username, u.Username),
264+
expected: fmt.Sprintf("User %s.%s is repeatedly mentioned, but not partially.", redactMask, redactMask),
265+
},
258266
}
259267
for _, test := range tests {
260268
t.Run(test.name, func(t *testing.T) {

0 commit comments

Comments
 (0)