|
| 1 | +import { afterEach, describe, expect } from "bun:test" |
| 2 | +import { Effect } from "effect" |
| 3 | +import { eq } from "drizzle-orm" |
| 4 | +import * as Database from "@/storage/db" |
| 5 | +import { ModelID, ProviderID } from "../../src/provider/schema" |
| 6 | +import { WithInstance } from "../../src/project/with-instance" |
| 7 | +import { Server } from "../../src/server/server" |
| 8 | +import { Session } from "@/session/session" |
| 9 | +import { SessionPaths } from "../../src/server/routes/instance/httpapi/groups/session" |
| 10 | +import { SyncPaths } from "../../src/server/routes/instance/httpapi/groups/sync" |
| 11 | +import { MessageID, PartID } from "../../src/session/schema" |
| 12 | +import { PartTable } from "@/session/session.sql" |
| 13 | +import { resetDatabase } from "../fixture/db" |
| 14 | +import { disposeAllInstances, tmpdir } from "../fixture/fixture" |
| 15 | +import { it } from "../lib/effect" |
| 16 | + |
| 17 | +afterEach(async () => { |
| 18 | + await disposeAllInstances() |
| 19 | + await resetDatabase() |
| 20 | +}) |
| 21 | + |
| 22 | +const withTmp = <A, E, R>( |
| 23 | + options: Parameters<typeof tmpdir>[0], |
| 24 | + fn: (tmp: Awaited<ReturnType<typeof tmpdir>>) => Effect.Effect<A, E, R>, |
| 25 | +) => |
| 26 | + Effect.acquireRelease( |
| 27 | + Effect.promise(() => tmpdir(options)), |
| 28 | + (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()), |
| 29 | + ).pipe(Effect.flatMap(fn)) |
| 30 | + |
| 31 | +async function seedCorruptStepFinishPart(directory: string) { |
| 32 | + return WithInstance.provide({ |
| 33 | + directory, |
| 34 | + fn: () => |
| 35 | + Effect.runPromise( |
| 36 | + Effect.gen(function* () { |
| 37 | + const session = yield* Session.Service |
| 38 | + const info = yield* session.create({}) |
| 39 | + const message = yield* session.updateMessage({ |
| 40 | + id: MessageID.ascending(), |
| 41 | + role: "user", |
| 42 | + sessionID: info.id, |
| 43 | + agent: "build", |
| 44 | + model: { providerID: ProviderID.make("test"), modelID: ModelID.make("test") }, |
| 45 | + time: { created: Date.now() }, |
| 46 | + }) |
| 47 | + const partID = PartID.ascending() |
| 48 | + yield* session.updatePart({ |
| 49 | + id: partID, |
| 50 | + sessionID: info.id, |
| 51 | + messageID: message.id, |
| 52 | + type: "step-finish", |
| 53 | + reason: "stop", |
| 54 | + cost: 0, |
| 55 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 56 | + }) |
| 57 | + // Schema.Finite still rejects NaN at encode — exact mirror of the |
| 58 | + // corrupt row that broke the user's session in the OMO/Windows bug. |
| 59 | + Database.use((db) => |
| 60 | + db |
| 61 | + .update(PartTable) |
| 62 | + .set({ |
| 63 | + data: { |
| 64 | + type: "step-finish", |
| 65 | + reason: "stop", |
| 66 | + cost: 0, |
| 67 | + tokens: { input: 0, output: NaN, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 68 | + } as never, // drizzle's .set() can't narrow the discriminated union |
| 69 | + }) |
| 70 | + .where(eq(PartTable.id, partID)) |
| 71 | + .run(), |
| 72 | + ) |
| 73 | + return info.id |
| 74 | + }).pipe(Effect.provide(Session.defaultLayer)), |
| 75 | + ), |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +describe("schema-rejection wire shape", () => { |
| 80 | + it.live( |
| 81 | + "Payload schema rejection returns NamedError-shaped JSON, not empty", |
| 82 | + withTmp({ git: true, config: { formatter: false, lsp: false } }, (tmp) => |
| 83 | + Effect.gen(function* () { |
| 84 | + const res = yield* Effect.promise(async () => |
| 85 | + Server.Default().app.request(SyncPaths.history, { |
| 86 | + method: "POST", |
| 87 | + headers: { "x-opencode-directory": tmp.path, "content-type": "application/json" }, |
| 88 | + body: JSON.stringify({ aggregate: -1 }), |
| 89 | + }), |
| 90 | + ) |
| 91 | + const body = yield* Effect.promise(async () => res.text()) |
| 92 | + expect(res.status).toBe(400) |
| 93 | + expect(res.headers.get("content-type") ?? "").toContain("application/json") |
| 94 | + const parsed = JSON.parse(body) |
| 95 | + expect(parsed).toMatchObject({ |
| 96 | + name: "BadRequest", |
| 97 | + data: { kind: expect.stringMatching(/^(Body|Payload)$/) }, |
| 98 | + }) |
| 99 | + expect(parsed.data.message).toEqual(expect.any(String)) |
| 100 | + expect(parsed.data.message.length).toBeGreaterThan(0) |
| 101 | + }), |
| 102 | + ), |
| 103 | + ) |
| 104 | + |
| 105 | + it.live( |
| 106 | + "Query schema rejection returns NamedError-shaped JSON", |
| 107 | + withTmp({ git: true, config: { formatter: false, lsp: false } }, (tmp) => |
| 108 | + Effect.gen(function* () { |
| 109 | + // /find/file?limit=999999 violates the limit constraint check. |
| 110 | + const url = `/find/file?query=foo&limit=999999&directory=${encodeURIComponent(tmp.path)}` |
| 111 | + const res = yield* Effect.promise(async () => Server.Default().app.request(url)) |
| 112 | + const body = yield* Effect.promise(async () => res.text()) |
| 113 | + expect(res.status).toBe(400) |
| 114 | + const parsed = JSON.parse(body) |
| 115 | + expect(parsed).toMatchObject({ name: "BadRequest", data: { kind: "Query" } }) |
| 116 | + }), |
| 117 | + ), |
| 118 | + ) |
| 119 | + |
| 120 | + it.live( |
| 121 | + "rejected request body never echoes back unbounded — message is capped", |
| 122 | + // Defense against DoS-amplification + secret-echo: Effect's Issue formatter |
| 123 | + // dumps the rejected `actual` verbatim. A multi-MB invalid array would |
| 124 | + // become a multi-MB 400 response and log line. Cap kicks in around 1KB. |
| 125 | + withTmp({ git: true, config: { formatter: false, lsp: false } }, (tmp) => |
| 126 | + Effect.gen(function* () { |
| 127 | + const huge = "X".repeat(50_000) |
| 128 | + const res = yield* Effect.promise(async () => |
| 129 | + Server.Default().app.request(SyncPaths.history, { |
| 130 | + method: "POST", |
| 131 | + headers: { "x-opencode-directory": tmp.path, "content-type": "application/json" }, |
| 132 | + body: JSON.stringify({ aggregate: huge }), |
| 133 | + }), |
| 134 | + ) |
| 135 | + const body = yield* Effect.promise(async () => res.text()) |
| 136 | + expect(res.status).toBe(400) |
| 137 | + // 1 KB cap + small JSON envelope ≈ <2 KB — never tens of KB. |
| 138 | + expect(body.length).toBeLessThan(2 * 1024) |
| 139 | + const parsed = JSON.parse(body) |
| 140 | + expect(parsed.data.message).not.toContain(huge) |
| 141 | + }), |
| 142 | + ), |
| 143 | + ) |
| 144 | + |
| 145 | + it.live( |
| 146 | + "response-encode failure: corrupted stored row returns NamedError-shaped JSON with field path", |
| 147 | + withTmp({ config: { formatter: false, lsp: false } }, (tmp) => |
| 148 | + Effect.gen(function* () { |
| 149 | + const sessionID = yield* Effect.promise(() => seedCorruptStepFinishPart(tmp.path)) |
| 150 | + const url = `${SessionPaths.messages.replace(":sessionID", sessionID)}?limit=80&directory=${encodeURIComponent(tmp.path)}` |
| 151 | + const res = yield* Effect.promise(async () => Server.Default().app.request(url)) |
| 152 | + const body = yield* Effect.promise(async () => res.text()) |
| 153 | + expect(res.status).toBe(400) |
| 154 | + expect(res.headers.get("content-type") ?? "").toContain("application/json") |
| 155 | + const parsed = JSON.parse(body) |
| 156 | + expect(parsed).toMatchObject({ name: "BadRequest", data: { kind: "Body" } }) |
| 157 | + // Field path in data.message — what made this PR worth shipping. |
| 158 | + expect(parsed.data.message).toMatch(/output/) |
| 159 | + }), |
| 160 | + ), |
| 161 | + ) |
| 162 | +}) |
0 commit comments