Skip to content

header_rewrite: Fix a leak and truncation in set-body-from#13303

Open
moonchen wants to merge 2 commits into
apache:masterfrom
moonchen:header_rewrite_set_body_from_leak
Open

header_rewrite: Fix a leak and truncation in set-body-from#13303
moonchen wants to merge 2 commits into
apache:masterfrom
moonchen:header_rewrite_set_body_from_leak

Conversation

@moonchen

@moonchen moonchen commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Two related fixes in the header_rewrite set-body-from operator's createRequestString():

  1. Memory leak. The request URL was fetched with TSUrlStringGet(), which returns a TSmalloc()-allocated buffer the caller owns, but it was never freed. Every non-internal transaction matching a set-body-from rule leaked one URL-sized allocation, growing unbounded over time. Free it, mirroring the existing owned-pointer handling in ConditionUrl.

  2. Truncation / buffer over-read. snprintf() returns the length it would have written, not the number of bytes actually written, and that value can exceed the 256-byte req_buf. Passing it to TSFetchUrl() as the request length reads past the stack buffer when a URL overflows. The request has always been capped at 256 bytes — snprintf() only ever writes that much into req_buf — so this changes nothing for requests that fit; it just returns TS_ERROR on overflow instead of over-reading. (Raised by Copilot review.)

🤖 Generated with Claude Code

TSUrlStringGet allocates the return value.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 19, 2026 20:08
@moonchen moonchen self-assigned this Jun 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a memory leak in the header_rewrite plugin’s set-body-from operator by releasing the TSmalloc()-owned buffer returned by TSUrlStringGet(), aligning ownership handling with existing ConditionUrl logic.

Changes:

  • Free the URL string returned by TSUrlStringGet() in createRequestString() to prevent per-transaction leaks when set-body-from rules match.

Comment thread plugins/header_rewrite/operators.cc Outdated
@moonchen moonchen added Leak header_rewrite header_rewrite plugin labels Jun 19, 2026
@moonchen moonchen added this to the 11.0.0 milestone Jun 19, 2026
snprintf() returns the length it would have written, which can exceed
the 256-byte request buffer. Passing that length to TSFetchUrl() would
read past req_buf, so error out instead of sending a truncated request.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@moonchen moonchen changed the title header_rewrite: Fix a leak in the set-body-from operator header_rewrite: Fix a leak and truncation in set-body-from Jun 19, 2026

@JosiahWI JosiahWI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good fixes.

TSfree(url);
TSMBufferDestroy(url_buf);

if (written < 0 || static_cast<unsigned int>(written) >= MAX_SIZE) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The >= relation is correct, because written does not include the null terminator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

header_rewrite header_rewrite plugin Leak

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants