Skip to content

[7.2] Fix lua-enable-insecure-api default value cannot be changed to yes (#3548) - #4199

Merged
sarthakaggarwal97 merged 1 commit into
valkey-io:7.2from
dubey02-valkey:backport-3548-7.2
Jul 17, 2026
Merged

[7.2] Fix lua-enable-insecure-api default value cannot be changed to yes (#3548)#4199
sarthakaggarwal97 merged 1 commit into
valkey-io:7.2from
dubey02-valkey:backport-3548-7.2

Conversation

@dubey02

@dubey02 dubey02 commented Jul 17, 2026

Copy link
Copy Markdown

Backport of #3548 to 7.2. Same adaptation as the 9.0 (#4182), 8.1 (#4183),
and 8.0 (#4188) backports.

Adaptation for 7.2: only the second fix from the original PR (the
lua_insecure_api_current sync in server.c) applies here. The first fix does
not exist on 7.2: the Lua engine is not modularized on this branch (Lua
scripting lives in src/script_lua.c / src/eval.c; there is no engine_lua.c).
The deprecated-API allowlist gate reads server.lua_enable_insecure_api
directly in luaNewIndexAllowList (src/script_lua.c), and scriptingInit()
runs in initServer() after loadServerConfig(), so the config-file value is
already honored at Lua state initialization. The engine_lua.c hunk is
therefore dropped. Note: on this branch updateLuaEnableInsecureApi() calls
scriptingReset() rather than evalReset() — behavior is equivalent for
this fix.

Validation on 7.2:

  • Full unit/scripting suite passes with the fix
  • Both new tests fail without the fix with the expected symptom: getfenv()
    remains accessible after CONFIG SET lua-enable-insecure-api no

…alkey-io#3548)

The default value of lua-enable-insecure-api cannot be safely changed
from no to yes due to two issues:

1. In createEngineContext(), lua_enable_insecure_api was hardcoded to 0
   before initializing Lua states, so deprecated APIs (newproxy, setfenv,
   getfenv) were never registered in the global table regardless of the
   actual config value. Once the global table is locked, the config
   change has no effect.

2. lua_insecure_api_current was initialized to 0 (struct zero-init) and
   never synced with the final config value. If the default was changed
   to yes(1), a subsequent CONFIG SET no would see both values as 0 and
   skip the evalReset() call in updateLuaEnableInsecureApi().

Fix by reading the real config via isLuaInsecureAPIEnabled() in
createEngineContext() before Lua state initialization, and syncing
lua_insecure_api_current after all config sources (default, config file,
command-line args) are applied.

7.2 backport note: only fix 2 (the lua_insecure_api_current sync in
server.c) applies to this branch. Bug 1 does not exist on 7.2: there is
no per-engine-context lua_enable_insecure_api field hardcoded to 0 --
the Lua engine has not been modularized here (Lua scripting lives in
src/script_lua.c / src/eval.c; there is no engine_lua.c). The deprecated-API allowlist gate reads
server.lua_enable_insecure_api directly (src/script_lua.c
luaNewIndexAllowList), and scriptingInit() runs in initServer() after
loadServerConfig(), so the config-file value is already honored at Lua
state initialization. The engine_lua.c hunk is therefore dropped.
Validated: the two new tests fail on 7.2 without the server.c sync and
pass with it.

Signed-off-by: Binbin <binloveplay1314@qq.com>
@coderabbitai

coderabbitai Bot commented Jul 17, 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

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 78b3adf8-2451-4b96-b96b-119c4b9d7b62

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

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

Comment @coderabbitai help to get the list of available commands.

@valkey-review-bot valkey-review-bot Bot 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.

I found one startup edge case in the new lua_insecure_api_current synchronization point: changes made from a module's OnLoad can still leave the Lua state stale.

Comment thread src/server.c
* config sources (default, config file, command-line args) have been
* applied, so that updateLuaEnableInsecureApi() can correctly detect
* subsequent changes via CONFIG SET. */
server.lua_insecure_api_current = server.lua_enable_insecure_api;

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.

lua_insecure_api_current needs to be initialized before initServer()/scriptingInit(1), not after moduleLoadFromQueue(). scriptingInit(1) already ran in src/server.c:2740, and startup modules are allowed to execute commands from OnLoad (src/module.c:12179-12181; tests/modules/basics.c:948-953). If the server starts with lua-enable-insecure-api yes and a loadmodule’s OnLoad does CONFIG SET lua-enable-insecure-api no (possible when enable-protected-configs yes), updateLuaEnableInsecureApi() sees _current == _enable_insecure_api == 0 and skips scriptingReset() (src/config.c:2580-2585), so the deprecated APIs stay enabled while this line records the config as no. Initialize _current right after loadServerConfig() (src/server.c:7265) and before initServer() (src/server.c:7316) so later startup-time CONFIG SETs are compared against the real startup value.

@sarthakaggarwal97 sarthakaggarwal97 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.

LGTM!

@sarthakaggarwal97
sarthakaggarwal97 merged commit 3d35026 into valkey-io:7.2 Jul 17, 2026
99 of 100 checks passed
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.

3 participants