Skip to content
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

make avatar lookup occur at image request #10540

Merged
merged 17 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Obfuscate the email address in base64
  • Loading branch information
zeripath committed Feb 29, 2020
commit 76090098e03fc0f6cc53827b6439823de7ee0ebd
2 changes: 1 addition & 1 deletion modules/base/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func SizedAvatarLink(email string, size int) string {
// which includes app sub-url as prefix. However, it is possible
// to return full URL if user enables Gravatar-like service.
func AvatarLink(email string) string {
return setting.AppSubURL + "/avatar/email-" + url.PathEscape(email)
return setting.AppSubURL + "/avatar/" + base64.RawURLEncoding.EncodeToString([]byte(email))
}

// FileSize calculates the file size and generate user-friendly string.
Expand Down
5 changes: 3 additions & 2 deletions modules/base/tool_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package base

import (
"encoding/base64"
"net/url"
"testing"

Expand Down Expand Up @@ -92,11 +93,11 @@ func TestSizedAvatarLink(t *testing.T) {

func TestAvatarLink(t *testing.T) {
disableGravatar()
assert.Equal(t, "/avatar/email-"+url.PathEscape("[email protected]"), AvatarLink("[email protected]"))
assert.Equal(t, "/avatar/"+base64.RawURLEncoding.EncodeToString([]byte("[email protected]")), AvatarLink("[email protected]"))

enableGravatar(t)
assert.Equal(t,
"/avatar/email-"+url.PathEscape("[email protected]"),
"/avatar/"+base64.RawURLEncoding.EncodeToString([]byte("[email protected]")),
AvatarLink("[email protected]"),
)
}
Expand Down
4 changes: 2 additions & 2 deletions modules/repository/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package repository

import (
"container/list"
"net/url"
"encoding/base64"
"testing"
"time"

Expand Down Expand Up @@ -115,7 +115,7 @@ func TestPushCommits_AvatarLink(t *testing.T) {
pushCommits.AvatarLink("[email protected]"))

assert.Equal(t,
"/avatar/email-"+url.PathEscape("[email protected]"),
"/avatar/"+base64.RawURLEncoding.EncodeToString([]byte("[email protected]")),
pushCommits.AvatarLink("[email protected]"))
}

Expand Down
2 changes: 1 addition & 1 deletion routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ func RegisterRoutes(m *macaron.Macaron) {
})
// ***** END: User *****

m.Get("/avatar/email-:email", user.AvatarByEmail)
m.Get("/avatar/:email", user.AvatarByEmail)

adminReq := context.Toggle(&context.ToggleOptions{SignInRequired: true, AdminRequired: true})

Expand Down
8 changes: 7 additions & 1 deletion routers/user/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package user

import (
"encoding/base64"
"errors"
"strconv"
"strings"
Expand Down Expand Up @@ -47,7 +48,12 @@ func Avatar(ctx *context.Context) {
// AvatarByEmail redirects the browser to the appropriate Avatar link
func AvatarByEmail(ctx *context.Context) {
zeripath marked this conversation as resolved.
Show resolved Hide resolved
email := ctx.Params(":email")
if email == "" {
addr, err := base64.RawURLEncoding.DecodeString(email)
email = string(addr)
if err != nil {
ctx.ServerError("invalid email address", err)
return
} else if email == "" {
ctx.ServerError("invalid email address", errors.New("email cannot be empty"))
return
}
Expand Down