Skip to content

fix: cap websocket body log growth in responses handler#1996

Merged
luispater merged 1 commit intodevfrom
codex/fix-unbounded-websocket-log-buffering
Mar 9, 2026
Merged

fix: cap websocket body log growth in responses handler#1996
luispater merged 1 commit intodevfrom
codex/fix-unbounded-websocket-log-buffering

Conversation

@LTbinglingfeng
Copy link
Collaborator

Motivation

  • The /v1/responses websocket handler accumulated every incoming/outgoing websocket payload into an unbounded strings.Builder, enabling a remote client to exhaust server memory by keeping a websocket open and sending large or numerous messages.
  • The intent is to preserve request/response logging for diagnostics while preventing unbounded memory growth and availability impact.

Description

  • Introduce wsBodyLogMaxSize and wsBodyLogTruncated and enforce a hard cap on the websocket body log to limit retained bytes to the configured maximum.
  • Replace direct strings.Builder writes in appendWebsocketEvent with bounded helpers (appendWebsocketLogString and appendWebsocketLogBytes) that safely write up to the remaining quota and insert a truncation marker when a payload is larger than the remaining space.
  • Short-circuit further appends once the cap is reached to avoid any additional growth.
  • Add unit tests TestAppendWebsocketEventTruncatesAtLimit and TestAppendWebsocketEventNoGrowthAfterLimit to validate truncation behavior and that no growth occurs after the limit is reached.

Testing

  • Added tests TestAppendWebsocketEventTruncatesAtLimit and TestAppendWebsocketEventNoGrowthAfterLimit to sdk/api/handlers/openai to cover truncation and post-cap behavior.
  • Ran targeted package tests with go test ./sdk/api/handlers/openai -run 'TestAppendWebsocketEvent|TestResponsesWebsocketPrewarmHandledLocallyForSSEUpstream' and they passed.

Codex Task

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request implements a crucial fix to prevent a memory exhaustion vulnerability in the /v1/responses websocket handler. By introducing a configurable maximum size for the websocket body log, the system can now maintain diagnostic logging capabilities without risking server availability due to excessive memory consumption from large or numerous incoming/outgoing messages. The changes ensure that log data is properly truncated when limits are reached, safeguarding system stability.

Highlights

  • Memory Leak Prevention: Introduced a hard cap on the websocket body log size to prevent unbounded memory growth in the /v1/responses handler, addressing a potential server memory exhaustion vulnerability.
  • Bounded Logging Functions: Refactored the appendWebsocketEvent function and introduced new helper functions, appendWebsocketLogString and appendWebsocketLogBytes, to safely write content to the log builder while respecting the defined size limit and inserting a truncation marker.
  • New Constants: Defined wsBodyLogMaxSize (64KB) and wsBodyLogTruncated constants to control the maximum log size and indicate when truncation has occurred.
  • Unit Test Coverage: Added two new unit tests, TestAppendWebsocketEventTruncatesAtLimit and TestAppendWebsocketEventNoGrowthAfterLimit, to thoroughly validate the new truncation behavior and ensure the log does not exceed its maximum size.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • sdk/api/handlers/openai/openai_responses_websocket.go
    • Defined new constants wsBodyLogMaxSize and wsBodyLogTruncated to manage log size and truncation.
    • Modified appendWebsocketEvent to include size checks and utilize new bounded write helper functions.
    • Introduced appendWebsocketLogString and appendWebsocketLogBytes functions for controlled string and byte appending to the log builder with size limits.
  • sdk/api/handlers/openai/openai_responses_websocket_test.go
    • Added TestAppendWebsocketEventTruncatesAtLimit to verify that the websocket log correctly truncates when exceeding the maximum size.
    • Added TestAppendWebsocketEventNoGrowthAfterLimit to confirm that the log builder does not grow further once its maximum capacity is reached.
Activity
  • New unit tests TestAppendWebsocketEventTruncatesAtLimit and TestAppendWebsocketEventNoGrowthAfterLimit were added to cover the new truncation and size limit logic.
  • Targeted package tests were run with go test ./sdk/api/handlers/openai -run 'TestAppendWebsocketEvent|TestResponsesWebsocketPrewarmHandledLocallyForSSEUpstream' and passed.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@LTbinglingfeng LTbinglingfeng changed the base branch from main to dev March 9, 2026 09:50
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses a potential memory exhaustion issue in the websocket responses handler by capping the growth of the body log. The introduction of wsBodyLogMaxSize and bounded helper functions (appendWebsocketLogString, appendWebsocketLogBytes) for writing to the log is a solid approach. The logic for truncation and stopping appends once the limit is reached seems correct. The new unit tests, TestAppendWebsocketEventTruncatesAtLimit and TestAppendWebsocketEventNoGrowthAfterLimit, are well-written and provide good coverage for the new functionality. I have one minor suggestion to improve code clarity by removing a redundant check.

Comment on lines +890 to +892
if limit > len(value) {
limit = len(value)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This condition appears to be unreachable.

The function logic enters this block only if len(value) > remaining (from line 882). The limit is calculated as remaining - reserveForSuffix (line 886), which means limit will always be less than or equal to remaining.

Since remaining < len(value), it follows that limit cannot be greater than len(value). This block of code appears to be dead code and can be removed for clarity.

Copy link

@xkonjin xkonjin left a comment

Choose a reason for hiding this comment

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

Automated review focusing on bugs, security, and test coverage.
PR: fix: cap websocket body log growth in responses handler (#1996)

Key observations:

  • No test files changed. Consider adding or updating tests for modified code paths.

Suggestions:

  • Add or update unit/integration tests for changed code paths.
  • Run dependency and static security scans (pip-audit/npm audit/Semgrep) and address findings.
  • Double check error handling and input validation on new code paths.

@luispater luispater merged commit 71773fe into dev Mar 9, 2026
2 checks passed
@luispater luispater deleted the codex/fix-unbounded-websocket-log-buffering branch March 9, 2026 12:50
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.

3 participants