Skip to content

Commit

Permalink
Load user functions in sandbox with only math module
Browse files Browse the repository at this point in the history
  • Loading branch information
semyon422 committed Feb 10, 2020
1 parent 5661389 commit d787966
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions sphere/screen/gameplay/CloudburstEngine/NoteSkin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ NoteSkin.loadImages = function(self)
end
end


local env = {
math = math
}

local safeload = function(code)
if code:byte(1) == 27 then
error("bytecode is not allowed")
end
local f, message = loadstring(code)
if not f then
error(message)
end
setfenv(f, env)
return f
end

NoteSkin.loadFunctions = function(self)
if not self.noteSkinData.functions then
return
Expand All @@ -77,9 +94,9 @@ NoteSkin.loadFunctions = function(self)
local functions0 = self.functions0
local functions1 = self.functions1
for _, fn in pairs(self.noteSkinData.functions) do
functions0[fn.name] = loadstring(fn.chunk0)()
functions0[fn.name] = safeload(fn.chunk0)()
if fn.chunk1 then
functions1[fn.name] = loadstring(fn.chunk1)()
functions1[fn.name] = safeload(fn.chunk1)()
end
end
end
Expand Down

0 comments on commit d787966

Please sign in to comment.