Skip to content

security: Terminal injection via unsanitized file path in error messages #3138

@louisgv

Description

@louisgv

File: packages/cli/src/index.ts
Lines: 323, 326, 329, 332
Severity: MEDIUM

Description:
The readPromptFile function (lines 337-368) accepts user-provided file paths via the --prompt-file flag and includes the raw path in error messages without sanitizing terminal control characters:

console.error(pc.red(`Prompt file not found: ${pc.bold(promptFile)}`));
console.error(pc.red(`Permission denied reading prompt file: ${pc.bold(promptFile)}`));
console.error(pc.red(`'${promptFile}' is a directory, not a file.`));
console.error(pc.red(`Error reading prompt file '${promptFile}': ${getErrorMessage(err)}`));

While the path is validated via validatePromptFilePath and validatePromptFileStats, these validators check for security properties (path traversal, symlinks) but don't strip ANSI escape sequences or terminal control characters.

If a user provides a malicious path containing ANSI escape sequences (e.g., --prompt-file $'\e[31mFAKE_ERROR\e[0m'), those sequences could be interpreted by the terminal, potentially:

  • Displaying misleading error messages
  • Hiding portions of the error output
  • Causing terminal confusion in automated CI/CD environments

Attack Vector:

  1. User runs: spawn claude sprite --prompt-file $'\e[2J\e[HMalicious Path'
  2. The \e[2J\e[H ANSI codes clear the screen and move cursor to home
  3. Error message is displayed, but previous terminal output is cleared

Recommendation:
Sanitize file paths before inclusion in error messages by stripping control characters:

function sanitizePathForDisplay(path: string): string {
  return path.replace(/[\x00-\x1F\x7F]/g, ''); // Strip ASCII control characters
}

// Usage:
const displayPath = sanitizePathForDisplay(promptFile);
console.error(pc.red(`Prompt file not found: ${pc.bold(displayPath)}`));

Alternatively, enhance validatePromptFilePath to reject paths containing control characters.

Impact:
Medium — Requires user to provide malicious input to their own CLI, but could affect automated systems parsing spawn output.

-- code-scanner

Metadata

Metadata

Assignees

No one assigned

    Labels

    safe-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