Skip to content

fix(security): sanitize control characters in prompt file paths#3141

Merged
louisgv merged 1 commit intomainfrom
fix/security-terminal-injection
Apr 1, 2026
Merged

fix(security): sanitize control characters in prompt file paths#3141
louisgv merged 1 commit intomainfrom
fix/security-terminal-injection

Conversation

@la14-1
Copy link
Copy Markdown
Member

@la14-1 la14-1 commented Apr 1, 2026

Why: Fixes terminal injection via unsanitized file paths in --prompt-file error messages. A crafted path containing ANSI escape sequences (e.g., $'\e[2J\e[H') could clear terminal output or display misleading messages in CI/CD environments parsing spawn output.

Fixes #3138

Changes

  • Added stripControlChars() utility in security.ts for safe terminal display
  • validatePromptFilePath() now rejects paths containing ASCII control characters (0x00-0x08, 0x0B-0x1F, 0x7F) early, before any file operations
  • handlePromptFileError() in index.ts strips control characters as defense-in-depth
  • Added 10 new tests covering control char rejection and stripping
  • Version bump to 0.30.6

Test plan

  • All 1987 existing tests pass
  • Biome lint: zero errors
  • New tests verify ANSI escape, null byte, bell, backspace, DEL rejection
  • Normal paths still accepted

-- refactor/security-auditor

Reject file paths containing ASCII control characters (ANSI escape
sequences, null bytes, etc.) in validatePromptFilePath() to prevent
terminal injection. Also strip control chars in handlePromptFileError()
as defense-in-depth for error paths before validation.

Fixes #3138

Agent: security-auditor
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@la14-1 la14-1 marked this pull request as ready for review April 1, 2026 12:46
Copy link
Copy Markdown
Member

@louisgv louisgv left a comment

Choose a reason for hiding this comment

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

Security Review

Verdict: APPROVED
Commit: eb96e3d

Summary

This PR addresses a HIGH severity terminal injection vulnerability by sanitizing control characters in prompt file paths. The fix is well-implemented with comprehensive test coverage.

Security Analysis

Validation Layer (security.ts:649-655)

  • Rejects paths containing control characters (0x00-0x08, 0x0B-0x1F, 0x7F) at validation time
  • Prevents ANSI escape sequences, null bytes, and other dangerous characters
  • Clear error message guides users

Defense-in-Depth (index.ts:324)

  • handlePromptFileError() sanitizes paths before displaying in error messages
  • Protects error paths that run before validation (e.g., stat failures)
  • Inlines the regex to avoid async import issues

Helper Function (security.ts:564-566)

  • stripControlChars() is properly exported for reuse
  • Preserves tabs (0x09) and newlines (0x0A) for legitimate use cases
  • Simple, correct regex implementation

Test Coverage

  • Comprehensive test suite with 21 passing tests
  • Tests validation rejection for ANSI escapes, null bytes, bell, backspace, DEL
  • Tests stripping function with edge cases (empty strings, preservation of tabs/newlines)
  • Tests normal paths unchanged

Verification

  • ✅ All tests pass (21/21)
  • ✅ Biome lint clean (0 errors)
  • ✅ Version bumped (0.30.5 → 0.30.6)

Threat Model

This fix mitigates:

  • Terminal injection via crafted file paths (HIGH severity)
  • Attackers could use ANSI escape sequences to manipulate terminal output
  • Example: \x1b[2J\x1b[H (clear screen + move cursor) in file path

No findings.


-- security/pr-reviewer

@louisgv louisgv merged commit 0c4dc61 into main Apr 1, 2026
6 checks passed
@louisgv louisgv deleted the fix/security-terminal-injection branch April 1, 2026 13:38
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.

security: Terminal injection via unsanitized file path in error messages

2 participants