-
Notifications
You must be signed in to change notification settings - Fork 902
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
Changes from 1 commit
22ed129
dc46e3e
70455d4
90ee439
94c4311
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1018,6 +1018,7 @@ func (api *API) putUserPassword(rw http.ResponseWriter, r *http.Request) { | |
ctx = r.Context() | ||
user = httpmw.UserParam(r) | ||
params codersdk.UpdateUserPasswordRequest | ||
apiKey = httpmw.APIKey(r) | ||
auditor = *api.Auditor.Load() | ||
aReq, commitAudit = audit.InitRequest[database.User](rw, &audit.RequestParams{ | ||
Audit: auditor, | ||
|
@@ -1045,7 +1046,25 @@ func (api *API) putUserPassword(rw http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
err := userpassword.Validate(params.Password) | ||
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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't necessary, you can check the RBAC roles of the user from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I tested the UserParam returns the user from the URL and not the user authenticated - did I missed something ? What I try to achieve here is to know if the user doing the request is an admin or not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, good point. I think there are still some subtleties here:
I think the reason for this check is to allow user administrators to reset a user's password without having to specify their existing password. A better check might be something along the lines of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @johnstcn as we discussed I changed a bit the logic to verify the user associated to the APIKey to the user we try to update - also validated the behavior for Auditor (it works as expected.) |
||
|
||
// 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)) { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Old password is required for non-admin users.", | ||
}) | ||
return | ||
} | ||
|
||
err = userpassword.Validate(params.Password) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Invalid password.", | ||
|
@@ -1059,7 +1078,6 @@ func (api *API) putUserPassword(rw http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
// admins can change passwords without sending old_password | ||
if params.OldPassword != "" { | ||
// if they send something let's validate it | ||
ok, err := userpassword.Compare(string(user.HashedPassword), params.OldPassword) | ||
|
Uh oh!
There was an error while loading. Please reload this page.