• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

APItools/sandbox.lua: A lua sandbox for executing non-trusted code

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

APItools/sandbox.lua

开源软件地址(OpenSource Url):

https://github.com/APItools/sandbox.lua

开源编程语言(OpenSource Language):

Lua 100.0%

开源软件介绍(OpenSource Introduction):

sandbox.lua

A pure-lua solution for running untrusted Lua code.

The default behavior is restricting access to "dangerous" functions in Lua, such as os.execute.

It's possible to provide extra functions via the options.env parameter.

Infinite loops are prevented via the debug library.

For now, sandbox.lua only works with Lua 5.1.x.

Usage

Require the module like this:

local sandbox = require 'sandbox'

sandbox.protect

sandbox.protect(f) (or sandbox(f)) produces a sandboxed version of f. f can be a Lua function or a string with Lua code.

A sandboxed function works as regular functions as long as they don't access any insecure features:

local sandboxed_f = sandbox(function() return 'hey' end)
local msg = sandboxed_f() -- msg is now 'hey'

Sandboxed options can not access unsafe Lua modules. (See the source code for a list)

When a sandboxed function tries to access an unsafe module, an error is produced.

local sf = sandbox.protect(function()
  os.execute('rm -rf /') -- this will throw an error, no damage done
end)

sf() -- error: os.execute not found

Sandboxed functions will eventually throw an error if they contain infinite loops:

local sf = sandbox.protect(function()
  while true do end
end)

sf() -- error: quota exceeded

options.quota

sandbox.lua prevents infinite loops from halting the program by hooking the debug library to the sandboxed function, and "counting instructions". When the instructions reach a certain limit, an error is produced.

This limit can be tweaked via the quota option. But default, it is 500000.

It is not possible to exhaust the machine with infinite loops; the following will throw an error after invoking 500000 instructions:

sandbox.run('while true do end') -- raise errors after 500000 instructions
sandbox.run('while true do end', {quota=10000}) -- raise error after 10000 instructions

Note that if the quota is low enough, sandboxed functions that do lots of calculations might fail:

local f = function()
  local count = 1
  for i=1, 400 do count = count + 1 end
  return count
end

sandbox.run(f, {quota=100}) -- raises error before the function ends

options.env

Use the env option to inject additional variables to the environment in which the sandboxed function is executed.

local msg = sandbox.run('return foo', {env = {foo = 'This is a global var on the the environment'}})

Note that the env variable will be modified by the sandbox (adding base modules like string). The sandboxed code can also modify it. It is recommended to discard it after use.

local env = {amount = 1}
sandbox.run('amount = amount + 1', {env = env})
assert(env.amount == 2)

sandbox.run

sandbox.run(f) sanboxes and executes f in a single line. f can be either a string or a function

You can pass options param, and it will work like in sandbox.protect. Any extra parameters will just be passed to the sandboxed function when executed.

In other words, sandbox.run(f, o, ...) is equivalent to sandbox.protect(f,o)(...).

Notice that if f throws an error, it is NOT captured by sandbox.run. Use pcall if you want your app to be immune to errors, like this:

local ok, result = pcall(sandbox.run, 'error("this just throws an error")')

Installation

Just copy sandbox.lua wherever you need it.

License

This library is released under the MIT license. See MIT-LICENSE.txt for details

Specs

This project uses telescope for its specs. In order to run them, install it and then:

cd /path/to/where/the/spec/folder/is
tsc spec/*

I would love to use busted, but it has some incompatibility with debug.sethook(f, "", quota) and the tests just hanged up.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap