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
17 changes: 10 additions & 7 deletions CLI/cmux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23703,16 +23703,19 @@ struct CMUXCLI {
}

private func createClaudeNodeOptionsRestoreModule() throws -> URL {
let rawTemporaryDirectory = ProcessInfo.processInfo.environment["TMPDIR"]?
// Must match the guard_dir in Resources/bin/cmux-claude-wrapper. NODE_OPTIONS
// outlives the session, so this cannot live under TMPDIR, and the path has to
// keep "/cmux-" for isCmuxNodeOptionsRestoreModulePath to recognise it.
let overrideDirectory = ProcessInfo.processInfo.environment["CMUX_NODE_OPTIONS_DIR"]?
.trimmingCharacters(in: .whitespacesAndNewlines)
let temporaryDirectory: String
if let rawTemporaryDirectory, !rawTemporaryDirectory.isEmpty {
temporaryDirectory = rawTemporaryDirectory
let root: URL
if let overrideDirectory, !overrideDirectory.isEmpty {
root = URL(fileURLWithPath: overrideDirectory, isDirectory: true)
Comment thread
ibrahimhajjaj marked this conversation as resolved.
Resolved
} else {
Comment thread
ibrahimhajjaj marked this conversation as resolved.
Resolved
temporaryDirectory = NSTemporaryDirectory()
root = FileManager.default.homeDirectoryForCurrentUser
.appendingPathComponent(".cmux", isDirectory: true)
.appendingPathComponent("cmux-node-options", isDirectory: true)
}
let root = URL(fileURLWithPath: temporaryDirectory, isDirectory: true)
.appendingPathComponent("cmux-claude-node-options", isDirectory: true)
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true, attributes: nil)
let restoreModuleURL = root.appendingPathComponent("restore-node-options.cjs", isDirectory: false)
try writeShimIfChanged(Self.claudeNodeOptionsRestoreModule, to: restoreModuleURL)
Expand Down
12 changes: 10 additions & 2 deletions Resources/bin/cmux-claude-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -848,8 +848,16 @@ exec_real_claude_passthrough() {
REAL_CLAUDE="$(find_real_claude)" || { echo "Error: claude not found in PATH" >&2; exit 127; }

ensure_node_options_restore_module() {
local guard_dir="${TMPDIR:-/tmp}"
guard_dir="${guard_dir%/}/cmux-claude-node-options"
# NODE_OPTIONS is inherited by every descendant for the life of the session, so
# the module it points at has to outlive the session. TMPDIR is swept, and once
# the file goes missing node refuses to start any script at all.
# Two constraints on this path, both load-bearing:
# - no spaces, because node splits NODE_OPTIONS on whitespace, so a
# --require= under "Application Support" breaks every node process instead
# - it must still contain "/cmux-", which is how
# isCmuxNodeOptionsRestoreModulePath recognises our own shim when stripping
# injected flags on restore
local guard_dir="${CMUX_NODE_OPTIONS_DIR:-${HOME}/.cmux/cmux-node-options}"
local guard_path="$guard_dir/restore-node-options.cjs"
mkdir -p "$guard_dir" || return 1
local temp_path
Expand Down
34 changes: 19 additions & 15 deletions tests/test_claude_wrapper_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def run_wrapper(
hooks_disabled: bool = False,
setup_sandbox=None,
process_timeout: float | None = None,
extra_env: dict[str, str] | None = None,
) -> tuple[int, list[str], list[str], str, str, str, str, str, str, str]:
with tempfile.TemporaryDirectory(prefix="cmux-claude-wrapper-test-") as td:
tmp = Path(td)
Expand Down Expand Up @@ -210,6 +211,8 @@ def run_wrapper(
env.pop("NODE_OPTIONS", None)
if tmpdir is not None:
env["TMPDIR"] = tmpdir
if extra_env:
env.update(extra_env)
Comment thread
ibrahimhajjaj marked this conversation as resolved.
Resolved
if node_options == "__CMUX_TEST_PRELOAD__":
preload_path = tmp / "cmux-test-preload.js"
preload_path.write_text("// benign preload used to test MCP env scrubbing\n", encoding="utf-8")
Expand Down Expand Up @@ -2438,23 +2441,23 @@ def test_live_socket_enforces_heap_cap_for_space_separated_flag(failures: list[s
expect(child_node_options == restored, f"space-separated heap flag: expected child NODE_OPTIONS restored, got {child_node_options!r}", failures)


def test_live_socket_tmpdir_failure_skips_node_options_injection(failures: list[str]) -> None:
with tempfile.TemporaryDirectory(prefix="cmux-claude-wrapper-bad-tmp-") as td:
bad_tmpdir = Path(td) / "not-a-directory"
bad_tmpdir.write_text("occupied", encoding="utf-8")
def test_live_socket_guard_dir_failure_skips_node_options_injection(failures: list[str]) -> None:
with tempfile.TemporaryDirectory(prefix="cmux-claude-wrapper-bad-guard-") as td:
blocker = Path(td) / "not-a-directory"
blocker.write_text("occupied", encoding="utf-8")
code, real_argv, cmux_log, stderr, claudecode, node_options, runtime_node_options, child_node_options, _, _ = run_wrapper(
socket_state="live",
argv=["hello"],
tmpdir=str(bad_tmpdir),
extra_env={"CMUX_NODE_OPTIONS_DIR": str(blocker / "node-options")},
)
expect(code == 0, f"tmpdir failure: wrapper exited {code}: {stderr}", failures)
expect("--settings" in real_argv, f"tmpdir failure: missing --settings in args: {real_argv}", failures)
expect("--session-id" in real_argv, f"tmpdir failure: missing --session-id in args: {real_argv}", failures)
expect(any(" ping" in line for line in cmux_log), f"tmpdir failure: expected cmux ping, got {cmux_log}", failures)
expect(claudecode == "__UNSET__", f"tmpdir failure: expected CLAUDECODE unset, got {claudecode!r}", failures)
expect(node_options == "__UNSET__", f"tmpdir failure: expected NODE_OPTIONS injection to be skipped, got {node_options!r}", failures)
expect(runtime_node_options == "__UNSET__", f"tmpdir failure: expected runtime NODE_OPTIONS passthrough, got {runtime_node_options!r}", failures)
expect(child_node_options == "__UNSET__", f"tmpdir failure: expected child NODE_OPTIONS passthrough, got {child_node_options!r}", failures)
expect(code == 0, f"guard dir failure: wrapper exited {code}: {stderr}", failures)
expect("--settings" in real_argv, f"guard dir failure: missing --settings in args: {real_argv}", failures)
expect("--session-id" in real_argv, f"guard dir failure: missing --session-id in args: {real_argv}", failures)
expect(any(" ping" in line for line in cmux_log), f"guard dir failure: expected cmux ping, got {cmux_log}", failures)
expect(claudecode == "__UNSET__", f"guard dir failure: expected CLAUDECODE unset, got {claudecode!r}", failures)
expect(node_options == "__UNSET__", f"guard dir failure: expected NODE_OPTIONS injection to be skipped, got {node_options!r}", failures)
expect(runtime_node_options == "__UNSET__", f"guard dir failure: expected runtime NODE_OPTIONS passthrough, got {runtime_node_options!r}", failures)
expect(child_node_options == "__UNSET__", f"guard dir failure: expected child NODE_OPTIONS passthrough, got {child_node_options!r}", failures)


def test_live_socket_preserves_explicit_bypass_availability_flag(failures: list[str]) -> None:
Expand All @@ -2481,13 +2484,14 @@ def test_live_socket_preserves_explicit_bypass_availability_flag(failures: list[
def test_live_socket_stale_mktemp_literal_does_not_warn(failures: list[str]) -> None:
with tempfile.TemporaryDirectory(prefix="cmux-claude-wrapper-tmp-") as td:
tmpdir = Path(td)
guard_dir = tmpdir / "cmux-claude-node-options"
guard_dir = tmpdir / "cmux-node-options"
guard_dir.mkdir(parents=True, exist_ok=True)
(guard_dir / "restore-node-options.XXXXXX.cjs").write_text("stale", encoding="utf-8")
code, _, _, stderr, _, node_options, runtime_node_options, child_node_options, _, _ = run_wrapper(
socket_state="live",
argv=["hello"],
tmpdir=str(tmpdir),
extra_env={"CMUX_NODE_OPTIONS_DIR": str(guard_dir)},
)
expect(code == 0, f"stale mktemp literal: wrapper exited {code}: {stderr}", failures)
expect("mktemp:" not in stderr, f"stale mktemp literal: unexpected mktemp warning: {stderr!r}", failures)
Expand Down Expand Up @@ -2673,7 +2677,7 @@ def main() -> int:
test_live_socket_auto_preserve_accepts_all_documented_truthy_variants(failures)
test_live_socket_explicit_key_list_is_additive_to_vertex_auto_preserve(failures)
test_live_socket_enforces_heap_cap_for_space_separated_flag(failures)
test_live_socket_tmpdir_failure_skips_node_options_injection(failures)
test_live_socket_guard_dir_failure_skips_node_options_injection(failures)
test_live_socket_preserves_explicit_bypass_availability_flag(failures)
test_live_socket_stale_mktemp_literal_does_not_warn(failures)
test_missing_socket_skips_hook_injection(failures)
Expand Down
Loading