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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an AI plugin logging sanitization bug by changing log-sanitize helpers to return plain Lua tables instead of pre-wrapped core.json.delay_encode pool slots, preventing accidental double-wrapping and incorrect/stale JSON output in logs.

Changes:

  • Update redact_params and redact_extra_opts to return raw redacted tables (no internal delay_encode wrapping).
  • Add regression tests validating (1) returned values are raw tables and (2) a single delay_encode at the call site produces correctly redacted JSON.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
t/utils/log-sanitize.t Adds tests to ensure sanitizers return raw tables and that single wrapping with delay_encode yields redacted JSON (not wrapper structures).
apisix/utils/log-sanitize.lua Stops returning pooled delay_encode wrappers from redaction helpers; returns raw redacted tables to avoid double-wrapping issues.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

2 participants