fix: cap websocket body log growth in responses handler#1996
Conversation
Summary of ChangesHello, 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 Highlights
🧠 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
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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.
| if limit > len(value) { | ||
| limit = len(value) | ||
| } |
There was a problem hiding this comment.
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.
xkonjin
left a comment
There was a problem hiding this comment.
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.
Motivation
/v1/responseswebsocket handler accumulated every incoming/outgoing websocket payload into an unboundedstrings.Builder, enabling a remote client to exhaust server memory by keeping a websocket open and sending large or numerous messages.Description
wsBodyLogMaxSizeandwsBodyLogTruncatedand enforce a hard cap on the websocket body log to limit retained bytes to the configured maximum.strings.Builderwrites inappendWebsocketEventwith bounded helpers (appendWebsocketLogStringandappendWebsocketLogBytes) that safely write up to the remaining quota and insert a truncation marker when a payload is larger than the remaining space.TestAppendWebsocketEventTruncatesAtLimitandTestAppendWebsocketEventNoGrowthAfterLimitto validate truncation behavior and that no growth occurs after the limit is reached.Testing
TestAppendWebsocketEventTruncatesAtLimitandTestAppendWebsocketEventNoGrowthAfterLimittosdk/api/handlers/openaito cover truncation and post-cap behavior.go test ./sdk/api/handlers/openai -run 'TestAppendWebsocketEvent|TestResponsesWebsocketPrewarmHandledLocallyForSSEUpstream'and they passed.Codex Task