Community Curator/Source/TSWRes Sandbox

-- The Sandbox Env that Lua Script Microchips run in. local Sandbox = {} Sandbox.Env = {} local Env = Sandbox.Env

-- run code under environment [Lua 5.2] function Sandbox.Load(untrusted_code) local untrusted_function, message = load(untrusted_code, nil, 't', Sandbox.Env) if not untrusted_function then return nil, message end return true, untrusted_function end

-- Prints everything in a table (T). function printTable(T) print("PrintTable", tostring(T)) for k, v in pairs(T) do     print(k, tostring(v)) end end

-- Clones a Table. function cloneTable(Table) local Clone = {} for k, v in pairs(Table) do     if type(v) == "table" then Clone[k] = cloneTable(v) else Clone[k] = v     end end return Clone end

-- functions for if a script attempts to do something not allowed -- function NotAllowedInSandbox error("The script attempted to run a function not allowed in the LittleBigPlanet Lua Sandbox.") end

function NAISGlobEnv print("Script attempted to escape to the global Env!") error("The script attempted to run a function not allowed in the LittleBigPlanet Lua Sandbox.") end

-- NAISGlobEnv

-- Env Vars. Env.assert = assert Env.collectgarbage = NotAllowedInSandbox Env.dofile = NAISGlobEnv Env.error = error Env.print = print Env.printTable = printTable Env._G = NAISGlobEnv Env.getfenv = NAISGlobEnv Env.getmetatable = NotAllowedInSandbox Env.ipairs = ipairs Env.load = NAISGlobEnv Env.next = next Env.pairs = pairs Env.pcall = pcall Env.rawequal = NotAllowedInSandbox Env.rawget = NotAllowedInSandbox Env.rawset = NotAllowedInSandbox Env.setfenv = NAISGlobEnv Env.setmetatable = NotAllowedInSandbox Env.tonumber = tonumber Env.tostring = tostring Env.type = type Env.unpack = unpack Env._VERSION = _VERSION Env.xpcall = xpcall Env.module = NAISGlobEnv Env.require = NAISGlobEnv

-- now for the tables --

Env.coroutine = {} Env.coroutine.yield = coroutine.yield

Env.package = {} -- all of this table is unsafe

Env.string = {}

Env.table = {} Env.table.insert = table.insert Env.table.maxn = table.maxn Env.table.remove = table.remove Env.table.sort = table.sort Env.table.clone = cloneTable

Env.math = {} Env.math.huge = math.huge Env.math.rand = math.rand

Env.io = {}

Env.os = {} Env.os.clock = os.clock Env.os.difftime = os.difftime Env.os.time = os.time

Env.debug = {} -- debug.* is unsafe

Env.LBPLua = {} -- Add LittleBigPlanet Lua Functions Env.LBPLua.Ver = "LBPLua Alpha 0.12"

-- The Env is set up. -- --local RTable = cloneTable(Env) --printTable(_G) --print("Ok")

return Sandbox