From 27f9bda769401626f78dc3ee1c4ebedab6550ca8 Mon Sep 17 00:00:00 2001 From: zeripath Date: Tue, 13 Apr 2021 01:57:12 +0100 Subject: [PATCH] Prevent NPE on avatar direct rendering if federated avatars disabled (#15434) #13649 assumed that direct avatar urls would always be libravatar urls - this leads to NPEs if federated avatar service is disabled. Fix #15421 Signed-off-by: Andrew Thornton Co-authored-by: techknowlogick --- routers/user/avatar.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/routers/user/avatar.go b/routers/user/avatar.go index c3ece49089..4287589d1a 100644 --- a/routers/user/avatar.go +++ b/routers/user/avatar.go @@ -7,12 +7,14 @@ package user import ( "errors" "net/url" + "path" "strconv" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" ) // Avatar redirect browser to user avatar of requested size @@ -70,8 +72,21 @@ func AvatarByEmailHash(ctx *context.Context) { } var avatarURL *url.URL - avatarURL, err = models.LibravatarURL(email) - if err != nil { + + if setting.EnableFederatedAvatar && setting.LibravatarService != nil { + avatarURL, err = models.LibravatarURL(email) + if err != nil { + avatarURL, err = url.Parse(models.DefaultAvatarLink()) + if err != nil { + ctx.ServerError("invalid default avatar url", err) + return + } + } + } else if !setting.DisableGravatar { + copyOfGravatarSourceURL := *setting.GravatarSourceURL + avatarURL = ©OfGravatarSourceURL + avatarURL.Path = path.Join(avatarURL.Path, hash) + } else { avatarURL, err = url.Parse(models.DefaultAvatarLink()) if err != nil { ctx.ServerError("invalid default avatar url", err)