Skip to content

Commit 060c258

Browse files
committed
feat: fix the double quotes bug
1 parent 8dd4940 commit 060c258

File tree

4 files changed

+78
-12
lines changed

4 files changed

+78
-12
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[request_definition]
2+
r = sub, obj, act
3+
4+
[policy_definition]
5+
p = sub_rule, obj_rule, act
6+
7+
[policy_effect]
8+
e = some(where (p.eft == allow))
9+
10+
[matchers]
11+
m = r.act == p.act && eval(p.sub_rule) && eval(p.obj_rule)

src/main/java/org/casbin/jcasbin/main/InternalEnforcer.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727

2828
import static java.util.Collections.singletonList;
29+
import static org.casbin.jcasbin.util.Util.splitCommaDelimitedList;
2930

3031
/**
3132
* InternalEnforcer = CoreEnforcer + Internal API.
@@ -73,12 +74,14 @@ private boolean notifyWatcher(String sec, String ptype, List<List<String>> rules
7374
* addPolicy adds a rule to the current policy.
7475
*/
7576
boolean addPolicy(String sec, String ptype, List<String> rule) {
77+
List<String> modifiedRule = splitCommaDelimitedList(rule);
78+
7679
if (mustUseDispatcher()) {
77-
dispatcher.addPolicies(sec, ptype, singletonList(rule));
80+
dispatcher.addPolicies(sec, ptype, singletonList(modifiedRule));
7881
return true;
7982
}
8083

81-
if (model.hasPolicy(sec, ptype, rule)) {
84+
if (model.hasPolicy(sec, ptype, modifiedRule)) {
8285
return false;
8386
}
8487

@@ -93,11 +96,11 @@ boolean addPolicy(String sec, String ptype, List<String> rule) {
9396
}
9497
}
9598

96-
model.addPolicy(sec, ptype, rule);
99+
model.addPolicy(sec, ptype, modifiedRule);
97100

98-
buildIncrementalRoleLinks(sec, ptype, singletonList(rule), Model.PolicyOperations.POLICY_ADD);
101+
buildIncrementalRoleLinks(sec, ptype, singletonList(modifiedRule), Model.PolicyOperations.POLICY_ADD);
99102

100-
return notifyWatcher(sec, ptype, singletonList(rule), WatcherEx.UpdateType.UpdateForAddPolicy);
103+
return notifyWatcher(sec, ptype, singletonList(modifiedRule), WatcherEx.UpdateType.UpdateForAddPolicy);
101104
}
102105

103106

@@ -156,8 +159,10 @@ public void buildIncrementalRoleLinks(Model.PolicyOperations op, String ptype, L
156159
* removePolicy removes a rule from the current policy.
157160
*/
158161
boolean removePolicy(String sec, String ptype, List<String> rule) {
162+
List<String> modifiedRule = splitCommaDelimitedList(rule);
163+
159164
if (mustUseDispatcher()) {
160-
dispatcher.removePolicies(sec, ptype, singletonList(rule));
165+
dispatcher.removePolicies(sec, ptype, singletonList(modifiedRule));
161166
return true;
162167
}
163168

@@ -172,15 +177,15 @@ boolean removePolicy(String sec, String ptype, List<String> rule) {
172177
}
173178
}
174179

175-
boolean ruleRemoved = model.removePolicy(sec, ptype, rule);
180+
boolean ruleRemoved = model.removePolicy(sec, ptype, modifiedRule);
176181

177182
if (!ruleRemoved) {
178183
return false;
179184
}
180185

181-
buildIncrementalRoleLinks(sec, ptype, singletonList(rule), Model.PolicyOperations.POLICY_REMOVE);
186+
buildIncrementalRoleLinks(sec, ptype, singletonList(modifiedRule), Model.PolicyOperations.POLICY_REMOVE);
182187

183-
return notifyWatcher(sec, ptype, singletonList(rule), WatcherEx.UpdateType.UpdateForRemovePolicy);
188+
return notifyWatcher(sec, ptype, singletonList(modifiedRule), WatcherEx.UpdateType.UpdateForRemovePolicy);
184189
}
185190

186191
/**
@@ -193,8 +198,11 @@ boolean removePolicy(String sec, String ptype, List<String> rule) {
193198
* @return succeeds or not.
194199
*/
195200
boolean updatePolicy(String sec, String ptype, List<String> oldRule, List<String> newRule) {
201+
List<String> modifiedOldRule = splitCommaDelimitedList(oldRule);
202+
List<String> modifiedNewRule = splitCommaDelimitedList(newRule);
203+
196204
if (mustUseDispatcher()) {
197-
dispatcher.updatePolicy(sec, ptype, oldRule, newRule);
205+
dispatcher.updatePolicy(sec, ptype, modifiedOldRule, modifiedNewRule);
198206
return true;
199207
}
200208

@@ -211,7 +219,7 @@ boolean updatePolicy(String sec, String ptype, List<String> oldRule, List<String
211219
}
212220
}
213221

214-
boolean ruleUpdated = model.updatePolicy(sec, ptype, oldRule, newRule);
222+
boolean ruleUpdated = model.updatePolicy(sec, ptype, modifiedOldRule, modifiedNewRule);
215223

216224
if (!ruleUpdated) {
217225
return false;
@@ -242,7 +250,7 @@ boolean updatePolicy(String sec, String ptype, List<String> oldRule, List<String
242250
if (watcher != null && autoNotifyWatcher) {
243251
try {
244252
if (watcher instanceof WatcherUpdatable) {
245-
((WatcherUpdatable) watcher).updateForUpdatePolicy(oldRule, newRule);
253+
((WatcherUpdatable) watcher).updateForUpdatePolicy(modifiedOldRule, modifiedNewRule);
246254
} else {
247255
watcher.update();
248256
}

src/main/java/org/casbin/jcasbin/util/Util.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,21 @@ public static String[] splitCommaDelimited(String s) {
284284
return records;
285285
}
286286

287+
/**
288+
* splits each string in the given list by commas according to CSV format
289+
* and removes any extra double quotes
290+
* @param rule the rule to be modified
291+
* @return the modified rule
292+
*/
293+
public static List<String> splitCommaDelimitedList(List<String> rule) {
294+
List<String> modifiedRule = new ArrayList<>();
295+
for (String s : rule) {
296+
String[] strings = splitCommaDelimited(s);
297+
modifiedRule.add(strings[0]);
298+
}
299+
return modifiedRule;
300+
}
301+
287302
/**
288303
* setEquals determines whether two string sets are identical.
289304
*

src/test/java/org/casbin/jcasbin/main/AbacAPIUnitTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616

1717
import org.casbin.jcasbin.util.Util;
1818
import org.junit.Test;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
1922
import java.util.Map;
2023
import java.util.HashMap;
2124

2225
import static org.casbin.jcasbin.main.TestUtil.testDomainEnforce;
2326
import static org.casbin.jcasbin.main.TestUtil.testEnforce;
27+
import static org.junit.Assert.*;
2428

2529
public class AbacAPIUnitTest {
2630
@Test
@@ -57,6 +61,34 @@ public void testEvalWithDomain() {
5761
testDomainEnforce(e, "bob", "domain2", "data2", "read", true);
5862
}
5963

64+
@Test
65+
public void testEvalWithComma() {
66+
Enforcer e = new Enforcer("examples/abac_rule_with_comma_model.conf");
67+
List<String> rule = new ArrayList<>();
68+
rule.add("true");
69+
rule.add("\"let test=seq.set('alice','bob');include(test,r.sub.name)\"");
70+
rule.add("read");
71+
List<String> newRule = new ArrayList<>();
72+
newRule.add("true");
73+
newRule.add("\"let test=seq.set('bob');include(test,r.sub.name)\"");
74+
newRule.add("read");
75+
assertTrue(e.addPolicy(rule));
76+
assertFalse(e.addPolicy(rule));
77+
78+
Map<String, Object> sub = new HashMap<>();
79+
sub.put("name", "alice");
80+
81+
testEnforce(e, sub, "data1", "read", true);
82+
83+
assertTrue(e.updatePolicy("p", "p", rule, newRule));
84+
testEnforce(e, sub, "data1", "read", false);
85+
sub.put("name", "bob");
86+
testEnforce(e, sub, "data1", "read", true);
87+
88+
assertTrue(e.removePolicy(newRule));
89+
testEnforce(e, sub, "data1", "read", false);
90+
}
91+
6092
@Test
6193
public void testABACMapRequest() {
6294
Enforcer e = new Enforcer("examples/abac_rule_map_model.conf");

0 commit comments

Comments
 (0)