Skip to content

Fix panic on CSI sequences with more than 32 parameters - #1009

Open
Ivan Wang (iwang-1) wants to merge 1 commit into
microsoft:mainfrom
iwang-1:fix-csi-param-overflow
Open

Ivan Wang (iwang-1) wants to merge 1 commit into
microsoft:mainfrom
iwang-1:fix-csi-param-overflow

Conversation

@iwang-1

Copy link
Copy Markdown

Csi::params has 32 slots, but each ; in a CSI sequence incremented Csi::param_count without a bound. A sequence with more than 32 parameters produced a Csi whose param_count was larger than params.len(), which panics in two places:

  • parse_xterm_mouse(&csi.params[..csi.param_count], ..) in input.rs (for ESC [ <): range end index 41 out of range for slice of length 32
  • the reset loop at the start of the next CSI sequence in vt.rs: index out of bounds: the len is 32 but the index is 40

Repro

Running edit in a pty (80x24, TERM=xterm-256color) and writing ESC [ < followed by 40 ; and m, then ESC [ A, aborts the editor (exit status 6) with the second panic above, losing any unsaved changes. With this change the editor keeps running.

Fix

Stop counting at params.len(), both on ; and on the final byte. That matches how the digits of excess parameters are already skipped: the first 32 parameters are kept and the rest are dropped.

Tests

Added vt::tests with two cases (40 numeric parameters followed by another sequence, and 40 empty parameters). Both fail on main and pass with the fix. cargo +nightly fmt --all -- --check, cargo +nightly test --workspace and cargo +nightly clippy --workspace --all-features --all-targets -- --no-deps --deny warnings pass locally (macOS arm64).

🤖 Generated with Claude Code

Each `;` in a CSI sequence incremented `Csi::param_count` without a
bound, while `Csi::params` only has 32 slots. A sequence with more than
32 parameters therefore produced a `Csi` whose `param_count` exceeded
`params.len()`. Slicing `params[..param_count]` (as `parse_xterm_mouse`
does) then panics, and so does the reset loop at the start of the next
CSI sequence.

Cap `param_count` at `params.len()`, which matches how the digits of
excess parameters are already skipped.
@iwang-1

Ivan Wang (iwang-1) commented Nov 17, 2026 via email

Copy link
Copy Markdown
Author

1AbdullahButt

This comment was marked as low quality.

@DHowett

Copy link
Copy Markdown
Member

Can you point us to a terminal emulator that emits CSI with more than 32 parameters? After all, this is only used in input decoding... so it stands to reason that this change should make edit work on some terminal emulator whose input encoder emits CSI with so many parameters, right? Which terminal would that be?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as requested above

@iwang-1

Copy link
Copy Markdown
Author

Fair question. I don't know of a terminal that emits this, and a well-behaved one shouldn't. The case is malformed input reaching the parser. Normal pastes are safe because bracketed paste bypasses it. But raw escape bytes pasted in a terminal without bracketed paste, or injected via something like tmux send-keys, make edit panic (index out of bounds) and exit instead of ignoring a bad sequence. If you'd rather treat terminal input as trusted and not take this, I'm happy to close it.

@DHowett

Copy link
Copy Markdown
Member

I'm inclined to reject this because we trust user input (in all forms), but I'll let Leonard Hecker (@lhecker) decide.

For what it's worth... if tmux send-keys or some wayward terminal emulator pasting control characters is an attack vector, then sending a 33-parameter CSI sequence is the least of my concerns. Consider that they could send \e\e\e\e\efxnnnnnnnnn or control character DC1/XON followed by nnnnnnnnn (that is, \x17nnnnnnnnnn); both of those will open the Quit dialog and then press Don't Save a bunch of times. This will predictably exit the editor and destroy the user's unsaved changes.

And at that point, why bother?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants