Skip to content

Commit b3090b5

Browse files
committed
avoid leak email address
1 parent 5644010 commit b3090b5

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

models/user/search.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,19 @@ func (opts *SearchUserOptions) toSearchQueryBase(ctx context.Context) *xorm.Sess
6565
builder.Like{"LOWER(full_name)", lowerKeyword},
6666
)
6767
if opts.SearchByEmail {
68-
keywordCond = keywordCond.Or(builder.Like{"LOWER(email)", lowerKeyword})
68+
var emailCond builder.Cond
69+
emailCond = builder.Like{"LOWER(email)", lowerKeyword}
70+
if opts.Actor == nil {
71+
emailCond = emailCond.And(builder.Eq{"keep_email_private": false})
72+
} else if !opts.Actor.IsAdmin {
73+
emailCond = emailCond.And(
74+
builder.Or(
75+
builder.Eq{"keep_email_private": false},
76+
builder.Eq{"id": opts.Actor.ID},
77+
),
78+
)
79+
}
80+
keywordCond = keywordCond.Or(emailCond)
6981
}
7082

7183
cond = cond.And(keywordCond)

tests/integration/api_user_search_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ func TestAPIUserSearchNotLoggedInUserHidden(t *testing.T) {
112112

113113
func TestAPIUserSearchByEmail(t *testing.T) {
114114
defer tests.PrepareTestEnv(t)()
115+
116+
// admin can search user with private email
115117
adminUsername := "user1"
116118
session := loginUser(t, adminUsername)
117119
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
@@ -124,4 +126,22 @@ func TestAPIUserSearchByEmail(t *testing.T) {
124126
DecodeJSON(t, resp, &results)
125127
assert.Equal(t, 1, len(results.Data))
126128
assert.Equal(t, query, results.Data[0].Email)
129+
130+
// no login user can not search user with private email
131+
req = NewRequestf(t, "GET", "/api/v1/users/search?q=%s", query)
132+
resp = MakeRequest(t, req, http.StatusOK)
133+
DecodeJSON(t, resp, &results)
134+
assert.Empty(t, results.Data)
135+
136+
// user can search self with private email
137+
user2 := "user2"
138+
session = loginUser(t, user2)
139+
token = getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
140+
req = NewRequestf(t, "GET", "/api/v1/users/search?q=%s", query).
141+
AddTokenAuth(token)
142+
resp = MakeRequest(t, req, http.StatusOK)
143+
144+
DecodeJSON(t, resp, &results)
145+
assert.Equal(t, 1, len(results.Data))
146+
assert.Equal(t, query, results.Data[0].Email)
127147
}

0 commit comments

Comments
 (0)