Skip to content

Commit a172857

Browse files
authored
fix(memory-host-sdk): decode JSON response bodies with fatal UTF-8 validation (#111279)
1 parent 94d6f1f commit a172857

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎packages/memory-host-sdk/src/host/response-snippet.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,16 @@ describe("readMemoryHostResponseTextSnippet", () => {
9696
await expect(read).rejects.toThrow("json aborted");
9797
expect(canceled).toBe(true);
9898
});
99+
100+
it("rejects a JSON body with invalid UTF-8 bytes", async () => {
101+
const body = new Uint8Array([
102+
...new TextEncoder().encode('{"ok":"val'),
103+
0xff,
104+
...new TextEncoder().encode('ue"}'),
105+
]);
106+
107+
await expect(
108+
readResponseJsonWithLimit(new Response(body), { errorPrefix: "remote memory" }),
109+
).rejects.toThrow(/not valid for encoding/);
110+
});
99111
});

‎packages/memory-host-sdk/src/host/response-snippet.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ async function readResponseTextWithLimit(
204204
} catch {}
205205
}
206206

207-
return new TextDecoder().decode(joinChunks(chunks, length));
207+
return new TextDecoder("utf-8", { fatal: true }).decode(joinChunks(chunks, length));
208208
}
209209

210210
async function cancelResponseBody(res: Response): Promise<void> {

0 commit comments

Comments
 (0)