Heuristic:1broseidon Ketch Terminal Safe Text
| Knowledge Sources | |
|---|---|
| Domains | Security, Rendering, Terminal |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
A rendering rule for the pi extension's UI projections: every value an agent returns — tool call arguments, result snippets, expanded details — is run through `safeText`, which strips ANSI/OSC/DCS sequences, C0/C1 control codes, and invisible bidirectional override characters, so remote content cannot inject terminal escapes or hide text via bidi tricks.
The Insight (Rule of Thumb)
- Action: `safeText` runs every string through five regex passes: OSC/DCS sequences (`\x1b]` / `\x9d` … `BEL`/`ST`), DCS/PMC/PMD strings (`\x1b[P^_]` … `ST`), CSI sequences (`\x1b[` / `\x9b` … final byte), two-byte escapes (`\x1b` + intermediate+final), and a final pass for C0/C1 controls plus the Unicode bidi overrides (``-``, ``-``). `preview` then collapses whitespace and truncates to a limit.
- Value: The UI projections are read-only and never mutate tool content — the model keeps the complete CLI response; the TUI shows a compact, bounded projection. Stripping escapes keeps remote content inert: a scraped page's title or snippet that contains a terminal escape sequence cannot reprogram the terminal, and a bidi override cannot hide a malicious fragment in an otherwise benign-looking line.
- Trade-off: The regex passes are lossy — a legitimately escaped sequence in content (e.g. a tutorial showing ANSI codes) is flattened. This is acceptable because the projection is a summary, not a faithful copy; `fullDetails` preserves the model's verbatim text but still runs it through `safeText`.
- Compatibility: `safeText` returns a plain string; `preview` bounds it (default 180 chars, `…` suffix). The renderers wrap each line in `theme.fg` (which adds colour on top of already-safe text), so theme styling is applied after sanitization, not before.
Reasoning
The pi extension renders tool output inside a terminal UI (`@earendil-works/pi-tui`), and the content comes from remote sources: a scraped page's title, a search result's snippet, a crawl's page markdown. Any of those can contain terminal escape sequences — an attacker-controlled page could include a CSI sequence that moves the cursor or changes the title bar, or a bidi override that renders `safe.com` to the left of `evil.com` so a URL looks benign. `safeText` (`plugins/pi/extensions/render.ts:13-20`) is the single sanitizer every projected string passes through before it reaches the `Text` widget.
The five-pass structure matches the escape families in order of complexity: OSC/DCS first (they have their own terminators), then CSI (parameter + intermediate + final byte), then two-byte escapes, then a sweep for residual C0 (`\x00`-`\x08`, `\x0b`-`\x1f`) and C1 (`\x7f`-`\x9f`) controls, plus the Unicode bidi block. The bidi pass is the defense against theTrojan Source-style attack where directionality marks hide code: even if a snippet contains RLO/PDI, it is removed before rendering.
The "UI-only projections, never mutate tool content" comment at the top of `render.ts` is the contract: the model holds the full response, the UI holds a safe summary. `fullDetails` (`render.ts:116-132`) shows the full structured response but still pipes it through `safeText`, so even an expanded view cannot be an injection vector.