diff --git a/ross/e2e/rbac.spec.ts b/ross/e2e/rbac.spec.ts index 7d73603d..6157e52f 100644 --- a/ross/e2e/rbac.spec.ts +++ b/ross/e2e/rbac.spec.ts @@ -164,7 +164,7 @@ const rbacE2EConfig: AppConfigInput = { }, }, wildcardEndpoint: { - description: 'Endpoint open to all authenticated roles via *', + description: 'Endpoint open to all roles via base viewer role inheritance', method: 'GET', path: '/wildcard-endpoint', handler: { @@ -217,14 +217,14 @@ const rbacE2EConfig: AppConfigInput = { }, 'custom.v1.all.unknown.editorDashboard': { authorization: true, - roles: ['editor:*'], + roles: ['editor:junior'], }, 'custom.v1.all.unknown.publicInfo': { authorization: false, }, 'custom.v1.all.unknown.wildcardEndpoint': { authorization: true, - roles: ['*'], + roles: ['viewer'], }, 'custom.v1.all.unknown.openAuthEndpoint': { authorization: true, @@ -525,7 +525,7 @@ test.describe.serial('Playwright E2E: RBAC Role Enforcement', () => { expect(res.status()).toBe(403); }); - test('editor:junior can access editor-dashboard via editor:* wildcard (200 OK)', async () => { + test('editor:junior can access editor-dashboard via concrete role (200 OK)', async () => { const res = await server.request.get( '/api/v1/custom-endpoints/editor-dashboard', { @@ -607,7 +607,7 @@ test.describe.serial('Playwright E2E: RBAC Role Enforcement', () => { expect(res.status()).toBe(403); }); - test('editor:senior can access editor-dashboard via editor:* wildcard (200 OK)', async () => { + test('editor:senior can access editor-dashboard via role inheritance (200 OK)', async () => { const res = await server.request.get( '/api/v1/custom-endpoints/editor-dashboard', { @@ -693,7 +693,7 @@ test.describe.serial('Playwright E2E: RBAC Role Enforcement', () => { expect(body.data.data[0].metric).toBe(42); }); - test('admin can access custom editor-dashboard via wildcard (200 OK)', async () => { + test('admin can access custom editor-dashboard via role hierarchy (200 OK)', async () => { const res = await server.request.get( '/api/v1/custom-endpoints/editor-dashboard', { @@ -704,8 +704,8 @@ test.describe.serial('Playwright E2E: RBAC Role Enforcement', () => { }); }); - test.describe('Universal Wildcard Route (roles: ["*"]) Workflows', () => { - test('viewer can access endpoint configured with roles: ["*"] (200 OK)', async () => { + test.describe('Base Role Inheritance Route (roles: ["viewer"]) Workflows', () => { + test('viewer can access endpoint configured with roles: ["viewer"] (200 OK)', async () => { const res = await server.request.get( '/api/v1/custom-endpoints/wildcard-endpoint', { @@ -718,7 +718,7 @@ test.describe.serial('Playwright E2E: RBAC Role Enforcement', () => { expect(body.data.data[0].wildcard_val).toBe(123); }); - test('editor:junior can access endpoint configured with roles: ["*"] (200 OK)', async () => { + test('editor:junior can access endpoint configured with roles: ["viewer"] (200 OK)', async () => { const res = await server.request.get( '/api/v1/custom-endpoints/wildcard-endpoint', { @@ -728,7 +728,7 @@ test.describe.serial('Playwright E2E: RBAC Role Enforcement', () => { expect(res.status()).toBe(200); }); - test('admin can access endpoint configured with roles: ["*"] (200 OK)', async () => { + test('admin can access endpoint configured with roles: ["viewer"] (200 OK)', async () => { const res = await server.request.get( '/api/v1/custom-endpoints/wildcard-endpoint', { @@ -738,7 +738,7 @@ test.describe.serial('Playwright E2E: RBAC Role Enforcement', () => { expect(res.status()).toBe(200); }); - test('unauthenticated request to roles: ["*"] endpoint returns 401 Unauthorized', async () => { + test('unauthenticated request to roles: ["viewer"] endpoint returns 401 Unauthorized', async () => { const res = await server.request.get( '/api/v1/custom-endpoints/wildcard-endpoint', ); @@ -979,6 +979,38 @@ test.describe ); }); + test('server startup rejects wildcards in apis route roles when rbac is enabled', async () => { + const universalWildcardConfig: AppConfigInput = { + ...rbacE2EConfig, + apis: { + ...rbacE2EConfig.apis, + 'custom.v1.all.unknown.wildcardEndpoint': { + authorization: true, + roles: ['*'], + }, + }, + }; + + await expect(createE2EServer(universalWildcardConfig)).rejects.toThrow( + 'wildcard pattern "*" is not allowed in route roles', + ); + + const namespacedWildcardConfig: AppConfigInput = { + ...rbacE2EConfig, + apis: { + ...rbacE2EConfig.apis, + 'custom.v1.all.unknown.editorDashboard': { + authorization: true, + roles: ['editor:*'], + }, + }, + }; + + await expect(createE2EServer(namespacedWildcardConfig)).rejects.toThrow( + 'wildcard pattern "editor:*" is not allowed in route roles', + ); + }); + test.beforeAll(async () => { disabledServer = await createE2EServer(disabledRbacConfig); diff --git a/ross/src/lib/config/api.ts b/ross/src/lib/config/api.ts index 44e6e55d..f71b2ef1 100644 --- a/ross/src/lib/config/api.ts +++ b/ross/src/lib/config/api.ts @@ -1,6 +1,5 @@ import {buildApiIdentifier, parseApiIdentifier} from '@/lib/config/identifier'; import {deriveApiTags} from '@/lib/config/tags'; -import {expandWildcards} from '@/lib/rbac'; import { Aggregation, @@ -94,8 +93,7 @@ export function resolveApiPolicy( let roles: Set | undefined; if (rawRoles && config.rbac?.enabled && config.rbac.roles) { - const declaredRoles = Object.keys(config.rbac.roles); - roles = expandWildcards(rawRoles, declaredRoles); + roles = new Set(rawRoles); } return { diff --git a/ross/src/lib/server/prevalidation.ts b/ross/src/lib/server/prevalidation.ts index 9d3bd450..027e851d 100644 --- a/ross/src/lib/server/prevalidation.ts +++ b/ross/src/lib/server/prevalidation.ts @@ -1,10 +1,6 @@ import {FastifyInstance, FastifyReply, FastifyRequest} from 'fastify'; -import { - buildRoleInclusionMap, - expandWildcards, - isRoleAuthorized, -} from '@/lib/rbac'; +import {buildRoleInclusionMap, isRoleAuthorized} from '@/lib/rbac'; import {RbacConfig, ResolvedAppConfig} from '@/interfaces/config'; @@ -52,12 +48,7 @@ export function buildPreValidation( if (roles instanceof Set) { allowedRolesSet = roles; } else if (Array.isArray(roles)) { - if (config.rbac?.enabled && config.rbac.roles) { - const declared = Object.keys(config.rbac.roles); - allowedRolesSet = expandWildcards(roles, declared); - } else { - allowedRolesSet = new Set(roles); - } + allowedRolesSet = new Set(roles); } return async (request, reply) => { diff --git a/ross/src/validators/config/validate-rbac.ts b/ross/src/validators/config/validate-rbac.ts index a5254e63..68ec3a8c 100644 --- a/ross/src/validators/config/validate-rbac.ts +++ b/ross/src/validators/config/validate-rbac.ts @@ -124,25 +124,17 @@ function validateRbacConstraints( ); } - apiConfig.roles.forEach((rolePattern, i) => { - if (rolePattern === '*') { - return; - } - - if (rolePattern.endsWith(':*')) { - const prefix = rolePattern.slice(0, -2); - const hasMatch = declaredRoles.some(r => r.startsWith(`${prefix}:`)); - if (!hasMatch) { - errors.push( - `apis/${key}/roles/${i}: wildcard pattern "${rolePattern}" does not match any declared role in rbac.roles`, - ); - } + apiConfig.roles.forEach((roleName, i) => { + if (roleName === '*' || roleName.endsWith(':*')) { + errors.push( + `apis/${key}/roles/${i}: wildcard pattern "${roleName}" is not allowed in route roles; wildcards are only supported in rbac.roles..includes`, + ); return; } - if (!Object.prototype.hasOwnProperty.call(roles, rolePattern)) { + if (!Object.prototype.hasOwnProperty.call(roles, roleName)) { errors.push( - `apis/${key}/roles/${i}: role "${rolePattern}" does not exist in rbac.roles`, + `apis/${key}/roles/${i}: role "${roleName}" does not exist in rbac.roles`, ); } }); diff --git a/ross/tests/fixtures/config/__goldens__/full.resolved.json b/ross/tests/fixtures/config/__goldens__/full.resolved.json index 88278de2..e93a7528 100644 --- a/ross/tests/fixtures/config/__goldens__/full.resolved.json +++ b/ross/tests/fixtures/config/__goldens__/full.resolved.json @@ -405,7 +405,8 @@ "model.test.posts.unknown.insert": { "authorization": true, "roles": [ - "editor:*" + "editor:senior", + "editor:junior" ], "serverSideParams": [ { @@ -418,7 +419,7 @@ "model.test.users.id.index": { "authorization": true, "roles": [ - "*" + "viewer" ], "supportedQueries": [ "eq", diff --git a/ross/tests/fixtures/config/full.json b/ross/tests/fixtures/config/full.json index 1677a159..a715d915 100644 --- a/ross/tests/fixtures/config/full.json +++ b/ross/tests/fixtures/config/full.json @@ -408,7 +408,7 @@ }, "model.test.posts.unknown.insert": { "authorization": true, - "roles": ["editor:*"], + "roles": ["editor:senior", "editor:junior"], "serverSideParams": [ { "type": "body", @@ -419,7 +419,7 @@ }, "model.test.users.id.index": { "authorization": true, - "roles": ["*"], + "roles": ["viewer"], "supportedQueries": ["eq", "sort"] }, "custom.test.all.unknown.updatePostTitle": { diff --git a/ross/tests/lib/config/api.test.ts b/ross/tests/lib/config/api.test.ts index a259b453..c53dbde3 100644 --- a/ross/tests/lib/config/api.test.ts +++ b/ross/tests/lib/config/api.test.ts @@ -269,54 +269,51 @@ describe('resolveApiPolicy', () => { expect(policy.roles).toBeUndefined(); }); - test('roles expands concrete roles and namespaced wildcards when RBAC is enabled', () => { + test('roles preserves concrete roles as a Set without wildcard expansion when RBAC is enabled', () => { const config = resolveWithRbac({ 'model.v1.posts.title.search': { - roles: ['editor:*', 'viewer'], + roles: ['editor:senior', 'viewer'], }, }); const policy = resolveApiPolicy(config, 'model.v1.posts.title.search'); - expect(policy.roles).toEqual( - new Set(['editor:senior', 'editor:junior', 'viewer']), - ); + expect(policy.roles).toEqual(new Set(['editor:senior', 'viewer'])); }); - test('roles expands universal wildcard "*" to all declared roles', () => { + test('roles does not expand wildcards in resolveApiPolicy', () => { const config = resolveWithRbac({ 'model.v1.posts.title.search': { roles: ['*'], }, + 'model.v1.posts.unknown.insert': { + roles: ['editor:*'], + }, }); - const policy = resolveApiPolicy(config, 'model.v1.posts.title.search'); - expect(policy.roles).toEqual( - new Set([ - 'admin', - 'lead_editor', - 'editor:senior', - 'editor:junior', - 'viewer', - ]), + expect(resolveApiPolicy(config, 'model.v1.posts.title.search').roles).toEqual( + new Set(['*']), ); + expect( + resolveApiPolicy(config, 'model.v1.posts.unknown.insert').roles, + ).toEqual(new Set(['editor:*'])); }); test('variant inherits roles from base override when not explicitly set', () => { const config = resolveWithRbac( { 'model.v1.posts.title.search': { - roles: ['editor:*'], + roles: ['editor:senior'], }, }, {'model.v1.posts.title.search': {variants: ['custom']}}, ); const policy = resolveApiPolicy(config, 'model.custom.posts.title.search'); - expect(policy.roles).toEqual(new Set(['editor:senior', 'editor:junior'])); + expect(policy.roles).toEqual(new Set(['editor:senior'])); }); test('variant explicit roles override base roles', () => { const config = resolveWithRbac( { 'model.v1.posts.title.search': { - roles: ['editor:*'], + roles: ['editor:senior'], }, 'model.custom.posts.title.search': { roles: ['admin'], @@ -338,13 +335,15 @@ describe('resolveApiPolicy', () => { expect(policy.roles).toBeUndefined(); }); - test('resolves wildcard and concrete role policies across a full role hierarchy', () => { + test('resolves concrete role policies across a full role hierarchy', () => { const config = resolveConfig( { ...baseInput({ 'aggregate.admin.users.id.getAggregation': {roles: ['admin']}, - 'model.test.posts.unknown.insert': {roles: ['editor:*']}, - 'model.test.users.id.index': {roles: ['*']}, + 'model.test.posts.unknown.insert': { + roles: ['editor:senior', 'editor:junior'], + }, + 'model.test.users.id.index': {roles: ['viewer']}, 'model.test.users.unknown.getAll': {authorization: true}, 'custom.test.all.unknown.updatePostTitle': { roles: ['editor:senior', 'admin'], @@ -379,7 +378,7 @@ describe('resolveApiPolicy', () => { ); expect(adminPolicy.roles).toEqual(new Set(['admin'])); - // 2. Namespaced wildcard "editor:*" -> expands to editor:senior, editor:junior + // 2. Concrete editor roles const insertPolicy = resolveApiPolicy( config, 'model.test.posts.unknown.insert', @@ -388,18 +387,9 @@ describe('resolveApiPolicy', () => { new Set(['editor:senior', 'editor:junior']), ); - // 3. Universal wildcard "*" -> expands to all declared roles + // 3. Base concrete role const indexPolicy = resolveApiPolicy(config, 'model.test.users.id.index'); - expect(indexPolicy.roles).toEqual( - new Set([ - 'admin', - 'lead_editor', - 'editor:senior', - 'editor:junior', - 'auditor', - 'viewer', - ]), - ); + expect(indexPolicy.roles).toEqual(new Set(['viewer'])); // 4. Multiple concrete roles const customPolicy = resolveApiPolicy( diff --git a/ross/tests/lib/server/prevalidation.test.ts b/ross/tests/lib/server/prevalidation.test.ts index 434d85f7..eb224a34 100644 --- a/ross/tests/lib/server/prevalidation.test.ts +++ b/ross/tests/lib/server/prevalidation.test.ts @@ -450,22 +450,31 @@ describe('buildPreValidation', () => { expect(reply.status).not.toHaveBeenCalled(); }); - test('normalizes string array of roles with wildcards', async () => { + test('normalizes string array of concrete roles without wildcard expansion', async () => { const app = buildMockApp(); const request = buildMockRequest({ user: {id: 9, email: 'editor@example.com', role: 'editor:junior'}, }); const reply = buildMockReply(); - const handler = buildPreValidation( + const concreteHandler = buildPreValidation( app as never, rbacEnabledConfig, true, - ['editor:*'], + ['editor:junior'], ); - await handler(request as never, reply as never); - + await concreteHandler(request as never, reply as never); expect(reply.status).not.toHaveBeenCalled(); + + const wildcardReply = buildMockReply(); + const wildcardHandler = buildPreValidation( + app as never, + rbacEnabledConfig, + true, + ['editor:*'], + ); + await wildcardHandler(request as never, wildcardReply as never); + expect(wildcardReply.status).toHaveBeenCalledWith(403); }); test('normalizes string array of roles when RBAC is disabled or roles is absent', async () => { diff --git a/ross/tests/routes/rbac-enforcement.test.ts b/ross/tests/routes/rbac-enforcement.test.ts index 2f1bd5c4..a69efff7 100644 --- a/ross/tests/routes/rbac-enforcement.test.ts +++ b/ross/tests/routes/rbac-enforcement.test.ts @@ -87,7 +87,7 @@ describe('RBAC Route-Level PreValidation Enforcement', () => { 'model.v1.users.unknown.insert': { enabled: true, authorization: true, - roles: ['editor:*'], + roles: ['editor:junior'], }, 'model.v1.users.id.edit': { enabled: true, @@ -116,7 +116,7 @@ describe('RBAC Route-Level PreValidation Enforcement', () => { 'custom.v1.all.unknown.wildcard': { enabled: true, authorization: true, - roles: ['*'], + roles: ['viewer'], }, }; @@ -288,7 +288,7 @@ describe('RBAC Route-Level PreValidation Enforcement', () => { expect(response.statusCode).toBe(200); }); - test('allows POST when user role matches wildcard endpoint roles (editor:junior matches editor:*)', async () => { + test('allows POST when user role matches concrete endpoint role (editor:junior)', async () => { pgClientQueryMock .mockResolvedValueOnce({rows: [], rowCount: 0}) // BEGIN .mockResolvedValueOnce({ diff --git a/ross/tests/validators/config/validate-rbac.test.ts b/ross/tests/validators/config/validate-rbac.test.ts index ab845ca9..2397d99e 100644 --- a/ross/tests/validators/config/validate-rbac.test.ts +++ b/ross/tests/validators/config/validate-rbac.test.ts @@ -494,7 +494,7 @@ describe('Static RBAC Constraint Validation (validateRbacConstraints)', () => { ); }); - it('rejects apis[key].roles with wildcard matching no declared roles', () => { + it('rejects apis[key].roles with namespaced wildcard (even if matching declared roles)', () => { const config = createBaseConfig(); config.rbac = createValidRbacConfig(); config.apis = { @@ -502,15 +502,22 @@ describe('Static RBAC Constraint Validation (validateRbacConstraints)', () => { authorization: true, roles: ['billing:*'], }, + 'model.test.posts.unknown.delete': { + authorization: true, + roles: ['editor:*'], + }, }; const errors = validateRbacConstraints(config); expect(errors).toContain( - 'apis/model.test.posts.unknown.insert/roles/0: wildcard pattern "billing:*" does not match any declared role in rbac.roles', + 'apis/model.test.posts.unknown.insert/roles/0: wildcard pattern "billing:*" is not allowed in route roles; wildcards are only supported in rbac.roles..includes', + ); + expect(errors).toContain( + 'apis/model.test.posts.unknown.delete/roles/0: wildcard pattern "editor:*" is not allowed in route roles; wildcards are only supported in rbac.roles..includes', ); }); - it('accepts apis[key].roles with universal wildcard and valid prefix wildcard', () => { + it('rejects apis[key].roles with universal wildcard "*"', () => { const config = createBaseConfig(); config.rbac = createValidRbacConfig(); config.apis = { @@ -518,9 +525,25 @@ describe('Static RBAC Constraint Validation (validateRbacConstraints)', () => { authorization: true, roles: ['*'], }, + }; + + const errors = validateRbacConstraints(config); + expect(errors).toContain( + 'apis/model.test.posts.unknown.insert/roles/0: wildcard pattern "*" is not allowed in route roles; wildcards are only supported in rbac.roles..includes', + ); + }); + + it('accepts apis[key].roles with concrete declared roles', () => { + const config = createBaseConfig(); + config.rbac = createValidRbacConfig(); + config.apis = { + 'model.test.posts.unknown.insert': { + authorization: true, + roles: ['viewer'], + }, 'model.test.posts.unknown.delete': { authorization: true, - roles: ['editor:*', 'admin'], + roles: ['editor:senior', 'admin'], }, }; @@ -559,7 +582,7 @@ describe('Static RBAC Constraint Validation (validateRbacConstraints)', () => { apis: { 'model.v1.posts.unknown.insert': { authorization: true, - roles: ['editor:*'], + roles: ['editor:senior', 'editor:junior'], }, }, }; @@ -569,7 +592,8 @@ describe('Static RBAC Constraint Validation (validateRbacConstraints)', () => { if (!resolved.rbac.enabled) throw new Error('expected rbac to be enabled'); expect(resolved.rbac.modelName).toBe('users'); expect(resolved.apis['model.v1.posts.unknown.insert'].roles).toEqual([ - 'editor:*', + 'editor:senior', + 'editor:junior', ]); });