diff --git a/plugins/permission-backend/src/service/router.test.ts b/plugins/permission-backend/src/service/router.test.ts index 80308d7ca7..9c8fa4ad5f 100644 --- a/plugins/permission-backend/src/service/router.test.ts +++ b/plugins/permission-backend/src/service/router.test.ts @@ -774,6 +774,35 @@ describe('createRouter', () => { { id: '123', permission: { attributes: { invalid: 'attribute' } } }, ], }, + { + items: [ + { + id: '123', + // basic permission can't have resourceRef + resourceRef: 'resource:1', + permission: { + type: 'basic', + name: 'test.permission', + attributes: {}, + }, + }, + ], + }, + { + items: [ + { + id: '123', + // resource ref should be a string + resourceRef: ['resource:1'], + permission: { + type: 'resource', + name: 'test.permission', + attributes: {}, + resourceType: 'test-resource-1', + }, + }, + ], + }, ])('returns a 400 error for invalid request %#', async requestBody => { const response = await request(app).post('/authorize').send(requestBody); diff --git a/plugins/permission-backend/src/service/router.ts b/plugins/permission-backend/src/service/router.ts index 20cfaff285..8ba8658397 100644 --- a/plugins/permission-backend/src/service/router.ts +++ b/plugins/permission-backend/src/service/router.ts @@ -62,27 +62,33 @@ const attributesSchema: z.ZodSchema = z.object({ .optional(), }); -const permissionSchema = z.union([ - z.object({ - type: z.literal('basic'), - name: z.string(), - attributes: attributesSchema, - }), - z.object({ - type: z.literal('resource'), - name: z.string(), - attributes: attributesSchema, - resourceType: z.string(), - }), -]); +const basicPermissionSchema = z.object({ + type: z.literal('basic'), + name: z.string(), + attributes: attributesSchema, +}); + +const resourcePermissionSchema = z.object({ + type: z.literal('resource'), + name: z.string(), + attributes: attributesSchema, + resourceType: z.string(), +}); const evaluatePermissionRequestSchema: z.ZodSchema< IdentifiedPermissionMessage -> = z.object({ - id: z.string(), - resourceRef: z.string().optional(), - permission: permissionSchema, -}); +> = z.union([ + z.object({ + id: z.string(), + resourceRef: z.undefined().optional(), + permission: basicPermissionSchema, + }), + z.object({ + id: z.string(), + resourceRef: z.string().optional(), + permission: resourcePermissionSchema, + }), +]); const evaluatePermissionRequestBatchSchema: z.ZodSchema = z.object({