casbin / Casbin.NET

An authorization library that supports access control models like ACL, RBAC, ABAC in .NET (C#)
https://casbin.org
Apache License 2.0
1.14k stars 110 forks source link

Eval of custom function with multiple parameters: issue with LoadPolicyLine that splits on comma #234

Closed slinders1985 closed 2 years ago

slinders1985 commented 2 years ago

Our model:

[request_definition]
r = dom, sub, obj, act

[policy_definition]
p = dom, sub, obj, act, eft

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

[matchers]
m = eval(p.dom) && eval(p.sub) && eval(p.obj) && keyMatch2(r.act, p.act)

The policies are loaded from our db using the EFCore-Adapter. In one of the eval() we want to evaluate a custom function which takes in more than one parameter. The V1 (sub) of the policy will refer to this custom function e.g. customFunction(r.sub, r.dom, "aString").

The custom function gets successfully added to the enforcer, but will never get to the point of execution because of following issue: when the LoadPolicyLine in NetCasbin.Persist.Helper gets hit, the line will split on the commas, which also splits the customFunction(r.sub, r.dom, "aString") into pieces. Therefore in the InternalEnforceWithChainEffector the policyValues.Count will always be larger than the context.PolicyTokens.Count and throw the ArgumentException.

When I adjust the customFunction so it only receives one parameter, the customFunction gets called correctly in the Casbin evaluation process.

Could the Helper be adjusted so that custom functions with multiple parameters are not split any longer on the comma? Are is there another solution to this issue that I can't see at the moment?

casbin-bot commented 2 years ago

@sagilio @xcaptain @huazhikui

hsluoyz commented 2 years ago

@sagilio @hackerchai

sagilio commented 2 years ago

I will add the more load line method to support this, like this: https://github.com/casbin/casbin/pull/887

pspeybro commented 2 years ago

@sagilio, the tryLoadPolicy pull request that you linked, that should resolve the issue?

sagilio commented 2 years ago

@sagilio, the tryLoadPolicy pull request that you linked, that should resolve the issue?

Just wait a moment. also need to update EFCore-Adapter

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 1.12.1 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket:

slinders1985 commented 2 years ago

Thank you! Works like a charm :ok_hand: