client: Sandbox works

This commit is contained in:
Perttu Ahola 2014-09-19 03:07:37 +03:00
parent 7fb086e399
commit 7557a1a318

View File

@ -5,6 +5,9 @@ function buildat:Logger(module)
function logger:info(text)
print(os.date("%b %d %H:%M:%S "..module..": "..text))
end
function logger:error(text)
print(os.date("%b %d %H:%M:%S "..module.." ERROR: "..text))
end
return logger
end
@ -36,17 +39,30 @@ function buildat:send_packet(name, data)
__buildat_send_packet(name, data)
end
function buildat:run_script_file(name)
local content = __buildat_get_file_content(name)
if not content then
return false
end
log:info("buildat:run_script_file("..name.."): #content="..#content)
local script, err = loadstring(content)
if not script then
log:error("Failed to load script: "+err)
return false
end
script()
local sandbox_env = {
buildat = buildat,
}
local function run_in_sandbox(untrusted_code)
if untrusted_code:byte(1) == 27 then return nil, "binary bytecode prohibited" end
local untrusted_function, message = loadstring(untrusted_code)
if not untrusted_function then return nil, message end
setfenv(untrusted_function, sandbox_env)
return pcall(untrusted_function)
end
function buildat:run_script_file(name)
local code = __buildat_get_file_content(name)
if not code then
log:error("Failed to load script file: "+name)
return false
end
log:info("buildat:run_script_file("..name.."): #code="..#code)
local status, err = run_in_sandbox(code)
--local status, err = run_in_sandbox([[buildat:Logger("foo"):info("Pihvi")]])
if status == false then
log:error("Failed to run script: "..err)
return false
end
return true
end