forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make avatar lookup occur at image request (go-gitea#10540)
speed up page generation by making avatar lookup occur at the browser not at page generation * Protect against evil email address ".." * hash the complete email address Signed-off-by: Andrew Thornton <[email protected]> Co-Authored-By: Lauris BH <[email protected]>
- Loading branch information
Showing
13 changed files
with
154 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2020 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package models | ||
|
||
import ( | ||
"crypto/md5" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/cache" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
// EmailHash represents a pre-generated hash map | ||
type EmailHash struct { | ||
Hash string `xorm:"pk varchar(32)"` | ||
Email string `xorm:"UNIQUE NOT NULL"` | ||
} | ||
|
||
// GetEmailForHash converts a provided md5sum to the email | ||
func GetEmailForHash(md5Sum string) (string, error) { | ||
return cache.GetString("Avatar:"+md5Sum, func() (string, error) { | ||
emailHash := EmailHash{ | ||
Hash: strings.ToLower(strings.TrimSpace(md5Sum)), | ||
} | ||
|
||
_, err := x.Get(&emailHash) | ||
return emailHash.Email, err | ||
}) | ||
} | ||
|
||
// AvatarLink returns an avatar link for a provided email | ||
func AvatarLink(email string) string { | ||
lowerEmail := strings.ToLower(strings.TrimSpace(email)) | ||
sum := fmt.Sprintf("%x", md5.Sum([]byte(lowerEmail))) | ||
_, _ = cache.GetString("Avatar:"+sum, func() (string, error) { | ||
emailHash := &EmailHash{ | ||
Email: lowerEmail, | ||
Hash: sum, | ||
} | ||
_, _ = x.Insert(emailHash) | ||
return lowerEmail, nil | ||
}) | ||
return setting.AppSubURL + "/avatar/" + url.PathEscape(sum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2020 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package migrations | ||
|
||
import "xorm.io/xorm" | ||
|
||
func addEmailHashTable(x *xorm.Engine) error { | ||
// EmailHash represents a pre-generated hash map | ||
type EmailHash struct { | ||
Hash string `xorm:"pk varchar(32)"` | ||
Email string `xorm:"UNIQUE NOT NULL"` | ||
} | ||
return x.Sync2(new(EmailHash)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,17 +90,6 @@ func TestSizedAvatarLink(t *testing.T) { | |
) | ||
} | ||
|
||
func TestAvatarLink(t *testing.T) { | ||
disableGravatar() | ||
assert.Equal(t, "/img/avatar_default.png", AvatarLink("[email protected]")) | ||
|
||
enableGravatar(t) | ||
assert.Equal(t, | ||
"https://secure.gravatar.com/avatar/353cbad9b58e69c96154ad99f92bedc7?d=identicon", | ||
AvatarLink("[email protected]"), | ||
) | ||
} | ||
|
||
func TestFileSize(t *testing.T) { | ||
var size int64 = 512 | ||
assert.Equal(t, "512 B", FileSize(size)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ package repository | |
|
||
import ( | ||
"container/list" | ||
"crypto/md5" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -114,7 +116,7 @@ func TestPushCommits_AvatarLink(t *testing.T) { | |
pushCommits.AvatarLink("[email protected]")) | ||
|
||
assert.Equal(t, | ||
"https://secure.gravatar.com/avatar/19ade630b94e1e0535b3df7387434154?d=identicon", | ||
"/avatar/"+fmt.Sprintf("%x", md5.Sum([]byte("nonexistent@example.com"))), | ||
pushCommits.AvatarLink("[email protected]")) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters