Repository navigation
fix(output): human output shows control characters from the API as visible escapes - #577
Conversation
jrusso1020
left a comment
There was a problem hiding this comment.
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,WarnandNoticelines
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):
- Progress lines skip it. Under
--humanthe wait spinner (renderSpinnerStatusininternal/output/spinner.go) and the non-TTYPolling: status=%sline (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))inrenderSpinnerStatus, andterminalText(status)in thePolling:line (exporting the helper or calling it from theoutputpackage). TheLogged in as %slines inauth_login.goprint the account's own name the same way, and the same one-call fix applies. - Bidi controls pass through. U+202A–U+202E and U+2066–U+2069 come back unchanged, so a name like
abctxt.exestill 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)toisTerminalControl. - The fast path keeps invalid UTF-8 bytes. A lone raw
0x9bbyte returns unchanged, becauseContainsFuncsees 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 onutf8.ValidString(s)and escape invalid bytes as\xNNin 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
What
--humanoutput now shows control characters in text from the API as visible\xNNescapes: 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.
sanitizeForTableonly 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:terminalTextescapes those runes and returns other text unchanged.humanizeKey, which builds labels and auto table headerswriteInline, which writes every key/value value and keeps the newline the layout splits onsanitizeForTable, which writes every table cellError,WarnandNoticeparamalready reaches the hint quoted with%q.Test plan
internal/output/terminal_text_test.go:human_formatter.go, the table, key/value and error tests fail.go test ./...,go vet ./internal/output/andgofmt -lpass (Go 1.26).