aboutsummaryrefslogtreecommitdiff
path: root/builtin/common/serialize.lua
diff options
context:
space:
mode:
authorsfan5 <sfan5@live.de>2020-03-05 22:03:04 +0100
committerGitHub <noreply@github.com>2020-03-05 22:03:04 +0100
commit8d6a0b917ce1e7f4f1017835af0ca76e79c98c38 (patch)
tree70bf61852c2f7efcb5c9620af6f60a7fb077516e /builtin/common/serialize.lua
parentef09e8a4d6671f5bfac7b6234fbe52c4b836c2be (diff)
downloadminetest-8d6a0b917ce1e7f4f1017835af0ca76e79c98c38.tar.gz
minetest-8d6a0b917ce1e7f4f1017835af0ca76e79c98c38.tar.bz2
minetest-8d6a0b917ce1e7f4f1017835af0ca76e79c98c38.zip
Fix potential security issue(s), documentation on minetest.deserialize() (#9369)
Also adds an unittest
Diffstat (limited to 'builtin/common/serialize.lua')
-rw-r--r--builtin/common/serialize.lua20
1 files changed, 13 insertions, 7 deletions
diff --git a/builtin/common/serialize.lua b/builtin/common/serialize.lua
index cf00107c2..163aa67ad 100644
--- a/builtin/common/serialize.lua
+++ b/builtin/common/serialize.lua
@@ -177,13 +177,16 @@ end
-- Deserialization
-local env = {
- loadstring = loadstring,
-}
+local function safe_loadstring(...)
+ local func, err = loadstring(...)
+ if func then
+ setfenv(func, {})
+ return func
+ end
+ return nil, err
+end
-local safe_env = {
- loadstring = function() end,
-}
+local function dummy_func() end
function core.deserialize(str, safe)
if type(str) ~= "string" then
@@ -195,7 +198,10 @@ function core.deserialize(str, safe)
end
local f, err = loadstring(str)
if not f then return nil, err end
- setfenv(f, safe and safe_env or env)
+
+ -- The environment is recreated every time so deseralized code cannot
+ -- pollute it with permanent references.
+ setfenv(f, {loadstring = safe and dummy_func or safe_loadstring})
local good, data = pcall(f)
if good then