mirror of
https://github.com/TangentFoxy/lua-sandbox.git
synced 2025-07-28 02:52:22 +00:00
feat(sandbox) explicitly drop support of quotas on LuaJIT
The solution we use in PUC Rio Lua (with debug.sethook) simply does not work in LuaJIT. * We have added a `sandbox.quota_supported` field to signal this feature (or lack of thereof) * We explicitly return an error if `options.quota` is passed on a LuaJIT environment, in order to prevent LuaJIT users from believing that they are protected against infinite loops.
This commit is contained in:
committed by
Enrique García Cota
parent
50bfa4abca
commit
485a14697c
11
sandbox.lua
11
sandbox.lua
@@ -25,9 +25,13 @@ local sandbox = {
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
]]
|
||||
]],
|
||||
|
||||
}
|
||||
|
||||
-- quotas don't work in LuaJIT since debug.sethook works differently there
|
||||
local quota_supported = type(_G.jit) == "nil"
|
||||
sandbox.quota_supported = quota_supported
|
||||
|
||||
-- PUC-Rio Lua 5.1 does not support deactivation of binary code
|
||||
local mode_supported = _ENV or type(_G.jit) == "table"
|
||||
@@ -125,6 +129,9 @@ function sandbox.protect(code, options)
|
||||
options = options or {}
|
||||
|
||||
local quota = false
|
||||
if options.quota and not quota_supported then
|
||||
error("options.quota is not supported on this environment (usually LuaJIT). Please unset options.quota")
|
||||
end
|
||||
if options.quota ~= false then
|
||||
quota = options.quota or 500000
|
||||
end
|
||||
@@ -148,7 +155,7 @@ function sandbox.protect(code, options)
|
||||
|
||||
return function(...)
|
||||
|
||||
if quota then
|
||||
if quota and quota_supported then
|
||||
local timeout = function()
|
||||
cleanup()
|
||||
error('Quota exceeded: ' .. tostring(quota))
|
||||
|
Reference in New Issue
Block a user