Skip to content
This repository was archived by the owner on Dec 11, 2020. It is now read-only.

Commit a12502d

Browse files
committed
added comments and informational items to the sandbox table
1 parent 83a5a2b commit a12502d

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

sandbox.lua

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,42 @@
1+
local sandbox = {
2+
_VERSION = "sandbox 0.5",
3+
_DESCRIPTION = "A pure-lua solution for running untrusted Lua code.",
4+
_COPYRIGHT = "Copyright (c) 2013 Enrique García Cota",
5+
_LICENSE = [[
6+
MIT LICENSE
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a
9+
copy of this software and associated documentation files (the
10+
"Software"), to deal in the Software without restriction, including
11+
without limitation the rights to use, copy, modify, merge, publish,
12+
distribute, sublicense, and/or sell copies of the Software, and to
13+
permit persons to whom the Software is furnished to do so, subject to
14+
the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included
17+
in all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26+
]]
27+
}
28+
29+
-- The base environment is merged with the given env option (or an empty table, if no env provided)
30+
--
131
local BASE_ENV = {}
2-
-- Non-safe :
32+
33+
-- List of non-safe packages/functions:
34+
--
335
-- * string.rep: can be used to allocate millions of bytes in 1 operation
436
-- * {set|get}metatable: can be used to modify the metatable of global objects (strings, integers)
537
-- * collectgarbage: can affect performance of other systems
638
-- * dofile: can access the server filesystem
7-
-- * _G: Unsafe. It can be mocked though
39+
-- * _G: It has access to everything. It could be mocked though.
840
-- * load{file|string}: All unsafe because they can grant acces to global env
941
-- * raw{get|set|equal}: Potentially unsafe
1042
-- * module|require|module: Can modify the host settings
@@ -13,6 +45,8 @@ local BASE_ENV = {}
1345
-- * math.randomseed: Can affect the host sytem
1446
-- * io.*, os.*: Most stuff there is non-save
1547

48+
49+
-- Safe packages/functions below
1650
([[
1751
1852
_VERSION assert error ipairs next pairs
@@ -58,6 +92,7 @@ end
5892
BASE_ENV[module_name] = protect_module(BASE_ENV[module_name], module_name)
5993
end)
6094

95+
-- auxiliary functions/variables
6196

6297
local string_rep = string.rep
6398

@@ -73,7 +108,8 @@ local function cleanup()
73108
string.rep = string_rep
74109
end
75110

76-
local function protect(f, options)
111+
-- Public interface: sandbox.protect
112+
function sandbox.protect(f, options)
77113
if type(f) == 'string' then f = assert(loadstring(f)) end
78114

79115
options = options or {}
@@ -101,8 +137,12 @@ local function protect(f, options)
101137
end
102138
end
103139

104-
local function run(f, options, ...)
105-
return protect(f, options)(...)
140+
-- Public interface: sandbox.run
141+
function sandbox.run(f, options, ...)
142+
return sandbox.protect(f, options)(...)
106143
end
107144

108-
return setmetatable({protect = protect, run = run}, {__call = function(_,f,o) return protect(f,o) end})
145+
-- make sandbox(f) == sandbox.protect(f)
146+
setmetatable(sandbox, {__call = function(_,f,o) return sandbox.protect(f,o) end})
147+
148+
return sandbox

0 commit comments

Comments
 (0)