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
50 changes: 49 additions & 1 deletion tests/config/config-ownership-removal.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, test } from "bun:test";
import { chmodSync, copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, renameSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs";
import { chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, realpathSync, renameSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
Expand Down Expand Up @@ -57,6 +57,53 @@ describe("owned path removal stays inside the config root", () => {
setWindowsElevationProbeForTests(null);
});

windows("elevated uninstall refuses a junction before changing owned state", () => {
const { parent, dir } = ownedHome("ocx-removal-windows-junction-");
const target = join(parent, "external"), link = join(dir, "artifacts");
try {
mkdirSync(target);
writeFileSync(join(target, "keep.txt"), "keep");
expect(recordOwnedConfigPath(dir, link)).toBe(true);
const manifest = readFileSync(join(dir, CONFIG_UNINSTALL_MANIFEST), "utf8");
const owner = readFileSync(join(dir, CONFIG_OWNER_FILE), "utf8");
const ownedConfig = readFileSync(join(dir, "config.json"), "utf8");
const lock = join(dir, CONFIG_OWNERSHIP_LOCK);
expect(existsSync(lock)).toBe(true);
unlinkSync(lock);
setUninstallElevationProbeForTests(() => true);
const sid = "S-1-5-21-9-9-9-1001";
const passing = (count: number) => [`U|${sid}|${sid}|False`,
...Array.from({ length: count }, (_, index) => [`E|${index}|${sid}|4100|1`, `A|${index}|0|3|2032127|${sid}|False`]).flat(),
"END", ""].join("\n");
let calls = 0;
setWindowsOwnerAclRunnerForTests(entries => { calls++; return { success: true, timedOut: false, stdout: passing(entries.length) }; });
expect(uninstallPrivilegeRefusal(dir)).toBeNull();
expect(calls).toBeGreaterThan(0);
symlinkSync(target, link, "junction");
expect(lstatSync(link).isSymbolicLink()).toBe(true);
const linkTarget = readlinkSync(link);
const entries = readdirSync(dir).sort();
calls = 0;
expect(uninstallPrivilegeRefusal(dir)).toBe(ELEVATED_UNINSTALL_REFUSAL);
expect(calls).toBe(0);
expect(lstatSync(link).isSymbolicLink()).toBe(true);
calls = 0;
const result = removeOwnedConfigState(dir);
expect(result.status).toBe("refused");
expect(result.reason).toBe(ELEVATED_UNINSTALL_REFUSAL);
expect(result.residualPaths).toEqual([dir]);
expect(calls).toBe(0);
expect(existsSync(lock)).toBe(false);
expect(readdirSync(dir).sort()).toEqual(entries);
expect(readFileSync(join(dir, CONFIG_UNINSTALL_MANIFEST), "utf8")).toBe(manifest);
expect(readFileSync(join(dir, CONFIG_OWNER_FILE), "utf8")).toBe(owner);
expect(readFileSync(join(dir, "config.json"), "utf8")).toBe(ownedConfig);
expect(lstatSync(link).isSymbolicLink()).toBe(true);
expect(readlinkSync(link)).toBe(linkTarget);
expect(readFileSync(join(target, "keep.txt"), "utf8")).toBe("keep");
} finally { removeTreeWithRetry(parent); }
});

posix("an elevated run refuses a recorded parent another account can write, before writing anything", () => {
const { parent, dir } = ownedHome("ocx-removal-shared-parent-");
const nested = join(dir, "nested");
Expand Down Expand Up @@ -561,6 +608,7 @@ describe("owned path removal stays inside the config root", () => {
mkdirSync(join(dir, "Nested"));
// A case-sensitive volume (most Linux runners) cannot show this; there is nothing to check.
if (!existsSync(join(dir, "nested"))) return;
if (process.platform === "win32") setUninstallElevationProbeForTests(() => false);
const recorded = join(dir, "nested", "owned.txt");
expect(recordOwnedConfigPath(dir, recorded)).toBe(true);
writeFileSync(recorded, "owned");
Expand Down
8 changes: 7 additions & 1 deletion tests/config/config-ownership-uninstall.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, spyOn, test } from "bun:test";
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test";
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
Expand All @@ -8,6 +8,7 @@ import {
recordOwnedConfigPath,
removeOwnedConfigState,
} from "../../src/lib/config-ownership";
import { setUninstallElevationProbeForTests } from "../../src/lib/config-ownership-removal";
import { writePristineCatalogBackup } from "../../src/codex/catalog/parsing";
import { getDefaultConfig, saveConfig } from "../../src/config";
import { removeTreeWithRetry } from "../helpers/remove-tree";
Expand All @@ -17,6 +18,11 @@ function manifestPaths(dir: string): string[] {
}

describe("owned config uninstall", () => {
// These removal fixtures exercise the owning user's unelevated path. Elevated Windows
// junction refusal is covered separately in config-ownership-removal.test.ts.
beforeEach(() => { if (process.platform === "win32") setUninstallElevationProbeForTests(() => false); });
afterEach(() => { if (process.platform === "win32") setUninstallElevationProbeForTests(null); });

(process.platform === "win32" ? test.skip : test)("refuses elevated removal when the root permits shared writes (POSIX modes required)", () => {
const parent = mkdtempSync(join(tmpdir(), "ocx-config-mode-"));
const dir = join(parent, "config");
Expand Down
Loading