Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions apisix/plugins/elasticsearch-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions t/plugin/elasticsearch-logger2.t
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading