Skip to content

fix: can not parse index operator in matcher #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Casbin.UnitTests/Fixtures/TestModelFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public class TestModelFixture
public static readonly string BackslashLineFeedModelText = ReadTestFile("backslash_feed_model.conf");
public static readonly string BackslashLineFeedPolicyText = ReadTestFile("backslash_feed_policy.csv");

// https://github.com/casbin/Casbin.NET/issues/353
public static readonly string RbacWithIndexMatcherModelText = ReadTestFile("rbac_with_index_matcher_model.conf");
public static readonly string RbacWithIndexMatcherPolicyText = ReadTestFile("rbac_with_index_matcher_policy.csv");

public static IModel GetNewAbacModel() => GetNewTestModel(AbacModelText);

public static IModel GetNewAbacWithEvalModel() => GetNewTestModel(AbacWithEvalModelText, AbacWithEvalPolicyText);
Expand Down
26 changes: 25 additions & 1 deletion Casbin.UnitTests/ModelTests/ModelTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Casbin.Model;
using Casbin.UnitTests.Fixtures;
using Casbin.UnitTests.Mock;
Expand Down Expand Up @@ -728,6 +730,28 @@ public void TestAccidentalCacheRead()
TestEnforce(e, "alice", "data", "1read", false);
}

[Fact]
public void TestRbacWithIndexMatcher()
{
Enforcer e = new(TestModelFixture.GetNewTestModel(
TestModelFixture.RbacWithIndexMatcherModelText,
TestModelFixture.RbacWithIndexMatcherPolicyText));
e.BuildRoleLinks();
var rule = new Dictionary<string, Dictionary<string, string>>
{
["CompanyData"] = new() { ["CompanyIsActive"] = "True", ["BusinessRole"] = "Role1" }
};
Assert.True(e.Enforce(rule, "WebApp", "/api/transactions/getTransactions", "POST"));
Assert.False(e.Enforce(rule, "Admin", "/api/transactions/getTransactions", "POST"));

rule = new Dictionary<string, Dictionary<string, string>>
{
["CompanyData"] = new() { ["CompanyIsActive"] = "False", ["BusinessRole"] = "Role1" }
};
Assert.False(e.Enforce(rule, "WebApp", "/api/transactions/getTransactions", "POST"));
Assert.True(e.Enforce(rule, "Admin", "/api/transactions/getTransactions", "POST"));
}

public class TestResource
{
public TestResource(string name, string owner)
Expand Down
14 changes: 14 additions & 0 deletions Casbin.UnitTests/examples/rbac_with_index_matcher_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[request_definition]
r = rule, sub, obj, act

[policy_definition]
p = sub_rule, sub, obj, act

[role_definition]
g = _, _

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

[matchers]
m = eval(p.sub_rule) && g(r.sub, p.sub) && keyMatch3(r.obj, p.obj) && (p.act == "*" || regexMatch(r.act, p.act))
2 changes: 2 additions & 0 deletions Casbin.UnitTests/examples/rbac_with_index_matcher_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
p, r.rule["CompanyData"]["CompanyIsActive"] == "True" && (r.rule["CompanyData"]["BusinessRole"] == "Role1" || r.rule["CompanyData"]["BusinessRole"] == "Role2"), WebApp, /api/transactions/getTransactions, POST
p, r.rule["CompanyData"]["CompanyIsActive"] == "False" && (r.rule["CompanyData"]["BusinessRole"] == "Role1" || r.rule["CompanyData"]["BusinessRole"] == "Role2"), Admin, /api/transactions/getTransactions, POST
4 changes: 2 additions & 2 deletions Casbin/EnforceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public static EnforceView CreateWithMatcher(
[SuppressMessage("ReSharper", "UseDeconstruction")]
public static string TransformMatcher(in EnforceView view, string matcher)
{
string perfix = @"(?<=(\s|^|\||&|!|=|\(|\)|<|>|,|\+|-|\*|\/|\\)\s*)";
string suffix = @"(?=\s*(\s|$|\||&|!|=|\(|\)|<|>|,|\+|-|\*|\/|\\|\.|in))";
string perfix = @"(?<=(\s|^|\||&|!|=|\(|\)|<|>|,|\+|-|\*|\/|\\|\[)\s*)";
string suffix = @"(?=\s*(\s|$|\||&|!|=|\(|\)|<|>|,|\+|-|\*|\/|\\|\[|\]|\.|in))";
if (view.SupportGeneric is false)
{
foreach (KeyValuePair<string, int> tokenPair in view.RequestAssertion.Tokens)
Expand Down
Loading