8000 fix(coderd): improve password update logic by defelmnq · Pull Request #15210 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix(coderd): improve password update logic #15210

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 5 commits into from
Oct 24, 2024
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
feat(password): add test for validate auditor use case and change logic
  • Loading branch information
defelmnq committed Oct 24, 2024
commit dc46e3e4da626e69601d28fc86335f74b565c810
19 changes: 4 additions & 15 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,25 +1046,14 @@
return
}

admin, err := api.Database.GetUserByID(ctx, apiKey.UserID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching user.",
Detail: err.Error(),
})
return
}

// only admins or owners can change passwords without sending old_password
if params.OldPassword == "" && (!slice.Contains(admin.RBACRoles, codersdk.RoleUserAdmin) &&
!slice.Contains(admin.RBACRoles, codersdk.RoleOwner)) {
// A user need to put its own password to update it
if apiKey.UserID == user.ID && params.OldPassword == "" {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{

Check failure on line 1051 in coderd/users.go

View workflow job for this annotation

GitHub Actions / lint

ruleguard: Forgot to return early after writing to the http response writer. (gocritic)
Message: "Old password is required for non-admin users.",
Message: "Old password is required.",
})
return
}

err = userpassword.Validate(params.Password)
err := userpassword.Validate(params.Password)
if err != nil {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid password.",
Expand Down
25 changes: 25 additions & 0 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,31 @@ func TestUpdateUserPassword(t *testing.T) {
require.NoError(t, err, "member should login successfully with the new password")
})

t.Run("AuditorCantUpdateOtherUserPassword", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
owner := coderdtest.CreateFirstUser(t, client)

auditor, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID, rbac.RoleAuditor())

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

member, err := client.CreateUserWithOrgs(ctx, codersdk.CreateUserRequestWithOrgs{
Email: "coder@coder.com",
Username: "coder",
Password: "SomeStrongPassword!",
OrganizationIDs: []uuid.UUID{owner.OrganizationID},
})
require.NoError(t, err, "create member")

err = auditor.UpdateUserPassword(ctx, member.ID.String(), codersdk.UpdateUserPasswordRequest{
Password: "SomeNewStrongPassword!",
})
require.Error(t, err, "auditor should not be able to update member password")
require.ErrorContains(t, err, "unexpected status code 404: Resource not found or you do not have access to this resource")
})

t.Run("MemberCanUpdateOwnPassword", func(t *testing.T) {
t.Parallel()
auditor := audit.NewMock()
Expand Down
Loading
0