Skip to content

Commit 98c2c69

Browse files
fix(agents): reject invalid UTF-8 LSP responses
1 parent bcff3a0 commit 98c2c69

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎src/agents/agent-bundle-lsp-runtime.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,41 @@ describe("bundle LSP runtime", () => {
367367
await runtime.dispose();
368368
});
369369

370+
it("rejects invalid UTF-8 in LSP JSON bodies", async () => {
371+
configureSingleLspServer();
372+
const child = new MockChildProcess("", new Set(["initialize"]));
373+
spawnMock.mockReturnValue(child);
374+
375+
const runtime = await createBundleLspToolRuntime({ workspaceDir: "/tmp/workspace" });
376+
const hoverTool = runtime.tools.find((tool) => tool.name === "lsp_hover_typescript");
377+
if (!hoverTool) {
378+
throw new Error("expected hover tool");
379+
}
380+
const request = hoverTool.execute("call-1", {
381+
uri: "file:///tmp/workspace/index.ts",
382+
line: 0,
383+
character: 0,
384+
});
385+
const hoverRequest = child.receivedMessages.find(
386+
(message) => message.method === "textDocument/hover",
387+
);
388+
if (typeof hoverRequest?.id !== "number") {
389+
throw new Error("expected numeric hover request id");
390+
}
391+
const body = Buffer.concat([
392+
Buffer.from(`{"jsonrpc":"2.0","id":${hoverRequest.id},"result":{"contents":"`),
393+
Buffer.from([0xff]),
394+
Buffer.from('"}}'),
395+
]);
396+
const header = Buffer.from(`Content-Length: ${body.length}\r\n\r\n`, "ascii");
397+
child.stdout.write(Buffer.concat([header, body]));
398+
399+
await expect(request).rejects.toThrow(/LSP framing error: body is not valid UTF-8/i);
400+
expect(killProcessTreeMock).toHaveBeenCalledWith(4321, { graceMs: 1000, detached: true });
401+
402+
await runtime.dispose();
403+
});
404+
370405
it.each([
371406
{
372407
name: "a suffixed Content-Length value",

‎src/agents/agent-bundle-lsp-runtime.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ function encodeLspMessage(body: unknown): string {
166166
const LSP_HEADER_SEPARATOR = Buffer.from("\r\n\r\n", "ascii");
167167
const MAX_LSP_HEADER_BYTES = 8 * 1024;
168168
const MAX_LSP_BODY_BYTES = 64 * 1024 * 1024;
169+
const LSP_BODY_DECODER = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
169170

170171
class LspFramingError extends Error {
171172
override readonly name = "LspFramingError";
@@ -241,7 +242,12 @@ function parseLspMessages(buffer: Buffer): LspParseResult {
241242
return { ok: true, messages, remaining };
242243
}
243244

244-
const body = remaining.subarray(bodyStart, bodyEnd).toString("utf8");
245+
let body: string;
246+
try {
247+
body = LSP_BODY_DECODER.decode(remaining.subarray(bodyStart, bodyEnd));
248+
} catch {
249+
return framingError(messages, "body is not valid UTF-8");
250+
}
245251
try {
246252
messages.push(JSON.parse(body));
247253
} catch (error) {

0 commit comments

Comments
 (0)