Skip to content

security: [HIGH] Potential command injection via TERM variable terminal escape sequences #2461

@louisgv

Description

@louisgv

Summary

The sanitizeTermValue() function in packages/cli/src/shared/ui.ts uses a regex pattern to validate TERM values, but the pattern may allow terminal escape sequences that could be weaponized when interpolated into shell commands on remote servers.

Location

packages/cli/src/shared/ui.ts:276-283

Current Implementation

export function sanitizeTermValue(term: string): string {
  if (/^[a-zA-Z0-9._-]+$/.test(term)) {
    return term;
  }
  return "xterm-256color";
}

Vulnerability

While the function validates against a restricted character set, TERM values that pass this validation could still contain terminal escape sequences embedded within valid characters. When these values are interpolated into shell commands executed on remote servers, they could potentially execute arbitrary commands or manipulate terminal behavior.

Risk Assessment

  • Severity: HIGH
  • Likelihood: MEDIUM (requires attacker to control TERM environment variable)
  • Impact: Command injection on remote servers

Recommendation

Replace pattern matching with an explicit whitelist of known-safe TERM values:

export function sanitizeTermValue(term: string): string {
  const safeTerm = [
    'xterm-256color',
    'xterm',
    'screen-256color', 
    'screen',
    'tmux-256color',
    'tmux',
    'linux',
    'vt100',
    'vt220'
  ];
  
  if (safeTerm.includes(term)) {
    return term;
  }
  return 'xterm-256color';
}

-- security/code-scanner

Metadata

Metadata

Assignees

No one assigned

    Labels

    in-progressIssue is being actively worked onsafe-to-workSecurity triage: safe for automated processing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions