fix(discord,gateway): inbound WS buffer overflow and WebChat drop - #81
Draft
cursor[bot] wants to merge 2 commits into
Draft
fix(discord,gateway): inbound WS buffer overflow and WebChat drop#81cursor[bot] wants to merge 2 commits into
cursor[bot] wants to merge 2 commits into
Conversation
discord_rx_append grew only when cap < rx_len+len, then wrote a NUL at rx_len+len. Two 64KiB LWS fragments filled a doubled buffer exactly, so the terminator was one byte past the heap block (typical READY payloads). Co-authored-by: esadrianno <esadrianno@gmail.com>
…split LWS delivers at most rx_buffer_size bytes per RECEIVE. The ws protocol used 256 with no fragment reassembly, so dashboard messages over ~220 characters were parsed as incomplete JSON and dropped. Size the buffer to fit a full WebChat frame (MSG_MAX plus JSON envelope). Distinct from outbound #62. Co-authored-by: esadrianno <esadrianno@gmail.com>
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.
Summary
Two inbound WebSocket buffer bugs: Discord Gateway RX wrote a NUL one byte past the heap block, and dashboard WebChat silently dropped messages larger than one 256-byte LWS callback.
Bug 1 — Discord Gateway RX heap overflow (CRITICAL)
src/channels/discord.c(discord_rx_append, nowdiscord_helpers_rx_append)rx_buffer_size = 65536. First 64KiB fragment doubles the buffer to 131072 withrx_len = 65536. Second 64KiB fragment:rx_cap < rx_len + lenis false (131072 < 131072), grow is skipped, thenrx_buf[rx_len] = '\0'writes at index 131072 (one past the allocation). Typical Discord READY (or any DISPATCH) payload >128KiB, well underRX_MAX(512KiB).parse_response_bodyrealloc-before-success. fix(webchat): align WebSocket payload limit with agent response buffer #62 is WebChat outbound 8KiB vs 32KiB (ws_send_to/MSG_MAX), not Discord client RX. Telegram/OpenAI curl writers already include+1for NUL.Bug 2 — WebChat inbound frames not reassembled (HIGH)
src/gateway/http.cprotocols"ws".rx_buffer_size = 256;src/gateway/http_lws.cws_callbackLWS_CALLBACK_RECEIVE{"type":"message","text":"..."}). LWS splits at 256 bytes; each chunk iscJSON_Parsed as a complete object and fails; the user message is dropped.rx_buffer_sizeor fragment reassembly.Fix
cap < rx_len + chunk + 1(payload plus trailing NUL). Discard remaining fragments after overflow untillws_is_final_fragment.rx_buffer_sizetoWS_RX_BUFFER_SIZE(32KiB) so a full frame atMSG_MAXplus JSON envelope arrives in one RECEIVE.Validation
CI=true make test_discord_helpers(includes exact-fill 4095+1 and two 64KiB fragments)test_discord_helpersGATEWAY=1compile ofdiscord.c,http.c,http_lws.c