Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Sandboxing :Methods in a wrapper?

Asked by 8 years ago

How would I sandbox methods like :ClearAllChildren? I am currently writing a sandbox for fun.

Here is what I have so far:

local global_env = getfenv(0)

local faked_functions = {
    ClearAllChildren = (function(...)
      return error('this has been blocked!',0)
    end)
}

local function wrap(...)
    for _,v in next,{...} do
        if pcall(game.IsA,v,"") then
            local proxy = newproxy(true)
            local proxy_meta = getmetatable(proxy)

            proxy_meta.__index = (function(t,k)
                local obj_k = v[k]

                if type(obj_k) == "function" then
                    if faked_functions[obj_k] then
                        return faked_functions[obj_k]
                    else
                        return function(self,...)
                            print(self)
                            return wrap(obj_k(v,...))
                        end
                    end
                else
                    return wrap(obj_k)
                end
            end)

            proxy_meta.__newindex = (function(t,k,r)
                v[k] = r
            end)

            proxy_meta.__tostring = (function()
                return tostring(v)
            end)

            proxy_meta.__metatable = "This metatable is locked!"

            return proxy
        elseif type(v) == "function" then
            if faked_functions[v] then
                return faked_functions[v]
            else
                return v
            end
        end
    end
end

setmetatable(sandbox,{ --hax
    __index = (function(t,i)
        return wrap(global_env[i]) -- lele
    end),
    __metatable = "This metatable is locked!"
})

return function()
    setfenv(2,sandbox)
    setfenv(1,sandbox)
    setfenv(0,sandbox)
end

I am very new to sandboxing if you can tell. Help would be appreciative!

0
No no no. You're only returning 1 thing per wrap (bad), and you're assuming member functions are methods (Technically wrong). https://github.com/CrescentCode/ValkyrieFramework/blob/bleeding-edge/Shared/Core/BaseLib.mod.lua User#6546 35 — 8y

Answer this question