Skip to content
Merged
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
64 changes: 0 additions & 64 deletions packages/cli/src/__tests__/history.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { filterHistory, HISTORY_SCHEMA_VERSION, loadHistory, saveSpawnRecord } from "../history.js";
import { getHistoryPath, getSpawnDir, getUserHome } from "../shared/paths.js";

describe("history", () => {
let testDir: string;
Expand Down Expand Up @@ -32,69 +31,6 @@ describe("history", () => {
}
});

// ── getSpawnDir ─────────────────────────────────────────────────────────

describe("getSpawnDir", () => {
it("returns SPAWN_HOME when set to valid path within home", () => {
const validPath = join(process.env.HOME ?? "", "custom", "spawn", "dir");
process.env.SPAWN_HOME = validPath;
expect(getSpawnDir()).toBe(validPath);
});

it("falls back to ~/.spawn when SPAWN_HOME is not set", () => {
delete process.env.SPAWN_HOME;
expect(getSpawnDir()).toBe(join(getUserHome(), ".spawn"));
});

it("throws for relative SPAWN_HOME path", () => {
process.env.SPAWN_HOME = "relative/path";
expect(() => getSpawnDir()).toThrow("must be an absolute path");
});

it("throws for dot-relative SPAWN_HOME path", () => {
process.env.SPAWN_HOME = "./local/dir";
expect(() => getSpawnDir()).toThrow("must be an absolute path");
});

it("resolves .. segments in absolute SPAWN_HOME within home", () => {
const pathWithDots = join(process.env.HOME ?? "", "foo", "..", "bar");
process.env.SPAWN_HOME = pathWithDots;
expect(getSpawnDir()).toBe(join(process.env.HOME ?? "", "bar"));
});

it("accepts normal absolute SPAWN_HOME within home", () => {
const validPath = join(process.env.HOME ?? "", ".spawn");
process.env.SPAWN_HOME = validPath;
expect(getSpawnDir()).toBe(validPath);
});

it("throws for SPAWN_HOME outside home directory", () => {
process.env.SPAWN_HOME = "/tmp/spawn";
expect(() => getSpawnDir()).toThrow("must be within your home directory");
});

it("throws for path traversal attempt to escape home directory", () => {
// Attempt to traverse outside home using .. segments
// e.g., /home/user/../../etc/.spawn
const traversalPath = join(process.env.HOME ?? "", "..", "..", "etc", ".spawn");
process.env.SPAWN_HOME = traversalPath;
expect(() => getSpawnDir()).toThrow("must be within your home directory");
});

it("accepts home directory itself as SPAWN_HOME", () => {
process.env.SPAWN_HOME = process.env.HOME ?? "";
expect(getSpawnDir()).toBe(process.env.HOME ?? "");
});
});

// ── getHistoryPath ──────────────────────────────────────────────────────

describe("getHistoryPath", () => {
it("returns history.json inside spawn dir", () => {
expect(getHistoryPath()).toBe(join(testDir, "history.json"));
});
});

// ── loadHistory ─────────────────────────────────────────────────────────

describe("loadHistory", () => {
Expand Down
21 changes: 21 additions & 0 deletions packages/cli/src/__tests__/paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,31 @@ describe("paths", () => {
expect(() => getSpawnDir()).toThrow("must be an absolute path");
});

it("rejects dot-relative SPAWN_HOME", () => {
process.env.SPAWN_HOME = "./local/dir";
expect(() => getSpawnDir()).toThrow("must be an absolute path");
});

it("resolves .. segments in absolute SPAWN_HOME within home", () => {
const pathWithDots = join(getUserHome(), "foo", "..", "bar");
process.env.SPAWN_HOME = pathWithDots;
expect(getSpawnDir()).toBe(join(getUserHome(), "bar"));
});

it("rejects SPAWN_HOME outside home directory", () => {
process.env.SPAWN_HOME = "/tmp/spawn";
expect(() => getSpawnDir()).toThrow("must be within your home directory");
});

it("rejects path traversal outside home directory", () => {
process.env.SPAWN_HOME = "/tmp/../../root/.spawn";
expect(() => getSpawnDir()).toThrow("must be within your home directory");
});

it("accepts home directory itself as SPAWN_HOME", () => {
process.env.SPAWN_HOME = getUserHome();
expect(getSpawnDir()).toBe(getUserHome());
});
});

describe("getHistoryPath", () => {
Expand Down