Skip to content

fix(log-sanitize): return raw tables to avoid double delay_encode#13605

Open
shreemaan-abhishek wants to merge 1 commit into
apache:masterfrom
shreemaan-abhishek:fix/log-sanitize-double-wrap
Open

fix(log-sanitize): return raw tables to avoid double delay_encode#13605
shreemaan-abhishek wants to merge 1 commit into
apache:masterfrom
shreemaan-abhishek:fix/log-sanitize-double-wrap

Conversation

@shreemaan-abhishek

Copy link
Copy Markdown
Contributor

Description

apisix/utils/log-sanitize.lua's redact_params / redact_extra_opts already return a recycled delay_encode pool slot (a {data=, force=} table with a __tostring metamethod). The AI plugin call sites then wrap that slot in core.json.delay_encode(..., true) a second time:

  • apisix/plugins/ai-proxy/base.lua:265
  • apisix/plugins/ai-providers/base.lua:132
  • apisix/plugins/ai-providers/base.lua:779

When the log line flushes, cjson encodes the inner wrapper table (cjson ignores __tostring), so the log emits the wrapper structure instead of the redacted JSON. Worse, delay_encode recycles a 16-slot pool, so by flush time the inner slot may already hold an unrelated request's data, risking stale or unredacted content from a different request in the logs. This triggers on every AI-proxy / ai-providers request log at info level.

This makes the sanitizer return raw tables and lets the single delay_encode already present at each call site do the encoding.

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 (no public behavior/docs change)
  • I have verified that this change is backward compatible (the log content is now correct; no API change)

redact_params/redact_extra_opts already returned a delay_encode pool
slot, but the AI plugin call sites wrap that slot in delay_encode a
second time. cjson ignores the inner slot's __tostring metamethod, so
the log line emits the wrapper table instead of the redacted JSON.
Since delay_encode recycles a 16-slot pool, the inner slot may also
hold another request's data by flush time, risking stale or unredacted
content in logs.

Return raw tables and let the existing single delay_encode at each
call site do the encoding.
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Jun 24, 2026
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:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant