mirror of
https://github.com/kikito/lua-sandbox.git
synced 2024-12-18 03:04:20 +00:00
made sandbox immune to while trues
This commit is contained in:
parent
95069bd456
commit
ea90a3c971
25
sandbox.lua
25
sandbox.lua
@ -48,15 +48,36 @@ end)
|
||||
|
||||
local string_rep = string.rep
|
||||
|
||||
local function cleanup()
|
||||
debug.sethook()
|
||||
string.rep = string_rep
|
||||
end
|
||||
|
||||
local function run(f, options)
|
||||
if type(f) == 'string' then f = loadstring(f) end
|
||||
|
||||
setfenv(f, BASE_ENV)
|
||||
|
||||
-- I would love to be able to make step greater than 1
|
||||
-- (say, 500000) but any value > 1 seems to choke with a simple while true do end
|
||||
-- After ~100 iterations, they stop calling timeout. So I need to use step = 1 and
|
||||
-- count the steps separatedly
|
||||
local step = 1
|
||||
local count = 0
|
||||
local timeout = function(str)
|
||||
count = count + step
|
||||
if count >= 500000 then
|
||||
cleanup()
|
||||
error('Timeout')
|
||||
end
|
||||
end
|
||||
debug.sethook(timeout, "", step)
|
||||
|
||||
string.rep = nil
|
||||
|
||||
setfenv(f, BASE_ENV)
|
||||
local ok, result = pcall(f)
|
||||
|
||||
string.rep = string_rep
|
||||
cleanup()
|
||||
|
||||
if not ok then error(result) end
|
||||
|
||||
|
@ -38,6 +38,10 @@ describe('sandbox', function()
|
||||
assert.equal('hellohello', string.rep('hello', 2))
|
||||
end)
|
||||
|
||||
it('#focus throws an error with infinite loops', function()
|
||||
assert.has_error(function() sandbox("while true do end") end)
|
||||
end)
|
||||
|
||||
|
||||
|
||||
end)
|
||||
|
Loading…
Reference in New Issue
Block a user