fix: handle small passwords in mask function #522 (#523)

This commit is contained in:
Marc Brugger
2025-03-13 19:10:37 +01:00
committed by GitHub
parent 327d67c9ef
commit 32c728ccf8
4 changed files with 29 additions and 2 deletions
+3
View File
@@ -20,6 +20,9 @@ fmt: tb.golines tb.gofumpt
# Run tests
test: generate fmt lint test-ci
fuzz:
go test -fuzz=FuzzMask -v ./pkg/types/ -fuzztime=60s
# Run ci tests
test-ci: mocks tidy tb.ginkgo
$(TB_GINKGO) --cover --coverprofile coverage.out.tmp ./...
+2 -2
View File
@@ -187,8 +187,8 @@ func (i *AdGuardInstance) Init() error {
}
func mask(s string) string {
if s == "" {
return "***"
if len(s) < 3 {
return strings.Repeat("*", len(s))
}
mask := strings.Repeat("*", len(s)-2)
return fmt.Sprintf("%v%s%v", string(s[0]), mask, string(s[len(s)-1]))
+15
View File
@@ -0,0 +1,15 @@
package types
import (
"testing"
)
func FuzzMask(f *testing.F) {
testcases := []string{"", "a", "ab", "abc", "abcd"}
for _, tc := range testcases {
f.Add(tc)
}
f.Fuzz(func(t *testing.T, value string) {
_ = mask(value)
})
}
+9
View File
@@ -83,6 +83,15 @@ var _ = Describe("Types", func() {
Ω(masked.API.Password).Should(Equal("p**s"))
})
})
DescribeTable("mask should work correctly",
func(value, expected string) {
Ω(mask(value)).Should(Equal(expected))
},
Entry(`Empty password`, "", ""),
Entry(`1 char password`, "a", "*"),
Entry(`2 char password`, "ab", "**"),
Entry(`3 char password`, "abc", "a*c"),
)
})
Context("Feature", func() {
Context("LogDisabled", func() {