Fix terminal escape sequence injection via HTTP response data#1817
Open
eddieran wants to merge 1 commit intohttpie:masterfrom
Open
Fix terminal escape sequence injection via HTTP response data#1817eddieran wants to merge 1 commit intohttpie:masterfrom
eddieran wants to merge 1 commit intohttpie:masterfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
Fix
Added
httpie/output/sanitize.pywith asanitize_output()function that strips dangerous terminal control sequences from output bytes while preserving:\t,\n,\rESC [ ... m) used by HTTPie's own syntax highlightingThe sanitization is applied in
write_stream()andwrite_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
ESC ] 0;title BELESC [ 2 J,ESC [ 1;1 HESC cBEL,BS,VT,FF,DELFiles changed
httpie/output/sanitize.pyβ New module withsanitize_output()functionhttpie/output/writer.pyβ Apply sanitization when writing to TTYtests/test_terminal_sanitize.pyβ 19 tests (14 unit + 5 integration)Test plan
printf 'HTTP/1.1 200\r\nX-Evil: \x1b]0;pwned\x07\r\n\r\nbody' | nc -l 8888thenhttp localhost:8888β terminal title should not change