|
1 | 1 | import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"; |
2 | | -import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmSync, truncateSync, unlinkSync, writeFileSync } from "node:fs"; |
| 2 | +import { chmodSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, renameSync, rmSync, symlinkSync, truncateSync, unlinkSync, writeFileSync } from "node:fs"; |
3 | 3 | import { homedir, tmpdir } from "node:os"; |
4 | 4 | import { join, resolve } from "node:path"; |
5 | 5 | import { |
@@ -27,7 +27,7 @@ import { |
27 | 27 | } from "../src/config"; |
28 | 28 |
|
29 | 29 | import * as windowsAcl from "../src/lib/windows-secret-acl"; |
30 | | -import { AtomicWriteResidualTempError, atomicWriteFile, hardenConfigDir, hardenExistingSecret, renameAtomicFile, saveConfig } from "../src/config"; |
| 30 | +import { AtomicWriteResidualTempError, atomicWriteFile, atomicWriteFileAsync, hardenConfigDir, hardenExistingSecret, renameAtomicFile, saveConfig } from "../src/config"; |
31 | 31 | let testDir = ""; |
32 | 32 |
|
33 | 33 | beforeEach(() => { |
@@ -1700,3 +1700,115 @@ describe("config.ts – Windows ACL hardening integration", () => { |
1700 | 1700 | spy.mockRestore(); |
1701 | 1701 | }); |
1702 | 1702 | }); |
| 1703 | + |
| 1704 | +describe("config.ts – atomic writes preserve symlinked destinations", () => { |
| 1705 | + test("a symlinked destination survives the write and the real file receives it", () => { |
| 1706 | + // Dotfiles shape: ~/.codex/config.toml -> ~/dotfiles/.codex/config.toml |
| 1707 | + const repoDir = join(testDir, "dotfiles"); |
| 1708 | + mkdirSync(repoDir, { recursive: true }); |
| 1709 | + const realFile = join(repoDir, "config.toml"); |
| 1710 | + writeFileSync(realFile, "original", "utf-8"); |
| 1711 | + const link = join(testDir, "config.toml"); |
| 1712 | + symlinkSync(realFile, link); |
| 1713 | + |
| 1714 | + atomicWriteFile(link, "rewritten"); |
| 1715 | + |
| 1716 | + expect(lstatSync(link).isSymbolicLink()).toBe(true); |
| 1717 | + expect(readlinkSync(link)).toBe(realFile); |
| 1718 | + expect(readFileSync(realFile, "utf8")).toBe("rewritten"); |
| 1719 | + expect(readFileSync(link, "utf8")).toBe("rewritten"); |
| 1720 | + }); |
| 1721 | + |
| 1722 | + test("no temp file is left beside the link or its target", () => { |
| 1723 | + const repoDir = join(testDir, "dotfiles-clean"); |
| 1724 | + mkdirSync(repoDir, { recursive: true }); |
| 1725 | + const realFile = join(repoDir, "config.toml"); |
| 1726 | + writeFileSync(realFile, "original", "utf-8"); |
| 1727 | + const link = join(testDir, "config-clean.toml"); |
| 1728 | + symlinkSync(realFile, link); |
| 1729 | + |
| 1730 | + atomicWriteFile(link, "rewritten"); |
| 1731 | + |
| 1732 | + expect(readdirSync(repoDir).filter(name => name.includes(".ocx."))).toEqual([]); |
| 1733 | + expect(readdirSync(testDir).filter(name => name.includes(".ocx."))).toEqual([]); |
| 1734 | + }); |
| 1735 | + |
| 1736 | + test("a plain destination is unaffected", () => { |
| 1737 | + const destination = join(testDir, "plain.toml"); |
| 1738 | + atomicWriteFile(destination, "first"); |
| 1739 | + atomicWriteFile(destination, "second"); |
| 1740 | + |
| 1741 | + expect(lstatSync(destination).isSymbolicLink()).toBe(false); |
| 1742 | + expect(readFileSync(destination, "utf8")).toBe("second"); |
| 1743 | + }); |
| 1744 | + |
| 1745 | + test("a destination that does not exist yet is created at the literal path", () => { |
| 1746 | + const destination = join(testDir, "created.toml"); |
| 1747 | + expect(existsSync(destination)).toBe(false); |
| 1748 | + |
| 1749 | + atomicWriteFile(destination, "fresh"); |
| 1750 | + |
| 1751 | + expect(readFileSync(destination, "utf8")).toBe("fresh"); |
| 1752 | + }); |
| 1753 | + |
| 1754 | + test("a dangling symlink is preserved and the write is refused", () => { |
| 1755 | + const link = join(testDir, "dangling.toml"); |
| 1756 | + symlinkSync(join(testDir, "gone", "config.toml"), link); |
| 1757 | + |
| 1758 | + // The target volume may only be temporarily unavailable; replacing the link |
| 1759 | + // would recreate the dotfiles divergence this fix exists to prevent. |
| 1760 | + expect(() => atomicWriteFile(link, "recovered")).toThrow(/unresolvable symlinked write target/); |
| 1761 | + expect(lstatSync(link).isSymbolicLink()).toBe(true); |
| 1762 | + expect(existsSync(join(testDir, "gone"))).toBe(false); |
| 1763 | + }); |
| 1764 | +}); |
| 1765 | + |
| 1766 | +describe("config.ts – async atomic writes preserve symlinked destinations", () => { |
| 1767 | + test("a symlinked destination survives the write and the real file receives it", async () => { |
| 1768 | + const repoDir = join(testDir, "dotfiles-async"); |
| 1769 | + mkdirSync(repoDir, { recursive: true }); |
| 1770 | + const realFile = join(repoDir, "config.toml"); |
| 1771 | + writeFileSync(realFile, "original", "utf-8"); |
| 1772 | + const link = join(testDir, "config-async.toml"); |
| 1773 | + symlinkSync(realFile, link); |
| 1774 | + |
| 1775 | + await atomicWriteFileAsync(link, "rewritten"); |
| 1776 | + |
| 1777 | + expect(lstatSync(link).isSymbolicLink()).toBe(true); |
| 1778 | + expect(readlinkSync(link)).toBe(realFile); |
| 1779 | + expect(readFileSync(realFile, "utf8")).toBe("rewritten"); |
| 1780 | + expect(readFileSync(link, "utf8")).toBe("rewritten"); |
| 1781 | + }); |
| 1782 | + |
| 1783 | + test("no temp file is left beside the link or its target", async () => { |
| 1784 | + const repoDir = join(testDir, "dotfiles-async-clean"); |
| 1785 | + mkdirSync(repoDir, { recursive: true }); |
| 1786 | + const realFile = join(repoDir, "config.toml"); |
| 1787 | + writeFileSync(realFile, "original", "utf-8"); |
| 1788 | + const link = join(testDir, "config-async-clean.toml"); |
| 1789 | + symlinkSync(realFile, link); |
| 1790 | + |
| 1791 | + await atomicWriteFileAsync(link, "rewritten"); |
| 1792 | + |
| 1793 | + expect(readdirSync(repoDir).filter(name => name.includes(".ocx."))).toEqual([]); |
| 1794 | + expect(readdirSync(testDir).filter(name => name.includes(".ocx."))).toEqual([]); |
| 1795 | + }); |
| 1796 | + |
| 1797 | + test("a plain destination is unaffected", async () => { |
| 1798 | + const destination = join(testDir, "plain-async.toml"); |
| 1799 | + await atomicWriteFileAsync(destination, "first"); |
| 1800 | + await atomicWriteFileAsync(destination, "second"); |
| 1801 | + |
| 1802 | + expect(lstatSync(destination).isSymbolicLink()).toBe(false); |
| 1803 | + expect(readFileSync(destination, "utf8")).toBe("second"); |
| 1804 | + }); |
| 1805 | + |
| 1806 | + test("a dangling symlink is preserved and the write is refused", async () => { |
| 1807 | + const link = join(testDir, "dangling-async.toml"); |
| 1808 | + symlinkSync(join(testDir, "gone-async", "config.toml"), link); |
| 1809 | + |
| 1810 | + await expect(atomicWriteFileAsync(link, "recovered")).rejects.toThrow(/unresolvable symlinked write target/); |
| 1811 | + expect(lstatSync(link).isSymbolicLink()).toBe(true); |
| 1812 | + expect(existsSync(join(testDir, "gone-async"))).toBe(false); |
| 1813 | + }); |
| 1814 | +}); |
0 commit comments