Skip to content

fix(elasticsearch-logger): guard os.date against invalid index template#13542

Open
AlinsRan wants to merge 1 commit into
apache:masterfrom
AlinsRan:fix/es-logger-osdate-pcall
Open

fix(elasticsearch-logger): guard os.date against invalid index template#13542
AlinsRan wants to merge 1 commit into
apache:masterfrom
AlinsRan:fix/es-logger-osdate-pcall

Conversation

@AlinsRan

@AlinsRan AlinsRan commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

TL;DR

Dynamic Elasticsearch index names containing {*t} / {!*t} produced a garbage index like services-table: 0x7f... instead of being rejected.

Cause

The index field supports {time_format} placeholders passed straight to os.date. The existing if not time guard assumes bad input returns nil, but on LuaJIT/OpenResty it never does:

  • {*t} / {!*t} make os.date return a table, not a string. It then flows into ngx.re.gsub, which stringifies it into the index name.

Fix

Require os.date to return a string before using it; otherwise log failed to parse time format and fall back to an empty replacement. Wrapped in pcall for defense-in-depth on non-LuaJIT runtimes (where os.date can raise on a bad escape).

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

Test

t/plugin/elasticsearch-logger2.t TEST 10: prefix{*t}suffix (and {!*t}) now resolves to prefixsuffix with a failed to parse time format log, instead of embedding a table: 0x... address.

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change (N/A)
  • I have verified that the changes pass the existing tests

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working labels Jun 12, 2026
@AlinsRan AlinsRan force-pushed the fix/es-logger-osdate-pcall branch 2 times, most recently from 6899fc4 to e1b209d Compare June 12, 2026 08:04
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:XS This PR changes 0-9 lines, ignoring generated files. labels Jun 12, 2026
The dynamic-index feature passes the user-controlled time format straight
to os.date in replace_time(). os.date raises (instead of returning nil) on
some invalid strftime escapes, so a crafted index template would throw in
the log phase rather than degrade gracefully. Wrap the call in pcall and
fall back to an empty replacement on failure, as the existing nil-check
already intended.

Signed-off-by: AlinsRan <alinsran@apache.org>
@AlinsRan AlinsRan force-pushed the fix/es-logger-osdate-pcall branch from e1b209d to b6cd667 Compare June 12, 2026 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant