
Originally Posted by
Syranide
There already is, but dangerous as it is to directly expose lua on a server, exposing DLLs would be pure madness.
Sandboxing allows you to pick and choose what functions are available. Hell, it's entirely opt-in. This stuff is safe, and if you don't think it's safe enough you can wrap functions in filters that remove abusive parameter values.
You can even wrap objects transparently whilst still imposing method-specific filters, all setup automatically.
Tada.
Code:
function wrap_object( obj)
local meta = { _F = {}, _M = {}, _R = {}}
function meta:__index( key)
if type( obj[key]) == "function" then
meta._M[key] = meta._M[key] or function (...)
local args = {...}
for i, j in ipairs( args) do
if j == self then args[i] = obj end
end
if meta._F[key] then meta._F[key]( args) end
local returns = {obj[key]( unpack( args))}
if meta._R[key] then meta._R[key]( returns) end
return unpack( returns)
end
end
return meta._M[key] or obj[key]
end
local wrapper = setmetatable({}, meta)
return wrapper
end I'll give you an example of adding a filter.
Code:
obj = wrap_object( obj)
getmetatable( obj)._F.method = function( args)
if not string.find( args[2], ".txt", 1, true) then
args[2] = args[2] .. ".txt"
end
end As a disclaimer, I'd not trust my code. It seems to work well for me, but if you want to incorporate it into something with security importance you should really test it properly yourself.
I'm feeling strangely productive, this was all written in the pat half an hour for a component in an image recognition system. I wanted to add some helper methods to a returned object, and didn't want to write to the object itself. Lua is entertaining for being capable of deluding itself.
Bookmarks