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-pycasbin.git
The following commit(s) were added to refs/heads/master by this push:
new bd137d0 fix: skip quoted literals and escape every r/p prefix in
escape_assertion
bd137d0 is described below
commit bd137d0c6ff2fd5560c4d06f4da71af7e03aac02
Author: Yang Luo <[email protected]>
AuthorDate: Tue Oct 20 22:30:01 2026 +0800
fix: skip quoted literals and escape every r/p prefix in escape_assertion
---
casbin/util/util.py | 39 +++++++++++++++++++++++----------------
tests/util/test_util.py | 8 ++++++++
2 files changed, 31 insertions(+), 16 deletions(-)
diff --git a/casbin/util/util.py b/casbin/util/util.py
index 618f124..09ea538 100644
--- a/casbin/util/util.py
+++ b/casbin/util/util.py
@@ -16,25 +16,32 @@ from collections import OrderedDict
import re
eval_reg = re.compile(r"\beval\((?P<rule>[^)]*)\)")
+escape_assertion_reg = re.compile(r"\b([rp]\d*)\.")
def escape_assertion(s):
- """escapes the dots in the assertion, because the expression evaluation
doesn't support such variable names."""
- eval_p = re.search(r"\bp(\d*)\.", s)
- if eval_p is not None:
- p_suffix = eval_p.group(1)
- p_before = re.compile(f"\\bp{p_suffix}\\.")
- p_after = f"p{p_suffix}_"
- s = re.sub(p_before, p_after, s)
-
- eval_r = re.search(r"\br(\d*)\.", s)
- if eval_r is not None:
- r_suffix = eval_r.group(1)
- r_before = re.compile(f"\\br{r_suffix}\\.")
- r_after = f"r{r_suffix}_"
- s = re.sub(r_before, r_after, s)
-
- return s
+ """escapes the dots in the assertion, because the expression evaluation
doesn't support such variable names.
+ Quoted string literals are kept as they are."""
+ res = []
+ start = 0
+ i = 0
+ while i < len(s):
+ quote = s[i]
+ if quote not in "\"'":
+ i += 1
+ continue
+ end = i + 1
+ while end < len(s) and s[end] != quote:
+ if s[end] == "\\":
+ end += 1
+ end += 1
+ end = min(end + 1, len(s))
+ res.append(escape_assertion_reg.sub(r"\1_", s[start:i]))
+ res.append(s[i:end])
+ start = end
+ i = end
+ res.append(escape_assertion_reg.sub(r"\1_", s[start:]))
+ return "".join(res)
def remove_comments(s):
diff --git a/tests/util/test_util.py b/tests/util/test_util.py
index 1300c72..3c6c25d 100644
--- a/tests/util/test_util.py
+++ b/tests/util/test_util.py
@@ -29,6 +29,14 @@ class TestUtil(TestCase):
util.escape_assertion("m = r.sub == p.sub && r.obj == p.obj &&
r.act == p.act"),
"m = r_sub == p_sub && r_obj == p_obj && r_act == p_act",
)
+ self.assertEqual(
+ util.escape_assertion('r.obj == "https://p.example.com" and p.sub
== r.sub'),
+ 'r_obj == "https://p.example.com" and p_sub == r_sub',
+ )
+ self.assertEqual(
+ util.escape_assertion("r.obj == 'a\\'p.b' or r2.obj == p2.obj"),
+ "r_obj == 'a\\'p.b' or r2_obj == p2_obj",
+ )
def test_array_remove_duplicates(self):
res = util.array_remove_duplicates(["data", "data1", "data2", "data1",
"data2", "data3"])
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]