Fix setting sandbox on Lua instance without strlib

This commit is contained in:
Alex Orlenko 2024-01-29 17:46:17 +00:00
parent 9c588be16d
commit 0e705fe1e3
No known key found for this signature in database
GPG key ID: 4C150C250863B96D
2 changed files with 18 additions and 3 deletions

View file

@ -43,9 +43,13 @@ void luaL_sandbox(lua_State* L)
// set all builtin metatables to read-only
lua_pushliteral(L, "");
lua_getmetatable(L, -1);
if (lua_getmetatable(L, -1))
{
lua_setreadonly(L, -1, true);
lua_pop(L, 2);
}
else
lua_pop(L, 1);
// set globals to readonly and activate safeenv since the env is immutable
lua_setreadonly(L, LUA_GLOBALSINDEX, true);

View file

@ -884,6 +884,17 @@ TEST_CASE("NewUserdataOverflow")
CHECK(strcmp(lua_tostring(L, -1), "memory allocation error: block too big") == 0);
}
TEST_CASE("SandboxWithoutLibs")
{
StateRef globalState(luaL_newstate(), lua_close);
lua_State* L = globalState.get();
luaopen_base(L); // Load only base library
luaL_sandbox(L);
CHECK(lua_getreadonly(L, LUA_GLOBALSINDEX));
}
TEST_CASE("ApiTables")
{
StateRef globalState(luaL_newstate(), lua_close);