123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 | local Errhand = require("error_handling")
local Text = require("text")
local SyntaxTree = require("syntax_tree")
local LuaCode, NomsuCode, Source
do
local _obj_0 = require("code_obj")
LuaCode, NomsuCode, Source = _obj_0.LuaCode, _obj_0.NomsuCode, _obj_0.Source
end
local fail_at
fail_at = require("nomsu_compiler").fail_at
local Files = require("files")
local bootstrap = require("bootstrap")
local Limits = require("nomsu_limits")
local shared_metatables = {
[getmetatable("")] = true,
[Errhand.ExitSignal] = true,
[SyntaxTree.__base] = true,
[LuaCode.__base] = true,
[NomsuCode.__base] = true,
[Source.__base] = true
}
local safe_getmetatable
safe_getmetatable = function(x)
local mt = getmetatable(x)
if shared_metatables[mt] then
return nil
end
return mt
end
local safe_setmetatable
safe_setmetatable = function(t, mt)
if type(mt) == 'table' and rawget(mt, "__gc") ~= nil then
error("A sandbox can't use the __gc metamethod.", 2)
end
return setmetatable(t, mt)
end
local read_only
read_only = function(t, call)
return setmetatable({ }, {
__index = t,
__call = call and (function(_, ...)
return call(...)
end) or nil,
__newindex = function()
return error("This can't be changed.", 2)
end,
__metatable = false
})
end
local copy
copy = function(t)
local _tbl_0 = { }
for k, v in pairs(t) do
_tbl_0[k] = v
end
return _tbl_0
end
local limited_table_library
limited_table_library = function()
local lib = copy(table)
lib.concat = Limits.checking_memory_after(table.concat)
return lib
end
local dangerous_values = { }
local _list_0 = {
"io",
"os",
"package",
"debug",
"jit"
}
for _index_0 = 1, #_list_0 do
local name = _list_0[_index_0]
do
local lib = _G[name]
if lib then
dangerous_values[lib] = true
for _, v in pairs(lib) do
dangerous_values[v] = true
end
end
end
end
local _list_1 = {
Files,
require('lpeg'),
require('re')
}
for _index_0 = 1, #_list_1 do
local lib = _list_1[_index_0]
dangerous_values[lib] = true
for _, v in pairs(lib) do
dangerous_values[v] = true
end
end
local _list_2 = {
"require",
"load",
"loadstring",
"loadfile",
"dofile",
"getfenv",
"setfenv",
"collectgarbage"
}
for _index_0 = 1, #_list_2 do
local name = _list_2[_index_0]
if _G[name] then
dangerous_values[_G[name]] = true
end
end
dangerous_values[getmetatable] = nil
dangerous_values[string] = true
local safe_value
safe_value = function(v)
if dangerous_values[v] then
return nil
end
if v == getmetatable then
return safe_getmetatable
end
if v == setmetatable then
return safe_setmetatable
end
return v
end
local safe_base_values
safe_base_values = function(environment)
local sources = environment.SOURCES
local safe_fail_at
safe_fail_at = function(source, msg)
local filename
if type(source) == 'string' then
filename = Source:from_string(source).filename
elseif Source:is_instance(source) then
filename = source.filename
elseif SyntaxTree:is_instance(source) then
filename = source.source.filename
end
if filename and rawget(sources, filename) then
return fail_at(source, tostring(msg))
end
return error(tostring(msg), 0)
end
local safe_exit
safe_exit = function(code)
if code == nil then
code = 0
end
return Errhand.exit_signal(tonumber(code) or 1)
end
return {
next = next,
select = select,
tonumber = tonumber,
tostring = tostring,
pairs = environment.pairs,
ipairs = ipairs,
error = error,
pcall = Limits.pcall,
xpcall = Limits.xpcall,
assert = assert,
rawequal = rawequal,
rawlen = rawlen,
rawget = rawget,
rawset = rawset,
setmetatable = safe_setmetatable,
getmetatable = safe_getmetatable,
unpack = unpack or table.unpack,
lua_type_of = type,
yield = coroutine.yield,
resume = Limits.resume,
coroutine_status_of = coroutine.status,
coroutine_wrap = Limits.wrap,
coroutine_from = coroutine.create,
coroutine_values = environment.coroutine_values,
math = copy(environment.math),
string = Limits.limited_string_library(),
table = limited_table_library(),
Text = read_only(Text, Limits.checking_memory_after(Text)),
_VERSION = environment._VERSION,
LUA_VERSION = environment.LUA_VERSION,
LUA_API = environment.LUA_API,
NOMSU_VERSION = copy(environment.NOMSU_VERSION),
enhance_error = Errhand.enhance_error,
IS_EXIT_SIGNAL = Errhand.is_exit_signal,
exit = safe_exit,
quit = safe_exit,
at_1_fail = safe_fail_at,
_1_as_nomsu = environment._1_as_nomsu,
_1_as_inline_nomsu = environment._1_as_inline_nomsu,
_1_as_text = environment._1_as_text,
_1_deeply_equals = environment._1_deeply_equals,
_1_fields_as_text = environment._1_fields_as_text
}
end
local internal_names = {
_ENV = true,
_G = true,
_IMPORTS = true,
COMPILE_RULES = true,
PUBLIC_COMPILE_RULES = true,
TESTS = true,
MODULE_NAME = true,
globals = true,
_rules_before_public = true,
_tests_to_compile = true,
COMPILING_TESTS = true,
WAS_RUN_DIRECTLY = true
}
local safe_core_modules = {
["core/collections"] = true,
["core/control_flow"] = true,
["core/coroutines"] = true,
["core/errors"] = true,
["core/math"] = true,
["core/operators"] = true,
["core/text"] = true,
["core/io"] = true
}
local safe_metaprogramming = {
_1_is = true,
_1_is_text = true,
_1_isn_x27_t_text = true,
_1_is_not_text = true,
type_of = true,
quote = true,
["* extra arguments *"] = true,
["->"] = true,
["1 ->"] = true,
["1 means"] = true,
["1 all mean"] = true,
["1 is not"] = true,
["1 isn ' t"] = true,
["nil"] = true,
["no"] = true,
["yes"] = true,
["return"] = true
}
local safe_bootstrap_rules = {
[""] = true,
["local"] = true,
["local 1 ="] = true,
["public"] = true,
["public 1 ="] = true,
["Lua version"] = true,
["is jit"] = true,
["use"] = true,
["export"] = true
}
local visible_fields
visible_fields = function(t)
return coroutine.wrap(function()
local seen = { }
for k, v in pairs(t) do
seen[k] = true
coroutine.yield(k, v)
end
for k, v in pairs(rawget(t, "_IMPORTS") or { }) do
if not (seen[k]) then
coroutine.yield(k, v)
end
end
end)
end
local make_sandbox
make_sandbox = function(environment, host_names)
local module_allowed
module_allowed = function(name, key)
if safe_core_modules[name] then
return true
end
if name == "core/metaprogramming" then
return safe_metaprogramming[key] or false
end
return false
end
local core_values, core_rules = { }, { }
local host_values, host_rules = { }, { }
for name, mod in pairs(environment.LOADED_MODULES) do
local _continue_0 = false
repeat
if not (type(name) == 'string' and not name:match("%.")) then
_continue_0 = true
break
end
if name:match("^core/") or name == "core" then
if name == "core" then
_continue_0 = true
break
end
for k, v in pairs(mod) do
local _continue_1 = false
repeat
if internal_names[k] then
_continue_1 = true
break
end
core_values[v] = (core_values[v] ~= false) and module_allowed(name, k)
_continue_1 = true
until true
if not _continue_1 then
break
end
end
for k, v in pairs(rawget(mod, "PUBLIC_COMPILE_RULES") or { }) do
core_rules[v] = (core_rules[v] ~= false) and module_allowed(name, k)
end
else
for k, v in pairs(mod) do
if not (internal_names[k]) then
host_values[v] = true
end
end
for k, v in pairs(rawget(mod, "PUBLIC_COMPILE_RULES") or { }) do
host_rules[v] = true
end
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
local runtime = { }
local base = safe_base_values(environment)
for k, v in visible_fields(environment) do
local _continue_0 = false
repeat
if internal_names[k] then
_continue_0 = true
break
end
if base[k] ~= nil then
runtime[k] = base[k]
elseif dangerous_values[v] then
_continue_0 = true
break
elseif core_values[v] ~= nil then
if core_values[v] then
local _exp_0 = v
if getmetatable == _exp_0 then
runtime[k] = safe_getmetatable
elseif setmetatable == _exp_0 then
runtime[k] = safe_setmetatable
else
runtime[k] = v
end
end
elseif host_names[k] or host_values[v] then
runtime[k] = v
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
runtime._ENV = runtime
runtime._G = runtime
local rules = { }
for stub, rule in visible_fields(environment.COMPILE_RULES) do
local _continue_0 = false
repeat
if stub == "_IMPORTS" then
_continue_0 = true
break
end
local allowed
if bootstrap[stub] == rule then
allowed = safe_bootstrap_rules[stub]
elseif core_rules[rule] ~= nil then
allowed = core_rules[rule]
else
allowed = host_rules[rule]
end
if allowed then
rules[stub] = rule
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
local context = environment.new_environment()
context.MODULE_NAME = "sandbox"
context.COMPILE_RULES = rules
context.RUNTIME_ENV = runtime
local sandbox_modules = { }
for name, mod in pairs(environment.LOADED_MODULES) do
local _continue_0 = false
repeat
if not (type(name) == 'string' and not name:match("%.")) then
_continue_0 = true
break
end
local is_core = name == "core" or name:match("^core/")
local values, module_rules = { }, { }
for k, v in pairs(mod) do
local _continue_1 = false
repeat
if internal_names[k] then
_continue_1 = true
break
end
if is_core and not core_values[v] then
_continue_1 = true
break
end
local sv = safe_value(v)
if sv ~= nil then
values[k] = sv
end
_continue_1 = true
until true
if not _continue_1 then
break
end
end
for k, rule in pairs(rawget(mod, "PUBLIC_COMPILE_RULES") or { }) do
local _continue_1 = false
repeat
if is_core and not core_rules[rule] then
_continue_1 = true
break
end
module_rules[k] = rule
_continue_1 = true
until true
if not _continue_1 then
break
end
end
sandbox_modules[name] = {
values = values,
rules = module_rules
}
_continue_0 = true
until true
if not _continue_0 then
break
end
end
local import_module
import_module = function(package_name)
local mod = sandbox_modules[package_name]
if not (mod) then
error(("The module '" .. tostring(package_name) .. "' isn't available in this sandbox. A sandbox ") .. "can only use modules that were loaded before it was sealed.", 0)
end
for k, v in pairs(mod.values) do
runtime[k] = v
end
for k, rule in pairs(mod.rules) do
rules[k] = rule
end
return nil
end
runtime.use = function(self, package_name)
return import_module(package_name)
end
runtime.export = runtime.use
return context
end
return {
make_sandbox = make_sandbox
}
|