Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions ross/e2e/rbac.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
},
},
wildcardEndpoint: {
description: 'Endpoint open to all authenticated roles via *',
description: 'Endpoint open to all roles via base viewer role inheritance',

Check failure on line 167 in ross/e2e/rbac.spec.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `⏎·······`
method: 'GET',
path: '/wildcard-endpoint',
handler: {
Expand Down Expand Up @@ -217,14 +217,14 @@
},
'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,
Expand Down Expand Up @@ -525,7 +525,7 @@
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',
{
Expand Down Expand Up @@ -607,7 +607,7 @@
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',
{
Expand Down Expand Up @@ -693,7 +693,7 @@
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',
{
Expand All @@ -704,8 +704,8 @@
});
});

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',
{
Expand All @@ -718,7 +718,7 @@
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',
{
Expand All @@ -728,7 +728,7 @@
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',
{
Expand All @@ -738,7 +738,7 @@
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',
);
Expand Down Expand Up @@ -979,6 +979,38 @@
);
});

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);

Expand Down
4 changes: 1 addition & 3 deletions ross/src/lib/config/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {buildApiIdentifier, parseApiIdentifier} from '@/lib/config/identifier';
import {deriveApiTags} from '@/lib/config/tags';
import {expandWildcards} from '@/lib/rbac';

import {
Aggregation,
Expand Down Expand Up @@ -94,8 +93,7 @@ export function resolveApiPolicy(
let roles: Set<string> | 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 {
Expand Down
13 changes: 2 additions & 11 deletions ross/src/lib/server/prevalidation.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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) => {
Expand Down
22 changes: 7 additions & 15 deletions ross/src/validators/config/validate-rbac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.<role>.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`,
);
}
});
Expand Down
5 changes: 3 additions & 2 deletions ross/tests/fixtures/config/__goldens__/full.resolved.json
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@
"model.test.posts.unknown.insert": {
"authorization": true,
"roles": [
"editor:*"
"editor:senior",
"editor:junior"
],
"serverSideParams": [
{
Expand All @@ -418,7 +419,7 @@
"model.test.users.id.index": {
"authorization": true,
"roles": [
"*"
"viewer"
],
"supportedQueries": [
"eq",
Expand Down
4 changes: 2 additions & 2 deletions ross/tests/fixtures/config/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@
},
"model.test.posts.unknown.insert": {
"authorization": true,
"roles": ["editor:*"],
"roles": ["editor:senior", "editor:junior"],
"serverSideParams": [
{
"type": "body",
Expand All @@ -419,7 +419,7 @@
},
"model.test.users.id.index": {
"authorization": true,
"roles": ["*"],
"roles": ["viewer"],
"supportedQueries": ["eq", "sort"]
},
"custom.test.all.unknown.updatePostTitle": {
Expand Down
56 changes: 23 additions & 33 deletions ross/tests/lib/config/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,54 +269,51 @@
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(

Check failure on line 291 in ross/tests/lib/config/api.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `resolveApiPolicy(config,·'model.v1.posts.title.search').roles).toEqual(⏎······new·Set(['*']),⏎····` with `⏎······resolveApiPolicy(config,·'model.v1.posts.title.search').roles,⏎····).toEqual(new·Set(['*'])`
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'],
Expand All @@ -338,13 +335,15 @@
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'],
Expand Down Expand Up @@ -379,7 +378,7 @@
);
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',
Expand All @@ -388,18 +387,9 @@
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(
Expand Down
19 changes: 14 additions & 5 deletions ross/tests/lib/server/prevalidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Loading
Loading