Skip to content

Commit 1cc837a

Browse files
siimveneclaude
andcommitted
Require explicit spawner ownership before removing a session dir
A cxc- name directly under the OS temp dir does not prove the plugin created the directory: a manual serve invocation with --pid-file /tmp/cxc-work/broker.pid would have had /tmp/cxc-work recursively deleted on shutdown, user files included. spawnBrokerProcess now passes --managed-session-dir after creating the directory via mkdtemp, and the broker only removes a directory recursively when that flag was given AND the path matches the plugin's own layout; any other invocation has only the broker's pid file and socket unlinked. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent c756492 commit 1cc837a

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

‎plugins/codex/scripts/app-server-broker.mjs‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ function writePidFile(pidFile) {
5050
async function main() {
5151
const [subcommand, ...argv] = process.argv.slice(2);
5252
if (subcommand !== "serve") {
53-
throw new Error("Usage: node scripts/app-server-broker.mjs serve --endpoint <value> [--cwd <path>] [--pid-file <path>]");
53+
throw new Error("Usage: node scripts/app-server-broker.mjs serve --endpoint <value> [--cwd <path>] [--pid-file <path>] [--managed-session-dir]");
5454
}
5555

5656
const { options } = parseArgs(argv, {
57-
valueOptions: ["cwd", "pid-file", "endpoint"]
57+
valueOptions: ["cwd", "pid-file", "endpoint"],
58+
booleanOptions: ["managed-session-dir"]
5859
});
5960

6061
if (!options.endpoint) {
@@ -65,6 +66,7 @@ async function main() {
6566
const endpoint = String(options.endpoint);
6667
const listenTarget = parseBrokerEndpoint(endpoint);
6768
const pidFile = options["pid-file"] ? path.resolve(options["pid-file"]) : null;
69+
const managedSessionDir = options["managed-session-dir"] === true;
6870
writePidFile(pidFile);
6971

7072
const appClient = await CodexAppServerClient.connect(cwd, { disableBroker: true });
@@ -188,11 +190,12 @@ async function main() {
188190
}
189191
// Remove the broker's own session directory (socket, pid file, log) so a
190192
// clean exit leaves nothing behind for the reaper to GC. Recursive removal
191-
// is restricted to directories this plugin provably created: mkdtemp with
192-
// the cxc- prefix directly under the OS temp dir. A manual invocation
193-
// pointing --pid-file anywhere else (even a directory that happens to be
194-
// named cxc-something) keeps its directory; only the broker's own files
195-
// are unlinked there.
193+
// requires the spawner to have declared it created the directory
194+
// (--managed-session-dir, set by spawnBrokerProcess after mkdtemp) AND the
195+
// plugin's own layout (cxc- prefix directly under the OS temp dir), so a
196+
// manual invocation can never have a caller-selected directory deleted,
197+
// even one named to look like ours; only the broker's own files are
198+
// unlinked there.
196199
const sessionDir = pidFile
197200
? path.dirname(pidFile)
198201
: listenTarget.kind === "unix"
@@ -215,6 +218,9 @@ async function main() {
215218
}
216219

217220
function isManagedSessionDir(dir) {
221+
if (!managedSessionDir) {
222+
return false;
223+
}
218224
try {
219225
return (
220226
path.basename(dir).startsWith("cxc-") &&

‎plugins/codex/scripts/lib/broker-lifecycle.mjs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export async function sendBrokerShutdown(endpoint) {
5858

5959
export function spawnBrokerProcess({ scriptPath, cwd, endpoint, pidFile, logFile, env = process.env }) {
6060
const logFd = fs.openSync(logFile, "a");
61-
const child = spawn(process.execPath, [scriptPath, "serve", "--endpoint", endpoint, "--cwd", cwd, "--pid-file", pidFile], {
61+
// --managed-session-dir: this spawner created the session directory
62+
// (createBrokerSessionDir's mkdtemp), so the broker may remove the whole
63+
// directory on clean exit. Manual invocations lack the flag and keep theirs.
64+
const child = spawn(process.execPath, [scriptPath, "serve", "--endpoint", endpoint, "--cwd", cwd, "--pid-file", pidFile, "--managed-session-dir"], {
6265
cwd,
6366
env,
6467
detached: true,

0 commit comments

Comments
 (0)