Repository navigation
fix(codex-proxy): cap app-server WebSocket frames (#1839) #4056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -512,6 +512,8 @@ const wsEncodeFrame = (payload, opcode = 0x1) => { | |
| return Buffer.concat([header, mask, masked]); | ||
| }; | ||
|
|
||
| const MAX_APP_SERVER_FRAME_BYTES = 1024 * 1024; | ||
|
|
||
| class AppServerClient { | ||
| constructor(socketPath) { | ||
| this.socketPath = socketPath; | ||
|
|
@@ -523,6 +525,7 @@ class AppServerClient { | |
| this.loadedThreads = new Set(); | ||
| this.buffer = Buffer.alloc(0); | ||
| this.fragments = []; | ||
| this.fragmentBytes = 0; | ||
| this.handshaked = false; | ||
| } | ||
|
|
||
|
|
@@ -592,9 +595,18 @@ class AppServerClient { | |
| this.pending.clear(); | ||
| for (const st of this.turnStates.values()) st.fail(err); | ||
| this.turnStates.clear(); | ||
| this.buffer = Buffer.alloc(0); | ||
| this.fragments = []; | ||
| this.fragmentBytes = 0; | ||
| if (appServerClient === this) appServerClient = null; | ||
| } | ||
|
|
||
| rejectFrameLength(length, kind = 'frame') { | ||
| const err = new Error(`app-server ${kind} length ${length} exceeds ${MAX_APP_SERVER_FRAME_BYTES}-byte limit`); | ||
| this.handleClose(err); | ||
| try { this.sock && this.sock.destroy(); } catch {} | ||
| } | ||
|
|
||
| send(obj) { | ||
| if (!this.sock || this.dead) throw new Error('app-server not connected'); | ||
| this.sock.write(wsEncodeFrame(JSON.stringify(obj))); | ||
|
|
@@ -614,7 +626,20 @@ class AppServerClient { | |
| len = this.buffer.readUInt16BE(off); off += 2; | ||
| } else if (len === 127) { | ||
| if (this.buffer.length < off + 8) return; | ||
| len = Number(this.buffer.readBigUInt64BE(off)); off += 8; | ||
| const claimed = this.buffer.readBigUInt64BE(off); off += 8; | ||
| if (claimed > BigInt(Number.MAX_SAFE_INTEGER)) { | ||
| this.rejectFrameLength(claimed); | ||
| return; | ||
| } | ||
| len = Number(claimed); | ||
| } | ||
| if (len > MAX_APP_SERVER_FRAME_BYTES) { | ||
| this.rejectFrameLength(len); | ||
| return; | ||
| } | ||
| if (this.fragmentBytes + len > MAX_APP_SERVER_FRAME_BYTES) { | ||
| this.rejectFrameLength(this.fragmentBytes + len, 'message'); | ||
| return; | ||
|
Comment on lines
+640
to
+642
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a ping arrives between fragments of a message near the cap, this check adds the ping payload to the accumulated message size before the ping handler can run. For example, a 1,048,575-byte first fragment, an interleaved 2-byte ping, and a 1-byte continuation form a valid message exactly at the limit, but the connection is destroyed on the ping with a claimed message length of 1,048,577. This unnecessarily fails active turns and triggers exec fallback, or a streaming error if output was already emitted. Apply the aggregate check only to data/continuation frames, excluding control frames. Useful? React with 👍 / 👎. |
||
| } | ||
| if (b1 & 0x80) off += 4; // server frames are unmasked; skip mask if set | ||
| if (this.buffer.length < off + len) return; | ||
|
|
@@ -630,9 +655,11 @@ class AppServerClient { | |
| return; | ||
| } | ||
| this.fragments.push(payload); | ||
| this.fragmentBytes += payload.length; | ||
| if (!fin) continue; | ||
| const text = Buffer.concat(this.fragments).toString('utf-8'); | ||
| this.fragments = []; | ||
| this.fragmentBytes = 0; | ||
| this.onMessage(text); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If an initialize RPC response and an oversized frame header arrive in the same socket data event, parsing the response queues the initialization success callback, but this call marks the client dead before that callback runs. The callback in
connect()then callsthis.send({ method: 'initialized' }), which throwsapp-server not connected; its returned promise has no rejection handler, so Node's default unhandled-rejection behavior terminates the proxy instead of falling back to exec. Reproducing the same input against the parent commit initializes successfully, whereas this commit produces the unhandled rejection. Ensure frame rejection also safely settles initialization and prevents the queued success callback from sending on a dead client.Useful? React with 👍 / 👎.