Skip to content

fix core: double truncation in request logging - #1344

Open
rjazhenka1 wants to merge 1 commit into
userver-framework:developfrom
rjazhenka1:fix-double-truncation
Open

rjazhenka1 wants to merge 1 commit into
userver-framework:developfrom
rjazhenka1:fix-double-truncation

Conversation

@rjazhenka1

@rjazhenka1 rjazhenka1 commented Sep 9, 2026

Copy link
Copy Markdown

Problem:

Request and response body logs are truncated twice, which results in (truncated, total X bytes) message having an incorrect size value (roughly around request_body_size_log_limit / response_data_size_log_limit).

1st truncation happens in GetRequestBodyForLogging() and GetResponseDataForLogging() (the resulting string is bigger than the limit due to the (truncated...) message).

2nd truncation happens in GetRequestBodyForLoggingChecked() and GetResponseDataForLoggingChecked(). Since the string from the 1st truncation is bigger than the limit, the string is truncated again and incorrect size is shown in the message.

Fix:

To fix the problem, I removed the 1st truncation call. This is okay, since request/response bodies are always truncated again in the 2nd call.

Before / after:

To showcase the issue, I used a simple request which consists of the letter "a" repeated for 10 KiB.

Before:
Screenshot_20260909_190244
After:
Screenshot_20260909_190823

@apolukhin apolukhin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice catch! A few comments follow:

const std::string& request_body
) const {
const size_t limit = GetConfig().request_body_size_log_limit;
return utils::log::ToLimitedHex(request_body, limit);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ToLimitedHex does a good job here. It saves memory

const std::string& response_data
) const {
const size_t limit = GetConfig().response_data_size_log_limit;
return utils::log::ToLimitedHex(response_data, limit);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ToLimitedHex does a good job here. It saves memory

) const {
const std::size_t limit = GetConfig().response_data_size_log_limit;
return utils::log::ToLimitedUtf8(response_data, limit);
return response_data;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please, add tests that catch the initial issue and make sure that id does not appear again

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants