Skip to content

fix: pop each entry of a Lua transformation table (v2) - #3739

Open
airween wants to merge 3 commits into
owasp-modsecurity:v2/masterfrom
airween:v2/lua-tfn-table-pop
Open

airween wants to merge 3 commits into
owasp-modsecurity:v2/masterfrom
airween:v2/lua-tfn-table-pop

Conversation

@airween

@airween airween commented Nov 2, 2026

Copy link
Copy Markdown
Member

what

  • resolve_tfns() (apache2/msc_lua.c) pops each entry of a transformation table after it has used the name, on the "none" path too.

  • Add a case to tests/regression/rule/00-script.t, with the script tests/regression/server_root/conf/tfn-long-table.lua (force-added, like the other scripts there, because the directory is in .gitignore): m.getvar("ARGS.a", t) with a 1000-entry "lowercase" table must return abc for ?a=ABC.

    On the current v2/master, without the fix, this case fails: the Apache child serving the request exits with a segmentation fault (AH00051: child pid ... exit signal Segmentation fault (11) in the error log), so no 200 comes back. With the fix, all 7 cases in 00-script.t pass.

why

references

AI disclosure

Made with help of Claude Code, Opus 5.5

resolve_tfns() pushed each entry of the transformation table given to
m.getvar() or m.getvars() with lua_rawgeti() and never popped it. A C
function only has LUA_MINSTACK free stack slots unless it calls
lua_checkstack(), so a longer table wrote past the Lua stack and
crashed the Apache child.

Pop the entry after its name has been used, on the "none" path too.

Add a regression test with a 1000-entry table.

Refs owasp-modsecurity#3737
@airween airween added the 2.x Related to ModSecurity version 2.x label Nov 2, 2026
@airween
airween requested a balanced review from Copilot November 2, 2026 13:28
@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 348f1700-4427-48f9-8a7c-7712debb103d

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the Lua transformation-table stack-growth fix and its added regression case.

No open issues 🎉

What changed in this PR

Fixes Lua stack growth in ModSecurity v2’s transformation-table handling, addressing the v2 portion of #3737.

Changes:

  • Pops each transformation entry after use, including the "none" reset path.
  • Adds a regression case using 1,000 "lowercase" transformations, checking for abc output and an HTTP 200 response.
File Description
tests/​regression/​server_root/​conf/​tfn-long-table.lua Adds a Lua script exercising a 1,000-entry transformation table.
tests/​regression/​rule/​00-script.t Checks the script’s transformed output and successful response.
apache2/​msc_lua.c Balances Lua stack pushes and pops in resolve_tfns().

Review effort: Balanced

resolve_tfns() read each table entry with luaL_checkstring(), so an
entry that isn't a string or a number, such as a table, raised a Lua
error and failed the whole script with "bad argument", and a number
was converted to a name that never resolves.

Check the type with lua_type() instead, and skip an entry that isn't
a string with a debug message, as v3 does since owasp-modsecurity#3738. Check a single
transformation name the same way; a number there now gets the
"Transformation parameter must be ..." message.

Add a regression test with a table and a number entry.

Refs owasp-modsecurity#3737
resolve_tfns() passed the stack index to lua_typename(), which takes
a type, so the message for a transformation parameter that is neither
a string nor a table named the wrong type: "userdata" for a boolean,
for example. v3 had the same bug, fixed in owasp-modsecurity#3738.

Add a regression test with a boolean parameter.

Refs owasp-modsecurity#3737
@sonarqubecloud

sonarqubecloud Bot commented Nov 2, 2026

Copy link
Copy Markdown

This branch has not been deployed

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

Labels

2.x Related to ModSecurity version 2.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants