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-node-casbin.git


The following commit(s) were added to refs/heads/master by this push:
     new 152ccd9  fix: compare repeated keyMatch4 tokens only with their own 
values (#574)
152ccd9 is described below

commit 152ccd9f31266722dd320ed8652ba33d22ada287
Author: Muhammad Basit Ali <[email protected]>
AuthorDate: Tue Oct 27 07:05:37 2026 +0500

    fix: compare repeated keyMatch4 tokens only with their own values (#574)
---
 src/util/builtinOperators.ts | 8 +++-----
 test/util.test.ts            | 7 +++++++
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/util/builtinOperators.ts b/src/util/builtinOperators.ts
index d05800c..94e3414 100644
--- a/src/util/builtinOperators.ts
+++ b/src/util/builtinOperators.ts
@@ -224,11 +224,9 @@ function keyMatch4(key1: string, key2: string): boolean {
   });
 
   for (const value of m.values()) {
-    if (value.length > 1) {
-      for (let i = 1; i < values.length; i++) {
-        if (values[i] !== values[0]) {
-          return false;
-        }
+    for (let i = 1; i < value.length; i++) {
+      if (value[i] !== value[0]) {
+        return false;
       }
     }
   }
diff --git a/test/util.test.ts b/test/util.test.ts
index a7bf769..bce2f09 100644
--- a/test/util.test.ts
+++ b/test/util.test.ts
@@ -91,6 +91,13 @@ test('test keyMatch4Func', () => {
   expect(util.keyMatch4Func('/parent/123/child/456/book/123', 
'/parent/{id}/child/{id}/book/{id}')).toEqual(false);
   expect(util.keyMatch4Func('/parent/123/child/456/book/', 
'/parent/{id}/child/{id}/book/{id}')).toEqual(false);
   expect(util.keyMatch4Func('/parent/123/child/456', 
'/parent/{id}/child/{id}/book/{id}')).toEqual(false);
+
+  // A repeated token must not be compared with the values of other tokens.
+  expect(util.keyMatch4Func('/parent/123/child/123/book/456', 
'/parent/{id}/child/{id}/book/{book_id}')).toEqual(true);
+  expect(util.keyMatch4Func('/parent/123/child/456/book/789', 
'/parent/{id}/child/{id}/book/{book_id}')).toEqual(false);
+  expect(util.keyMatch4Func('/org/1/user/2/org/1', 
'/org/{org}/user/{user}/org/{org}')).toEqual(true);
+  expect(util.keyMatch4Func('/org/1/user/2/org/3', 
'/org/{org}/user/{user}/org/{org}')).toEqual(false);
+  expect(util.keyMatch4Func('/a/x/b/y/c/y', 
'/a/{p}/b/{q}/c/{q}')).toEqual(true);
 });
 
 test('test keyMatch5Func', () => {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to