-
-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix/project role permission grant (#8084)
## Background In #6380 we fixed a privilege escalation bug that allowed members of a project that had permission to add users to the project with roles that had a higher permission set than themselves. The PR linked essentially constricts you only be able to assign users to roles that you possess yourself if you are not an Admin or Project owner. This fix broke expectations for another customer who needed to have a project owner without the DELETE_PROJECT permission. The fix above made it so that their custom project owner role only was able to assign users to the project with the role that they posessed. ## Fix Instead of looking directly at which role the role granter has, this PR addresses the issue by making the assessment based on the permission sets of the user and the roles to be granted. If the granter has all the permissions of the role being granted, the granter is permitted to assign the role. ## Other considerations The endpoint to get roles was changed in this PR. It previously only retrieved the roles that the user had in the project. This no-longer makes sense because the user should be able to see other project roles than the one they themselves hold when assigning users to the project. The drawback of returning all project roles is that there may be a project role in the list that the user does not have access to assign, because they do not hold all the permissions required of the role. This was discussed internally and we decided that it's an acceptable trade-off for now because the complexities of returning a role list based on comparing permissions set is not trivial. We would have to retrieve each project role with permissions from the database, and run the same in-memory check against the users permission to determine which roles to return from this endpoint. Instead we opted for returning all project roles and display an error if you try to assign a role that you do not have access to. ## Follow up When this is merged, there's no longer need for the frontend logic that filters out roles in the role assignment form. I deliberately left this out of the scope for this PR because I couldn't wrap my head around everything that was going on there and I thought it was better to pair on this with @chriswk or @nunogois in order to make sure we get this right as the logic for this filtering seemed quite complex and was touching multiple different components. --------- Co-authored-by: Fredrik Strand Oseberg <fredrikstrandoseberg@Fredrik-sin-MacBook-Pro.local>
- Loading branch information
1 parent
1532f48
commit e1b7cfd
Showing
7 changed files
with
247 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ lerna-debug | |
npm-debug | ||
.DS_Store | ||
/dist | ||
.vscode | ||
|
||
# Logs | ||
logs | ||
|
121 changes: 121 additions & 0 deletions
121
src/lib/features/project/can-grant-project-role.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { canGrantProjectRole } from './can-grant-project-role'; | ||
|
||
describe('canGrantProjectRole', () => { | ||
test('should return true if the granter has all the permissions the receiver needs', () => { | ||
const granterPermissions = [ | ||
{ | ||
project: 'test', | ||
environment: undefined, | ||
permission: 'CREATE_FEATURE', | ||
}, | ||
{ | ||
project: 'test', | ||
environment: 'production', | ||
permission: 'UPDATE_FEATURE_ENVIRONMENT', | ||
}, | ||
{ | ||
project: 'test', | ||
environment: 'production', | ||
permission: 'APPROVE_CHANGE_REQUEST', | ||
}, | ||
]; | ||
|
||
const receiverPermissions = [ | ||
{ | ||
id: 28, | ||
name: 'UPDATE_FEATURE_ENVIRONMENT', | ||
environment: 'production', | ||
displayName: 'Enable/disable flags', | ||
type: 'environment', | ||
}, | ||
{ | ||
id: 29, | ||
name: 'APPROVE_CHANGE_REQUEST', | ||
environment: 'production', | ||
displayName: 'Enable/disable flags', | ||
type: 'environment', | ||
}, | ||
]; | ||
|
||
canGrantProjectRole(granterPermissions, receiverPermissions); | ||
}); | ||
|
||
test('should return false if the granter and receiver permissions have different environments', () => { | ||
const granterPermissions = [ | ||
{ | ||
project: 'test', | ||
environment: 'production', | ||
permission: 'UPDATE_FEATURE_ENVIRONMENT', | ||
}, | ||
{ | ||
project: 'test', | ||
environment: 'production', | ||
permission: 'APPROVE_CHANGE_REQUEST', | ||
}, | ||
]; | ||
|
||
const receiverPermissions = [ | ||
{ | ||
id: 28, | ||
name: 'UPDATE_FEATURE_ENVIRONMENT', | ||
environment: 'development', | ||
displayName: 'Enable/disable flags', | ||
type: 'environment', | ||
}, | ||
{ | ||
id: 29, | ||
name: 'APPROVE_CHANGE_REQUEST', | ||
environment: 'development', | ||
displayName: 'Enable/disable flags', | ||
type: 'environment', | ||
}, | ||
]; | ||
|
||
expect( | ||
canGrantProjectRole(granterPermissions, receiverPermissions), | ||
).toBeFalsy(); | ||
}); | ||
|
||
test('should return false if the granter does not have all receiver permissions', () => { | ||
const granterPermissions = [ | ||
{ | ||
project: 'test', | ||
environment: 'production', | ||
permission: 'UPDATE_FEATURE_ENVIRONMENT', | ||
}, | ||
{ | ||
project: 'test', | ||
environment: 'production', | ||
permission: 'APPROVE_CHANGE_REQUEST', | ||
}, | ||
]; | ||
|
||
const receiverPermissions = [ | ||
{ | ||
id: 28, | ||
name: 'UPDATE_FEATURE_ENVIRONMENT', | ||
environment: 'production', | ||
displayName: 'Enable/disable flags', | ||
type: 'environment', | ||
}, | ||
{ | ||
id: 29, | ||
name: 'APPROVE_CHANGE_REQUEST', | ||
environment: 'production', | ||
displayName: 'Enable/disable flags', | ||
type: 'environment', | ||
}, | ||
{ | ||
id: 26, | ||
name: 'UPDATE_FEATURE_STRATEGY', | ||
environment: 'production', | ||
displayName: 'Update activation strategies', | ||
type: 'environment', | ||
}, | ||
]; | ||
|
||
expect( | ||
canGrantProjectRole(granterPermissions, receiverPermissions), | ||
).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { IPermission } from '../../types'; | ||
import type { IUserPermission } from '../../types/stores/access-store'; | ||
|
||
export const canGrantProjectRole = ( | ||
granterPermissions: IUserPermission[], | ||
receiverPermissions: IPermission[], | ||
) => { | ||
return receiverPermissions.every((receiverPermission) => { | ||
return granterPermissions.some((granterPermission) => { | ||
if (granterPermission.environment) { | ||
return ( | ||
granterPermission.permission === receiverPermission.name && | ||
granterPermission.environment === | ||
receiverPermission.environment | ||
); | ||
} | ||
|
||
return granterPermission.permission === receiverPermission.name; | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.