Skip to content

Commit

Permalink
Fix API leaking Usermail if not logged in (go-gitea#25097)
Browse files Browse the repository at this point in the history
The API should only return the real Mail of a User, if the caller is
logged in. The check do to this don't work. This PR fixes this. This not
really a security issue, but can lead to Spam.

---------

Co-authored-by: silverwind <me@silverwind.io>
  • Loading branch information
2 people authored and wxiaoguang committed Aug 5, 2023
1 parent 0b97463 commit 1d0f9e8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
7 changes: 6 additions & 1 deletion models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,16 @@ func UpdateUserTheme(u *User, themeName string) error {
return UpdateUserCols(db.DefaultContext, u, "theme")
}

// GetPlaceholderEmail returns an noreply email
func (u *User) GetPlaceholderEmail() string {
return fmt.Sprintf("%s@%s", u.LowerName, setting.Service.NoReplyAddress)
}

// GetEmail returns an noreply email, if the user has set to keep his
// email address private, otherwise the primary email address.
func (u *User) GetEmail() string {
if u.KeepEmailPrivate {
return fmt.Sprintf("%s@%s", u.LowerName, setting.Service.NoReplyAddress)
return u.GetPlaceholderEmail()
}
return u.Email
}
Expand Down
2 changes: 1 addition & 1 deletion services/convert/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func toUser(ctx context.Context, user *user_model.User, signed, authed bool) *ap
ID: user.ID,
UserName: user.Name,
FullName: user.FullName,
Email: user.GetEmail(),
Email: user.GetPlaceholderEmail(),
AvatarURL: user.AvatarLink(ctx),
Created: user.CreatedUnix.AsTime(),
Restricted: user.IsRestricted,
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/api_user_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"testing"

auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/tests"

Expand All @@ -21,6 +23,8 @@ func TestAPIUserInfo(t *testing.T) {
user := "user1"
user2 := "user31"

user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user3"})

session := loginUser(t, user)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)

Expand All @@ -36,6 +40,18 @@ func TestAPIUserInfo(t *testing.T) {

req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s", user2))
MakeRequest(t, req, http.StatusNotFound)

// test if the placaholder Mail is returned if a User is not logged in
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s", user3.Name))
resp = MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &u)
assert.Equal(t, user3.GetPlaceholderEmail(), u.Email)

// Test if the correct Mail is returned if a User is logged in
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s?token=%s", user3.Name, token))
resp = MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &u)
assert.Equal(t, user3.GetEmail(), u.Email)
})

t.Run("GetAuthenticatedUser", func(t *testing.T) {
Expand Down
8 changes: 1 addition & 7 deletions tests/integration/api_user_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
package integration

import (
"fmt"
"net/http"
"testing"

auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/tests"

Expand Down Expand Up @@ -54,11 +52,7 @@ func TestAPIUserSearchNotLoggedIn(t *testing.T) {
for _, user := range results.Data {
assert.Contains(t, user.UserName, query)
modelUser = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: user.ID})
if modelUser.KeepEmailPrivate {
assert.EqualValues(t, fmt.Sprintf("%s@%s", modelUser.LowerName, setting.Service.NoReplyAddress), user.Email)
} else {
assert.EqualValues(t, modelUser.Email, user.Email)
}
assert.EqualValues(t, modelUser.GetPlaceholderEmail(), user.Email)
}
}

Expand Down

0 comments on commit 1d0f9e8

Please sign in to comment.