Skip to content

Fix enforce crash when matcher functions throw string errors#169

Closed
Copilot wants to merge 5 commits intomasterfrom
copilot/fix-keymatch4-error
Closed

Fix enforce crash when matcher functions throw string errors#169
Copilot wants to merge 5 commits intomasterfrom
copilot/fix-keymatch4-error

Conversation

Copy link

Copilot AI commented Feb 5, 2026

  • Understand the root cause: err.message concatenation fails when err is a plain string
  • Fix CoreEnforcer.lua line 449 to handle both string and table errors
  • Fix CoreEnforcer.lua line 494 to handle both string and table errors
  • Create test case for keyMatch4 error handling
  • Run tests to validate the fix - all 30 tests pass including new keyMatch4 test
  • Address code review feedback
  • Fix CI benchmark job failure by adding retry logic for luarocks installs
Original prompt

This section details on the original issue you should resolve

<issue_title>enforce error</issue_title>
<issue_description>there is a error when use keymatch4 in model.conf

[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub) && keyMatch4(r.obj, p.obj) && regexMatch(r.act, p.act)
local Enforcer = require("casbin")

local e = Enforcer:new("/opt/openresty/nginx/conf/lua/casbin/rbac_model.conf", "/opt/openresty/nginx/conf/lua/casbin/examples/rbac_policy.csv")

local cjson = require "cjson"
local response = {}

local sub = "alice" -- the user that wants to access a resource.
local obj = "data1" -- the resource that is going to be accessed.
local act = "read" -- the operation that the user performs on the resource.

if e:enforce(sub, obj, act) then
  response['code'] = 200
  response['result'] = true
  response['msg'] = e:GetPolicy()
  ngx.say(cjson.encode(response))
end
2023/12/12 18:42:33 [error] 208585#0: *4 lua entry thread aborted: runtime error: 
...openresty/luajit/share/lua/5.1/src/main/CoreEnforcer.lua:449: attempt to concatenate field 'message' (a nil value)
stack traceback:
coroutine 0:
        ...openresty/luajit/share/lua/5.1/src/main/CoreEnforcer.lua: in function 'enforceEx'
        ...openresty/luajit/share/lua/5.1/src/main/CoreEnforcer.lua:534: in function 'enforce'

```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@hsluoyz hsluoyz marked this pull request as ready for review February 5, 2026 02:16
Copilot AI and others added 3 commits February 5, 2026 02:16
… errors

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix error with keymatch4 in model configuration Fix enforce crash when matcher functions throw string errors Feb 5, 2026
Copilot AI requested a review from hsluoyz February 5, 2026 02:25
@hsluoyz
Copy link
Member

hsluoyz commented Feb 5, 2026

@copilot fix: https://github.com/casbin/lua-casbin/actions/runs/21696318490/job/62569433929?pr=169

Run luarocks install lualogging
  luarocks install lualogging
  luarocks install lrexlib-pcre2
  luarocks install luaposix
  luarocks install luasocket
  luarocks install busted
  luarocks install busted-htest
  shell: /usr/bin/bash -e {0}
  env:
    MSYS: winsymlinks:nativestrict
    LUA_PATH: ;;/home/runner/.luarocks/share/lua/5.1/?.lua;/home/runner/.luarocks/share/lua/5.1/?/init.lua;/home/runner/work/lua-casbin/lua-casbin/.luarocks/share/lua/5.1/?.lua;/home/runner/work/lua-casbin/lua-casbin/.luarocks/share/lua/5.1/?/init.lua
    LUA_CPATH: ;;/home/runner/.luarocks/lib/lua/5.1/?.so;/home/runner/work/lua-casbin/lua-casbin/.luarocks/lib/lua/5.1/?.so
Warning: Failed searching manifest: Failed loading manifest for [https://luarocks.org:](https://luarocks.org/) Error loading file: [string "/home/runner/.cache/luarocks/https___luarocks..."]:227122: main function has more than 65536 constants
Warning: Failed searching manifest: Failed loading manifest for https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/: Error loading file: [string "/home/runner/.cache/luarocks/https___raw.gith..."]:225673: main function has more than 65536 constants
Warning: Failed searching manifest: Failed downloading http://luafr.org/moonrocks - Failed downloading http://luafr.org/moonrocks/manifest-5.1 - /home/runner/.cache/luarocks/http___luafr.org_moonrocks/manifest-5.1
Warning: Failed searching manifest: Failed downloading http://luarocks.logiceditor.com/rocks - Failed downloading http://luarocks.logiceditor.com/rocks/manifest-5.1 - /home/runner/.cache/luarocks/http___luarocks.logiceditor.com_rocks/manifest-5.1

Error: No results matching query were found for Lua 5.1.
To check if it is available for other Lua versions, use --check-lua-versions.
Error: Process completed with exit code 1.

Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
Copy link
Author

Copilot AI commented Feb 5, 2026

@copilot fix: https://github.com/casbin/lua-casbin/actions/runs/21696318490/job/62569433929?pr=169

...

Fixed in 6550a94. The benchmark job was missing the retry logic that the test job has for handling transient luarocks manifest loading failures. Added until...do...done loops around all luarocks install commands in the benchmark job to match the test job's resilient approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

enforce error

3 participants