From 5b854e5c0d7c4b91eead50400023e47c260598fd Mon Sep 17 00:00:00 2001 From: Muhammad Basit Ali <62793038+basitalisandhu@users.noreply.github.com> Date: Mon, 26 Oct 2026 14:26:17 +0500 Subject: [PATCH] fix: compare repeated keyMatch4 tokens only with their own values keyMatch4 grouped the captured values by token name, but the check for a repeated token then compared every captured value with the first one. Any pattern with a repeated token plus another token rejected valid paths, for example /parent/123/child/123/book/456 against /parent/{id}/child/{id}/book/{book_id}. Compare the values within each token group instead, as Go Casbin does. --- 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', () => {