From 0b2019576dfda488c1d12c09a9fc260eeeb5747e Mon Sep 17 00:00:00 2001 From: rg2011 <52279456+rg2011@users.noreply.github.com> Date: Wed, 4 Feb 2026 23:29:44 +0100 Subject: [PATCH] fix: prevent model sharing across enforcers --- src/model/Model.lua | 10 ++++---- tests/main/enforcer_spec.lua | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/model/Model.lua b/src/model/Model.lua index 424517a..4efa7d1 100644 --- a/src/model/Model.lua +++ b/src/model/Model.lua @@ -23,8 +23,8 @@ function Model:new() local o = {} setmetatable(o, self) self.__index = self - self.model = {} - self.sectionNameMap = { + o.model = {} + o.sectionNameMap = { ["r"] = "request_definition", ["p"] = "policy_definition", ["g"] = "role_definition", @@ -32,11 +32,11 @@ function Model:new() ["m"] = "matchers" } - self.requiredSections = {"r", "p", "e", "m"} -- Minimal required sections for a model to be valid - self.modCount = 0 -- used by CoreEnforcer to detect changes to Model + o.requiredSections = {"r", "p", "e", "m"} -- Minimal required sections for a model to be valid + o.modCount = 0 -- used by CoreEnforcer to detect changes to Model -- PolicyOperations: [key] = POLICY_ADD/POLICY_REMOVE and value = string(key) - self.PolicyOperations = { + o.PolicyOperations = { POLICY_ADD = "POLICY_ADD", POLICY_REMOVE = "POLICY_REMOVE" } diff --git a/tests/main/enforcer_spec.lua b/tests/main/enforcer_spec.lua index 122d226..3337ab3 100644 --- a/tests/main/enforcer_spec.lua +++ b/tests/main/enforcer_spec.lua @@ -516,6 +516,54 @@ describe("Enforcer tests", function () assert.is.False(e:enforce("bogus", "data2", "write")) -- Non-existent subject end) + it("multiple newEnforcerFromText with distinct definitions", function () + local model1 = [[ + [request_definition] + r = path, method + + [policy_definition] + p = path, method + + [policy_effect] + e = some(where (p.eft == allow)) + + [matchers] + m = r.path == p.path && r.method == p.method + ]] + + local policy1 = [[ + p, /alpha, GET + ]] + + local model2 = [[ + [request_definition] + r = user, path, method + + [policy_definition] + p = user, path, method + + [policy_effect] + e = some(where (p.eft == allow)) + + [matchers] + m = r.user == p.user && r.path == p.path && r.method == p.method + ]] + + local policy2 = [[ + p, alice, /alpha, GET + ]] + + local e2 = Enforcer:newEnforcerFromText(model2, policy2) + assert.is.True(e2:enforce("alice", "/alpha", "GET")) + + local e1 = Enforcer:newEnforcerFromText(model1, policy1) + assert.is.True(e1:enforce("/alpha", "GET")) + + local ok, res = pcall(e2.enforce, e2, "alice", "/alpha", "GET") + assert.is.True(ok) + assert.is.True(res) + end) + it("regexMatch test", function ()