import { afterEach, describe, expect, test } from "bun:test"; import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { writeSecureFile } from "./secure-file.js"; /** * Direct unit tests for the credential write path. * * Indirect coverage exists through loader/token-store mode assertions, but * this helper is the single choke point that keeps existing world-readable * credential files from staying world-readable — the P0 from the 2026-10-17 * architecture review. Its guarantees deserve their own colocated tests so a * refactor of either caller cannot silently drop them. */ const tempDirs: string[] = []; function tempDir(): string { const dir = mkdtempSync(join(tmpdir(), "seal-secure-file-")); tempDirs.push(dir); return dir; } afterEach(() => { for (const dir of tempDirs.splice(0)) { rmSync(dir, { recursive: true, force: true }); } }); function fileMode(path: string): number { return statSync(path).mode & 0o777; } describe("writeSecureFile", () => { test("creates a new credential file as 0600 with exact content", () => { const dir = tempDir(); const target = join(dir, "tokens.json"); writeSecureFile(target, '{"token":"secret"}\n'); expect(fileMode(target)).toBe(0o600); expect(readFileSync(target, "utf8")).toBe('{"token":"secret"}\n'); }); test("tightens a pre-existing 0644 file to 0600 on write", () => { const dir = tempDir(); const target = join(dir, "corp.json"); writeFileSync(target, '{"old":"legacy-inherited"}', { mode: 0o644 }); chmodSync(target, 0o644); expect(fileMode(target)).toBe(0o644); writeSecureFile(target, '{"new":"rotated-secret"}'); expect(fileMode(target)).toBe(0o600); expect(readFileSync(target, "utf8")).toBe('{"new":"rotated-secret"}'); }); test("truncates longer previous content so no secret residue survives", () => { const dir = tempDir(); const target = join(dir, "tokens.json"); writeSecureFile(target, '{"access":"a","refresh":"b","extra":"padding-padding"}'); writeSecureFile(target, "{}"); expect(readFileSync(target, "utf8")).toBe("{}"); }); test("accepts binary content such as embedded skill assets", () => { const dir = tempDir(); const target = join(dir, "asset.png"); const bytes = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x00, 0xff]); writeSecureFile(target, bytes); expect(fileMode(target)).toBe(0o600); expect(new Uint8Array(readFileSync(target))).toEqual(bytes); }); test("creates missing parent directories as 0700", () => { const dir = tempDir(); const target = join(dir, "nested", "deeper", "tokens.json"); writeSecureFile(target, "{}"); expect(fileMode(target)).toBe(0o600); expect(fileMode(join(dir, "nested"))).toBe(0o700); expect(fileMode(join(dir, "nested", "deeper"))).toBe(0o700); }); test("does not rewrite the mode of a user-owned parent directory", () => { const dir = tempDir(); const shared = join(dir, "shared"); mkdirSync(shared, { mode: 0o755 }); chmodSync(shared, 0o755); writeSecureFile(join(shared, "corp.json"), "{}"); expect(fileMode(shared)).toBe(0o755); expect(fileMode(join(shared, "corp.json"))).toBe(0o600); }); });