Skip to content

Commit 188d36e

Browse files
committed
fix: prevent user assignment to elevated groups
1 parent 42a7218 commit 188d36e

3 files changed

Lines changed: 61 additions & 2 deletions

File tree

server/core/auth.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,49 @@ module.exports = {
317317
return true
318318
},
319319

320+
/**
321+
* Check if user can perform assignment to a group with elevated permissions
322+
*
323+
* @param {User} user
324+
* @param {Array<Number>} groupIds
325+
* @returns {Boolean}
326+
*/
327+
async checkAssignUserToGroupAccess(user, groupIds = []) {
328+
if (!groupIds || groupIds.length < 1) {
329+
return true
330+
}
331+
332+
const userPermissions = user.permissions ? user.permissions : user.getGlobalPermissions()
333+
334+
// System Admin
335+
if (userPermissions.includes('manage:system')) {
336+
return true
337+
}
338+
339+
// Ensure basic user management permission
340+
if (!userPermissions.some(p => ['write:users', 'manage:users', 'write:groups', 'manage:groups'].includes(p))) {
341+
return false
342+
}
343+
344+
const groups = await WIKI.models.groups.query().whereIn('id', groupIds)
345+
return !groups.some(grp => {
346+
// Check for manage:system permission
347+
if (grp.permissions.includes('manage:system') && !userPermissions.includes('manage:groups')) {
348+
return false
349+
}
350+
351+
// Check for elevated permissions
352+
if (grp.permissions.some(p => {
353+
const permType = _.last(p.split(':'))
354+
return ['users', 'groups', 'navigation', 'theme', 'api'].includes(permType)
355+
}) && !(userPermissions.includes('write:groups') || userPermissions.includes('manage:groups'))) {
356+
return false
357+
}
358+
359+
return true
360+
})
361+
},
362+
320363
/**
321364
* Check and apply Page Rule specificity
322365
*

server/graph/resolvers/group.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ module.exports = {
5656
throw new gql.GraphQLError('You are not authorized to assign a user to this elevated group.')
5757
}
5858

59+
// Check assigned permissions for manage:groups
60+
if (
61+
WIKI.auth.checkExclusiveAccess(req.user, ['manage:groups'], ['manage:system']) &&
62+
grp.permissions.some(p => _.last(p.split(':')) === 'system')
63+
) {
64+
throw new gql.GraphQLError('You are not authorized to assign a user to a group with the manage:system permission.')
65+
}
66+
5967
// Check for valid user
6068
const usr = await WIKI.models.users.query().findById(args.userId)
6169
if (!usr) {

server/graph/resolvers/user.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ module.exports = {
6262
}
6363
},
6464
UserMutation: {
65-
async create (obj, args) {
65+
async create (obj, args, context) {
6666
try {
67+
if (!(await WIKI.auth.checkAssignUserToGroupAccess(context.req.user, args.groups))) {
68+
throw new Error('You are not authorized to assign a user to a group with elevated permissions.')
69+
}
70+
6771
await WIKI.models.users.createNewUser(args)
6872

6973
return {
@@ -94,8 +98,12 @@ module.exports = {
9498
}
9599
}
96100
},
97-
async update (obj, args) {
101+
async update (obj, args, context) {
98102
try {
103+
if (!(await WIKI.auth.checkAssignUserToGroupAccess(context.req.user, args.groups))) {
104+
throw new Error('You are not authorized to assign a user to a group with elevated permissions.')
105+
}
106+
99107
await WIKI.models.users.updateUser(args)
100108

101109
return {

0 commit comments

Comments
 (0)