lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Andrew> setfenv doesn't exist in 5.2+; in current versions you use the
Andrew> environment parameter to load() instead.

I tried the "sandbox enviroment" method you propopsed. After some googling, I tried the following code (with call to "PushSandboxEnvironmentTable" inserted between the "luaL_loadbuffer" and the "lua_pcall" to set a local environment with only function "print" available.

But at execution with this code, lua is complaining that "print" does not exist.

Any idea of what is wrong with this approach ?

Regards,

Brice


static void PushSandboxEnvironmentTableEntry(lua_State* L , char* key , char* value) {
    lua_pushstring(L, key);
    lua_pushstring(L, value);
    lua_settable(L, -3);
}

static int PushSandboxEnvironmentTable(lua_State* L)
{
    lua_newtable(L);
    PushSandboxEnvironmentTableEntry(L, "print", "print");
    lua_setupvalue(L, -2, 1);
    return LUA_OK;
}


2018-09-03 22:05 GMT+02:00 Andrew Gierth <andrew@tao11.riddles.org.uk>:
>>>>> "Leonardo" == Leonardo Gomes <leonardo.alves.gomes@usp.br> writes:

 Leonardo> If you control the code that loads the untrusted Lua script
 Leonardo> and don't intend on modifying lua itself, I think you can
 Leonardo> achieve a "sandboxed" environment by using the setfenv
 Leonardo> function.

setfenv doesn't exist in 5.2+; in current versions you use the
environment parameter to load() instead.

--
Andrew.