Skip to content

Commit 424486f

Browse files
authored
fix: fix ABAC rule with attribute that does not exist (casbin#333)
Signed-off-by: imp2002 <[email protected]>
1 parent 9d2f491 commit 424486f

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

examples/abac_rule_policy.csv

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
p, r.sub.not_exist_attribute_test == true, /data0, read
12
p, r.sub.age > 18 && r.sub.age < 25, /data1, read
23
p, r.sub.age < 60, /data2, write

src/main/java/org/casbin/jcasbin/util/BuiltInFunctions.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,11 @@ public String getName() {
402402
public static boolean eval(String eval, Map<String, Object> env, AviatorEvaluatorInstance aviatorEval) {
403403
boolean res;
404404
if (aviatorEval != null) {
405-
res = (boolean) aviatorEval.execute(eval, env);
405+
try {
406+
res = (boolean) aviatorEval.execute(eval, env);
407+
} catch (Exception e) {
408+
res = false;
409+
}
406410
} else {
407411
res = (boolean) AviatorEvaluator.execute(eval, env);
408412
}

src/test/java/org/casbin/jcasbin/main/AbacAPIUnitTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class AbacAPIUnitTest {
2525
public void testEval() {
2626
Enforcer e = new Enforcer("examples/abac_rule_model.conf", "examples/abac_rule_policy.csv");
2727
TestEvalRule alice = new TestEvalRule("alice", 18);
28+
// rule with attribute not exist in object will return false, then check the following policy of ACL
29+
testEnforce(e, alice, "/data0", "read", false);
2830
testEnforce(e, alice, "/data1", "read", false);
2931
testEnforce(e, alice, "/data1", "write", false);
3032
alice.setAge(19);

0 commit comments

Comments
 (0)