Security —

iTerm2 allows arbitrary command execution via Kitty protocol APC response injection

The vulnerability stems from the Kitty image protocol's handling of file transmit error responses within iTerm2.

This flaw allows for the execution of arbitrary commands because the error response writes literal newlines from a manipulated file path into the PTY's standard input stream, which the shell interprets as command delimiters.

This issue is a logical defect, requiring neither memory corruption nor a race condition to manifest, and it is entirely reproducible. The exploit functions across all shells, including bash, zsh, and fish, and operates on a standard iTerm2 setup without requiring any configuration adjustments from the user.

The chain of exploitation involves three distinct elements: firstly, the APC handler lacks the security verification present in OSC handlers; secondly, the Kitty file transmit error path retains actual newlines from the `NSError.localizedDescription`; and thirdly, the response is transmitted to PTY stdin without any conditional checks.

When the VT100 parser receives an APC sequence, it invokes `executeAPC:`. At point [A], it checks for tmux mode, returning early if it is active. At point [B], the parser only verifies if the string begins with "G," and at point [C], it generates a Kitty image command, passing it to the delegate. In contrast to how OSC sequences are managed, the `terminalIsTrusted` validation is entirely absent here; any APC reaching the VT100 parser is executed without regard to its origin.

Furthermore, there is no trust validation within the delegate chain. At [A], the command is forwarded directly to the `kittyImageController`. When this controller receives a t=f (file transmit) command, it decodes the base64 payload, treating it as a file path. At [A], a check via `URL.resourceValues()` determines if the file exists; if it does not, an exception is raised. Although a `filenameIsSafe()` blocklist check exists at [B], this code is only reachable after `resourceValues()` confirms existence, meaning it never runs for non-existent files. At [C], the error message embeds `error.localizedDescription` directly into the error notification. Because macOS's `NSError.localizedDescription` preserves raw newlines (0x0A) within file names without escaping them, the vulnerability is established.

The response carrying this error message is sent to PTY stdin. At [A], the default verbosity setting of `.normal` always transmits this response, irrespective of whether an error occurred. At [B], this response is encapsulated using the APC format (`ESC_G...ESC\`) and passed to the delegate. At the conclusion of the delegate sequence, the response data is written to the PTY via `screenSendReportData:`. This function routes data through the same mechanism used for keyboard input, causing the shell to process the data as if the user had typed it manually.

The developer confirmed this issue and implemented a fix across two separate commits. Implementing a `terminalIsTrusted` check within the APC handler would not have secured default installations because `terminalIsTrusted` is merely the opposite of the advanced setting “Disable potentially insecure escape sequences,” which is disabled by default, meaning the terminal is considered trustworthy by default. The resolution involves sanitizing the response to close the loophole for all users. The initial patch introduced `sanitizedResponseMessage(_:)` in `KittyImageController.swift`, which replaces every C0 control character (0x00–0x1F, including ESC, CR, LF) and DEL (0x7F) with a space before the response is formatted and dispatched. This sanitizer is applied to the error payload within `respondToTransmit()` and `respondToDisplay()`.

The subsequent patch introduced a shared sanitizer, `stringByReplacingControlCharactersWithSpace` on `NSString`, which extends protection to C1 characters (0x80–0x9F) while maintaining the proper Unicode representation for characters like ZWJ and bidi marks. This replaces the previous file-specific sanitizer and is applied across three reporting pathways: Kitty image, Kitty drag-and-drop (OSC 72), and title reporting. The sanitization for Kitty drag-and-drop was upgraded from only removing CR/LF to eliminating all control characters, and the title report sanitizer was hardened in a similar manner.

The attack vector involves embedding APC escape sequence bytes into a malicious file. When the entire file content is displayed to the terminal (for instance, using `cat`), the raw bytes are delivered to the terminal and subsequently processed by the VT100 parser. This method functions universally across all shells—bash, zsh, fish, etc.—making the shell type irrelevant. The APC sequence, which begins with `ESC _` and concludes with `ESC \`, is consumed by iTerm2’s VT100 parser and remains unseen in the `cat` output; the file appears to be a standard document. However, when the exploit activates, the error response injected into PTY stdin results in visible, corrupted shell errors appearing on the terminal. The command execution itself occurs between these garbled lines, making the execution visible to the operator. The research did not investigate hiding this output. The attack's foundation rests on two specific conditions: macOS's `NSError.localizedDescription` retaining literal newlines in filenames, and the error message utilizing Unicode curly quotes (“,”) instead of standard ASCII double quotes. The reason ASCII double quotes are avoided is that if ASCII `"` were present in the file path, it would appear unaltered in the error message. When delivered to the shell via PTY, bash enters double-quote mode upon encountering `"`, which consequently prevents newlines from acting as command separators. macOS’s `NSError.localizedDescription` employs Unicode curly quotes (“,”), which are not special characters within bash and do not trigger quote mode. The file path constructed by the adversary (prior to base64 encoding) is structured as: `/tmp/x\nopen -a Calculator\nz ^^^^^^^^^^^^^^^^^^^^ injected command (between newlines)`. The attack sequence involves: 1. Attacker: Creating an exploit file └── Any file (README.md, .txt, .log, etc.) containing concealed APC bytes. 2. Delivery: Transmitting this file via a Git repository, shared folder, archive, or email attachment. 3. Victim: Executing `cat <file>`. 4. iTerm2 processing: ├── Normal text output (visible) ├── APC bytes $\rightarrow$ VT100 Parser (invisible, consumed) │ └── `executeAPC:` $\leftarrow$ NO trust.

These developments highlight a critical dependency on sanitization routines when handling external input streams that interface with terminal emulation layers. The reliance on default settings for trust, as noted by the developer, underscores the need for robust input validation across all protocol handlers, not just those explicitly flagged as potentially insecure.

Earlier coverage

Reported by Lobste.

security

Related coverage

Elsewhere in this edition