From 324e1d3ffa71d9d551138607a23c7e50b1c7117f Mon Sep 17 00:00:00 2001 From: Tim Van Wassenhove Date: Tue, 24 Nov 2026 07:47:45 +0100 Subject: [PATCH 1/3] fix: never type control characters from the sidecar into a pane Restore types the resume line with tmux send-keys. Quoting keeps a value to one shell word, but control characters still reach the pane's line editor as keystrokes: a CR in a saved cwd submits the rest of the directory name as its own command, and ^C or ^U discards the opening quote. A pane whose cwd came from a hostile archive could therefore run text from the directory name on the next (unattended) restore. No shell quotes this portably (csh, tcsh, fish and nu have no $'...'), so restore now skips and logs any entry with a control character in any value or key. Tab and newline in cli_args stay allowed: restore word-splits that field, so they never reach the pane. Fixes #136 Claude-Session: https://claude.ai/code/session_01HMZBke9DGibyryaca6akpt --- docs/architecture.md | 6 ++++ scripts/restore-assistant-sessions.sh | 14 +++++++++ test/restore-unit-tests.sh | 45 +++++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index 514dda4..50674c1 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -72,6 +72,12 @@ fields are missing (old-format JSON), falls back to bare resume commands. For a `.relaunch` entry, it instead requires an exact current voucher match and builds the command from the matching voucher line. +An entry with a control character in any value or key (cwd, captured env, +session id, ...) is skipped and logged rather than replayed: `send-keys` +delivers those bytes as keystrokes, where no quoting stops a CR or ^C from +reaching the pane's line editor. Tab and newline inside `cli_args` are exempt, +because that field is word-split before anything is typed. + ## Repository structure ``` diff --git a/scripts/restore-assistant-sessions.sh b/scripts/restore-assistant-sessions.sh index 99958ec..8e3e42d 100755 --- a/scripts/restore-assistant-sessions.sh +++ b/scripts/restore-assistant-sessions.sh @@ -222,6 +222,20 @@ while read -r entry; do log "malformed sidecar entry $entry_number, skipping" continue fi + # Everything below is typed into the pane with send-keys, and quoting does + # not protect control characters there: they reach the line editor as + # keystrokes, where a CR submits the rest as a separate command and ^C or + # ^U discards the opening quote. No shell can quote that portably, so an + # entry carrying one in any value or key (a cwd from #{pane_current_path}, + # a captured env value, ...) is not replayed. Tab and newline in cli_args + # are exempt: restore word-splits that field, so they never reach the pane. + if printf '%s\n' "$entry" | jq -e ' + def has_ctl: test("[[:cntrl:]]"); + (del(.cli_args) | [.. | (strings, (objects | keys[]))] | any(has_ctl)) + or ((.cli_args // "") | gsub("[\t\n]"; "") | has_ctl)' >/dev/null 2>&1; then + log "sidecar entry $entry_number contains control characters, skipping" + continue + fi pane=$(printf '%s\n' "$entry" | jq -r '.pane') tool=$(printf '%s\n' "$entry" | jq -r '.tool') diff --git a/test/restore-unit-tests.sh b/test/restore-unit-tests.sh index a599808..7609eaf 100755 --- a/test/restore-unit-tests.sh +++ b/test/restore-unit-tests.sh @@ -968,6 +968,44 @@ assert_not_contains "stale cwd pane is not cleared" "$(cat "$TMUX_LOG")" "send-k assert_contains "later valid entry is still replayed" "$(cat "$TMUX_LOG")" "send-keys|%4|command claude --resume 'sid-good'" assert_contains "only the valid entry is counted" "$restore_log" "restored 1 of 5" +echo "== control characters are never typed into a pane ==" +# send-keys delivers them as keystrokes: a CR in a cwd would submit the rest +# of the directory name as its own command, whatever the quoting. +export MOCK_PANES='%1|0|0|cr-cwd +%2|0|0|esc-env +%3|0|0|ctl-key +%4|0|0|ctl-args +%5|0|0|ws-args +%6|0|0|good' +export MOCK_SHELLS='' +export MOCK_CAPTURE_ENV='FOO' +mkdir -p "$SANDBOX/ws-ok" +jq -n --arg ok "$SANDBOX/ws-ok" '{sessions:[ + {pane:"cr-cwd:0.0",tool:"claude",session_id:"sid-cr",cwd:"/tmp/x\rtouch /tmp/pwned"}, + {pane:"esc-env:0.0",tool:"claude",session_id:"sid-esc",cwd:"",env:{FOO:"a\u001b[2Jb"}}, + {pane:"ctl-key:0.0",tool:"claude",session_id:"sid-key",cwd:"",env:{"FOO\u0003":"v"}}, + {pane:"ctl-args:0.0",tool:"claude",session_id:"sid-args",cwd:"",cli_args:"--model\u0015 opus"}, + {pane:"ws-args:0.0",tool:"claude",session_id:"sid-ws",cwd:$ok,cli_args:"--model\topus\n--debug"}, + {pane:"good:0.0",tool:"claude",session_id:"sid-good",cwd:""} +]}' >"$RESURRECT_DIR/assistant-sessions.json" +run_restore +restore_log=$(cat "$RESURRECT_DIR/assistant-restore.log") +tmux_log=$(cat "$TMUX_LOG") +assert_eq "control characters do not make restore fail" "0" "$RESTORE_STATUS" +assert_contains "a CR in the cwd is rejected" "$restore_log" "sidecar entry 1 contains control characters" +assert_contains "an ESC in a captured env value is rejected" "$restore_log" "sidecar entry 2 contains control characters" +assert_contains "a control character in an env key is rejected" "$restore_log" "sidecar entry 3 contains control characters" +assert_contains "a control character in cli_args is rejected" "$restore_log" "sidecar entry 4 contains control characters" +for rejected in %1 %2 %3 %4; do + assert_not_contains "nothing is typed into rejected pane $rejected" "$tmux_log" "send-keys|$rejected|" +done +assert_not_contains "the injected text never reaches send-keys" "$tmux_log" "pwned" +assert_contains "tab and newline in cli_args still split into arguments" "$tmux_log" \ + "send-keys|%5|cd '$SANDBOX/ws-ok' && command claude '--model' 'opus' '--debug' --resume 'sid-ws'" +assert_contains "a clean entry after them is still replayed" "$tmux_log" "send-keys|%6|command claude --resume 'sid-good'" +assert_contains "only the clean entries are counted" "$restore_log" "restored 2 of 6" +export MOCK_CAPTURE_ENV='' + export MOCK_PANES='%6|0|0|broader-id' jq -n '{sessions:[{ pane:"broader-id:0.0",tool:"codex",session_id:"_base64/id+=",cwd:"" @@ -1011,8 +1049,11 @@ jq -n --arg tool "$evil_tool" '{sessions:[ ]}' >"$RESURRECT_DIR/assistant-sessions.json" run_restore restore_log=$(cat "$RESURRECT_DIR/assistant-restore.log") -escaped_lines=$(grep -c 'claude.*FORGED-LOG-LINE' "$RESURRECT_DIR/assistant-restore.log" || true) -assert_eq "embedded newline is escaped onto one log line" "1" "$escaped_lines" +# The entry is now refused for its control character before any of its values +# reach the log; either way no line of the log may start with the forged text. +forged_lines=$(grep -c '^FORGED-LOG-LINE' "$RESURRECT_DIR/assistant-restore.log" || true) +assert_eq "an embedded newline cannot forge a log line" "0" "$forged_lines" +assert_contains "a newline in the tool name is refused" "$restore_log" "sidecar entry 1 contains control characters" forged_lines=$(grep -c '^FORGED-LOG-LINE' "$RESURRECT_DIR/assistant-restore.log" || true) assert_eq "standalone injected log line is rejected" "0" "$forged_lines" From 2e5e66483ffaa91173a4f04a6f7f9e3f318891e2 Mon Sep 17 00:00:00 2001 From: Tim Van Wassenhove Date: Tue, 24 Nov 2026 08:09:56 +0100 Subject: [PATCH 2/3] test: make the control-character tests portable and independent The tab/newline case saved a sandbox cwd, which native jq on Git Bash writes as a Windows path, so the Windows canary expected the wrong form. It needs no cwd. The log-forgery check now starts from an empty log, so an earlier block's rejection line cannot make it pass. Claude-Session: https://claude.ai/code/session_01HMZBke9DGibyryaca6akpt --- test/restore-unit-tests.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/restore-unit-tests.sh b/test/restore-unit-tests.sh index 7609eaf..b70261a 100755 --- a/test/restore-unit-tests.sh +++ b/test/restore-unit-tests.sh @@ -979,13 +979,13 @@ export MOCK_PANES='%1|0|0|cr-cwd %6|0|0|good' export MOCK_SHELLS='' export MOCK_CAPTURE_ENV='FOO' -mkdir -p "$SANDBOX/ws-ok" -jq -n --arg ok "$SANDBOX/ws-ok" '{sessions:[ +: >"$RESURRECT_DIR/assistant-restore.log" +jq -n '{sessions:[ {pane:"cr-cwd:0.0",tool:"claude",session_id:"sid-cr",cwd:"/tmp/x\rtouch /tmp/pwned"}, {pane:"esc-env:0.0",tool:"claude",session_id:"sid-esc",cwd:"",env:{FOO:"a\u001b[2Jb"}}, {pane:"ctl-key:0.0",tool:"claude",session_id:"sid-key",cwd:"",env:{"FOO\u0003":"v"}}, {pane:"ctl-args:0.0",tool:"claude",session_id:"sid-args",cwd:"",cli_args:"--model\u0015 opus"}, - {pane:"ws-args:0.0",tool:"claude",session_id:"sid-ws",cwd:$ok,cli_args:"--model\topus\n--debug"}, + {pane:"ws-args:0.0",tool:"claude",session_id:"sid-ws",cwd:"",cli_args:"--model\topus\n--debug"}, {pane:"good:0.0",tool:"claude",session_id:"sid-good",cwd:""} ]}' >"$RESURRECT_DIR/assistant-sessions.json" run_restore @@ -1001,7 +1001,7 @@ for rejected in %1 %2 %3 %4; do done assert_not_contains "the injected text never reaches send-keys" "$tmux_log" "pwned" assert_contains "tab and newline in cli_args still split into arguments" "$tmux_log" \ - "send-keys|%5|cd '$SANDBOX/ws-ok' && command claude '--model' 'opus' '--debug' --resume 'sid-ws'" + "send-keys|%5|command claude '--model' 'opus' '--debug' --resume 'sid-ws'" assert_contains "a clean entry after them is still replayed" "$tmux_log" "send-keys|%6|command claude --resume 'sid-good'" assert_contains "only the clean entries are counted" "$restore_log" "restored 2 of 6" export MOCK_CAPTURE_ENV='' @@ -1044,6 +1044,8 @@ assert_contains "duplicate pane is reported" "$restore_log" "duplicate sidecar e assert_contains "duplicate pane is excluded from summary" "$restore_log" "restored 1 of 2" evil_tool=$(printf 'claude\nFORGED-LOG-LINE') +# Start from an empty log: earlier blocks already wrote the rejection message. +: >"$RESURRECT_DIR/assistant-restore.log" jq -n --arg tool "$evil_tool" '{sessions:[ {pane:"duplicate:0.0",tool:$tool,session_id:"--invalid",cwd:""} ]}' >"$RESURRECT_DIR/assistant-sessions.json" From 0b06a64179b28dfcfa654b7133127e1fd30e4ab6 Mon Sep 17 00:00:00 2001 From: Tim Van Wassenhove Date: Tue, 24 Nov 2026 08:27:49 +0100 Subject: [PATCH 3/3] fix: keep jq's CRLF out of the typed line on Windows A native Windows jq writes a newline inside cli_args as CRLF. Restore word-splits on the newline, but the CR stayed attached to the previous argument and was typed into the pane, which the Windows canary caught. The entry check already rejects any CR in the saved cli_args, so the ones left after reading the field are jq's and are dropped. Restore also refuses, as a last check before send-keys, any command line that still contains a control character, whatever produced it. Claude-Session: https://claude.ai/code/session_01HMZBke9DGibyryaca6akpt --- scripts/restore-assistant-sessions.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/scripts/restore-assistant-sessions.sh b/scripts/restore-assistant-sessions.sh index 8e3e42d..1b26c90 100755 --- a/scripts/restore-assistant-sessions.sh +++ b/scripts/restore-assistant-sessions.sh @@ -249,6 +249,10 @@ while read -r entry; do relaunch_cmd=$(printf '%s\n' "$entry" | jq -r '.cmd // empty') cwd=$(printf '%s\n' "$entry" | jq -r '.cwd // empty') cli_args=$(printf '%s\n' "$entry" | jq -r '.cli_args // empty') + # A native Windows jq writes each newline inside the value as CRLF. The + # entry check above rejected any CR in the saved cli_args, so every CR here + # came from that translation; left in, it would be typed into the pane. + cli_args="${cli_args//$'\r'/}" # The save path stops writing credential flags into the sidecar, but files # written before that are still on disk and are read verbatim here. Without # this the old value would be replayed into the pane and copied into the @@ -712,6 +716,16 @@ while read -r entry; do fi fi + # Last line of defence for the entry check above: whatever produced it + # (another field, a platform's jq output), a control character in the + # typed line would reach the pane's line editor as a keystroke. + case "$full_cmd" in + *[[:cntrl:]]*) + log "replay command for $tool in $pane contains control characters, skipping" + continue + ;; + esac + if [ "$kind" = "relaunch" ]; then log "relaunching $tool in $pane (cmd: $log_cmd)" else