Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 134 additions & 10 deletions Sources/GhosttyTerminalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9855,8 +9855,11 @@ final class GhosttySurfaceScrollView: NSView {
// MARK: - NSTextInputClient

extension GhosttyNSView: NSTextInputClient {
/// Match upstream Ghostty committed-text behavior: committed IME/AX text
/// should go through the paste-like text path, not keyboard event encoding.
fileprivate func sendTextToSurface(_ chars: String) {
guard let surface = surface else { return }
guard let data = chars.data(using: .utf8), !data.isEmpty else { return }
#if DEBUG
let typingTimingStart = CmuxTypingTiming.start()
#endif
Expand All @@ -9869,15 +9872,9 @@ extension GhosttyNSView: NSTextInputClient {
increments: ["probeInsertTextCount": 1]
)
#endif
chars.withCString { ptr in
var keyEvent = ghostty_input_key_s()
keyEvent.action = GHOSTTY_ACTION_PRESS
keyEvent.keycode = 0
keyEvent.mods = GHOSTTY_MODS_NONE
keyEvent.consumed_mods = GHOSTTY_MODS_NONE
keyEvent.text = ptr
keyEvent.composing = false
_ = ghostty_surface_key(surface, keyEvent)
data.withUnsafeBytes { rawBuffer in
guard let baseAddress = rawBuffer.baseAddress?.assumingMemoryBound(to: CChar.self) else { return }
ghostty_surface_text(surface, baseAddress, UInt(rawBuffer.count))
}
#if DEBUG
CmuxTypingTiming.logDuration(
Expand All @@ -9888,6 +9885,116 @@ extension GhosttyNSView: NSTextInputClient {
#endif
}

/// External accessibility/dictation tools should commit plain text, but
/// some inject a leading escape/control sequence first. Strip those bytes
/// on the committed-text path so they can't leak into the PTY as literals.
static func sanitizeExternalCommittedText(_ text: String) -> String {
let bytes = Array(text.utf8)
guard !bytes.isEmpty else { return text }

var index = 0
while index < bytes.count {
let byte = bytes[index]
if byte == 0x1B {
index = consumeLeadingEscapeSequence(in: bytes, from: index)
continue
}

if byte == 0x9B {
index = consumeLeadingCSISequence(in: bytes, from: index + 1)
continue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 C1 CSI (0x9B) check is unreachable for Swift String input

Swift's String stores characters in UTF-8. The Unicode character U+009B (C1 CSI) encodes as the two-byte sequence 0xC2 0x9B. When iterating over text.utf8, byte index 0 will be 0xC2, which is ≥ 0x20 and not 0x1B, so the outer loop's break fires immediately before this branch is ever reached.

The 0x9B check would only trigger for ISO-8859-1 / Latin-1 raw bytes — not for valid Swift String input.

This is dead code on the committed-text path (no real-world dictation tool produces a bare 0x9B via the Cocoa text-input pipeline), but it could create a false sense of coverage. A quick comment documenting this limitation would help future readers, or the branch could be removed.

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

if byte < 0x20 || byte == 0x7F {
index += 1
continue
}

break
}

if index == 0 {
return text
}

guard index < bytes.count else { return "" }
return String(decoding: bytes[index...], as: UTF8.self)
}

private static func consumeLeadingEscapeSequence(
in bytes: [UInt8],
from start: Int
) -> Int {
let next = start + 1
guard next < bytes.count else { return bytes.count }

switch bytes[next] {
case 0x5B:
// CSI: ESC [ ... final
return consumeLeadingCSISequence(in: bytes, from: next + 1)
case 0x4F:
// SS3: ESC O final
return min(bytes.count, next + 2)
case 0x50, 0x5D, 0x5E, 0x5F:
// DCS/OSC/PM/APC: consume until BEL/ST or EOF.
return consumeLeadingEscapedStringSequence(in: bytes, from: next + 1)
default:
// Single-character escape.
return min(bytes.count, next + 1)
}
Comment on lines +10007 to +10013

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 nF multi-byte escape sequences (ESC + 0x20–0x2F) are only partially consumed

The default branch handles every byte after ESC that isn't covered by the explicit case labels, including the 0x20–0x2F range. Those bytes are ECMA-48 "intermediate" bytes for nF sequences that require one or more additional intermediate bytes followed by a final byte — e.g. ESC SP F (3 bytes) for C0 charset designation.

Because default only consumes one byte past the ESC (return min(bytes.count, next + 1)), the remaining intermediate/final bytes of any nF sequence are left in the buffer. The outer sanitizeExternalCommittedText loop then encounters the final byte (e.g. F = 0x46, which is ≥ 0x20), hits the break, and includes that byte in the returned string — producing stray output.

In practice dictation tools do not inject nF sequences, so this shouldn't affect the Speakly fix. Worth a comment or a narrow guard to prevent silent partial strips if the sanitizer is extended later:

case 0x20...0x2F:
    // nF sequences: intermediate + final. Consume through the final byte (0x20–0x7E).
    var i = next + 1
    while i < bytes.count, (0x20...0x2F).contains(bytes[i]) { i += 1 }
    return i < bytes.count ? i + 1 : i

}

private static func consumeLeadingCSISequence(
in bytes: [UInt8],
from start: Int
) -> Int {
var index = start
while index < bytes.count {
let byte = bytes[index]
if (0x20...0x3F).contains(byte) {
index += 1
continue
}

if (0x40...0x7E).contains(byte) {
return index + 1
}

break
}

return index
}

private static func consumeLeadingEscapedStringSequence(
in bytes: [UInt8],
from start: Int
) -> Int {
var index = start
while index < bytes.count {
let byte = bytes[index]
if byte == 0x07 {
return index + 1
}

if byte == 0x1B {
let next = index + 1
if next < bytes.count, bytes[next] == 0x5C {
return next + 1
}
return index
}

if byte < 0x20 || byte == 0x7F {
return index + 1
}

index += 1
}

return bytes.count
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

func hasMarkedText() -> Bool {
return markedText.length > 0
}
Expand Down Expand Up @@ -10113,8 +10220,25 @@ extension GhosttyNSView: NSTextInputClient {
return
}

let sanitizedChars = if NSApp.currentEvent == nil {
Self.sanitizeExternalCommittedText(chars)
} else {
chars
}

#if DEBUG
if sanitizedChars != chars {
dlog(
"ime.insertText.sanitized originalBytes=\(chars.utf8.count) " +
"sanitizedBytes=\(sanitizedChars.utf8.count)"
)
}
#endif

guard !sanitizedChars.isEmpty else { return }

// Otherwise send directly to the terminal
sendTextToSurface(chars)
sendTextToSurface(sanitizedChars)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}

Expand Down
25 changes: 25 additions & 0 deletions cmuxTests/CJKIMEInputTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,31 @@ final class CJKIMEKeyTextAccumulatorTests: XCTestCase {
}
}

// MARK: - External committed-text sanitization

final class ExternalCommittedTextSanitizationTests: XCTestCase {
func testStripsLeadingCSISequenceFromExternalCommittedText() {
XCTAssertEqual(
GhosttyNSView.sanitizeExternalCommittedText("\u{1B}[Chello"),
"hello"
)
}

func testStripsMultipleLeadingControlAndEscapeSequences() {
XCTAssertEqual(
GhosttyNSView.sanitizeExternalCommittedText("\u{1B}[1;5C\u{1B}OChello"),
"hello"
)
}

func testLeavesLiteralBracketPrefixedTextUntouched() {
XCTAssertEqual(
GhosttyNSView.sanitizeExternalCommittedText("[Code] review"),
"[Code] review"
)
}
}
Comment on lines +875 to +914

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Regression test commit policy: test and fix bundled in one commit

Per CLAUDE.md ("Regression test commit policy"), regression tests for a bug fix must be split into two commits so CI can prove the test fails without the fix:

  1. Commit 1 — failing test only (CI goes red)
  2. Commit 2 — the fix (CI goes green)

Both the new ExternalCommittedTextSanitizationTests and the sanitizer implementation landed in the same commit (e63d3ebc). The CI evidence that these tests catch the bug without the fix is therefore missing from the PR's commit history.


// MARK: - Shift+Space fallback suppression (IME source-switch shortcut)

final class CJKIMEShiftSpaceFallbackTests: XCTestCase {
Expand Down
Loading