casbin / jcasbin

An authorization library that supports access control models like ACL, RBAC, ABAC in Java
https://casbin.org
Apache License 2.0
2.4k stars 464 forks source link

NullPointerException occurred when init with empty policy #94

Closed cici-debug closed 4 years ago

cici-debug commented 4 years ago

Member functions of Policy class do not check if the key ptype exists in the map, so when I tried to call enforcer.addPolicy() or enforcer.getPolicy() after init enforcer with empty policy, NullPointerException was throw. Is it a design issue? Ptype's existence should be checked and if it does not exist, empty list should be returned, not NullPointerException.

hsluoyz commented 4 years ago

@cici-debug can you give a full minimized example to reproduce it?

cici-debug commented 4 years ago

I used an customized adapter and the database is empty.

MyAdapter a = new MyAdapter();        
 Enforcer e = new Enforcer("examples/rbac_model.conf", a);
e.addPolicy("alice", "data", "query")

I found that the problems happened because of this line: https://github.com/casbin/jcasbin/blob/7dc03b642b571ff5d71b96188bc2621ff059dcf2/src/main/java/org/casbin/jcasbin/model/Policy.java#L118

hsluoyz commented 4 years ago

Then your adapter causes this crash. @tldyl please follow up this issue.

Acutally we make sure our official code is correct. If you added your custom code, please see if you have bugs in your own code.

cici-debug commented 4 years ago

If i do not use the adpter, just new the enforcer this way: Enforcer enforcer = new Enforcer("./src/main/resources/model.conf", "./src/main/resources/policy.conf"); enforcer.getPolicy(); NullPointerException was also throw. The policy file is an empty file btw. It is not my adapter causing the crash.

hsluoyz commented 4 years ago

@cici-debug please review this fix: https://github.com/casbin/jcasbin/pull/95

cici-debug commented 4 years ago

That's my config file that caused the problem. I will close the issue. Thanks for the help.