From 779d1185e744c6a73c8e481c91aa9875022d29d4 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Mon, 12 Apr 2021 22:50:07 -0400 Subject: [PATCH] Prevent NPE on avatar direct rendering if federated avatars disabled (#15434) (#15439) #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 Co-authored-by: zeripath --- 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 c3ece4908..4287589d1 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)