Skip to content

Commit 688430e

Browse files
meln5674wxiaoguang
andauthored
Allow admins to rename non-local users (#35970)
Presently, attempting to rename a non-local (e.g. Oauth2 or LDAP) user results in an error, even if the requester is an administrator. As far as I can tell, this is a security feature, not architectural in nature, as automatic account linking could be used to take control of another user's account. This is not a concern for an administrator, who we should trust to know what they are doing. This patch allows admins, and only admins, to rename non-local users. Fixes #18308 (sort of) --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 87d5a85 commit 688430e

File tree

8 files changed

+32
-25
lines changed

8 files changed

+32
-25
lines changed

routers/api/v1/admin/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ func RenameUser(ctx *context.APIContext) {
480480
newName := web.GetForm(ctx).(*api.RenameUserOption).NewName
481481

482482
// Check if username has been changed
483-
if err := user_service.RenameUser(ctx, ctx.ContextUser, newName); err != nil {
483+
if err := user_service.RenameUser(ctx, ctx.ContextUser, newName, ctx.Doer); err != nil {
484484
if user_model.IsErrUserAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNamePatternNotAllowed(err) || db.IsErrNameCharsNotAllowed(err) {
485485
ctx.APIError(http.StatusUnprocessableEntity, err)
486486
} else {

routers/api/v1/org/org.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func Rename(ctx *context.APIContext) {
340340

341341
form := web.GetForm(ctx).(*api.RenameOrgOption)
342342
orgUser := ctx.Org.Organization.AsUser()
343-
if err := user_service.RenameUser(ctx, orgUser, form.NewName); err != nil {
343+
if err := user_service.RenameUser(ctx, orgUser, form.NewName, ctx.Doer); err != nil {
344344
if user_model.IsErrUserAlreadyExist(err) || db.IsErrNameReserved(err) || db.IsErrNamePatternNotAllowed(err) || db.IsErrNameCharsNotAllowed(err) {
345345
ctx.APIError(http.StatusUnprocessableEntity, err)
346346
} else {

routers/web/admin/users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func EditUserPost(ctx *context.Context) {
345345
}
346346

347347
if form.UserName != "" {
348-
if err := user_service.RenameUser(ctx, u, form.UserName); err != nil {
348+
if err := user_service.RenameUser(ctx, u, form.UserName, ctx.Doer); err != nil {
349349
switch {
350350
case user_model.IsErrUserIsNotLocal(err):
351351
ctx.Data["Err_UserName"] = true

routers/web/org/setting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func SettingsRenamePost(ctx *context.Context) {
213213
return
214214
}
215215

216-
if err := user_service.RenameUser(ctx, ctx.Org.Organization.AsUser(), newOrgName); err != nil {
216+
if err := user_service.RenameUser(ctx, ctx.Org.Organization.AsUser(), newOrgName, ctx.Doer); err != nil {
217217
if user_model.IsErrUserAlreadyExist(err) {
218218
ctx.JSONError(ctx.Tr("org.form.name_been_taken", newOrgName))
219219
} else if db.IsErrNameReserved(err) {

routers/web/user/setting/profile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func ProfilePost(ctx *context.Context) {
7575
ctx.Redirect(setting.AppSubURL + "/user/settings")
7676
return
7777
}
78-
if err := user_service.RenameUser(ctx, ctx.Doer, form.Name); err != nil {
78+
if err := user_service.RenameUser(ctx, ctx.Doer, form.Name, ctx.Doer); err != nil {
7979
switch {
8080
case user_model.IsErrUserIsNotLocal(err):
8181
ctx.Flash.Error(ctx.Tr("form.username_change_not_local_user"))

services/user/user.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,15 @@ import (
3131
)
3232

3333
// RenameUser renames a user
34-
func RenameUser(ctx context.Context, u *user_model.User, newUserName string) error {
34+
func RenameUser(ctx context.Context, u *user_model.User, newUserName string, doer *user_model.User) error {
3535
if newUserName == u.Name {
3636
return nil
3737
}
3838

39-
// Non-local users are not allowed to change their username.
40-
if !u.IsOrganization() && !u.IsLocal() {
41-
return user_model.ErrUserIsNotLocal{
42-
UID: u.ID,
43-
Name: u.Name,
44-
}
39+
// Non-local users are not allowed to change their own username, but admins are
40+
isExternalUser := !u.IsOrganization() && !u.IsLocal()
41+
if isExternalUser && !doer.IsAdmin {
42+
return user_model.ErrUserIsNotLocal{UID: u.ID, Name: u.Name}
4543
}
4644

4745
if err := user_model.IsUsableUsername(newUserName); err != nil {

services/user/user_test.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
org_service "code.gitea.io/gitea/services/org"
2121

2222
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
2324
)
2425

2526
func TestMain(m *testing.M) {
@@ -101,23 +102,31 @@ func TestRenameUser(t *testing.T) {
101102
assert.NoError(t, unittest.PrepareTestDatabase())
102103
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 21})
103104

104-
t.Run("Non-Local", func(t *testing.T) {
105-
u := &user_model.User{
106-
Type: user_model.UserTypeIndividual,
107-
LoginType: auth.OAuth2,
105+
t.Run("External user", func(t *testing.T) {
106+
adminUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1, IsAdmin: true})
107+
externalUser := &user_model.User{
108+
Name: "external_user",
109+
110+
LoginType: auth.LDAP,
108111
}
109-
assert.ErrorIs(t, RenameUser(t.Context(), u, "user_rename"), user_model.ErrUserIsNotLocal{})
112+
require.NoError(t, user_model.CreateUser(t.Context(), externalUser, &user_model.Meta{}))
113+
114+
err := RenameUser(t.Context(), externalUser, externalUser.Name+"_changed", externalUser)
115+
assert.True(t, user_model.IsErrUserIsNotLocal(err), "external user is not allowed to rename themselves")
116+
117+
err = RenameUser(t.Context(), externalUser, externalUser.Name+"_changed", adminUser)
118+
assert.NoError(t, err, "admin can rename external user")
110119
})
111120

112121
t.Run("Same username", func(t *testing.T) {
113-
assert.NoError(t, RenameUser(t.Context(), user, user.Name))
122+
assert.NoError(t, RenameUser(t.Context(), user, user.Name, user))
114123
})
115124

116125
t.Run("Non usable username", func(t *testing.T) {
117126
usernames := []string{"--diff", ".well-known", "gitea-actions", "aaa.atom", "aa.png"}
118127
for _, username := range usernames {
119128
assert.Error(t, user_model.IsUsableUsername(username), "non-usable username: %s", username)
120-
assert.Error(t, RenameUser(t.Context(), user, username), "non-usable username: %s", username)
129+
assert.Error(t, RenameUser(t.Context(), user, username, user), "non-usable username: %s", username)
121130
}
122131
})
123132

@@ -126,7 +135,7 @@ func TestRenameUser(t *testing.T) {
126135
unittest.AssertNotExistsBean(t, &user_model.User{ID: user.ID, Name: caps})
127136
unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: user.ID, OwnerName: user.Name})
128137

129-
assert.NoError(t, RenameUser(t.Context(), user, caps))
138+
assert.NoError(t, RenameUser(t.Context(), user, caps, user))
130139

131140
unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: user.ID, Name: caps})
132141
unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: user.ID, OwnerName: caps})
@@ -135,17 +144,17 @@ func TestRenameUser(t *testing.T) {
135144
t.Run("Already exists", func(t *testing.T) {
136145
existUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
137146

138-
assert.ErrorIs(t, RenameUser(t.Context(), user, existUser.Name), user_model.ErrUserAlreadyExist{Name: existUser.Name})
139-
assert.ErrorIs(t, RenameUser(t.Context(), user, existUser.LowerName), user_model.ErrUserAlreadyExist{Name: existUser.LowerName})
147+
assert.ErrorIs(t, RenameUser(t.Context(), user, existUser.Name, user), user_model.ErrUserAlreadyExist{Name: existUser.Name})
148+
assert.ErrorIs(t, RenameUser(t.Context(), user, existUser.LowerName, user), user_model.ErrUserAlreadyExist{Name: existUser.LowerName})
140149
newUsername := fmt.Sprintf("uSEr%d", existUser.ID)
141-
assert.ErrorIs(t, RenameUser(t.Context(), user, newUsername), user_model.ErrUserAlreadyExist{Name: newUsername})
150+
assert.ErrorIs(t, RenameUser(t.Context(), user, newUsername, user), user_model.ErrUserAlreadyExist{Name: newUsername})
142151
})
143152

144153
t.Run("Normal", func(t *testing.T) {
145154
oldUsername := user.Name
146155
newUsername := "User_Rename"
147156

148-
assert.NoError(t, RenameUser(t.Context(), user, newUsername))
157+
assert.NoError(t, RenameUser(t.Context(), user, newUsername, user))
149158
unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: user.ID, Name: newUsername, LowerName: strings.ToLower(newUsername)})
150159

151160
redirectUID, err := user_model.LookupUserRedirect(t.Context(), oldUsername)

templates/admin/user/edit.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{{.CsrfTokenHtml}}
1010
<div class="field {{if .Err_UserName}}error{{end}}">
1111
<label for="user_name">{{ctx.Locale.Tr "username"}}</label>
12-
<input id="user_name" name="user_name" value="{{.User.Name}}" {{if not .User.IsLocal}}disabled{{end}} maxlength="40">
12+
<input id="user_name" name="user_name" value="{{.User.Name}}" maxlength="40">
1313
</div>
1414
<!-- Types and name -->
1515
<div class="inline required field {{if .Err_LoginType}}error{{end}}">

0 commit comments

Comments
 (0)