diff --git a/apisix/plugins/elasticsearch-logger.lua b/apisix/plugins/elasticsearch-logger.lua index a53f3c051a34..e03d75c72098 100644 --- a/apisix/plugins/elasticsearch-logger.lua +++ b/apisix/plugins/elasticsearch-logger.lua @@ -24,6 +24,8 @@ local ngx_re = ngx.re local str_format = core.string.format local math_random = math.random local os_date = os.date +local pcall = pcall +local type = type local pairs = pairs local plugin_name = "elasticsearch-logger" @@ -204,8 +206,12 @@ end local function replace_time(m) local time_format = m[1] - local time = os_date(time_format) - if not time then + -- os.date returns a *table* (not a string) for the "*t"/"!*t" formats, and + -- on non-LuaJIT runtimes can raise on a bad strftime escape. Either way the + -- result would be stringified into a garbage index name, so require a + -- string and fall back to an empty replacement otherwise. + local ok, time = pcall(os_date, time_format) + if not ok or type(time) ~= "string" then core.log.error("failed to parse time format: ", time_format) return "" end diff --git a/t/plugin/elasticsearch-logger2.t b/t/plugin/elasticsearch-logger2.t index e28cabe1dd3f..108176a8e401 100644 --- a/t/plugin/elasticsearch-logger2.t +++ b/t/plugin/elasticsearch-logger2.t @@ -464,3 +464,27 @@ qr/body: \{"index":\{"_index":"services-myservice-\d\d\d\d\.\d\d\.\d\d"\}\}/ --- no_error_log failed to parse time format --- timeout: 5 + + + +=== TEST 10: non-string time format (os.date "*t" returns a table) falls back +--- config + location /t { + content_by_lua_block { + local plugin = require("apisix.plugins.elasticsearch-logger") + -- "*t"/"!*t" make os.date return a table instead of a string; + -- replace_time must reject it rather than stringify a garbage index. + for _, fmt in ipairs({"*t", "!*t"}) do + local new = plugin._resolve_index_vars("prefix{" .. fmt .. "}suffix") + if new ~= "prefixsuffix" then + ngx.say("error: " .. new) + return + end + end + ngx.say("ok") + } + } +--- response_body +ok +--- error_log +failed to parse time format