ration/pkg/textsanitize/textsanitize.go
caelum0x 7bb72dbaa8 feat(tui): add an Agent Decisions tab
docs/THREAT-MODEL.md states plainly: "The TUI does not currently
display rationale/agent decisions at all — only tenants, deposits,
cards, and the live activity stream." Add a 5th tab (new
view/decisions.go, wired into model.go/state.go's tab state) that
fetches every tenant's ListAgentDecisions each refresh, sorted newest
first across tenants, and renders WHEN/TENANT/TRIGGER/ACTION/
RATIONALE/POLICY SNAPSHOT.

Extract the CLI's existing sanitizeControlChars into pkg/textsanitize
and apply it (via the new format.SanitizedCell, sanitize-then-truncate)
to every LLM-authored field this new tab renders, closing the same
terminal-escape-sequence injection gap for this surface at the same
time it's built, not after.
2026-10-23 18:53:58 +03:00

46 lines
1.7 KiB
Go

// Package textsanitize strips terminal-escape-sequence injection risk
// out of free text before it is ever written to an operator's
// terminal.
//
// The risk this closes is documented in docs/THREAT-MODEL.md: LLM-
// authored, attacker-influenceable text (an agent decision's
// Rationale/Trigger, ultimately derived from a trigger->prompt->
// rationale chain an adversarial deposit/webhook payload could steer)
// is printed verbatim in more than one place now — the CLI's `status`
// command, and services/tui's "Agent Decisions" tab. A sufficiently
// adversarial chain could, in principle, inject ANSI/VT100 escape
// sequences (cursor manipulation, hidden text, even OSC 8 hyperlink or
// terminal-title tricks) into whichever terminal eventually renders it.
//
// Originally lived only in services/cli/cmd/ration/status.go
// (sanitizeControlChars); extracted here so every new surface that
// renders this same LLM-authored text — starting with the TUI's
// decisions tab — gets the identical protection applied before
// rendering, not bolted on after the fact.
package textsanitize
import (
"strings"
"unicode"
)
// StripControlChars removes C0 and C1 control characters (including
// ESC, so no ANSI/VT100 escape sequence can survive) from s.
//
// Ordinary whitespace (tab, newline, carriage return) is preserved
// since typical terminal rendering (a tabwriter, a line-wrapped
// textarea) relies on it for layout; every other control character
// (0x00-0x1F, 0x7F, and the UTF-8-encoded C1 range U+0080-U+009F) is
// dropped.
func StripControlChars(s string) string {
return strings.Map(func(r rune) rune {
if r == '\t' || r == '\n' || r == '\r' {
return r
}
if unicode.IsControl(r) {
return -1
}
return r
}, s)
}