Skip to content

Conversation

@vibhordubey333
Copy link
Contributor

Fix: Random Character Input from Terminal Escape Sequences

Issue Description

Random characters were being input to k9s after periods of inactivity. The issue was observed on both Linux and Mac, across different terminal emulators. Users reported seeing strings like 1212/1212/1212\[7;15R appearing in the command prompt without any user input.

Symptoms:

  • Random character sequences appearing in the command prompt
  • Occurs after long periods without user input
  • Affects both command buffer and filter buffer
  • Observed across different terminal emulators (Linux and macOS)

Example of problematic input:

> 1212/1212/1212\[7;15R

The string \[7;15R is a terminal escape sequence (cursor position report) that was being interpreted as user input.

Root Cause

The prompt handler in internal/ui/prompt.go was accepting all tcell.KeyRune events without validating that the runes were valid user input. Terminal escape sequences, particularly cursor position reports and other control sequences, were being misinterpreted as keyboard input by tcell and passed through to the command buffer.

The issue occurred because:

  1. Terminal escape sequences can be sent by the terminal emulator or system after idle periods
  2. These sequences contain control characters and special sequences
  3. The prompt handler accepted all KeyRune events without filtering
  4. Control characters from escape sequences were added to the command buffer

Solution

Added input validation to filter out control characters and non-printable runes before they're added to the command buffer.

Changes Made

File: internal/ui/prompt.go

  1. Added unicode import for character validation
  2. Created isValidInputRune() helper function that:
    • Rejects control characters (0x00-0x1F, 0x7F) except common whitespace (tab, newline, carriage return)
    • Only accepts printable characters or space characters
  3. Updated the KeyRune case in the keyboard() handler to validate runes before adding them to the buffer

Code Changes

case tcell.KeyRune:
    r := evt.Rune()
    // Filter out control characters and non-printable runes that may come from
    // terminal escape sequences (e.g., cursor position reports like [7;15R)
    // Only accept printable characters for user input
    if isValidInputRune(r) {
        p.model.Add(r)
    }
// isValidInputRune checks if a rune is valid for user input.
// It filters out control characters and non-printable characters that may
// come from terminal escape sequences (e.g., cursor position reports).
func isValidInputRune(r rune) bool {
    // Reject control characters (0x00-0x1F, 0x7F) except for common whitespace
    if unicode.IsControl(r) && r != '\t' && r != '\n' && r != '\r' {
        return false
    }
    // Only accept printable characters
    return unicode.IsPrint(r) || unicode.IsSpace(r)
}

Testing

Unit Tests

Added comprehensive unit tests in internal/ui/prompt_validation_test.go:

  • TestPrompt_FiltersControlCharacters - Verifies control characters (NULL, ESC, DEL, etc.) are filtered
  • TestPrompt_AcceptsPrintableCharacters - Verifies valid printable characters are accepted
  • TestPrompt_FiltersEscapeSequencePattern - Verifies escape sequence patterns don't introduce control characters

All tests pass successfully.

Manual Testing

  1. Build k9s: make build
  2. Run k9s: ./execs/k9s
  3. Test normal input - all keyboard input works as expected
  4. Leave idle for extended periods - no random characters appear

Impact

  • Fixes: Random character input from terminal escape sequences
  • Maintains: All existing functionality and normal keyboard input
  • Applies to: Both command buffers and filter buffers (they share the same prompt handler)

Related

Copy link
Owner

@derailed derailed left a comment

Choose a reason for hiding this comment

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

@vibhordubey333 Nice! Thank you for this update.

@derailed derailed merged commit f27846c into derailed:master Dec 13, 2025
3 checks passed
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.

2 participants