First of all, execute untrusted code only in an isolated environment - as was said by other posters. With the exception of loading bytecode blocks, Lua allows you to cover all other sandbox issues. (And problems with bytecode blocks are fixed quickly, as detected.)
See Lua Live Demo for an example of a sandbox. Sources are available here .
Your specific problem with metatables is solved by setting the __metatable field:
If you set the __metatable field in a meta, getmetatable will return the value of this field, while setmetatable will setmetatable error.
- Roberto Ierusalimschy, Programming in Lua 1st Edition, 13.3 - Methods defined by the library
For example:
> mt = { __metatable = true } > t = {} > setmetatable(t, mt) > setmetatable(t, mt) stdin:1: cannot change a protected metatable stack traceback: [C]: in function 'setmetatable' stdin:1: in main chunk [C]: ?
So all you have to do is:
getmetatable("").__metatable = true
Alexander Gladysh
source share