mirror of
https://github.com/kikito/lua-sandbox.git
synced 2024-12-18 03:04:20 +00:00
do not persist changes to the environment from sandbox to sandbox
This commit is contained in:
parent
c1e5b44938
commit
36be73e3a9
14
sandbox.lua
14
sandbox.lua
@ -1,5 +1,4 @@
|
|||||||
local BASE_ENV = {}
|
local BASE_ENV = {}
|
||||||
|
|
||||||
-- Non-safe :
|
-- Non-safe :
|
||||||
-- string.rep: can be used to allocate millions of bytes in 1 operation
|
-- string.rep: can be used to allocate millions of bytes in 1 operation
|
||||||
-- {set|get}metatable: can be used to modify the metatable of global objects (strings, integers)
|
-- {set|get}metatable: can be used to modify the metatable of global objects (strings, integers)
|
||||||
@ -48,6 +47,15 @@ end)
|
|||||||
|
|
||||||
local string_rep = string.rep
|
local string_rep = string.rep
|
||||||
|
|
||||||
|
local function copy(other)
|
||||||
|
if type(other) ~= 'table' then return other end
|
||||||
|
local c = {}
|
||||||
|
for k,v in pairs(other) do
|
||||||
|
c[copy(k)] = copy(v)
|
||||||
|
end
|
||||||
|
return c
|
||||||
|
end
|
||||||
|
|
||||||
local function cleanup()
|
local function cleanup()
|
||||||
debug.sethook()
|
debug.sethook()
|
||||||
string.rep = string_rep
|
string.rep = string_rep
|
||||||
@ -60,7 +68,9 @@ local function run(f, options)
|
|||||||
|
|
||||||
local limit = options.limit or 500000
|
local limit = options.limit or 500000
|
||||||
|
|
||||||
setfenv(f, BASE_ENV)
|
local env = copy(BASE_ENV)
|
||||||
|
|
||||||
|
setfenv(f, env)
|
||||||
|
|
||||||
-- I would love to be able to make step greater than 1
|
-- 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
|
-- (say, 500000) but any value > 1 seems to choke with a simple while true do end
|
||||||
|
@ -38,6 +38,11 @@ describe('sandbox', function()
|
|||||||
assert.equal('hellohello', string.rep('hello', 2))
|
assert.equal('hellohello', string.rep('hello', 2))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('should not persist modifying the packages', function()
|
||||||
|
sandbox("string.foo = 1")
|
||||||
|
assert.is_nil(sandbox("return string.foo"))
|
||||||
|
end)
|
||||||
|
|
||||||
|
|
||||||
describe('when handling infinite loops', function()
|
describe('when handling infinite loops', function()
|
||||||
|
|
||||||
@ -54,6 +59,9 @@ describe('sandbox', function()
|
|||||||
assert.no_has_error(function() sandbox("for i=1,100 do end") end)
|
assert.no_has_error(function() sandbox("for i=1,100 do end") end)
|
||||||
assert.has_error(function() sandbox("for i=1,100 do end", {limit = 50}) end)
|
assert.has_error(function() sandbox("for i=1,100 do end", {limit = 50}) end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user