Lua 5.3.2
I have a service written in Lua/C, that executes lua files in the same Lua_State.
I need to provide all the standard libraries for the file execution environment.
The simplest thing is to execute files this way: loadfile(file_path, "bt", _G)
The problem is: code in the file is able to corrupt the service's global state, so this method is not secure.
So, I need to create a sandboxed environment loadfile(file_path, "bt", env)
The question: how to register all the standard libraries from linit.c in the env
variable?
I can simply register all the libs from the linit.c, except luaopen_base, because it contains lua_pushglobaltable
I thought about this:
local env = {}
for k,v in pairs(_G) do
if type(v)=="function" then
env[k] = v
end
end
But it looks like a pathetic decision. Is anyone have a better solution?