Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Terminal escape sequence injection via malicious HTTP response data

Đang mở
#1,812 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
python
Lĩnh vực
cli, security

Hướng nghiên cứu

Bắt đầu bằng cách đọc httpie/output/streams.py, đặc biệt là BaseStream.iter, EncodedStream và PrettyStream, sau đó theo dõi cách httpie/output/writer.py xử lý write_stream và write_stream_with_colors_win. Xác minh hành vi bằng bản tái hiện máy chủ độc hại được cung cấp trong một TTY. Hoàn tất khi các chuỗi điều khiển terminal được làm sạch đối với đầu ra TTY, trong khi đầu ra được chuyển hướng vẫn ở dạng thô, với độ bao phủ cho các header và nội dung body.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Terminal escape sequence injection via HTTP response data

Summary

HTTPie writes HTTP response headers and body content to the terminal without stripping or escaping terminal control sequences. A malicious HTTP server can embed ANSI escape codes or other terminal control characters in response headers or body text, potentially manipulating the user's terminal display, changing the terminal title, injecting clipboard content (on supported terminals), or in worst-case scenarios with vulnerable terminal emulators, executing commands.

Details

When HTTPie renders a response to a TTY, the data flows through output/streams.py and output/writer.py without any sanitization of terminal control characters. While Pygments-based colorization adds its own ANSI escape sequences for syntax highlighting, it does not filter out escape sequences that are already present in the response data.

The relevant code path is:

  1. Response received from server with embedded escape sequences
  2. BaseStream.__iter__() yields header and body chunks (streams.py, line 63)
  3. write_stream() writes chunks directly to the output buffer (writer.py, line 73)
  4. Terminal interprets all embedded escape sequences

Affected locations:

  • httpie/output/streams.py - BaseStream.__iter__(), EncodedStream, PrettyStream
  • httpie/output/writer.py - write_stream(), write_stream_with_colors_win()

Impact

Terminal display manipulation: An attacker controlling a server can overwrite previous terminal output, hide the real response, or display misleading content. For example, a response could clear the screen and display fake content that looks like a different response.

Terminal title/clipboard injection: On terminals that support OSC escape sequences:

  • \x1b]2;text\x07 changes the terminal window title
  • \x1b]52;c;base64\x07 sets clipboard content on some terminals (iTerm2, some xterm configs)

Terminal emulator vulnerabilities: Some terminal emulators have had historical vulnerabilities where specific escape sequences can trigger command execution or file writes (e.g., CVE-2003-0063 for xterm window title reporting, various iTerm2 and Konsole issues).

This is particularly relevant for HTTPie because it's commonly used in security research and API testing, contexts where users frequently interact with untrusted servers.

Reproduction

Start a malicious server:

import http.server

class Handler(http.server.BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        # Inject terminal title change via header
        self.send_header('X-Custom', '\x1b]2;INJECTED TITLE\x07normal value')
        self.end_headers()
        # Body: clear screen + fake content
        body = '\x1b[2J\x1b[HHTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nEverything is fine.'
        self.wfile.write(body.encode())

http.server.HTTPServer(('localhost', 9999), Handler).serve_forever()

Then:

http localhost:9999

The terminal title will change, and the real response content will be replaced with fake output.

Suggested Fix

Filter terminal control characters when outputting to a TTY. A conservative approach:

import re

# Match ANSI escape sequences and other control characters
CONTROL_CHAR_RE = re.compile(
    r'[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]'  # C0 control chars (except \t, \n, \r)
    r'|\x1b(?:\[[^a-zA-Z]*[a-zA-Z]'          # CSI sequences
    r'|\][^\x07]*\x07'                         # OSC sequences
    r'|\(.'                                     # charset selection
    r'|.)'                                      # other escape sequences
)

def sanitize_for_terminal(text: str) -> str:
    return CONTROL_CHAR_RE.sub('', text)

Apply this sanitization in the stream output path when env.stdout_isatty is True, but not when output is redirected to a file or pipe (to preserve raw data for scripting).

This could be implemented as a flag (e.g., --raw-terminal to disable sanitization) for users who explicitly want raw output.

Ngôn ngữ chính
Python
Star
38.6k
Fork
4k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của httpie/cli

Tất cả issue của httpie/cli

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.