fix(qoder): prevent signed request replay and surface upstream errors

This commit is contained in:
yiwen65
2026-09-22 15:01:06 +07:00
parent 0d50fe3610
commit 782c137b1f
6 changed files with 243 additions and 51 deletions
+7 -6
View File
@@ -506,14 +506,14 @@ describe("wrapQoderSSE", () => {
// Regression for review finding #3: chunks could leak past [DONE] when
// the success branch had no doneEmitted guard. We synthesize an error
// envelope (which sets doneEmitted=true) followed by a valid envelope
// envelope after content (which sets doneEmitted=true), followed by a valid envelope
// and assert the second envelope is NOT forwarded.
it("does not forward chunks after [DONE] has been emitted", async () => {
const errorEnv = JSON.stringify({ statusCodeValue: 500, body: "boom" });
const validInner = JSON.stringify({ choices: [{ delta: { content: "leak" } }] });
const validEnv = JSON.stringify({ statusCodeValue: 200, body: validInner });
const wrapped = await wrapQoderSSE(
makeResponse([`data: ${errorEnv}\n\ndata: ${validEnv}\n\n`]),
makeResponse([envelope(JSON.stringify({ choices: [{ delta: { content: "hi" } }] })) + `data: ${errorEnv}\n\ndata: ${validEnv}\n\n`]),
"qoder/auto",
);
const out = await drain(wrapped);
@@ -539,12 +539,13 @@ describe("wrapQoderSSE", () => {
expect(() => JSON.parse(dataLine.slice("data: ".length))).not.toThrow();
});
it("upstream error envelope produces an error chunk + [DONE]", async () => {
it("upstream first-frame error envelope produces an HTTP error", async () => {
const env = JSON.stringify({ statusCodeValue: 503, body: "service unavailable" });
const wrapped = await wrapQoderSSE(makeResponse([`data: ${env}\n\n`]), "qoder/lite");
const out = await drain(wrapped);
expect(out).toContain("[qoder error 503");
expect(out).toContain("data: [DONE]\n\n");
expect(wrapped.status).toBe(503);
expect(await wrapped.json()).toEqual({
error: { message: "service unavailable", code: 503 },
});
});
it("non-ok responses are returned unchanged (no transform)", async () => {