mirror of
https://github.com/TangentFoxy/lua-sandbox.git
synced 2025-07-29 03:22:20 +00:00
Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
1660780960 | |||
e0710a284d | |||
|
e04ddbe3ae | ||
|
0108834dd3 | ||
|
ee3285e2fd | ||
|
07a01090e7 | ||
|
35714d7a92 | ||
|
fd442fd395 | ||
|
3f11f19ba3 | ||
|
26553beec7 | ||
|
e28e0bef65 |
@@ -1,4 +1,8 @@
|
||||
v1.0.0 (2021-01)
|
||||
# v1.0.1 (2021-01)
|
||||
|
||||
- Fix a bug in which the base environment wasn't overrideable with `false`
|
||||
|
||||
# v1.0.0 (2021-01)
|
||||
|
||||
- Added support for all major versions of PUC Rio Lua and LuaJIT
|
||||
- Only Lua strings are admitted now, "naked Lua" functions are not permitted any more
|
||||
@@ -8,6 +12,6 @@ v1.0.0 (2021-01)
|
||||
- Environments can have metatables with indexes, and they are respected
|
||||
- Environments can override the base environment
|
||||
|
||||
v0.5.0 (2013)
|
||||
# v0.5.0 (2013)
|
||||
|
||||
Initial version
|
||||
|
13
README.md
13
README.md
@@ -140,30 +140,39 @@ If you want to turn off the quota completely, pass `quota=false` instead.
|
||||
|
||||
Use the `env` option to inject additional variables to the environment in which the sandboxed code is executed.
|
||||
|
||||
```lua
|
||||
local msg = sandbox.run('return foo', {env = {foo = 'This is a global var on the the environment'}})
|
||||
```
|
||||
|
||||
The `env` variable will be used as an "index" by the sandbox environment, but it will *not* be modified at all (changes
|
||||
to the environment are thus lost). The only way to "get information out" from the sandboxed environments are:
|
||||
|
||||
Through side effects, like writing to a database. You will have to provide the side-effects functions in `env`:
|
||||
|
||||
```lua
|
||||
local val = 1
|
||||
local env = { write_db = function(new_val) val = new_val end }
|
||||
sandbox.run('write_db(2)')
|
||||
sandbox.run('write_db(2)', { env = env })
|
||||
assert(val = 2)
|
||||
```
|
||||
|
||||
Through returned values:
|
||||
|
||||
```lua
|
||||
local env = { amount = 1 }
|
||||
local result = sandbox.run('return amount + 1', { env = env })
|
||||
assert(result = 2)
|
||||
|
||||
```
|
||||
|
||||
Installation
|
||||
============
|
||||
|
||||
Just copy sandbox.lua wherever you need it.
|
||||
|
||||
Alternatively, you can use luarocks:
|
||||
|
||||
luarocks install kikito/sandbox
|
||||
|
||||
License
|
||||
=======
|
||||
|
||||
|
@@ -1,15 +1,15 @@
|
||||
package = "sandbox.lua"
|
||||
package = "sandbox"
|
||||
|
||||
version = "0.0.1-0"
|
||||
version = "1.0.1-4"
|
||||
|
||||
source = {
|
||||
url = "git://github.com/kikito/sandbox.lua.git",
|
||||
tag = "0.0.1"
|
||||
url = "git+https://github.com/kikito/lua-sandbox",
|
||||
tag = "v1.0.1"
|
||||
}
|
||||
|
||||
description = {
|
||||
summary = "A pure-lua solution for running untrusted Lua code.",
|
||||
homepage = "https://github.com/kikito/sandbox.lua",
|
||||
homepage = "https://github.com/kikito/lua-sandbox",
|
||||
}
|
||||
|
||||
dependencies = {
|
@@ -134,7 +134,11 @@ function sandbox.protect(code, options)
|
||||
local env = {}
|
||||
for k, v in pairs(BASE_ENV) do
|
||||
local pv = passed_env[k]
|
||||
env[k] = pv ~= nil and pv or v
|
||||
if pv ~= nil then
|
||||
env[k] = pv
|
||||
else
|
||||
env[k] = v
|
||||
end
|
||||
end
|
||||
setmetatable(env, { __index = options.env })
|
||||
env._G = env
|
||||
|
@@ -135,6 +135,11 @@ describe('sandbox.run', function()
|
||||
local env = { tostring = function(x) return "hello " .. x end }
|
||||
assert.equal("hello peter", sandbox.run("return tostring('peter')", { env = env }))
|
||||
end)
|
||||
|
||||
it('can override the base env with false', function()
|
||||
local env = { tostring = false }
|
||||
assert.equal(false, sandbox.run("return tostring", { env = env }))
|
||||
end)
|
||||
end)
|
||||
|
||||
end)
|
||||
|
Reference in New Issue
Block a user