Skip to content

Commit 0b20195

Browse files
committed
fix: prevent model sharing across enforcers
1 parent 01ab558 commit 0b20195

2 files changed

Lines changed: 53 additions & 5 deletions

File tree

src/model/Model.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ function Model:new()
2323
local o = {}
2424
setmetatable(o, self)
2525
self.__index = self
26-
self.model = {}
27-
self.sectionNameMap = {
26+
o.model = {}
27+
o.sectionNameMap = {
2828
["r"] = "request_definition",
2929
["p"] = "policy_definition",
3030
["g"] = "role_definition",
3131
["e"] = "policy_effect",
3232
["m"] = "matchers"
3333
}
3434

35-
self.requiredSections = {"r", "p", "e", "m"} -- Minimal required sections for a model to be valid
36-
self.modCount = 0 -- used by CoreEnforcer to detect changes to Model
35+
o.requiredSections = {"r", "p", "e", "m"} -- Minimal required sections for a model to be valid
36+
o.modCount = 0 -- used by CoreEnforcer to detect changes to Model
3737

3838
-- PolicyOperations: [key] = POLICY_ADD/POLICY_REMOVE and value = string(key)
39-
self.PolicyOperations = {
39+
o.PolicyOperations = {
4040
POLICY_ADD = "POLICY_ADD",
4141
POLICY_REMOVE = "POLICY_REMOVE"
4242
}

tests/main/enforcer_spec.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,54 @@ describe("Enforcer tests", function ()
516516
assert.is.False(e:enforce("bogus", "data2", "write")) -- Non-existent subject
517517
end)
518518

519+
it("multiple newEnforcerFromText with distinct definitions", function ()
520+
local model1 = [[
521+
[request_definition]
522+
r = path, method
523+
524+
[policy_definition]
525+
p = path, method
526+
527+
[policy_effect]
528+
e = some(where (p.eft == allow))
529+
530+
[matchers]
531+
m = r.path == p.path && r.method == p.method
532+
]]
533+
534+
local policy1 = [[
535+
p, /alpha, GET
536+
]]
537+
538+
local model2 = [[
539+
[request_definition]
540+
r = user, path, method
541+
542+
[policy_definition]
543+
p = user, path, method
544+
545+
[policy_effect]
546+
e = some(where (p.eft == allow))
547+
548+
[matchers]
549+
m = r.user == p.user && r.path == p.path && r.method == p.method
550+
]]
551+
552+
local policy2 = [[
553+
p, alice, /alpha, GET
554+
]]
555+
556+
local e2 = Enforcer:newEnforcerFromText(model2, policy2)
557+
assert.is.True(e2:enforce("alice", "/alpha", "GET"))
558+
559+
local e1 = Enforcer:newEnforcerFromText(model1, policy1)
560+
assert.is.True(e1:enforce("/alpha", "GET"))
561+
562+
local ok, res = pcall(e2.enforce, e2, "alice", "/alpha", "GET")
563+
assert.is.True(ok)
564+
assert.is.True(res)
565+
end)
566+
519567

520568
it("regexMatch test", function ()
521569

0 commit comments

Comments
 (0)