Skip to content

Commit

Permalink
fix: fix ABAC rule with attribute that does not exist (#333)
Browse files Browse the repository at this point in the history
Signed-off-by: imp2002 <imp07@qq.com>
  • Loading branch information
imp2002 authored Feb 7, 2023
1 parent 9d2f491 commit 424486f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/abac_rule_policy.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
p, r.sub.not_exist_attribute_test == true, /data0, read
p, r.sub.age > 18 && r.sub.age < 25, /data1, read
p, r.sub.age < 60, /data2, write
6 changes: 5 additions & 1 deletion src/main/java/org/casbin/jcasbin/util/BuiltInFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,11 @@ public String getName() {
public static boolean eval(String eval, Map<String, Object> env, AviatorEvaluatorInstance aviatorEval) {
boolean res;
if (aviatorEval != null) {
res = (boolean) aviatorEval.execute(eval, env);
try {
res = (boolean) aviatorEval.execute(eval, env);
} catch (Exception e) {
res = false;
}
} else {
res = (boolean) AviatorEvaluator.execute(eval, env);
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/casbin/jcasbin/main/AbacAPIUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class AbacAPIUnitTest {
public void testEval() {
Enforcer e = new Enforcer("examples/abac_rule_model.conf", "examples/abac_rule_policy.csv");
TestEvalRule alice = new TestEvalRule("alice", 18);
// rule with attribute not exist in object will return false, then check the following policy of ACL
testEnforce(e, alice, "/data0", "read", false);
testEnforce(e, alice, "/data1", "read", false);
testEnforce(e, alice, "/data1", "write", false);
alice.setAge(19);
Expand Down

0 comments on commit 424486f

Please sign in to comment.