This commit is contained in:
Pavel Zeman 2026-05-25 10:10:27 +00:00 committed by GitHub
commit 6463f476eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 1 deletions

View file

@ -135,7 +135,14 @@ func createProfileImage(username string, userID string, initialFont string) ([]b
h.Write([]byte(userID))
seed := h.Sum32()
initial := string(strings.ToUpper(username)[0])
// Guard against empty/whitespace-only usernames which can occur when user records
// bypass model validation (e.g. LDAP/SAML sync, direct DB inserts, data migrations).
// See: api4.getProfileImage → app.GetProfileImage → GetDefaultProfileImage → createProfileImage
trimmedUsername := strings.TrimSpace(username)
initial := "?"
if trimmedUsername != "" {
initial = string([]rune(strings.ToUpper(trimmedUsername))[0])
}
font, err := getFont(initialFont)
if err != nil {

View file

@ -24,3 +24,21 @@ func TestCreateProfileImage(t *testing.T) {
require.Equal(t, colorful, img.At(1, 1), "Failed to create correct color")
}
func TestCreateProfileImage_EmptyUsername(t *testing.T) {
t.Run("empty username should not panic", func(t *testing.T) {
require.NotPanics(t, func() {
b, err := createProfileImage("", "eo1zkdr96pdj98pjmq8zy35wba", "nunito-bold.ttf")
require.NoError(t, err)
require.NotEmpty(t, b)
})
})
t.Run("whitespace-only username should not panic", func(t *testing.T) {
require.NotPanics(t, func() {
b, err := createProfileImage(" ", "eo1zkdr96pdj98pjmq8zy35wba", "nunito-bold.ttf")
require.NoError(t, err)
require.NotEmpty(t, b)
})
})
}