2013-09-02 20:11:33 +00:00
|
|
|
local sandbox = require 'sandbox'
|
|
|
|
|
2013-09-05 22:40:43 +00:00
|
|
|
describe('sandbox.run', function()
|
2013-09-02 20:11:33 +00:00
|
|
|
|
2013-09-05 22:40:43 +00:00
|
|
|
describe('when handling base cases', function()
|
|
|
|
it('can run harmless functions', function()
|
|
|
|
local r = sandbox.run(function() return 'hello' end)
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.equal(r, 'hello')
|
2013-09-05 22:40:43 +00:00
|
|
|
end)
|
2013-09-02 20:11:33 +00:00
|
|
|
|
2013-09-05 22:40:43 +00:00
|
|
|
it('can run harmless strings', function()
|
|
|
|
local r = sandbox.run("return 'hello'")
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.equal(r, 'hello')
|
2013-09-05 22:40:43 +00:00
|
|
|
end)
|
2013-09-02 20:11:33 +00:00
|
|
|
|
2013-09-05 22:40:43 +00:00
|
|
|
it('has access to safe methods', function()
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.equal(10, sandbox.run("return tonumber('10')"))
|
|
|
|
assert.equal('HELLO', sandbox.run("return string.upper('hello')"))
|
|
|
|
assert.equal(1, sandbox.run("local a = {3,2,1}; table.sort(a); return a[1]"))
|
|
|
|
assert.equal(10, sandbox.run("return math.max(1,10)"))
|
2013-09-05 22:40:43 +00:00
|
|
|
end)
|
2013-09-02 20:11:33 +00:00
|
|
|
|
2013-09-05 22:40:43 +00:00
|
|
|
it('does not allow access to not-safe stuff', function()
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.error(function() sandbox.run('return setmetatable({}, {})') end)
|
|
|
|
assert.error(function() sandbox.run('return string.rep("hello", 5)') end)
|
2013-09-05 22:40:43 +00:00
|
|
|
end)
|
2013-09-02 20:11:33 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-05 22:40:43 +00:00
|
|
|
describe('when handling string.rep', function()
|
|
|
|
it('does not allow pesky string:rep', function()
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.error(function() sandbox.run('return ("hello"):rep(5)') end)
|
2013-09-05 22:40:43 +00:00
|
|
|
end)
|
2013-09-02 20:11:33 +00:00
|
|
|
|
2013-09-05 22:40:43 +00:00
|
|
|
it('restores the value of string.rep', function()
|
|
|
|
sandbox.run("")
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.equal('hellohello', string.rep('hello', 2))
|
2013-09-05 22:40:43 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('restores string.rep even if there is an error', function()
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.error(function() sandbox.run("error('foo')") end)
|
|
|
|
assert.equal('hellohello', string.rep('hello', 2))
|
2013-09-05 22:40:43 +00:00
|
|
|
end)
|
2013-09-02 20:11:33 +00:00
|
|
|
|
2013-09-05 22:40:43 +00:00
|
|
|
it('passes parameters to the function', function()
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.equal(sandbox.run(function(a,b) return a + b end, {}, 1,2), 3)
|
2013-09-05 22:40:43 +00:00
|
|
|
end)
|
2013-09-03 09:53:41 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-05 22:40:43 +00:00
|
|
|
|
|
|
|
describe('when the sandboxed function tries to modify the base environment', function()
|
|
|
|
|
|
|
|
it('does not allow modifying the modules', function()
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.error(function() sandbox.run("string.foo = 1") end)
|
|
|
|
assert.error(function() sandbox.run("string.char = 1") end)
|
2013-09-05 22:40:43 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('does not persist modifications of base functions', function()
|
|
|
|
sandbox.run('error = function() end')
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.error(function() sandbox.run("error('this should be raised')") end)
|
2013-09-05 22:40:43 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('DOES persist modification to base functions when they are provided by the base env', function()
|
|
|
|
local env = {['next'] = 'hello'}
|
|
|
|
sandbox.run('next = "bye"', {env=env})
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.equal(env['next'], 'bye')
|
2013-09-05 22:40:43 +00:00
|
|
|
end)
|
2013-09-03 14:07:28 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-03 10:53:26 +00:00
|
|
|
|
2013-09-05 22:40:43 +00:00
|
|
|
describe('when given infinite loops', function()
|
2013-09-03 11:20:38 +00:00
|
|
|
|
2013-09-03 11:14:42 +00:00
|
|
|
it('throws an error with infinite loops', function()
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.error(function() sandbox.run("while true do end") end)
|
2013-09-03 11:14:42 +00:00
|
|
|
end)
|
2013-09-02 20:11:33 +00:00
|
|
|
|
2013-09-03 11:20:38 +00:00
|
|
|
it('restores string.rep even after a while true', function()
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.error(function() sandbox.run("while true do end") end)
|
|
|
|
assert.equal('hellohello', string.rep('hello', 2))
|
2013-09-03 11:20:38 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-03 14:41:46 +00:00
|
|
|
it('accepts a quota param', function()
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.has_no.errors(function() sandbox.run("for i=1,100 do end") end)
|
|
|
|
assert.error(function() sandbox.run("for i=1,100 do end", {quota = 20}) end)
|
2013-09-03 11:14:42 +00:00
|
|
|
end)
|
2013-09-03 14:07:28 +00:00
|
|
|
|
2013-09-14 10:54:49 +00:00
|
|
|
it('does not use quotes if the quote param is false', function()
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.has_no.errors(function() sandbox.run("for i=1,1000000 do end", {quota = false}) end)
|
2013-09-14 10:54:49 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-03 11:14:42 +00:00
|
|
|
end)
|
2013-09-02 20:11:33 +00:00
|
|
|
|
2013-09-05 22:40:43 +00:00
|
|
|
|
2013-09-03 16:07:03 +00:00
|
|
|
describe('when given an env option', function()
|
2013-09-13 11:26:08 +00:00
|
|
|
it('is available on the sandboxed env as the _G variable', function()
|
|
|
|
local env = {foo = 1}
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.equal(1, sandbox.run("return foo", {env = env}))
|
|
|
|
assert.equal(env, sandbox.run("return _G", {env = env}))
|
2013-09-03 14:41:46 +00:00
|
|
|
end)
|
|
|
|
|
2013-09-03 16:07:03 +00:00
|
|
|
it('does not hide base env', function()
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.equal('HELLO', sandbox.run("return string.upper(foo)", {env = {foo = 'hello'}}))
|
2013-09-03 14:41:46 +00:00
|
|
|
end)
|
2013-09-03 16:07:03 +00:00
|
|
|
|
2013-09-05 22:40:43 +00:00
|
|
|
it('can modify the env', function()
|
2013-09-03 16:07:03 +00:00
|
|
|
local env = {foo = 1}
|
2013-09-05 22:40:43 +00:00
|
|
|
sandbox.run("foo = 2", {env = env})
|
2020-12-13 17:54:19 +00:00
|
|
|
assert.equal(env.foo, 2)
|
2013-09-03 16:07:03 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2013-09-02 20:11:33 +00:00
|
|
|
end)
|