Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:users): ensure permission guards correctly evaluate array permissions

johavenc27dc7b7ac20
diff --git a/backend/src/applications/users/guards/permissions.guard.spec.ts b/backend/src/applications/users/guards/permissions.guard.spec.ts
index 25e1234f..273f7fe6 100644
--- a/backend/src/applications/users/guards/permissions.guard.spec.ts
+++ b/backend/src/applications/users/guards/permissions.guard.spec.ts
@@ -31,7 +31,7 @@ describe(UserPermissionsGuard.name, () => {
expect(userTest).toBeDefined()
})
- it('should pass with a valid permission', async () => {
+ it('should pass with a valid permission', () => {
userTest.applications = [USER_PERMISSION.PERSONAL_SPACE]
context = createMock<ExecutionContext>()
UserHavePermission(USER_PERMISSION.PERSONAL_SPACE)(context.getHandler())
@@ -41,7 +41,7 @@ describe(UserPermissionsGuard.name, () => {
expect(permissionsGuard.canActivate(context)).toBe(true)
})
- it('should pass if any of the permissions are granted', async () => {
+ it('should pass if any of the permissions are granted', () => {
userTest.applications = [USER_PERMISSION.PERSONAL_SPACE]
context = createMock<ExecutionContext>()
UserHavePermission([USER_PERMISSION.SPACES, USER_PERMISSION.PERSONAL_SPACE])(context.getHandler())
@@ -51,7 +51,7 @@ describe(UserPermissionsGuard.name, () => {
expect(permissionsGuard.canActivate(context)).toBe(true)
})
- it('should not pass with a bad permission', async () => {
+ it('should not pass with a bad permission', () => {
userTest.applications = []
context = createMock<ExecutionContext>()
UserHavePermission(USER_PERMISSION.PERSONAL_SPACE)(context.getHandler())
@@ -61,7 +61,7 @@ describe(UserPermissionsGuard.name, () => {
expect(() => permissionsGuard.canActivate(context)).toThrow(HttpException)
})
- it('should pass with no permissions but with the admin role', async () => {
+ it('should pass with no permissions but with the admin role', () => {
userTest.applications = []
userTest.role = USER_ROLE.ADMINISTRATOR
context = createMock<ExecutionContext>()
@@ -74,7 +74,7 @@ describe(UserPermissionsGuard.name, () => {
userTest.role = USER_ROLE.USER
})
- it('should not pass with a missing decorator', async () => {
+ it('should not pass with a missing decorator', () => {
userTest.applications = [USER_PERMISSION.PERSONAL_SPACE]
context = createMock<ExecutionContext>()
context.switchToHttp().getRequest.mockReturnValue({
@@ -83,7 +83,7 @@ describe(UserPermissionsGuard.name, () => {
expect(permissionsGuard.canActivate(context)).toBeFalsy()
})
- it('should pass with an empty decorator', async () => {
+ it('should pass with an empty decorator', () => {
userTest.applications = [USER_PERMISSION.PERSONAL_SPACE]
context = createMock<ExecutionContext>()
UserHavePermission()(context.getHandler())
diff --git a/backend/src/applications/users/guards/permissions.guard.ts b/backend/src/applications/users/guards/permissions.guard.ts
index 39368434..a7cbed64 100644
--- a/backend/src/applications/users/guards/permissions.guard.ts
+++ b/backend/src/applications/users/guards/permissions.guard.ts
@@ -18,14 +18,14 @@ export class UserPermissionsGuard implements CanActivate {
canActivate(ctx: ExecutionContext): boolean {
const permissions: USER_PERMISSION | USER_PERMISSION[] = this.reflector.getAllAndOverride(UserHavePermission, [ctx.getHandler(), ctx.getClass()])
- if (typeof permissions === 'object') {
- // used to bypass the check, the guard is called without argument, the value is '{}'
- return true
- }
if (permissions === undefined) {
this.logger.warn(`no application defined on ${ctx.getClass().name}:${ctx.getHandler().name}`)
return false
}
+ if (Object.keys(permissions).length === 0) {
+ // used to bypass the check, the guard is called without argument, the value is '{}'
+ return true
+ }
const req: FastifyAuthenticatedRequest = ctx.switchToHttp().getRequest()
if (req.user.isAdmin) {
return true