Skip to content

Commit 5a339d8

Browse files
fix: escape assertion bug (#394)
1 parent ebf68a0 commit 5a339d8

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

examples/abac_attr_model.conf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[request_definition]
2+
r = sub_data, act
3+
4+
[policy_definition]
5+
p = sub, act
6+
7+
[policy_effect]
8+
e = some(where (p.eft == allow))
9+
10+
[matchers]
11+
m = r.sub_data.attr.id == p.sub && r.act == p.act

examples/abac_attr_policy.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
p, alice, read
2+
p, bob, write

src/util/util.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ import * as fs from 'fs';
1717
// escapeAssertion escapes the dots in the assertion,
1818
// because the expression evaluation doesn't support such variable names.
1919
function escapeAssertion(s: string): string {
20-
s = s.replace(/r\./g, 'r_');
21-
s = s.replace(/p\./g, 'p_');
20+
s = ' ' + s;
21+
s = s.replace(/(?<=[\(| ])r\./g, 'r_');
22+
s = s.replace(/(?<=[\(| ])p\./g, 'p_');
23+
s = s.trim();
2224
return s;
2325
}
2426

test/enforcer.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,17 @@ test('TestEnforceExWithPriorityModel', async () => {
697697
testEnforceEx(e, 'bob', 'data2', 'read', [true, ['data2_allow_group', 'data2', 'read', 'allow']]);
698698
testEnforceEx(e, 'alice', 'data2', 'read', [false, []]);
699699
});
700+
701+
test('TestABACAtrrModel', async () => {
702+
const e = await newEnforcer('examples/abac_attr_model.conf', 'examples/abac_attr_policy.csv');
703+
expect(
704+
await e.enforce(
705+
{
706+
attr: {
707+
id: 'alice',
708+
},
709+
},
710+
'read'
711+
)
712+
).toBe(true);
713+
});

0 commit comments

Comments
 (0)