Skip to content

Commit e859e31

Browse files
only21milcestercianclaudejuliusmarminge
authored
fix(codex): end the session when an incoming message is too long (pingdotgg#19733)
Co-authored-by: Cestercian <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 1a684f0 commit e859e31

2 files changed

Lines changed: 100 additions & 16 deletions

File tree

‎packages/effect-codex-app-server/src/protocol.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,80 @@ it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
371371
}),
372372
);
373373

374+
it.effect.each(["newline-terminated", "unfinished"])(
375+
"rejects an oversized %s message using UTF-8 bytes before logging or parsing",
376+
(framing) =>
377+
Effect.gen(function* () {
378+
const { stdio, input, output } = yield* makeInMemoryStdio();
379+
const notifications: Array<CodexProtocol.CodexAppServerIncomingNotification> = [];
380+
const logged: Array<unknown> = [];
381+
const termination = yield* Deferred.make<CodexError.CodexAppServerError>();
382+
let reachedEnd = false;
383+
const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({
384+
stdio: Stdio.make({
385+
...stdio,
386+
stdin: stdio.stdin.pipe(
387+
Stream.concat(
388+
Stream.suspend(() => {
389+
reachedEnd = true;
390+
return Stream.empty;
391+
}),
392+
),
393+
),
394+
}),
395+
maxIncomingMessageBytes: 16,
396+
logIncoming: true,
397+
logger: (event) =>
398+
Effect.sync(() => {
399+
logged.push(event.payload);
400+
}),
401+
onNotification: (notification) =>
402+
Effect.sync(() => {
403+
notifications.push(notification);
404+
}),
405+
onTermination: (error) => Deferred.succeed(termination, error).pipe(Effect.asVoid),
406+
});
407+
const pending = yield* Effect.forEach(["thread/read", "thread/list"], (method) =>
408+
transport.request(method, {}).pipe(Effect.forkScoped),
409+
);
410+
yield* Effect.forEach(pending, () => Queue.take(output));
411+
412+
// Two 16-byte lines must survive; the 17-byte line is only 15 UTF-16 code units.
413+
const firstLine = '{"method":"xé"}';
414+
const secondLine = '{"method":"x/a"}';
415+
const bytes = encoder.encode(
416+
`${firstLine}\n${secondLine}\n{"method":"éé"}${framing === "unfinished" ? "" : "\n"}`,
417+
);
418+
// Split a UTF-8 sequence on both sides of the byte limit.
419+
const firstSplit = bytes.indexOf(0xc3) + 1;
420+
yield* Queue.offer(input, bytes.subarray(0, firstSplit));
421+
if (framing === "unfinished") {
422+
const lastSplit = bytes.lastIndexOf(0xc3) + 1;
423+
yield* Queue.offer(input, bytes.subarray(firstSplit, lastSplit));
424+
yield* Queue.offer(input, bytes.subarray(lastSplit));
425+
} else {
426+
yield* Queue.offer(input, bytes.subarray(firstSplit));
427+
}
428+
yield* Queue.end(input);
429+
430+
const error = yield* Deferred.await(termination);
431+
assert.instanceOf(error, CodexError.CodexAppServerTransportError);
432+
assert.equal(error.operation, "read-input-stream");
433+
assert.isFalse(reachedEnd, "Must reject the unfinished line before reading EOF");
434+
for (const response of pending) {
435+
const failure = yield* Fiber.join(response).pipe(
436+
Effect.match({
437+
onFailure: (error) => error,
438+
onSuccess: () => assert.fail("Expected the oversized message to fail the request"),
439+
}),
440+
);
441+
assert.strictEqual(failure, error);
442+
}
443+
assert.deepEqual(notifications, [{ method: "xé" }, { method: "x/a" }]);
444+
assert.deepEqual(logged, [firstLine, { method: "xé" }, secondLine, { method: "x/a" }]);
445+
}),
446+
);
447+
374448
it.effect("reports a malformed fragmented final line before input stream termination", () =>
375449
Effect.gen(function* () {
376450
const { stdio, input, output } = yield* makeInMemoryStdio();

‎packages/effect-codex-app-server/src/protocol.ts‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const isJsonRpcId = Schema.is(JsonRpcId);
1616
const isJsonRpcResponseEnvelope = Schema.is(JsonRpcResponseEnvelope);
1717
const isCodexAppServerError = Schema.is(CodexError.CodexAppServerError);
1818
const MAX_BUFFERED_RAW_MESSAGES = 32;
19+
// Bound JSONL lines before joining and parsing; the LF delimiter is excluded.
20+
const MAX_INCOMING_MESSAGE_BYTES = 128 * 1024 * 1024;
1921

2022
export interface CodexAppServerProtocolLogEvent {
2123
readonly direction: "incoming" | "outgoing";
@@ -39,6 +41,8 @@ export interface CodexAppServerPatchedProtocolOptions {
3941
readonly terminationError?: Effect.Effect<CodexError.CodexAppServerError>;
4042
readonly logIncoming?: boolean;
4143
readonly logOutgoing?: boolean;
44+
// Test override; production uses MAX_INCOMING_MESSAGE_BYTES.
45+
readonly maxIncomingMessageBytes?: number;
4246
readonly logger?: (event: CodexAppServerProtocolLogEvent) => Effect.Effect<void, never>;
4347
readonly onNotification?: (
4448
notification: CodexAppServerIncomingNotification,
@@ -164,7 +168,9 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
164168
yield* Queue.sliding<CodexAppServerIncomingRequest>(MAX_BUFFERED_RAW_MESSAGES);
165169
const pending = yield* Ref.make(new Map<string, CodexAppServerPendingRequest>());
166170
const nextRequestId = yield* Ref.make(1);
171+
const maxIncomingMessageBytes = options.maxIncomingMessageBytes ?? MAX_INCOMING_MESSAGE_BYTES;
167172
const remainder: Array<string> = [];
173+
let remainderBytes = 0;
168174
const terminationHandled = yield* Ref.make(false);
169175
const terminationFailure = yield* Ref.make(Option.none<CodexError.CodexAppServerError>());
170176
const terminationSignal = yield* Deferred.make<void>();
@@ -399,26 +405,30 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
399405
yield* options.stdio.stdin.pipe(
400406
Stream.interruptWhen(Deferred.await(terminationSignal)),
401407
Stream.decodeText(),
402-
Stream.runForEach((chunk) =>
403-
Effect.sync(() => {
404-
const lines: Array<string> = [];
408+
Stream.runForEach(
409+
Effect.fnUntraced(function* (chunk) {
405410
let start = 0;
406-
for (
407-
let newline = chunk.indexOf("\n");
408-
newline !== -1;
409-
newline = chunk.indexOf("\n", start)
410-
) {
411-
remainder.push(chunk.slice(start, newline));
412-
lines.push(remainder.join("").replace(/\r$/, ""));
411+
while (start < chunk.length) {
412+
const newline = chunk.indexOf("\n", start);
413+
const fragment = chunk.slice(start, newline === -1 ? chunk.length : newline);
414+
// decodeText holds back split UTF-8 sequences, so fragment byte sums are exact.
415+
remainderBytes += Buffer.byteLength(fragment, "utf8");
416+
if (remainderBytes > maxIncomingMessageBytes) {
417+
remainder.length = 0;
418+
return yield* new CodexError.CodexAppServerTransportError({
419+
operation: "read-input-stream",
420+
cause: new Error(`Incoming message exceeded ${maxIncomingMessageBytes} bytes.`),
421+
});
422+
}
423+
remainder.push(fragment);
424+
if (newline === -1) break;
425+
const line = remainder.join("").replace(/\r$/, "");
413426
remainder.length = 0;
427+
remainderBytes = 0;
428+
yield* handleLine(line);
414429
start = newline + 1;
415430
}
416-
// Keep unfinished lines in fragments so each chunk is scanned only once.
417-
if (start < chunk.length) {
418-
remainder.push(chunk.slice(start));
419-
}
420-
return lines;
421-
}).pipe(Effect.flatMap((lines) => Effect.forEach(lines, handleLine, { discard: true }))),
431+
}),
422432
),
423433
Effect.matchEffect({
424434
onFailure: (error) =>

0 commit comments

Comments
 (0)