From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 56AEC2256C for ; Wed, 4 Sep 2019 17:41:21 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id weGKs6i4t16Y for ; Wed, 4 Sep 2019 17:41:21 -0400 (EDT) Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 1656C21B00 for ; Wed, 4 Sep 2019 17:41:21 -0400 (EDT) From: Vladislav Shpilevoy Subject: [tarantool-patches] [PATCH 4/4] app: allow to raise an error on too nested tables Date: Wed, 4 Sep 2019 23:44:47 +0200 Message-Id: <1aa97faf9668394a853a46c9fa8ff202b03ac669.1567633062.git.v.shpilevoy@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-Help: List-Unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-Subscribe: List-Owner: List-post: List-Archive: To: tarantool-patches@freelists.org Cc: alexander.turenko@tarantool.org Closes #4434 Follow-up #4366 @TarantoolBot document Title: json/msgpack.cfg.encode_crop_too_deep option Tarantool has several so called serializers to convert data between Lua and another format: YAML, JSON, msgpack. YAML is a crazy serializer without depth restrictions. But for JSON and msgpack a user could set encode_max_depth option. That option led to crop of a table when it had too many nested levels. Sometimes such behaviour is undesirable. Now a user can choose to raise an error instead of data corruption: msgpack.cfg({encode_crop_too_deep = false}) t = nil for i = 1, 100 do t = {t} end msgpack.encode(t) -- Here an exception is thrown. Option encode_crop_too_deep works for JSON and msgpack modules, and is true by default, to keep backward compatibility. --- src/lua/msgpack.c | 4 ++++ src/lua/msgpackffi.lua | 3 +++ src/lua/utils.c | 1 + src/lua/utils.h | 6 ++++++ test/app-tap/lua/serializer_test.lua | 20 +++++++++++++++++++- test/app-tap/msgpackffi.test.lua | 6 +++++- third_party/lua-cjson/lua_cjson.c | 10 ++++++++-- 7 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/lua/msgpack.c b/src/lua/msgpack.c index c2be0b3e8..b1354776d 100644 --- a/src/lua/msgpack.c +++ b/src/lua/msgpack.c @@ -143,6 +143,8 @@ restart: /* used by MP_EXT */ case MP_MAP: /* Map */ if (level >= cfg->encode_max_depth) { + if (! cfg->encode_crop_too_deep) + return luaL_error(L, "Too high nest level"); mpstream_encode_nil(stream); /* Limit nested maps */ return MP_NIL; } @@ -164,6 +166,8 @@ restart: /* used by MP_EXT */ case MP_ARRAY: /* Array */ if (level >= cfg->encode_max_depth) { + if (! cfg->encode_crop_too_deep) + return luaL_error(L, "Too high nest level"); mpstream_encode_nil(stream); /* Limit nested arrays */ return MP_NIL; } diff --git a/src/lua/msgpackffi.lua b/src/lua/msgpackffi.lua index 65c57aa6e..73d0d6fe2 100644 --- a/src/lua/msgpackffi.lua +++ b/src/lua/msgpackffi.lua @@ -219,6 +219,9 @@ local function encode_r(buf, obj, level) encode_str(buf, obj) elseif type(obj) == "table" then if level >= msgpack.cfg.encode_max_depth then + if not msgpack.cfg.encode_crop_too_deep then + error('Too high nest level') + end encode_nil(buf) return end diff --git a/src/lua/utils.c b/src/lua/utils.c index ebbe208b4..ddc5f933a 100644 --- a/src/lua/utils.c +++ b/src/lua/utils.c @@ -237,6 +237,7 @@ static struct { OPTION(LUA_TNUMBER, encode_sparse_ratio, 2), OPTION(LUA_TNUMBER, encode_sparse_safe, 10), OPTION(LUA_TNUMBER, encode_max_depth, 32), + OPTION(LUA_TBOOLEAN, encode_crop_too_deep, 1), OPTION(LUA_TBOOLEAN, encode_invalid_numbers, 1), OPTION(LUA_TNUMBER, encode_number_precision, 14), OPTION(LUA_TBOOLEAN, encode_load_metatables, 1), diff --git a/src/lua/utils.h b/src/lua/utils.h index e802b8c4c..00a5d16cf 100644 --- a/src/lua/utils.h +++ b/src/lua/utils.h @@ -212,6 +212,12 @@ struct luaL_serializer { int encode_sparse_safe; /** Max recursion depth for encoding (MsgPack, CJSON only) */ int encode_max_depth; + /** + * A flag whether a table with too high nest level should + * be cropped. If not set, than this is considered an + * error. + */ + int encode_crop_too_deep; /** Enables encoding of NaN and Inf numbers */ int encode_invalid_numbers; /** Floating point numbers precision (YAML, CJSON only) */ diff --git a/test/app-tap/lua/serializer_test.lua b/test/app-tap/lua/serializer_test.lua index a44247d70..4fa2924cf 100644 --- a/test/app-tap/lua/serializer_test.lua +++ b/test/app-tap/lua/serializer_test.lua @@ -403,7 +403,7 @@ local function test_ucdata(test, s) end local function test_depth(test, s) - test:plan(1) + test:plan(3) -- -- gh-4434: serializer update should be reflected in Lua. -- @@ -412,6 +412,24 @@ local function test_depth(test, s) test:is(s.cfg.encode_max_depth, max_depth + 5, "cfg({ = value}) is reflected in cfg.") s.cfg({encode_max_depth = max_depth}) + + -- + -- gh-4434 (yes, the same issue): let users choose whether + -- they want to raise an error on tables with too high nest + -- level. + -- + s.cfg({encode_crop_too_deep = false}) + + local t = nil + for i = 1, max_depth + 1 do t = {t} end + local ok, err = pcall(s.encode, t) + test:ok(not ok, "too deep encode depth") + + s.cfg({encode_max_depth = max_depth + 1}) + ok, err = pcall(s.encode, t) + test:ok(ok, "no throw in a corner case") + + s.cfg({encode_crop_too_deep = true, encode_max_depth = max_depth}) end return { diff --git a/test/app-tap/msgpackffi.test.lua b/test/app-tap/msgpackffi.test.lua index e26247625..4417e92bd 100755 --- a/test/app-tap/msgpackffi.test.lua +++ b/test/app-tap/msgpackffi.test.lua @@ -36,7 +36,7 @@ local function test_offsets(test, s) end local function test_other(test, s) - test:plan(21) + test:plan(22) local buf = string.char(0x93, 0x6e, 0xcb, 0x42, 0x2b, 0xed, 0x30, 0x47, 0x6f, 0xff, 0xff, 0xac, 0x77, 0x6b, 0x61, 0x71, 0x66, 0x7a, 0x73, 0x7a, 0x75, 0x71, 0x71, 0x78) @@ -91,6 +91,10 @@ local function test_other(test, s) result_depth = check_depth(max_depth + 5) test:is(result_depth, max_depth + 5, "and uses it dynamically") + msgpack.cfg({encode_crop_too_deep = false}) + local ok = pcall(check_depth, max_depth + 6) + test:ok(not ok, "exception is thrown when crop is not allowed") + msgpack.cfg({encode_max_depth = max_depth}) end diff --git a/third_party/lua-cjson/lua_cjson.c b/third_party/lua-cjson/lua_cjson.c index 6b03e391a..0c325d03c 100644 --- a/third_party/lua-cjson/lua_cjson.c +++ b/third_party/lua-cjson/lua_cjson.c @@ -400,14 +400,20 @@ static void json_append_data(lua_State *l, struct luaL_serializer *cfg, json_append_nil(cfg, json); break; case MP_MAP: - if (current_depth >= cfg->encode_max_depth) + if (current_depth >= cfg->encode_max_depth) { + if (! cfg->encode_crop_too_deep) + luaL_error(l, "Too high nest level"); return json_append_nil(cfg, json); /* Limit nested maps */ + } json_append_object(l, cfg, current_depth + 1, json); return; case MP_ARRAY: /* Array */ - if (current_depth >= cfg->encode_max_depth) + if (current_depth >= cfg->encode_max_depth) { + if (! cfg->encode_crop_too_deep) + luaL_error(l, "Too high nest level"); return json_append_nil(cfg, json); /* Limit nested arrays */ + } json_append_array(l, cfg, current_depth + 1, json, field.size); return; case MP_EXT: -- 2.20.1 (Apple Git-117)