Skip to content

Commit 8727bcf

Browse files
committed
fix: guard workspace rename to owner only, validate SELF_DIR JSON safety
Address greptile P1/P2 review findings: 1. **Non-owner sessions unconditionally reset workspace name (greptile P1)** cmux-session-namer.sh called `cmux rename-workspace` on every SessionStart regardless of ownership. If Session A (owner) already had a good AI-generated workspace name, Session B starting in the same workspace would flash the name back to its project basename. Fix: guard the `rename-workspace` call with `[ "$WS_OWNER" = "$CMUX_SURFACE_ID" ]` so only the owning session sets the initial workspace name. 2. **$SELF_DIR injected into JSON without escaping (greptile P2)** The claude wrapper interpolates $SELF_DIR directly into a JSON string literal. A path containing '"' or '\' would produce malformed JSON and silently disable all hooks. Fix: validate $SELF_DIR before building HOOKS_JSON. If the path contains JSON-unsafe characters, emit a clear warning to stderr and fall through to exec the real claude without hooks (safe degradation vs. silent failure).
1 parent a80edd0 commit 8727bcf

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

Resources/bin/claude

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ export CMUX_CLAUDE_PID=$$
8383
# Resolve the directory containing this wrapper (for sibling scripts).
8484
SELF_DIR="$(cd "$(dirname "$0")" && pwd)"
8585

86+
# Guard: $SELF_DIR is interpolated directly into a JSON string literal.
87+
# A path containing '"' or '\' would produce malformed JSON and silently
88+
# disable all hooks. This is extremely unlikely in practice but worth catching.
89+
if [[ "$SELF_DIR" =~ [\"\\] ]]; then
90+
echo "cmux: warning: wrapper directory path contains JSON-unsafe characters: $SELF_DIR" >&2
91+
echo "cmux: hooks disabled — move cmux to a path without '\"' or '\\'" >&2
92+
exec "$REAL_CLAUDE" "$@"
93+
fi
94+
8695
# Build hooks settings JSON.
8796
# Claude Code merges --settings additively with the user's own settings.json.
8897
# - SessionStart/Stop/Notification: existing lifecycle hooks

Resources/bin/cmux-session-namer.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ fi
5656
rm -f "$CUSTOM_MARKER" 2>/dev/null
5757
rm -f "/tmp/cmux-tab-cache-${CMUX_SURFACE_ID}" 2>/dev/null
5858

59-
# Set project basename as initial workspace name (overridden by AI summary after first response)
60-
CWD=$(echo "$INPUT" | python3 -c "import sys,json; print(json.load(sys.stdin).get('cwd',''))" 2>/dev/null || true)
61-
if [ -n "$CWD" ]; then
62-
BASENAME=$(basename "$CWD")
63-
cmux rename-workspace --workspace "$CMUX_WORKSPACE_ID" "$BASENAME" 2>/dev/null || true
59+
# Set project basename as initial workspace name — only if this tab owns the workspace.
60+
# Non-owner sessions must not overwrite an already-established workspace name.
61+
WS_OWNER=$(cat "$WS_OWNER_FILE" 2>/dev/null || true)
62+
if [ "$WS_OWNER" = "$CMUX_SURFACE_ID" ]; then
63+
CWD=$(echo "$INPUT" | python3 -c "import sys,json; print(json.load(sys.stdin).get('cwd',''))" 2>/dev/null || true)
64+
if [ -n "$CWD" ]; then
65+
BASENAME=$(basename "$CWD")
66+
cmux rename-workspace --workspace "$CMUX_WORKSPACE_ID" "$BASENAME" 2>/dev/null || true
67+
fi
6468
fi

0 commit comments

Comments
 (0)