Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setting sandbox on Lua instance without strlib #1156

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions VM/src/linit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ void luaL_sandbox(lua_State* L)

// set all builtin metatables to read-only
lua_pushliteral(L, "");
lua_getmetatable(L, -1);
lua_setreadonly(L, -1, true);
lua_pop(L, 2);
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);
Expand Down
11 changes: 11 additions & 0 deletions tests/Conformance.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading