Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Add authorization checks to user role update endpoint (#366)

Hakan Shehu77a188067d71
diff --git a/apps/server/src/api/client/routes/workspaces/users/user-role-update.ts b/apps/server/src/api/client/routes/workspaces/users/user-role-update.ts
index cf8d975b..97461f87 100644
--- a/apps/server/src/api/client/routes/workspaces/users/user-role-update.ts
+++ b/apps/server/src/api/client/routes/workspaces/users/user-role-update.ts
@@ -55,6 +55,7 @@ export const userRoleUpdateRoute: FastifyPluginCallbackZod = (
.selectFrom('users')
.selectAll()
.where('id', '=', userId)
+ .where('workspace_id', '=', workspace.id)
.executeTakeFirst();
if (!userToUpdate) {
@@ -64,6 +65,37 @@ export const userRoleUpdateRoute: FastifyPluginCallbackZod = (
});
}
+ if (userToUpdate.id === workspace.user.id) {
+ return reply.code(403).send({
+ code: ApiErrorCode.UserUpdateNoAccess,
+ message: 'You cannot modify your own role.',
+ });
+ }
+
+ if (userToUpdate.role === 'owner') {
+ return reply.code(403).send({
+ code: ApiErrorCode.UserUpdateNoAccess,
+ message: 'The workspace owner role cannot be modified.',
+ });
+ }
+
+ if (input.role === 'owner') {
+ return reply.code(403).send({
+ code: ApiErrorCode.UserUpdateNoAccess,
+ message: 'The owner role cannot be assigned through this endpoint.',
+ });
+ }
+
+ if (
+ workspace.user.role !== 'owner' &&
+ (userToUpdate.role === 'admin' || input.role === 'admin')
+ ) {
+ return reply.code(403).send({
+ code: ApiErrorCode.UserUpdateNoAccess,
+ message: 'Only the workspace owner can manage admins.',
+ });
+ }
+
const status =
input.role === 'none' ? UserStatus.Removed : UserStatus.Active;
@@ -77,6 +109,7 @@ export const userRoleUpdateRoute: FastifyPluginCallbackZod = (
updated_by: request.account.id,
})
.where('id', '=', userToUpdate.id)
+ .where('workspace_id', '=', workspace.id)
.executeTakeFirst();
if (!updatedUser) {