Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/server/api/checkpoint-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ export async function handleCheckpointDelete(
const rootDir = ctx.getCurrentCwd?.();
if (!rootDir) return { status: 400, body: { error: "no active workspace" } };

let parsed: { id?: string };
let parsed: { id?: unknown };
try {
parsed = JSON.parse(body);
} catch {
return { status: 400, body: { error: "invalid JSON" } };
}
if (!parsed.id) return { status: 400, body: { error: "missing id" } };
if (typeof parsed.id !== "string" || !parsed.id) {
return { status: 400, body: { error: "missing id" } };
}

const ok = deleteCheckpoint(rootDir, parsed.id);
return ok
Expand Down
6 changes: 4 additions & 2 deletions src/server/api/checkpoint-restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ export async function handleCheckpointRestore(
const rootDir = ctx.getCurrentCwd?.();
if (!rootDir) return { status: 400, body: { error: "no active workspace" } };

let parsed: { id?: string };
let parsed: { id?: unknown };
try {
parsed = JSON.parse(body);
} catch {
return { status: 400, body: { error: "invalid JSON" } };
}
if (!parsed.id) return { status: 400, body: { error: "missing id" } };
if (typeof parsed.id !== "string" || !parsed.id) {
return { status: 400, body: { error: "missing id" } };
}

const result: RestoreResult = restoreCheckpoint(rootDir, parsed.id);
return { status: 200, body: result };
Expand Down
40 changes: 40 additions & 0 deletions tests/dashboard-checkpoint-id.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { handleCheckpointDelete } from "../src/server/api/checkpoint-delete.js";
import { handleCheckpointRestore } from "../src/server/api/checkpoint-restore.js";
import type { DashboardContext } from "../src/server/context.js";

let root: string;

function ctx(): DashboardContext {
return {
mode: "standalone",
configPath: join(root, ".carboncode", "config.json"),
usageLogPath: join(root, ".carboncode", "usage.jsonl"),
getCurrentCwd: () => root,
};
}

beforeEach(() => {
root = mkdtempSync(join(tmpdir(), "carboncode-checkpoint-id-"));
});

afterEach(() => {
rmSync(root, { recursive: true, force: true });
});

describe("dashboard checkpoint id validation", () => {
it("rejects non-string restore ids", async () => {
const result = await handleCheckpointRestore("POST", [], JSON.stringify({ id: 123 }), ctx());

expect(result).toEqual({ status: 400, body: { error: "missing id" } });
});

it("rejects non-string delete ids", async () => {
const result = await handleCheckpointDelete("POST", [], JSON.stringify({ id: ["cp"] }), ctx());

expect(result).toEqual({ status: 400, body: { error: "missing id" } });
});
});
Loading