This is an automated email from the ASF dual-hosted git repository.
hsluoyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/casbin-jcasbin.git
The following commit(s) were added to refs/heads/master by this push:
new cf15ca0 fix: leave quoted strings untouched in escapeAssertion
cf15ca0 is described below
commit cf15ca0d801659da53f3882fa658eb9c7a1314fb
Author: Yang Luo <[email protected]>
AuthorDate: Tue Oct 20 22:31:16 2026 +0800
fix: leave quoted strings untouched in escapeAssertion
---
src/main/java/org/casbin/jcasbin/util/Util.java | 28 +++++++++++++++++++++-
.../java/org/casbin/jcasbin/main/UtilTest.java | 3 +++
2 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/casbin/jcasbin/util/Util.java
b/src/main/java/org/casbin/jcasbin/util/Util.java
index 2b7163a..0d70645 100644
--- a/src/main/java/org/casbin/jcasbin/util/Util.java
+++ b/src/main/java/org/casbin/jcasbin/util/Util.java
@@ -146,12 +146,38 @@ public class Util {
/**
* escapeAssertion escapes the dots in the assertion, because the
expression
- * evaluation doesn't support such variable names.
+ * evaluation doesn't support such variable names. Quoted string literals
+ * are kept as they are.
*
* @param s the value of the matcher and effect assertions.
* @return the escaped value.
*/
public static String escapeAssertion(String s) {
+ StringBuilder sb = new StringBuilder();
+ int start = 0;
+ for (int i = 0; i < s.length(); i++) {
+ char quote = s.charAt(i);
+ if (quote != '"' && quote != '\'') {
+ continue;
+ }
+ int end = i + 1;
+ while (end < s.length() && s.charAt(end) != quote) {
+ if (s.charAt(end) == '\\') {
+ end++;
+ }
+ end++;
+ }
+ end = Math.min(end + 1, s.length());
+ sb.append(escapeAssertionUnquoted(s.substring(start, i)));
+ sb.append(s, i, end);
+ start = end;
+ i = end - 1;
+ }
+ sb.append(escapeAssertionUnquoted(s.substring(start)));
+ return sb.toString();
+ }
+
+ private static String escapeAssertionUnquoted(String s) {
Matcher m = escapeAssertionRegex.matcher(s);
StringBuffer sb = new StringBuffer();
diff --git a/src/test/java/org/casbin/jcasbin/main/UtilTest.java
b/src/test/java/org/casbin/jcasbin/main/UtilTest.java
index b3ff06b..f2f159d 100644
--- a/src/test/java/org/casbin/jcasbin/main/UtilTest.java
+++ b/src/test/java/org/casbin/jcasbin/main/UtilTest.java
@@ -54,6 +54,9 @@ public class UtilTest {
assertEquals("g(r_sub, p_sub) == p_attr", Util.escapeAssertion("g(r.sub,
p.sub) == p.attr"));
assertEquals("g(r_sub,p_sub) == p_attr",
Util.escapeAssertion("g(r.sub,p.sub) == p.attr"));
assertEquals("(r_attp.value || p_attr)p_u",
Util.escapeAssertion("(r.attp.value || p.attr)p.u"));
+ assertEquals("r_obj == \"https://p.example.com\"",
Util.escapeAssertion("r.obj == \"https://p.example.com\""));
+ assertEquals("r_obj == 'https://p.example.com' && p_sub == r_sub",
Util.escapeAssertion("r.obj == 'https://p.example.com' && p.sub == r.sub"));
+ assertEquals("r_obj == \"a\\\"p.b\" || p_obj == 'r.c'",
Util.escapeAssertion("r.obj == \"a\\\"p.b\" || p.obj == 'r.c'"));
}
@Test
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]