Skip to content

Commit 998529f

Browse files
fix(signal): reject invalid UTF-8 in container REST JSON responses (#121569)
* fix(signal): reject invalid UTF-8 in container REST JSON responses * fix(signal): reject invalid UTF-8 in native JSON responses * fix(signal): keep health checks status-only * fix(signal): reject malformed JSON bytes at the decoder owner Preserve bounded native response bytes until JSON parsing so health checks remain status-only, and reject malformed UTF-8 in both native and container JSON. Remove the unused native statusText result to keep production code net-neutral. Co-authored-by: sunlit-deng <[email protected]> --------- Co-authored-by: Peter Steinberger <[email protected]> Co-authored-by: sunlit-deng <[email protected]>
1 parent 1f2acb6 commit 998529f

4 files changed

Lines changed: 69 additions & 11 deletions

File tree

‎extensions/signal/src/client-container.real-server.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { AddressInfo } from "node:net";
99
import { tmpdir } from "node:os";
1010
import { join } from "node:path";
1111
import { afterEach, describe, expect, it } from "vitest";
12-
import { containerRpcRequest } from "./client-container.js";
12+
import { containerCheck, containerRpcRequest } from "./client-container.js";
1313

1414
type StartedServer = { baseUrl: string; close: () => Promise<void> };
1515

@@ -41,6 +41,35 @@ async function startServer(handler: http.RequestListener): Promise<StartedServer
4141
}
4242

4343
describe("signal REST real-server deadline", () => {
44+
it.each([{ bytes: [0xff] }, { bytes: [0xc3] }])(
45+
"rejects malformed UTF-8 bytes $bytes before JSON parsing",
46+
async ({ bytes }) => {
47+
const server = await startServer((_req, res) => {
48+
res.writeHead(200, { "content-type": "application/json" });
49+
res.end(
50+
Buffer.concat([Buffer.from('{"versions":["'), Buffer.from(bytes), Buffer.from('"]}')]),
51+
);
52+
});
53+
54+
await expect(
55+
containerRpcRequest("version", undefined, { baseUrl: server.baseUrl }),
56+
).rejects.toBeInstanceOf(TypeError);
57+
},
58+
);
59+
60+
it("uses only the status when the unused health response is malformed UTF-8", async () => {
61+
const server = await startServer((_req, res) => {
62+
res.writeHead(200);
63+
res.end(Buffer.from([0xff]));
64+
});
65+
66+
await expect(containerCheck(server.baseUrl)).resolves.toEqual({
67+
ok: true,
68+
status: 200,
69+
error: null,
70+
});
71+
});
72+
4473
it("aborts a slow-drip body that never idles, at the request deadline", async () => {
4574
// Drip a byte every 50ms: below the 300ms idle guard, so only the total request
4675
// deadline can stop it. This is the exact slow-drip case the fix bounds.

‎extensions/signal/src/client-container.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ async function readSignalRestText(
176176
onTimeout: signalRestRequestTimeoutError,
177177
onOverflow: ({ maxBytes }) => new Error(`Signal REST: text response exceeds ${maxBytes} bytes`),
178178
});
179-
return new TextDecoder().decode(bytes);
179+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
180180
}
181181

182182
async function readSignalRestErrorText(

‎extensions/signal/src/client.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@ afterEach(async () => {
7171
});
7272

7373
describe("signalRpcRequest", () => {
74+
it.each([{ bytes: [0xff] }, { bytes: [0xc3] }])(
75+
"rejects malformed UTF-8 bytes $bytes before JSON parsing",
76+
async ({ bytes }) => {
77+
const baseUrl = await withSignalServer((_req, res) => {
78+
res.writeHead(200, { "Content-Type": "application/json" });
79+
res.end(
80+
Buffer.concat([
81+
Buffer.from('{"jsonrpc":"2.0","result":{"version":"'),
82+
Buffer.from(bytes),
83+
Buffer.from('"},"id":"test-id"}'),
84+
]),
85+
);
86+
});
87+
88+
await expect(signalRpcRequest("version", undefined, { baseUrl })).rejects.toBeInstanceOf(
89+
TypeError,
90+
);
91+
},
92+
);
93+
7494
it("returns parsed RPC result", async () => {
7595
const baseUrl = await withSignalServer(async (req, res) => {
7696
expect(req.method).toBe("POST");
@@ -263,6 +283,15 @@ describe("signalRpcRequest", () => {
263283
});
264284

265285
describe("signalCheck", () => {
286+
it("uses only the status when the unused response body is malformed UTF-8", async () => {
287+
const baseUrl = await withSignalServer((_req, res) => {
288+
res.writeHead(200);
289+
res.end(Buffer.from([0xff]));
290+
});
291+
292+
await expect(signalCheck(baseUrl)).resolves.toEqual({ ok: true, status: 200, error: null });
293+
});
294+
266295
it("returns ok for a healthy signal-cli check", async () => {
267296
const baseUrl = await withSignalServer((req, res) => {
268297
expect(req.method).toBe("GET");

‎extensions/signal/src/client.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ const MAX_SIGNAL_SSE_EVENT_DATA_BYTES = 1_048_576;
3838

3939
type SignalHttpResponse = {
4040
status: number;
41-
statusText: string;
42-
text: string;
41+
body: Buffer;
4342
};
4443

4544
function createSignalSseAbortError(): Error {
@@ -116,7 +115,7 @@ function normalizeSignalSseTimeoutMs(timeoutMs: number): number | null {
116115
return resolveTimerTimeoutMs(timeoutMs, DEFAULT_TIMEOUT_MS);
117116
}
118117

119-
function requestSignalHttpText(
118+
function requestSignalHttp(
120119
url: URL,
121120
options: {
122121
method: "GET" | "POST";
@@ -181,8 +180,7 @@ function requestSignalHttpText(
181180
res.on("end", () => {
182181
resolveOnce({
183182
status: res.statusCode ?? 0,
184-
statusText: res.statusMessage || "error",
185-
text: Buffer.concat(chunks).toString("utf8"),
183+
body: Buffer.concat(chunks),
186184
});
187185
});
188186
},
@@ -210,7 +208,7 @@ export async function signalRpcRequest<T = unknown>(
210208
params,
211209
id,
212210
});
213-
const res = await requestSignalHttpText(resolveSignalEndpointUrl(opts.baseUrl, "/api/v1/rpc"), {
211+
const res = await requestSignalHttp(resolveSignalEndpointUrl(opts.baseUrl, "/api/v1/rpc"), {
214212
method: "POST",
215213
headers: {
216214
"Content-Type": "application/json",
@@ -223,10 +221,12 @@ export async function signalRpcRequest<T = unknown>(
223221
if (res.status === 201) {
224222
return undefined as T;
225223
}
226-
if (!res.text) {
224+
// Decode JSON bodies here; health checks use status without interpreting their bytes.
225+
const text = new TextDecoder("utf-8", { fatal: true }).decode(res.body);
226+
if (!text) {
227227
throw new Error(`Signal RPC empty response (status ${res.status})`);
228228
}
229-
const parsed = parseSignalRpcResponse<T>(res.text, res.status);
229+
const parsed = parseSignalRpcResponse<T>(text, res.status);
230230
if (parsed.error) {
231231
const code = parsed.error.code ?? "unknown";
232232
const msg = parsed.error.message ?? "Signal RPC error";
@@ -240,7 +240,7 @@ export async function signalCheck(
240240
timeoutMs = DEFAULT_TIMEOUT_MS,
241241
): Promise<{ ok: boolean; status?: number | null; error?: string | null }> {
242242
try {
243-
const res = await requestSignalHttpText(resolveSignalEndpointUrl(baseUrl, "/api/v1/check"), {
243+
const res = await requestSignalHttp(resolveSignalEndpointUrl(baseUrl, "/api/v1/check"), {
244244
method: "GET",
245245
timeoutMs,
246246
});

0 commit comments

Comments
 (0)