Skip to content

Commit

Permalink
fix: Ensure tab characters in policy files are parsed as white-space (#…
Browse files Browse the repository at this point in the history
…324)

* fix: Ensure tab characters in policy files are parsed as white-space

* tests: Add unit tests
  • Loading branch information
janpieterz authored Aug 4, 2023
1 parent 9be6024 commit 766bf05
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Casbin.UnitTests/Fixtures/TestModelFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class TestModelFixture
//https://github.com/casbin/Casbin.NET/issues/310
internal readonly string _commaAndQuotationsModelText = ReadTestFile("comma_quotations_model.conf");
internal readonly string _commaAndQuotationsPolicyText = ReadTestFile("comma_quotations_policy.csv");
internal readonly string _tabsModelText = ReadTestFile("tabs_model.conf");
internal readonly string _tabsPolicyText = ReadTestFile("tabs_policy.csv");
internal readonly string _ipMatchModelText = ReadTestFile("ipmatch_model.conf");
internal readonly string _ipMatchPolicyText = ReadTestFile("ipmatch_policy.csv");
internal readonly string _keyMatch2ModelText = ReadTestFile("keymatch2_model.conf");
Expand Down Expand Up @@ -152,6 +154,9 @@ public IModel GetNewRbacWithResourceRoleTestModel() =>
public IModel GetNewCommaAndQuotationsModel() =>
GetNewTestModel(_commaAndQuotationsModelText, _commaAndQuotationsPolicyText);

public IModel GetNewTabsModel() =>
GetNewTestModel(_tabsModelText, _tabsPolicyText);

public static IModel GetNewTestModel(string modelText) => DefaultModel.CreateFromText(modelText);

public static IModel GetNewTestModel(string modelText, string policyText) =>
Expand Down
9 changes: 9 additions & 0 deletions Casbin.UnitTests/ModelTests/ModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,15 @@ public void TestModelWithCommaAndQuotations()
TestEnforce(e, "cindy", "\"\"Muti Quotations Test\"", "Get", false);
}

[Fact]
public void TestModelWithTabs()
{
Enforcer e = new Enforcer(_testModelFixture.GetNewTabsModel());
e.AddRoleForUserInDomain("/user/john", "admin", "/tenant/1");

Assert.True(e.Enforce("/user/john", "/tenant/1", "/tenant/1/resource", "Write"));
}

[Fact]
public void TestRbacTokensWithSubstringRelation()
{
Expand Down
15 changes: 15 additions & 0 deletions Casbin.UnitTests/examples/tabs_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[request_definition]
r = a, b, c, d

[policy_definition]
p = a, b, c, d
#p = wallet, ecosystem, resource, action

[role_definition]
g = _, _, _

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

[matchers]
m = (keyMatch2(r.a, p.a) || g(r.a, p.a, r.b)) && keyMatch2(r.c, p.c) && keyMatch(r.d, p.d) && keyMatch2(r.b, p.b) && keyGet2(r.b, p.b, "id") == keyGet2(r.c, p.c, "id")
1 change: 1 addition & 0 deletions Casbin.UnitTests/examples/tabs_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p, admin, /tenant/:id, /tenant/:id/*, *
3 changes: 2 additions & 1 deletion Casbin/Extensions/Persist/PolicyStoreExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public static bool TryLoadPolicyLine(this IPolicyStore store, string line)
HasHeaderRecord = false,
TrimOptions = TrimOptions.Trim,
IgnoreBlankLines = true,
BadDataFound = null
BadDataFound = null,
WhiteSpaceChars = new []{' ', '\t'}
});
if (parser.Read() is false)
{
Expand Down
18 changes: 12 additions & 6 deletions Casbin/Persist/Adapter/File/FileAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ private static IEnumerable<IPersistPolicy> ReadPersistPolicy(string filePath)
HasHeaderRecord = false,
TrimOptions = TrimOptions.Trim,
IgnoreBlankLines = true,
BadDataFound = null
BadDataFound = null,
WhiteSpaceChars = new []{' ', '\t'}
});

while (parser.Read())
Expand All @@ -293,7 +294,8 @@ private static IEnumerable<IPersistPolicy> ReadPersistPolicy(System.IO.Stream st
HasHeaderRecord = false,
TrimOptions = TrimOptions.Trim,
IgnoreBlankLines = true,
BadDataFound = null
BadDataFound = null,
WhiteSpaceChars = new []{' ', '\t'}
});

while (parser.Read())
Expand Down Expand Up @@ -327,7 +329,8 @@ private static async IAsyncEnumerable<IPersistPolicy> ReadPersistPolicyAsync(str
HasHeaderRecord = false,
TrimOptions = TrimOptions.Trim,
IgnoreBlankLines = true,
BadDataFound = null
BadDataFound = null,
WhiteSpaceChars = new []{' ', '\t'}
});

while (await parser.ReadAsync())
Expand Down Expand Up @@ -355,7 +358,8 @@ private static async Task<IEnumerable<IPersistPolicy>> ReadPersistPolicyAsync(st
HasHeaderRecord = false,
TrimOptions = TrimOptions.Trim,
IgnoreBlankLines = true,
BadDataFound = null
BadDataFound = null,
WhiteSpaceChars = new []{' ', '\t'}
});
var list = new List<PersistPolicy>();
while (await parser.ReadAsync())
Expand Down Expand Up @@ -386,7 +390,8 @@ private static async IAsyncEnumerable<IPersistPolicy> ReadPersistPolicyAsync(Sys
HasHeaderRecord = false,
TrimOptions = TrimOptions.Trim,
IgnoreBlankLines = true,
BadDataFound = null
BadDataFound = null,
WhiteSpaceChars = new []{' ', '\t'}
});

while (await parser.ReadAsync())
Expand All @@ -413,7 +418,8 @@ private static async Task<IEnumerable<IPersistPolicy>> ReadPersistPolicyAsync(Sy
HasHeaderRecord = false,
TrimOptions = TrimOptions.Trim,
IgnoreBlankLines = true,
BadDataFound = null
BadDataFound = null,
WhiteSpaceChars = new []{' ', '\t'}
});
var list = new List<PersistPolicy>();
while (await parser.ReadAsync())
Expand Down

0 comments on commit 766bf05

Please sign in to comment.