Skip to content

fix: disallow deleting self #6306

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 3 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions coderd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ func (api *API) deleteUser(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
auditor := *api.Auditor.Load()
user := httpmw.UserParam(r)
auth := httpmw.UserAuthorization(r)
aReq, commitAudit := audit.InitRequest[database.User](rw, &audit.RequestParams{
Audit: auditor,
Log: api.Logger,
Expand All @@ -401,6 +402,13 @@ func (api *API) deleteUser(rw http.ResponseWriter, r *http.Request) {
return
}

if auth.Actor.ID == user.ID.String() {
httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{
Message: "You cannot delete yourself!",
})
return
}

workspaces, err := api.Database.GetWorkspaces(ctx, database.GetWorkspacesParams{
OwnerID: user.ID,
})
Expand Down
10 changes: 10 additions & 0 deletions coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,16 @@ func TestDeleteUser(t *testing.T) {
require.ErrorAs(t, err, &apiErr)
require.Equal(t, http.StatusExpectationFailed, apiErr.StatusCode())
})
t.Run("Self", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
err := client.DeleteUser(context.Background(), user.UserID)
var apiErr *codersdk.Error
require.Error(t, err, "should not be able to delete self")
require.ErrorAs(t, err, &apiErr, "should be a coderd error")
require.Equal(t, http.StatusForbidden, apiErr.StatusCode(), "should be forbidden")
})
}

func TestPostLogout(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions site/src/components/TableRowMenu/TableRowMenu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export const Example = Template.bind({})
Example.args = {
data: { id: "123" },
menuItems: [
{ label: "Suspend", onClick: (data) => alert(data.id) },
{ label: "Update", onClick: (data) => alert(data.id) },
{ label: "Delete", onClick: (data) => alert(data.id) },
{ label: "Suspend", onClick: (data) => alert(data.id), disabled: false },
{ label: "Update", onClick: (data) => alert(data.id), disabled: false },
{ label: "Delete", onClick: (data) => alert(data.id), disabled: false },
{ label: "Explode", onClick: (data) => alert(data.id), disabled: true },
],
}
2 changes: 2 additions & 0 deletions site/src/components/TableRowMenu/TableRowMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface TableRowMenuProps<TData> {
data: TData
menuItems: Array<{
label: string
disabled: boolean
onClick: (data: TData) => void
}>
}
Expand Down Expand Up @@ -47,6 +48,7 @@ export const TableRowMenu = <T,>({
{menuItems.map((item) => (
<MenuItem
key={item.label}
disabled={item.disabled}
onClick={() => {
handleClose()
item.onClick(data)
Expand Down
1 change: 1 addition & 0 deletions site/src/components/UsersTable/UsersTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe("AuditPage", () => {
onResetUserPassword={() => jest.fn()}
onUpdateUserRoles={() => jest.fn()}
isNonInitialPage={false}
actorID="12345678-1234-1234-1234-123456789012"
/>,
)

Expand Down
3 changes: 3 additions & 0 deletions site/src/components/UsersTable/UsersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface UsersTableProps {
roles: TypesGen.Role["name"][],
) => void
isNonInitialPage: boolean
actorID: string
}

export const UsersTable: FC<React.PropsWithChildren<UsersTableProps>> = ({
Expand All @@ -48,6 +49,7 @@ export const UsersTable: FC<React.PropsWithChildren<UsersTableProps>> = ({
canEditUsers,
isLoading,
isNonInitialPage,
actorID,
}) => {
return (
<TableContainer>
Expand Down Expand Up @@ -82,6 +84,7 @@ export const UsersTable: FC<React.PropsWithChildren<UsersTableProps>> = ({
onSuspendUser={onSuspendUser}
onUpdateUserRoles={onUpdateUserRoles}
isNonInitialPage={isNonInitialPage}
actorID={actorID}
/>
</TableBody>
</Table>
Expand Down
7 changes: 7 additions & 0 deletions site/src/components/UsersTable/UsersTableBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface UsersTableBodyProps {
roles: TypesGen.Role["name"][],
) => void
isNonInitialPage: boolean
actorID: string
}

export const UsersTableBody: FC<
Expand All @@ -61,6 +62,7 @@ export const UsersTableBody: FC<
canEditUsers,
isLoading,
isNonInitialPage,
actorID,
}) => {
const styles = useStyles()
const { t } = useTranslation("usersPage")
Expand Down Expand Up @@ -165,26 +167,31 @@ export const UsersTableBody: FC<
{
label: t("suspendMenuItem"),
onClick: onSuspendUser,
disabled: false,
},
]
: [
{
label: t("activateMenuItem"),
onClick: onActivateUser,
disabled: false,
},
]
).concat(
{
label: t("deleteMenuItem"),
onClick: onDeleteUser,
disabled: user.id === actorID,
},
{
label: t("listWorkspacesMenuItem"),
onClick: onListWorkspaces,
disabled: false,
},
{
label: t("resetPasswordMenuItem"),
onClick: onResetUserPassword,
disabled: false,
},
)
}
Expand Down
1 change: 1 addition & 0 deletions site/src/pages/GroupsPage/GroupPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export const GroupPage: React.FC = () => {
userId: member.id,
})
},
disabled: false,
},
]}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export const TemplatePermissionsPageView: FC<
{
label: "Remove",
onClick: () => onRemoveGroup(group),
disabled: false,
},
]}
/>
Expand Down Expand Up @@ -328,6 +329,7 @@ export const TemplatePermissionsPageView: FC<
{
label: "Remove",
onClick: () => onRemoveUser(user),
disabled: false,
},
]}
/>
Expand Down
4 changes: 4 additions & 0 deletions site/src/pages/UsersPage/UsersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getPaginationContext,
nonInitialPage,
} from "components/PaginationWidget/utils"
import { useMe } from "hooks/useMe"
import { usePermissions } from "hooks/usePermissions"
import { FC, ReactNode } from "react"
import { Helmet } from "react-helmet-async"
Expand Down Expand Up @@ -70,6 +71,8 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
usersState.matches("gettingUsers") ||
(canEditUsers && rolesState.matches("gettingRoles"))

const me = useMe()

return (
<>
<Helmet>
Expand Down Expand Up @@ -126,6 +129,7 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
}}
paginationRef={paginationRef}
isNonInitialPage={nonInitialPage(searchParams)}
actorID={me.id}
/>

<DeleteDialog
Expand Down
3 changes: 3 additions & 0 deletions site/src/pages/UsersPage/UsersPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface UsersPageViewProps {
onFilter: (query: string) => void
paginationRef: PaginationMachineRef
isNonInitialPage: boolean
actorID: string
}

export const UsersPageView: FC<React.PropsWithChildren<UsersPageViewProps>> = ({
Expand All @@ -51,6 +52,7 @@ export const UsersPageView: FC<React.PropsWithChildren<UsersPageViewProps>> = ({
onFilter,
paginationRef,
isNonInitialPage,
actorID,
}) => {
const presetFilters = [
{ query: userFilterQuery.active, name: Language.activeUsersFilterName },
Expand Down Expand Up @@ -79,6 +81,7 @@ export const UsersPageView: FC<React.PropsWithChildren<UsersPageViewProps>> = ({
canEditUsers={canEditUsers}
isLoading={isLoading}
isNonInitialPage={isNonInitialPage}
actorID={actorID}
/>

<PaginationWidget numRecords={count} paginationRef={paginationRef} />
Expand Down