This is an automated email from the ASF dual-hosted git repository.

AlinsRan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 6fb2f0947 fix(elasticsearch-logger): guard os.date against invalid 
index template (#13542)
6fb2f0947 is described below

commit 6fb2f0947358f6dce39bd2b90724ea4ab77b7058
Author: AlinsRan <[email protected]>
AuthorDate: Mon Jun 15 10:49:53 2026 +0800

    fix(elasticsearch-logger): guard os.date against invalid index template 
(#13542)
---
 apisix/plugins/elasticsearch-logger.lua | 10 ++++++++--
 t/plugin/elasticsearch-logger2.t        | 24 ++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/apisix/plugins/elasticsearch-logger.lua 
b/apisix/plugins/elasticsearch-logger.lua
index a53f3c051..e03d75c72 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 e28cabe1d..108176a8e 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

Reply via email to