caelum0x 4b501667e2 fix(cli,tui): tenant Name rendered raw in status/TUI — a fourth unsanitized terminal-escape-injection call site rounds 13/16/23 never reached
ration status (the single most-run command in this repo, per round 13's
own framing)'s printTenantHeader printed tenant.Name completely raw, with
no sanitization at all. Same gap, same root cause, across the TUI: the
Tenants tab table, the Activity tab's per-row TenantName, and the card
form's tenant picker + adjust-limit infoLine all rendered a Tenant's own
Name raw too. services/tenant-registry's Create only validates name
non-emptiness, no charset check — round 16 already proved a raw ESC byte
survives into a persisted Tenant record via "tenant create --name".
Rounds 13/16/23 sanitized CardholderName, the CLI's own tenant-create
echo, and the Activity feed's Summary, but never Tenant.Name as read
back through these five separate call sites.

Reproduced live against the real bare-metal 8-service stack
(scripts/start-individually.sh): created a tenant with
--name $'Evil\x1b[31;1mINJECTED\x1b[0m Status Co', then byte-counted real
stdout — `ration status --tenant <id>` carried 2 raw ESC bytes pre-fix,
0 after. Drove the real bin/tui through a tmux pty: Tenants tab and the
card-issue form's tenant picker both rendered the literal bracket text
pre-fix verification showed was otherwise a live ANSI escape (confirmed
via `tmux capture-pane -e` byte inspection, distinguishing it from tmux/
lipgloss's own legitimate styling codes).

Fix: sanitizeControlChars(tenant.Name) in status.go's printTenantHeader;
format.SanitizedCell(t.GetName(), ...) in tui/view/tenants.go;
textsanitize.StripControlChars on ActivityRow.TenantName in
tui/view/activity.go; and on both the tenant picker's display name and
newAdjustLimitForm's tenantName param in tui/model/cardform.go — all via
the same helpers this repo's prior sanitization rounds already
established.

go build/go vet/go test clean for services/cli and services/tui (0
regressions), gofmt clean, full make build-go (10 binaries) clean.
2026-10-29 17:26:40 +03:00

113 lines
3.5 KiB
Go

package view
import (
"fmt"
"strings"
"github.com/caelum0x/ration/pkg/textsanitize"
"github.com/caelum0x/ration/services/tui/internal/format"
)
// ActivityRow is one line in the live activity feed, already resolved to a
// display-ready tenant name (the model does the tenant_id -> name lookup
// once per event, not per render).
type ActivityRow struct {
ID string
TenantName string
Kind string
Summary string
AtUnix int64
}
// Activity renders the activity feed tab: a scrolling, newest-first list
// fed live by the gateway's StreamActivity RPC. rows is expected
// newest-first (the model prepends on arrival); height is the number of
// visible lines available for the list.
func Activity(rows []ActivityRow, cursor, height int) string {
if len(rows) == 0 {
return EmptyStyle.Render("(waiting for activity…)")
}
start, end := visibleWindow(len(rows), cursor, height)
var b strings.Builder
for i := start; i < end; i++ {
r := rows[i]
// Summary is built server-side (services/gateway/internal/
// grpcserver/server.go's publish calls) by interpolating
// free-text, operator/LLM-influenceable fields verbatim — e.g.
// IssueCard's CardholderName and AdjustCardLimit's Reason —
// into the activity feed's Summary string with no
// sanitization anywhere upstream, unlike every other surface
// that renders CardholderName (round 13/16's own
// textsanitize/sanitizeControlChars sweep never reached this
// RPC's own Summary field, a separate code path). Strip here,
// at the render boundary, the same place every other
// terminal-facing field in this package already does it
// (format.SanitizedCell's own doc comment), so no ANSI/VT100
// escape sequence embedded in a cardholder name or an
// adjust-limit reason can survive into this tab.
summary := textsanitize.StripControlChars(r.Summary)
// r.TenantName is resolveTenantName's lookup of the Tenant's own
// operator-supplied Name (round-trips raw if the tenant_id isn't
// yet known locally) — the same unsanitized-free-text-into-
// terminal class as Summary just above, on a sibling field this
// line renders raw via format.Truncate alone (which only cuts
// length, never strips control bytes, per that function's own
// doc comment). Strip it the same way.
tenantName := textsanitize.StripControlChars(r.TenantName)
line := fmt.Sprintf("%s %-8s %-16s %s",
format.Unix(r.AtUnix),
strings.ToUpper(r.Kind),
format.Truncate(tenantName, 16),
summary,
)
kindLabel := strings.ToUpper(r.Kind)
styled := activityKindStyle(r.Kind).Render(kindLabel)
line = strings.Replace(line, kindLabel, styled, 1)
if i == cursor {
line = SelectedRowStyle.Render(format.Truncate(
fmt.Sprintf("%s %-8s %-16s %s", format.Unix(r.AtUnix), kindLabel, format.Truncate(r.TenantName, 16), summary),
200,
))
}
b.WriteString(line)
if i < end-1 {
b.WriteString("\n")
}
}
return b.String()
}
// visibleWindow computes the [start, end) slice of a total-length list to
// display given the available height, keeping cursor inside the window.
// Pure and independent of any terminal/render state, so it's unit tested
// directly.
func visibleWindow(total, cursor, height int) (int, int) {
if height <= 0 || total == 0 {
return 0, 0
}
if total <= height {
return 0, total
}
if cursor < 0 {
cursor = 0
}
if cursor >= total {
cursor = total - 1
}
start := cursor - height/2
if start < 0 {
start = 0
}
end := start + height
if end > total {
end = total
start = end - height
}
return start, end
}