From adc1ac689efb23c6de2a22b0d6226d02b1641222 Mon Sep 17 00:00:00 2001 From: Andrew Burns Date: Fri, 5 Dec 2014 10:58:49 -0700 Subject: [PATCH 1/2] HashEmail function should also remove spaces According to the [Gravatar API](https://en.gravatar.com/site/implement/hash/) whitespace should also be removed from the email, it was not doing this previously. --- modules/avatar/avatar.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/avatar/avatar.go b/modules/avatar/avatar.go index 144fda387..225d6c818 100644 --- a/modules/avatar/avatar.go +++ b/modules/avatar/avatar.go @@ -48,8 +48,12 @@ func init() { // hash email to md5 string // keep this func in order to make this package indenpent func HashEmail(email string) string { + // https://en.gravatar.com/site/implement/hash/ + email = strings.TrimSpace(email) + email = strings.ToLower(email) + h := md5.New() - h.Write([]byte(strings.ToLower(email))) + h.Write([]byte(email)) return hex.EncodeToString(h.Sum(nil)) } From 35b02997f8599c8e260b70bc5b3239b0109c05cf Mon Sep 17 00:00:00 2001 From: Andrew Burns Date: Fri, 5 Dec 2014 11:02:59 -0700 Subject: [PATCH 2/2] Fix Gravatar images in web view (like commit listing) Related to #700 In the original bug report it was referencing only the sytem avatar images for setup users (like in the header); however the problem also persists with things like commit history. This commit fixes the `tool.AvatarLink` function so that it also uses the already existing `avatar.HashEmail` function. I also refactored the `tool.AvatarLink` method some to make the control flow more apparent and adhere better to DRY (there were multiple calls to the `EncodeMd5` function that the `HashEmail` function call replaced, now there is only one.) --- modules/base/tool.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/base/tool.go b/modules/base/tool.go index 0e083c8d0..14c0e7d08 100644 --- a/modules/base/tool.go +++ b/modules/base/tool.go @@ -23,6 +23,7 @@ import ( "github.com/Unknwon/com" "github.com/Unknwon/i18n" + "github.com/gogits/gogs/modules/avatar" "github.com/gogits/gogs/modules/setting" ) @@ -177,10 +178,13 @@ func CreateTimeLimitCode(data string, minutes int, startInf interface{}) string func AvatarLink(email string) string { if setting.DisableGravatar { return setting.AppSubUrl + "/img/avatar_default.jpg" - } else if setting.Service.EnableCacheAvatar { - return setting.AppSubUrl + "/avatar/" + EncodeMd5(email) } - return setting.GravatarSource + EncodeMd5(email) + + gravatarHash := avatar.HashEmail(email) + if setting.Service.EnableCacheAvatar { + return setting.AppSubUrl + "/avatar/" + gravatarHash + } + return setting.GravatarSource + gravatarHash } // Seconds-based time units