Skip to content

fix(output): human output shows control characters from the API as visible escapes - #577

Merged
miguel-heygen merged 2 commits into
mainfrom
fix/human-output-escapes-controls
Nov 16, 2026
Merged

miguel-heygen merged 2 commits into
mainfrom
fix/human-output-escapes-controls

Conversation

@miguel-heygen

@miguel-heygen miguel-heygen commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What

--human output now shows control characters in text from the API as visible \xNN escapes: C0 controls other than tab and newline, DEL, and C1 controls (U+0080 to U+009F). This covers table cells and headers, key/value labels and values, and the error message, hint, warning and note lines. The formatter's own colors and layout are unchanged.

Why

The human formatter wrote API strings to the terminal as they came. sanitizeForTable only turned whitespace into spaces. A value holding an escape sequence could move the cursor, rewrite the lines above, retitle the window or show link text that points elsewhere.

To reproduce on main: render {"title":"a\u001b]0;title\u0007b"} with the human formatter. The raw ESC and BEL reach the terminal, so a terminal that honours the title sequence retitles its window.

How

  • internal/output/terminal_text.go: terminalText escapes those runes and returns other text unchanged.
  • It is applied where the human formatter writes text:
    • humanizeKey, which builds labels and auto table headers
    • writeInline, which writes every key/value value and keeps the newline the layout splits on
    • sanitizeForTable, which writes every table cell
    • Error, Warn and Notice
  • The API error's param already reaches the hint quoted with %q.

Test plan

  • internal/output/terminal_text_test.go:
    • a table with a hostile key and value, auto and curated columns
    • key/value output with a hostile label, value, multiline value and array
    • error, hint, warning and note
    • plain text, tabs, newlines and non-ASCII come back unchanged
  • Fails without the fix: with main's human_formatter.go, the table, key/value and error tests fail.
  • The new tests pass three runs in a row. go test ./..., go vet ./internal/output/ and gofmt -l pass (Go 1.26).

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

APPROVE. Reviewed at 298d6aff0ef2cfac1731d48453faa307b5cbedf4 (base is current main).

Every place the human formatter writes API text now goes through terminalText:

  • table headers (auto headers via humanizeKey) and cells (sanitizeForTable)
  • key/value labels and values (writeInline, after newline splitting is preserved)
  • nested-object labels
  • the Error/Hint, Warn and Notice lines

Column widths and label padding are computed from the same escaped strings, so alignment stays consistent. JSON output is untouched: the diff changes only human_formatter.go plus the new file and its test.

Fails without the fix: verified. With main's human_formatter.go restored, the new tests fail:

--- FAIL: TestHumanFormatter_TableEscapesControlsInCellsAndHeaders
--- FAIL: TestHumanFormatter_KeyValueEscapesControlsInLabelsAndValues
--- FAIL: TestHumanFormatter_ErrorWarnNoticeEscapeControls

At head, go test ./internal/output/ passes (Go 1.26).

Probed terminalText directly (throwaway test, deleted):

input result
OSC 8 hyperlink ESC]8;;https://…ESC\ every ESC escaped, link inert
OSC 52 clipboard ESC]52;c;…BEL ESC and BEL escaped
CSI ESC[2J ESC[1;1H escaped
C1 CSI U+009B \x9b
literal text \x1b already in the string unchanged; a second pass is a no-op, so nothing is double-escaped

Every ESC-introduced sequence (CSI, OSC, DCS and so on) is covered because ESC itself is escaped.

Nits (non-blocking):

  1. Progress lines skip it. Under --human the wait spinner (renderSpinnerStatus in internal/output/spinner.go) and the non-TTY Polling: status=%s line (cmd/heygen/builder.go) print the API's status field as-is. It's a server-side enum rather than user-authored text, so the risk is low, but it is the same class. Fix: text := terminalText(strings.TrimSpace(status)) in renderSpinnerStatus, and terminalText(status) in the Polling: line (exporting the helper or calling it from the output package). The Logged in as %s lines in auth_login.go print the account's own name the same way, and the same one-call fix applies.
  2. Bidi controls pass through. U+202A–U+202E and U+2066–U+2069 come back unchanged, so a name like abc‮txt.exe still displays reversed in a table. They are formatting controls, not RTL letters, so escaping them would not affect Arabic or Hebrew text. If you want them in scope, add (r >= 0x202a && r <= 0x202e) || (r >= 0x2066 && r <= 0x2069) to isTerminalControl.
  3. The fast path keeps invalid UTF-8 bytes. A lone raw 0x9b byte returns unchanged, because ContainsFunc sees it as U+FFFD. JSON-decoded strings are always valid UTF-8, so API data can't reach this. If you want it airtight, gate the fast path on utf8.ValidString(s) and escape invalid bytes as \xNN in the loop.

Need: yes, API strings reached the terminal raw in human output · Size: ok · Reuse: nothing to reuse (no existing sanitizer beyond the whitespace-only sanitizeForTable, which now calls it) · Simpler: already minimal · Structure: fine (checked sanitizeForTable and the spinner's renderSpinnerStatus; one helper, no split)
Risk: low — display-only change to human output; JSON untouched.

— Rames

@miguel-heygen miguel-heygen changed the title fix(output): human output shows control characters from the API as visible escapes (PRINFRA-6687) fix(output): human output shows control characters from the API as visible escapes Nov 16, 2026
@miguel-heygen
miguel-heygen merged commit 37bb13a into main Nov 16, 2026
10 checks passed
@miguel-heygen
miguel-heygen deleted the fix/human-output-escapes-controls branch November 16, 2026 02:29
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.

2 participants