Skip to content

Commit 50d1967

Browse files
authored
feat: fix the double quotes caused eval() bug (#443)
* feat: fix the double quotes caused eval() bug * feat: fix the conf file * feat: fix the test case
1 parent 16ced48 commit 50d1967

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
import java.util.function.BiPredicate;
4343
import java.util.function.Function;
4444

45+
import static org.casbin.jcasbin.util.Util.hasEval;
46+
import static org.casbin.jcasbin.util.Util.splitCommaDelimitedList;
47+
4548
/**
4649
* CoreEnforcer defines the core functionality of an enforcer.
4750
*/
@@ -580,6 +583,7 @@ private EnforceResult enforce(String matcher, Object... rvals) {
580583
} else {
581584
expString = Util.removeComments(Util.escapeAssertion(matcher));
582585
}
586+
boolean hasEval = hasEval(expString);
583587

584588
// json process
585589
if (acceptJsonRequest) {
@@ -629,6 +633,9 @@ private EnforceResult enforce(String matcher, Object... rvals) {
629633

630634
for (int i = 0; i < policy.size(); i++) {
631635
List<String> pvals = policy.get(i);
636+
if (hasEval) {
637+
pvals = splitCommaDelimitedList(pvals);
638+
}
632639
Map<String, Object> parameters = new HashMap<>(rvals.length + pTokens.length);
633640
getPTokens(parameters, pType, pvals, pTokens);
634641
getRTokens(parameters, rType, rvals);

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

+16-1
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
*
@@ -314,7 +329,7 @@ public static boolean setEquals(List<String> a, List<String> b) {
314329
}
315330

316331
public static boolean hasEval(String exp) {
317-
return evalReg.matcher(exp).matches();
332+
return evalReg.matcher(exp).find();
318333
}
319334

320335
public static String replaceEval(String s, String replacement) {

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
package org.casbin.jcasbin.main;
1616

17-
import org.casbin.jcasbin.util.Util;
1817
import org.junit.Test;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
1921
import java.util.Map;
2022
import java.util.HashMap;
2123

@@ -42,6 +44,15 @@ public void testEval() {
4244
alice.setAge(60);
4345
testEnforce(e, alice, "/data2", "read", false);
4446
testEnforce(e, alice, "/data2", "write", false);
47+
48+
List<String> rule = new ArrayList<>();
49+
rule.add("\"r.sub.name == 'alice,green'\"");
50+
rule.add("data1");
51+
rule.add("read");
52+
e.addPolicy(rule);
53+
54+
TestEvalRule aliceGreen = new TestEvalRule("alice,green", 18);
55+
testEnforce(e, aliceGreen, "data1", "read", true);
4556
}
4657

4758
@Test

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

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.IOException;
2828
import java.io.StringReader;
2929

30+
import static org.casbin.jcasbin.util.Util.hasEval;
3031
import static org.junit.Assert.*;
3132
import static org.mockito.ArgumentMatchers.*;
3233

@@ -84,6 +85,13 @@ public void testSplitCommaDelimited(){
8485
assertArrayEquals(new String[]{"a b", "c", "d"}, Util.splitCommaDelimited("\"a b\", c, d"));
8586
}
8687

88+
@Test
89+
public void testHasEval() {
90+
assertTrue(hasEval("eval(test)"));
91+
assertTrue(hasEval("r_act == p_act && eval(p_sub_rule) && eval(p_obj_rule)"));
92+
assertFalse(hasEval("evaltest"));
93+
}
94+
8795
@Test
8896
public void testReplaceEval() {
8997
Util.logPrint(Util.replaceEval("eval(test)", "testEval"));

0 commit comments

Comments
 (0)