-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
make avatar lookup occur at image request #10540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c185a23
make avatar lookup occur at image request
zeripath bdcf287
Protect against evil email address ".."
zeripath 9914e80
Fix tests
zeripath 933320f
fix tests again
zeripath 7609009
Obfuscate the email address in base64
zeripath 58593d4
hash the complete email address
zeripath b4d479f
Merge branch 'master' into make-avatar-lookup-image
zeripath 57339c0
Move to use a (potentially cached) db entry for emails
zeripath c6cca50
As per @lafriks and @guillep2k
zeripath 08cf870
fix unit-tests
zeripath 1e6efeb
Update models/avatar.go
zeripath 2945e0b
Merge branch 'master' into make-avatar-lookup-image
zeripath d32222b
Merge branch 'master' into make-avatar-lookup-image
zeripath bb85931
Merge branch 'master' into make-avatar-lookup-image
zeripath c8fab76
Merge branch 'master' into make-avatar-lookup-image
zeripath 4c09604
Merge branch 'master' into make-avatar-lookup-image
zeripath b5365ba
Add migration to create the EmailHash table
zeripath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hash UNIQUE NOT NULL"` | ||
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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/example.com/"+fmt.Sprintf("%x", md5.Sum([]byte("[email protected]"))), | ||
pushCommits.AvatarLink("[email protected]")) | ||
} | ||
|
||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.