Skip to content

fix(markdown): strip terminal controls from rendered text - #4948

Open
rumpl wants to merge 1 commit into
mainfrom
rumpl/markdown-strip-terminal-controls
Open

rumpl wants to merge 1 commit into
mainfrom
rumpl/markdown-strip-terminal-controls

Conversation

@rumpl

@rumpl rumpl commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Summary

The Markdown renderer only removed \r, \b, \f and \v, so escape sequences in model, tool, or shell output reached the terminal. They could clear the screen, hide the cursor, set the window title, or write the clipboard via OSC 52.

The renderer now strips:

  • every escape sequence: CSI, OSC, and two-byte and charset escapes;
  • DCS, SOS, PM and APC control strings, with their payload (an unterminated one runs to the end of the text);
  • all C0 and C1 controls other than newline and tab. C1 controls, whether UTF-8 encoded or raw 8-bit bytes, are first normalized to their 7-bit escape forms, so their parameters and payload are removed too. Other invalid UTF-8 becomes U+FFFD.

The incremental renderer sanitizes the whole input before choosing block boundaries, so a control string spanning a blank line is removed exactly as in one-shot rendering.

Renderer-generated controls, such as hyperlinks, are added after sanitizing and are unaffected. Image labels and media fallbacks are inserted after rendering, so they are sanitized separately through the new markdown.StripTerminalControls.

Behavior change: colors in ! shell output, rendered as a console block, are stripped along with the other controls. Code-block copy buttons copy the sanitized text, matching what is displayed. This was already the case for \r\b\f\v.

First of a stack, split out of #4952:

  1. This PR: strip terminal controls
  2. feat(markdown): render trusted SGR styling and replay it across wraps #4949: render trusted SGR styling
  3. feat: add display-only Markdown transformers #4952: display-only Markdown transformers
  4. feat(leantui): apply display-only Markdown transformers #4953: Markdown transformers in the lean TUI

Validation

  • task build; race tests for pkg/tui/... and pkg/leantui/...
  • golangci-lint reports no new issues

Copilot AI left a comment

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.

🟡 Changes recommended

Incremental rendering sanitizes blank-line fragments independently, allowing control-string payloads hidden by one-shot rendering to become visible when they span fragment boundaries.

1 open issue
What changed in this PR

This PR hardens the shared Markdown renderer against terminal-control injection from untrusted text, laying groundwork for trusted styling and display-only Markdown transformers.

Changes:

  • Strips input escape sequences and control characters while preserving renderer-generated controls.
  • Sanitizes image labels and media fallbacks inserted after Markdown rendering.
  • Adds sanitization tests; rendered code and code-block copies now omit input terminal styling.
File Description
pkg/​tui/​components/​message/​message.go Sanitizes image labels and media fallbacks.
pkg/​tui/​components/​message/​append_content_test.go Tests label and fallback sanitization.
pkg/​tui/​components/​markdown/​sanitize.go Adds shared escape-sequence and control-character removal.
pkg/​tui/​components/​markdown/​sanitize_test.go Tests control removal and preservation of generated hyperlinks.
pkg/​tui/​components/​markdown/​fast_renderer.go Removes the limited sanitizer in favor of the shared implementation.

Review effort: Balanced

Comment thread pkg/tui/components/markdown/sanitize.go Resolved
@aheritier aheritier added area/tui For features/issues/fixes related to the TUI kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Nov 17, 2026
@rumpl
rumpl added this pull request to stack #4954 November 17, 2026 22:41
@rumpl
rumpl force-pushed the rumpl/markdown-strip-terminal-controls branch from 631822c to 7aceebe Compare November 17, 2026 22:47
Copilot AI balanced review requested due to automatic review settings November 17, 2026 22:47

Copilot AI left a comment

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.

🟡 Changes recommended

The sanitizer still allows raw 8-bit C1 bytes to bypass filtering, leaving executable terminal control sequences intact on terminals that accept them.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread pkg/tui/components/markdown/sanitize.go Resolved
The renderer only removed \r, \b, \f and \v, so escape sequences in
model, tool, or shell output reached the terminal: they could clear the
screen, hide the cursor, set the title, or write the clipboard via OSC 52.

Strip every escape sequence, including DCS/SOS/PM/APC control strings
with their payload, and all C0/C1 controls other than newline and tab.
Renderer-generated controls such as hyperlinks are added afterwards and
are unaffected. Image labels and media fallbacks are inserted after
rendering, so they are sanitized separately.

Signed-off-by: Djordje Lukic <[email protected]>
@rumpl
rumpl force-pushed the rumpl/markdown-strip-terminal-controls branch from 7aceebe to 24315e2 Compare November 17, 2026 22:58
Copilot AI balanced review requested due to automatic review settings November 17, 2026 22:58

Copilot AI left a comment

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.

🟢 Approval recommended

No active review issues are recorded for the Markdown terminal-control sanitization changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/tui For features/issues/fixes related to the TUI kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants