Skip to content
Closed
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
39 changes: 32 additions & 7 deletions src/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2329,9 +2329,14 @@ export function refreshOverlayHome(
realHome: string,
overlay: string,
generatedFile: string | string[],
opts?: { sharedSqlite?: boolean },
opts?: { sharedSqlite?: boolean; localOnlyEntries?: ReadonlySet<string> },
): boolean {
const sharedSqlite = opts?.sharedSqlite === true;
// #5585: entries the client owns INSIDE the overlay (codex's .tmp staging).
// Never linked in from the real home, never merged back into it, and a stale
// escaping symlink left by a pre-fix launch is dropped so the client recreates
// them as genuine per-overlay paths (their git repos then stay inside home).
const localOnlyEntries = opts?.localOnlyEntries;
const generatedFiles = new Set(Array.isArray(generatedFile) ? generatedFile : [generatedFile]);
const isGeneratedDraft = (name: string): boolean =>
[...generatedFiles].some((g) => name.startsWith(`.${g}.`) && name.endsWith(".tmp"));
Expand Down Expand Up @@ -2401,6 +2406,16 @@ export function refreshOverlayHome(
} catch {
continue;
}
// #5585: local-only entries are owned by the client running under this
// overlay. A real dir/file is left untouched (never merged back); a
// stale escaping symlink from a pre-fix launch is dropped so the client
// recreates it as a genuine per-overlay path.
if (localOnlyEntries?.has(entry)) {
if (st.isSymbolicLink()) {
try { fs.unlinkSync(overlayPath); } catch {}
}
continue;
}
if (st.isSymbolicLink()) {
let target: string | undefined;
try {
Expand Down Expand Up @@ -2490,6 +2505,7 @@ export function refreshOverlayHome(
for (const entry of copyPhaseEntries) {
if (generatedFiles.has(entry)) continue;
if (sharedSqlite && realDbMembers.has(entry)) continue;
if (localOnlyEntries?.has(entry)) continue;
total += 1;
const overlayPath = path.join(overlay, entry);
let present = false;
Expand Down Expand Up @@ -3106,13 +3122,14 @@ export function renderCodexDotEnv(userText: string | undefined, values: { origin
* (both generations survive, loser as .bili-conflict).
*
* Returns true when nothing remains pending in the overlay. */
export function finalizeCodexHome(realHome: string, overlay: string, generatedFiles?: readonly string[]): boolean {
export function finalizeCodexHome(realHome: string, overlay: string, generatedFiles?: readonly string[], localOnlyEntries?: ReadonlySet<string>): boolean {
const generatedSet = new Set<string>(generatedFiles ?? []);
const isExcluded = (name: string): boolean =>
name === SQLITE_ORIGIN_FILE ||
name === `${SQLITE_ORIGIN_FILE}.tmp` ||
name === path.basename(overlayLockPath(overlay)) ||
name === path.basename(overlayLeaseDir(overlay)) || // release-failure residue must never merge into the real home
localOnlyEntries?.has(name) === true || // #5585: codex's .tmp stays overlay-owned; never merged back nor relinked
generatedSet.has(name) ||
[...generatedSet].some((g) => name.startsWith(`.${g}.`) && name.endsWith(".tmp"));
let overlayEntries: string[];
Expand Down Expand Up @@ -3186,6 +3203,14 @@ export function finalizeCodexHome(realHome: string, overlay: string, generatedFi
* directory per session. */
const CODEX_SESSION_SLOT_MAX = 16;

/** #5585: codex keeps its marketplace/plugin git checkouts under $CODEX_HOME/.tmp.
* Sharing that directory into the overlay via a symlink makes every repo's canonical
* path resolve back into the REAL home, so codex's "trusted Git repository root escapes
* Codex home" containment check rejects each git-backed marketplace update under bili.
* The overlay must own .tmp itself — never link it in from the real home, never merge
* it back out — so those repos stay inside the active home. */
const CODEX_LOCAL_ONLY_HOME_ENTRIES: ReadonlySet<string> = new Set([".tmp"]);

/** #3203: claim a free per-session slot for this launch. Each slot is an
* ordinary overlay with its own lease — concurrent claims serialize on the
* atomic lock mkdir, so two launches never share a slot; a slot held by a
Expand Down Expand Up @@ -3233,7 +3258,7 @@ function migrateLegacyCodexOverlay(codexHome: string): void {
if (!st.isDirectory()) return;
acquireOverlayLease(legacy);
try {
finalizeCodexHome(codexHome, legacy, [".env", "config.toml"]);
finalizeCodexHome(codexHome, legacy, [".env", "config.toml"], CODEX_LOCAL_ONLY_HOME_ENTRIES);
} finally {
releaseOverlayLease();
}
Expand Down Expand Up @@ -3362,7 +3387,7 @@ export function prepareCodexHome(opts: {
} else {
overlay = `${codexHome}-bili`;
}
if (!refreshOverlayHome(codexHome, overlay, generatedFiles, { sharedSqlite: opts.sharedSqlite })) return undefined;
if (!refreshOverlayHome(codexHome, overlay, generatedFiles, { sharedSqlite: opts.sharedSqlite, localOnlyEntries: CODEX_LOCAL_ONLY_HOME_ENTRIES })) return undefined;
if (manageDotEnv) {
try {
const st = fs.lstatSync(path.join(overlay, ".env"));
Expand Down Expand Up @@ -6380,7 +6405,7 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {}
let opencodeTmpFile: string | undefined;
let dshOverlayHome: string | undefined;
let gooseOverlay: GooseOverlay | undefined;
let codexOverlay: { realHome: string; overlay: string; generated: readonly string[] } | undefined;
let codexOverlay: { realHome: string; overlay: string; generated: readonly string[]; localOnly: ReadonlySet<string> } | undefined;
const tmpFiles: string[] = [];
const directUrl = launcherDirectUrl(process.env);
if (directUrl) {
Expand Down Expand Up @@ -6896,7 +6921,7 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {}
if (activeOverlay !== undefined) {
const generated: string[] = [".env"];
if (codexConversationId !== undefined) generated.push("config.toml");
codexOverlay = { realHome: codexRealHome, overlay: activeOverlay, generated };
codexOverlay = { realHome: codexRealHome, overlay: activeOverlay, generated, localOnly: CODEX_LOCAL_ONLY_HOME_ENTRIES };
}
}
} else if (base === "codebuddy") {
Expand Down Expand Up @@ -7019,7 +7044,7 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {}
}
if (codexOverlay) {
try {
finalizeCodexHome(codexOverlay.realHome, codexOverlay.overlay, codexOverlay.generated);
finalizeCodexHome(codexOverlay.realHome, codexOverlay.overlay, codexOverlay.generated, codexOverlay.localOnly);
} catch {}
}
releaseOverlayLease();
Expand Down
117 changes: 117 additions & 0 deletions tests/codex-overlay-tmp-containment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { prepareCodexHome, refreshOverlayHome, finalizeCodexHome } from "../src/launcher.js";

/** #5585: the codex-local set the launcher passes to the overlay folds. */
const LOCAL_ONLY = new Set([".tmp"]);
const GENERATED = [".env", "config.toml"];

function mkRoot(): string {
return fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), "bili-codex-tmp-"));
}

// The launcher warns on console.error along several paths; swallow those lines so
// the suite output stays readable. The boolean return values still carry the verdict.
function quiet(fn: () => void): void {
const orig = console.error;
console.error = (): void => {};
try { fn(); } finally { console.error = orig; }
}

// A real codex home: a git-backed marketplace checkout under $CODEX_HOME/.tmp plus an
// unrelated shared entry, and a marker proving this .tmp is distinct from any overlay copy.
function makeRealHome(root: string): string {
const home = path.join(root, ".codex");
const repo = path.join(home, ".tmp", "marketplaces", "context-mode", ".git");
fs.mkdirSync(repo, { recursive: true });
fs.writeFileSync(path.join(repo, "HEAD"), "ref: refs/heads/main\n");
fs.writeFileSync(path.join(home, ".tmp", "real-only.marker"), "real\n");
fs.writeFileSync(path.join(home, "auth.json"), "{}\n");
return home;
}

function resolvesInside(candidate: string, base: string): boolean {
const c = fs.realpathSync(candidate);
const b = fs.realpathSync(base);
return c === b || c.startsWith(b + path.sep);
}

test("#5585: refresh never links the real-home .tmp into the codex overlay", (t) => {
const root = mkRoot();
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const real = makeRealHome(root);
const overlay = `${real}-bili`;
assert.equal(refreshOverlayHome(real, overlay, GENERATED, { localOnlyEntries: LOCAL_ONLY }), true);
assert.ok(!fs.existsSync(path.join(overlay, ".tmp")), "overlay must not gain a .tmp from the real home");
assert.ok(fs.existsSync(path.join(overlay, "auth.json")), "unrelated entries still link through");
assert.ok(fs.existsSync(path.join(real, ".tmp", "marketplaces", "context-mode", ".git", "HEAD")), "real home untouched");
});

test("#5585: a client-owned .tmp stays inside the overlay across launch + exit (no merge-back, no relink, no pollution)", (t) => {
const root = mkRoot();
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const real = makeRealHome(root);
const overlay = `${real}-bili`;
assert.equal(refreshOverlayHome(real, overlay, GENERATED, { localOnlyEntries: LOCAL_ONLY }), true);

// Codex running under the overlay creates its OWN .tmp holding a marketplace clone.
const ovRepo = path.join(overlay, ".tmp", "marketplaces", "context-mode", ".git");
fs.mkdirSync(ovRepo, { recursive: true });
fs.writeFileSync(path.join(ovRepo, "HEAD"), "ref: refs/heads/main\n");
fs.writeFileSync(path.join(overlay, ".tmp", "overlay-only.marker"), "overlay\n");

// The containment invariant: the repo's canonical path resolves INSIDE the active home.
assert.ok(resolvesInside(ovRepo, overlay), "marketplace repo must resolve inside the active home");

// Re-launch: refresh leaves the client-owned .tmp alone (never merged back).
assert.equal(refreshOverlayHome(real, overlay, GENERATED, { localOnlyEntries: LOCAL_ONLY }), true);
assert.ok(fs.existsSync(path.join(overlay, ".tmp", "overlay-only.marker")), "client-owned .tmp survives a re-launch");
assert.ok(!fs.lstatSync(path.join(overlay, ".tmp")).isSymbolicLink(), ".tmp stays a real dir, never relinked");

// Exit fold: finalize neither merges the overlay .tmp into the real home nor relinks it.
quiet(() => assert.equal(finalizeCodexHome(real, overlay, GENERATED, LOCAL_ONLY), true));
assert.ok(fs.existsSync(path.join(overlay, ".tmp", "overlay-only.marker")), "finalize leaves the client-owned .tmp in place");
assert.ok(!fs.lstatSync(path.join(overlay, ".tmp")).isSymbolicLink(), "finalize must not relink .tmp back to the real home");
assert.ok(!fs.existsSync(path.join(real, ".tmp", "overlay-only.marker")), "real home .tmp must not absorb the overlay's .tmp");
assert.ok(fs.existsSync(path.join(real, ".tmp", "real-only.marker")), "real home .tmp keeps its own content");
});

test("#5585: prepareCodexHome migrates a stale escaping .tmp symlink away", (t) => {
const root = mkRoot();
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const real = makeRealHome(root);
const overlay = `${real}-bili`;
fs.mkdirSync(overlay, { recursive: true });
let madeLink = false;
try {
fs.symlinkSync(path.join(real, ".tmp"), path.join(overlay, ".tmp"), process.platform === "win32" ? "junction" : "dir");
madeLink = true;
} catch {}
if (!madeLink) { t.skip("directory symlink/junction unsupported on this platform"); return; }
// Pre-fix state: overlay/.tmp -> real/.tmp, so the marketplace escapes the active home.
const escaped = path.join(overlay, ".tmp", "marketplaces", "context-mode", ".git");
assert.ok(!resolvesInside(escaped, overlay), "precondition: the stale symlink escapes the overlay");

const prepared = prepareCodexHome({ codexHome: real, origin: "http://127.0.0.1:8787", caPath: "/nonexistent/ca.pem", conversationId: "conv-tmp", manageRouting: false });
assert.ok(prepared, "prepareCodexHome should succeed");
const tmpPath = path.join(overlay, ".tmp");
if (fs.existsSync(tmpPath)) {
assert.ok(!fs.lstatSync(tmpPath).isSymbolicLink(), "stale escaping .tmp symlink must be dropped");
assert.ok(resolvesInside(path.join(tmpPath, "marketplaces", "context-mode", ".git"), overlay), "post-migration repo stays inside the active home");
}
// Absent is the expected steady state: codex recreates .tmp as a genuine per-overlay dir.
});

test("#5585 negative control: without the exclusion the real-home .tmp IS linked in and escapes", (t) => {
const root = mkRoot();
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const real = makeRealHome(root);
const overlay = `${real}-bili`;
assert.equal(refreshOverlayHome(real, overlay, GENERATED), true);
const linked = path.join(overlay, ".tmp");
assert.ok(fs.existsSync(linked), "legacy behavior links the real-home .tmp into the overlay");
assert.equal(fs.realpathSync(linked), fs.realpathSync(path.join(real, ".tmp")), "linked .tmp resolves OUTSIDE the active home (the bug)");
});
Loading