From 2ff88feb305814beb6c74d994db7857eca1739fd Mon Sep 17 00:00:00 2001 From: repo-worker-code-g Date: Sat, 7 Nov 2026 14:16:35 -0600 Subject: [PATCH] fix(github-ci): validate the Turso pipeline response envelope (#6949) batch() decoded the /v2/pipeline body with a type assertion and read .results unguarded: a null 200 body escaped as a raw TypeError, a results-less 200 ({} or a scalar) resolved execute() as undefined and was read by callers as a successful ci_events/sink write, and a non-JSON body escaped as a bare SyntaxError. Validate the envelope in the client: non-JSON, non-object, and missing/non-array results all throw in the existing contract form (turso pipeline failed: malformed response), and execute() throws when the statement result is absent. Regression tests: test/turso.test.ts (7 cases) plus an end-to-end worker test that a results-less Turso 200 fails the delivery with 500 instead of answering ok. Suite 18/18 on the branch; the 6 new shape tests fail pre-fix. --- pkgs/github-ci/worker/src/turso.ts | 25 +++++-- pkgs/github-ci/worker/test/turso.test.ts | 86 +++++++++++++++++++++++ pkgs/github-ci/worker/test/worker.test.ts | 7 ++ 3 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 pkgs/github-ci/worker/test/turso.test.ts diff --git a/pkgs/github-ci/worker/src/turso.ts b/pkgs/github-ci/worker/src/turso.ts index c57cc6800..95629e45d 100644 --- a/pkgs/github-ci/worker/src/turso.ts +++ b/pkgs/github-ci/worker/src/turso.ts @@ -53,17 +53,34 @@ export class Turso { }), }); if (!res.ok) throw new Error(`turso pipeline failed: HTTP ${res.status}`); - const out = (await res.json()) as { results?: PipelineResult[] }; - const failed = (out.results ?? []).find((r) => r.type === "error"); + // The envelope is validated before anything reads it (#6949): a 200 + // whose body is not the pipeline shape (null, a scalar, an object + // without a results array, non-JSON) must fail in this client's own + // contract form, never resolve as a successful write or escape as a + // raw TypeError/SyntaxError from a downstream dereference. + let out: { results?: PipelineResult[] }; + try { + out = (await res.json()) as { results?: PipelineResult[] }; + } catch { + throw new Error("turso pipeline failed: malformed response (not JSON)"); + } + if (!out || typeof out !== "object" || !Array.isArray(out.results)) { + throw new Error("turso pipeline failed: malformed response (missing results)"); + } + const failed = out.results.find((r) => r?.type === "error"); if (failed) { throw new Error(`turso pipeline failed: ${failed.error?.message ?? "pipeline error"}`); } - return out.results ?? []; + return out.results; } async execute(sql: string, args?: TursoArg[]): Promise { const results = await this.batch([{ sql, args }]); - return results[0]; + const first: PipelineResult | undefined = results[0]; + if (!first || typeof first !== "object") { + throw new Error("turso pipeline failed: malformed response (missing statement result)"); + } + return first; } /** First column of first row as text, or null when no row. */ diff --git a/pkgs/github-ci/worker/test/turso.test.ts b/pkgs/github-ci/worker/test/turso.test.ts new file mode 100644 index 000000000..5b7cd0423 --- /dev/null +++ b/pkgs/github-ci/worker/test/turso.test.ts @@ -0,0 +1,86 @@ +import { fetchMock } from "cloudflare:test"; +import { afterEach, beforeAll, describe, expect, it } from "vitest"; +import { Turso } from "../src/turso"; + +// Response-envelope contract for the Turso pipeline client (#6949): +// a 200 whose body is not the pipeline shape must fail as +// "turso pipeline failed: …", never resolve as a successful write and +// never escape as a raw TypeError/SyntaxError. +const DB = "https://turso-shape.test"; + +beforeAll(() => { + fetchMock.activate(); + fetchMock.disableNetConnect(); +}); +afterEach(() => fetchMock.assertNoPendingInterceptors()); + +function replyOnce(body: string, contentType = "application/json") { + fetchMock + .get(DB) + .intercept({ path: "/v2/pipeline", method: "POST" }) + .reply(200, body, { headers: { "content-type": contentType } }); +} + +const client = () => new Turso(DB, "test-token"); + +const okEnvelope = JSON.stringify({ + results: [ + { type: "ok", response: { result: { rows: [[{ type: "text", value: "main" }]] } } }, + { type: "ok" }, + ], +}); + +describe("Turso pipeline response envelope", () => { + it("resolves a well-formed envelope", async () => { + replyOnce(okEnvelope); + await expect(client().queryText("SELECT default_branch FROM t")).resolves.toBe("main"); + }); + + it("keeps the per-statement error contract", async () => { + replyOnce(JSON.stringify({ results: [{ type: "error", error: { message: "no such table" } }] })); + await expect(client().execute("SELECT 1")).rejects.toThrow("turso pipeline failed: no such table"); + }); + + it("rejects a null body as malformed, not a TypeError", async () => { + replyOnce("null"); + await expect(client().execute("INSERT INTO ci_events VALUES (1)")).rejects.toThrow( + "turso pipeline failed: malformed response (missing results)", + ); + }); + + it("rejects a results-less object body instead of reading the write as success", async () => { + replyOnce("{}"); + await expect(client().execute("INSERT INTO ci_events VALUES (1)")).rejects.toThrow( + "turso pipeline failed: malformed response (missing results)", + ); + }); + + it("rejects a scalar body and a queryText on a results-less body", async () => { + replyOnce('"ok"'); + await expect(client().execute("INSERT INTO ci_events VALUES (1)")).rejects.toThrow( + "turso pipeline failed: malformed response (missing results)", + ); + replyOnce("{}"); + await expect(client().queryText("SELECT default_branch FROM t")).rejects.toThrow( + "turso pipeline failed: malformed response (missing results)", + ); + }); + + it("rejects a non-JSON body in contract form, not a raw SyntaxError", async () => { + replyOnce("bad gateway", "text/html"); + await expect(client().execute("SELECT 1")).rejects.toThrow( + "turso pipeline failed: malformed response (not JSON)", + ); + }); + + it("rejects an envelope whose results lack the statement result", async () => { + replyOnce(JSON.stringify({ results: [] })); + await expect(client().execute("SELECT 1")).rejects.toThrow( + "turso pipeline failed: malformed response (missing statement result)", + ); + replyOnce(JSON.stringify({ results: [null, { type: "ok" }] })); + await expect(client().execute("SELECT 1")).rejects.toThrow( + "turso pipeline failed: malformed response (missing statement result)", + ); + }); +}); diff --git a/pkgs/github-ci/worker/test/worker.test.ts b/pkgs/github-ci/worker/test/worker.test.ts index 238bfe1b0..a99cd1ee9 100644 --- a/pkgs/github-ci/worker/test/worker.test.ts +++ b/pkgs/github-ci/worker/test/worker.test.ts @@ -93,6 +93,13 @@ describe("github-ci worker", () => { expect(sqlSeen).toContain("github:developing-today/code:999:1:completed"); }); + it("fails the delivery (500) when Turso answers a results-less 200 for the ci_events insert (#6949)", async () => { + fetchMock.get(PLATFORM).intercept({ path: "/v2/pipeline", method: "POST" }).reply(200, "{}"); + const res = await post(runPayload, "workflow_run", await sign(runPayload)); + expect(res.status).toBe(500); + expect(await res.text()).toContain("turso pipeline failed: malformed response"); + }); + it("skips non-completed runs without touching Turso", async () => { const p = JSON.stringify({ action: "requested",