Skip to content

Commit 733bf1b

Browse files
Copilothsluoyz
andcommitted
Fix enforce error when using keyMatch4 - handle both string and table errors
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
1 parent 6ba6538 commit 733bf1b

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

examples/keymatch4_model.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[request_definition]
2+
r = sub, obj, act
3+
4+
[policy_definition]
5+
p = sub, obj, act
6+
7+
[role_definition]
8+
g = _, _
9+
10+
[policy_effect]
11+
e = some(where (p.eft == allow))
12+
13+
[matchers]
14+
m = g(r.sub, p.sub) && keyMatch4(r.obj, p.obj) && regexMatch(r.act, p.act)

examples/keymatch4_policy.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
p, alice, /parent/{id}/child/{id}, (GET)|(POST)
2+
p, bob, /parent/{id}/child/{another_id}, (GET)|(POST)
3+
g, alice, alice
4+
g, bob, bob

src/main/CoreEnforcer.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ function CoreEnforcer:enforceEx(...)
446446
res, err = luaxp.evaluate(tExpString, context)
447447
end
448448
if err then
449-
error("evaluation error: " .. err.message)
449+
local errMsg = type(err) == "table" and err.message or tostring(err)
450+
error("evaluation error: " .. errMsg)
450451
end
451452

452453
local c = true
@@ -491,7 +492,8 @@ function CoreEnforcer:enforceEx(...)
491492

492493
local res, err = luaxp.run(compiledExpression, context)
493494
if err then
494-
error("evaluation error: " .. err.message)
495+
local errMsg = type(err) == "table" and err.message or tostring(err)
496+
error("evaluation error: " .. errMsg)
495497
end
496498

497499
if res then

tests/main/enforcer_spec.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ describe("Enforcer tests", function ()
9494
assert.is.True(e:enforce("alice", "/alice_data2/123/using/456", "GET"))
9595
end)
9696

97+
it("keyMatch4 test", function ()
98+
local model = path .. "/examples/keymatch4_model.conf"
99+
local policy = path .. "/examples/keymatch4_policy.csv"
100+
101+
local e = Enforcer:new(model, policy)
102+
-- Test alice with matching IDs (same placeholder value)
103+
assert.is.True(e:enforce("alice", "/parent/123/child/123", "GET"))
104+
assert.is.True(e:enforce("alice", "/parent/456/child/456", "POST"))
105+
-- Test alice with non-matching IDs (different placeholder values)
106+
assert.is.False(e:enforce("alice", "/parent/123/child/456", "GET"))
107+
-- Test bob with different IDs (different placeholders, should work)
108+
assert.is.True(e:enforce("bob", "/parent/123/child/456", "GET"))
109+
assert.is.True(e:enforce("bob", "/parent/789/child/012", "POST"))
110+
end)
111+
97112
it("priority test", function ()
98113
local model = path .. "/examples/priority_model.conf"
99114
local policy = path .. "/examples/priority_policy.csv"

0 commit comments

Comments
 (0)