Skip to content
Merged
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
6 changes: 6 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
28 changes: 28 additions & 0 deletions scripts/restore-assistant-sessions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -235,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
Expand Down Expand Up @@ -698,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
Expand Down
47 changes: 45 additions & 2 deletions test/restore-unit-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
: >"$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:"",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|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:""
Expand Down Expand Up @@ -1006,13 +1044,18 @@ 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"
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"
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
forged_lines=$(grep -c '^FORGED-LOG-LINE' "$RESURRECT_DIR/assistant-restore.log" || true)
assert_eq "standalone injected log line is rejected" "0" "$forged_lines"

Expand Down
Loading