Skip to content

Commit

Permalink
fix: stackoverflow in getImplicitRolesForUser
Browse files Browse the repository at this point in the history
  • Loading branch information
nodece committed May 9, 2020
1 parent d88bece commit d0fc49f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
16 changes: 16 additions & 0 deletions examples/issues/node_casbin_150_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# https://github.com/casbin/node-casbin/issues/150
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && regexMatch(r.act, p.act)

5 changes: 5 additions & 0 deletions examples/issues/node_casbin_150_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
p, book_admin , /book/1, GET
p, pen_admin , /pen/1, GET

g, *, book_admin
g, *, pen_admin
20 changes: 12 additions & 8 deletions src/enforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,16 +280,20 @@ export class Enforcer extends ManagementEnforcer {
* But getImplicitRolesForUser("alice") will get: ["role:admin", "role:user"].
*/
public async getImplicitRolesForUser(name: string, ...domain: string[]): Promise<string[]> {
const res: string[] = [];
const roles = await this.rm.getRoles(name, ...domain);
res.push(...roles);

for (const n of roles) {
const role = await this.getImplicitRolesForUser(n, ...domain);
res.push(...role);
const res = new Set<string>();
const q = [name];
let n: string | undefined;
while ((n = q.shift()) != undefined) {
const role = await this.getRoleManager().getRoles(n, ...domain);
role.forEach(r => {
if (!res.has(r)) {
res.add(r);
q.push(r);
}
});
}

return res;
return Array.from(res);
}

/**
Expand Down
12 changes: 11 additions & 1 deletion test/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import * as _ from 'lodash';
import { DefaultRoleManager, Enforcer, newEnforcer, newModel } from '../src';
import { keyMatch2Func, keyMatch3Func } from '../src/util';
import { keyMatch2Func, keyMatch3Func, keyMatchFunc } from '../src/util';

async function testEnforce(e: Enforcer, sub: string, obj: any, act: string, res: boolean): Promise<void> {
await expect(e.enforce(sub, obj, act)).resolves.toBe(res);
Expand Down Expand Up @@ -334,3 +334,13 @@ test('TestRBACModelWithPattern', async () => {
await testEnforce(e, 'bob', '/pen2/1', 'GET', true);
await testEnforce(e, 'bob', '/pen2/2', 'GET', true);
});

test('TestNodeCasbin150', async () => {
const e = await newEnforcer('examples/issues/node_casbin_150_model.conf', 'examples/issues/node_casbin_150_policy.csv');

const rm = e.getRoleManager() as DefaultRoleManager;
await rm.addMatchingFunc('KeyMatch', keyMatchFunc);
await e.buildRoleLinks();

await e.getImplicitRolesForUser('alice');
});

0 comments on commit d0fc49f

Please sign in to comment.