Skip to content

Fix terminal escape sequence injection via HTTP response data#1817

Open
eddieran wants to merge 1 commit intohttpie:masterfrom
eddieran:fix/terminal-escape-sanitization
Open

Fix terminal escape sequence injection via HTTP response data#1817
eddieran wants to merge 1 commit intohttpie:masterfrom
eddieran:fix/terminal-escape-sanitization

Conversation

@eddieran
Copy link
Copy Markdown

Summary

Fixes #1812 β€” Terminal escape sequence injection via HTTP response data.

A malicious HTTP server can embed ANSI escape sequences in response headers or body to manipulate the user's terminal when HTTPie displays the output. This can be used to:

  • Set the terminal window title (OSC 0)
  • Inject clipboard content (OSC 52)
  • Move the cursor to overwrite previously displayed content (CSI H)
  • Clear the screen (CSI J)
  • Reset the terminal (ESC c)

Fix

Added httpie/output/sanitize.py with a sanitize_output() function that strips dangerous terminal control sequences from output bytes while preserving:

  • Safe whitespace: \t, \n, \r
  • SGR color codes (ESC [ ... m) used by HTTPie's own syntax highlighting

The sanitization is applied in write_stream() and write_stream_with_colors_win() only when output is to a TTY (stdout_isatty=True). Piped/redirected output is left untouched so raw data is preserved for scripts.

What is stripped

Sequence type Example Purpose
OSC sequences ESC ] 0;title BEL Window title, clipboard manipulation
Non-SGR CSI sequences ESC [ 2 J, ESC [ 1;1 H Screen clear, cursor movement
Single-char escapes ESC c Terminal reset
Dangerous C0 chars BEL, BS, VT, FF, DEL Bell, backspace, etc.

Files changed

  • httpie/output/sanitize.py β€” New module with sanitize_output() function
  • httpie/output/writer.py β€” Apply sanitization when writing to TTY
  • tests/test_terminal_sanitize.py β€” 19 tests (14 unit + 5 integration)

Test plan

  • All 19 new tests pass
  • All 137 existing output/stream tests pass (0 regressions)
  • Manual test: printf 'HTTP/1.1 200\r\nX-Evil: \x1b]0;pwned\x07\r\n\r\nbody' | nc -l 8888 then http localhost:8888 β€” terminal title should not change

Strip dangerous terminal control sequences (OSC, non-SGR CSI, C0 control
chars) from HTTP response output when writing to a TTY.  This prevents
malicious servers from injecting escape codes that manipulate the terminal
title, clipboard, cursor position, or display.

Sanitization is only applied to TTY output β€” piped/redirected output is
left untouched to preserve raw data for scripts.

HTTPie's own SGR color sequences (ESC [ ... m) are preserved so syntax
highlighting continues to work normally.

Fixes httpie#1812
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.

Terminal escape sequence injection via malicious HTTP response data

1 participant