Skip to content

Add Lua SWIG bindings#91

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/add-swig-support-for-lua
Draft

Add Lua SWIG bindings#91
Copilot wants to merge 3 commits into
mainfrom
copilot/add-swig-support-for-lua

Conversation

Copilot AI commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

Adds Lua SWIG bindings for libcoraza, enabling Kong/OpenResty integrators to build a connector, following the same patterns as the Python and Java connectors.

coraza.i

  • %typemap(argout) char **er: Add #elif defined(SWIGLUA) branch — SWIG Lua has no SWIG_Error; use lua_pushstring + SWIG_fail instead.
  • #ifdef SWIGLUA block:
    • Byte-buffer typemap: collapses (const unsigned char *data, int length) → single Lua string via luaL_checklstring.
    • Callback trampolines: _swig_lua_error_trampoline / _swig_lua_debug_trampoline store Lua functions in the registry and invoke them synchronously (safe because Go's CGO runtime pins the goroutine to the calling OS thread).
    • %native wrappers for coraza_set_error_callback and coraza_set_debug_log_callback — note: %ignore must not precede %native in SWIG Lua or the function is silently dropped from the module table.

examples/lua/

  • Makefile: swig -luacoraza.so; pkg-config for Lua headers; Linux/macOS aware.
  • simple_get.lua: 7 test cases — full transaction lifecycle, request body from file, WAF merge, error + debug callbacks, redirect intervention, WAF creation error, invalid input rejection.
  • README.md: Prerequisites, build/run instructions, and a Kong/OpenResty usage snippet:
local coraza = require("coraza")
local cfg = coraza.coraza_new_waf_config()
coraza.coraza_rules_add(cfg, 'SecRule ...')
coraza.coraza_set_error_callback(cfg, function(rule_handle)
    ngx.log(ngx.WARN, coraza.coraza_matched_rule_get_error_log(rule_handle))
end)
local waf = coraza.coraza_new_waf(cfg)
coraza.coraza_free_waf_config(cfg)
-- per-request
local tx = coraza.coraza_new_transaction(waf)
-- ... process connection, URI, headers ...
local it = coraza.coraza_intervention(tx)
if it ~= nil then
    local status = it.status
    coraza.coraza_free_intervention(it)
    coraza.coraza_free_transaction(tx)
    ngx.exit(status)
end

CI

  • New swig-lua job in swig.yml: ubuntu-latest + macos-latest × 2 Go versions; installs lua5.4/liblua5.4-dev (Linux) or lua via Homebrew (macOS).

Copilot AI linked an issue Apr 12, 2026 that may be closed by this pull request
Copilot AI and others added 2 commits April 12, 2026 23:53
Copilot AI changed the title [WIP] Add swig support for LUA and testable example Add Lua SWIG bindings Apr 12, 2026
Copilot AI requested a review from jptosso April 12, 2026 23:55
@Hathoute

Hathoute commented May 2, 2026

Copy link
Copy Markdown

So I'm already using this branch for APISIX, everything works.

However there's an issue with callbacks (debug and error) which SIGSEGV (happens frequently when configuring coraza using SecDebugLogLevel 9 and OWASP CRS. Not sure if this is specific to APISIX (my guess) since I've read your code and it seems correct.

@jptosso

jptosso commented May 2, 2026

Copy link
Copy Markdown
Member

There are many improvements from upstream wasm project we should apply here, I will try to bring them

@Hathoute

Hathoute commented May 2, 2026

Copy link
Copy Markdown

Maybe better in a new pull request after this one is merged (hopefully soon...)

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.

Add swig support for LUA

3 participants