Skip to content

Commit

Permalink
fix: escape assertion bug (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivansh-yadav13 committed Oct 4, 2022
1 parent ebf68a0 commit 5a339d8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
11 changes: 11 additions & 0 deletions examples/abac_attr_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[request_definition]
r = sub_data, act

[policy_definition]
p = sub, act

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

[matchers]
m = r.sub_data.attr.id == p.sub && r.act == p.act
2 changes: 2 additions & 0 deletions examples/abac_attr_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
p, alice, read
p, bob, write
6 changes: 4 additions & 2 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import * as fs from 'fs';
// escapeAssertion escapes the dots in the assertion,
// because the expression evaluation doesn't support such variable names.
function escapeAssertion(s: string): string {
s = s.replace(/r\./g, 'r_');
s = s.replace(/p\./g, 'p_');
s = ' ' + s;
s = s.replace(/(?<=[\(| ])r\./g, 'r_');
s = s.replace(/(?<=[\(| ])p\./g, 'p_');
s = s.trim();
return s;
}

Expand Down
14 changes: 14 additions & 0 deletions test/enforcer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,17 @@ test('TestEnforceExWithPriorityModel', async () => {
testEnforceEx(e, 'bob', 'data2', 'read', [true, ['data2_allow_group', 'data2', 'read', 'allow']]);
testEnforceEx(e, 'alice', 'data2', 'read', [false, []]);
});

test('TestABACAtrrModel', async () => {
const e = await newEnforcer('examples/abac_attr_model.conf', 'examples/abac_attr_policy.csv');
expect(
await e.enforce(
{
attr: {
id: 'alice',
},
},
'read'
)
).toBe(true);
});

0 comments on commit 5a339d8

Please sign in to comment.