header_rewrite: Fix a leak and truncation in set-body-from#13303
Open
moonchen wants to merge 2 commits into
Open
header_rewrite: Fix a leak and truncation in set-body-from#13303moonchen wants to merge 2 commits into
moonchen wants to merge 2 commits into
Conversation
TSUrlStringGet allocates the return value. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
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()increateRequestString()to prevent per-transaction leaks whenset-body-fromrules match.
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>
JosiahWI
approved these changes
Jun 19, 2026
| TSfree(url); | ||
| TSMBufferDestroy(url_buf); | ||
|
|
||
| if (written < 0 || static_cast<unsigned int>(written) >= MAX_SIZE) { |
Contributor
There was a problem hiding this comment.
The >= relation is correct, because written does not include the null terminator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two related fixes in the header_rewrite
set-body-fromoperator'screateRequestString():Memory leak. The request URL was fetched with
TSUrlStringGet(), which returns aTSmalloc()-allocated buffer the caller owns, but it was never freed. Every non-internal transaction matching aset-body-fromrule leaked one URL-sized allocation, growing unbounded over time. Free it, mirroring the existing owned-pointer handling inConditionUrl.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-bytereq_buf. Passing it toTSFetchUrl()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 intoreq_buf— so this changes nothing for requests that fit; it just returnsTS_ERRORon overflow instead of over-reading. (Raised by Copilot review.)🤖 Generated with Claude Code