caelum0x 143ad32a84 fix(cli,tui): sanitize CardholderName on ration status/card list and the TUI Cards tab + adjust-limit modal
CardholderName is operator-authored free text (ration card issue
--cardholder, and the TUI's own issue-card form) — the same class of
terminal-escape-injection risk already closed for Trigger/Rationale/
PolicySnapshotJson. card.go's printCard (card issue/get/adjust-limit/
close) already sanitized it, but status.go's printCards (used by both
`ration status` and `ration card list`) printed it raw, and the TUI's
Cards tab table (view/cards.go) and the adjust-limit modal's infoLine
(model/cardform.go) did too. A cardholder name containing an ANSI
escape sequence would inject into `ration status` output — the single
most commonly run command in this whole repo's demo flow — and into
the TUI dashboard a judge/operator is meant to watch live.

Fixed all three raw render sites to go through the same
sanitizeControlChars/format.SanitizedCell/textsanitize.StripControlChars
helpers already used everywhere else free text is displayed.

Verified live against the real bare-metal 8-service stack: issued a
card with a cardholder name containing a real ESC (0x1b) byte +
"[31;1mINJECTED[0m", confirmed `ration status` and `ration card list`
output now contain zero raw ESC bytes (byte-counted, not just visual),
and drove the real TUI through a tmux pty — both the Cards tab table
and the adjust-limit modal's header render the literal bracket text
with no live escape sequence. go build/go vet/go test clean for cli
and tui, gofmt clean, 0 regressions.
2026-10-29 13:36:20 +03:00

36 lines
1.5 KiB
Go

package view
import (
gatewaypb "github.com/caelum0x/ration/pkg/pb/gateway"
"github.com/caelum0x/ration/services/tui/internal/format"
)
var cardHeaders = []string{"TENANT", "CARDHOLDER", "STATUS", "LIMIT", "INTERVAL", "CREATED"}
var cardWidths = []int{16, 18, 10, 14, 9, 19}
// Cards renders the cards tab: every issued card across all tenants, with
// status color-coded so a RATIONED card (spend capped below the request
// due to bridge funding, per docs/AGENT-DESIGN.md) is visible at a glance
// next to plain ACTIVE/SUSPENDED/CLOSED cards.
//
// CardholderName is operator-authored free text (set via "ration card
// issue --cardholder" or this TUI's own issue-card form) — the same class
// of terminal-escape-injection risk already closed for Trigger/Rationale/
// PolicySnapshotJson in decisions.go via format.SanitizedCell. It goes
// through the identical helper here so a malicious/careless cardholder
// name can't inject escape sequences into this table's rendering.
func Cards(cards []*gatewaypb.Card, tenantNames map[string]string, cursor int) string {
rows := make([][]string, 0, len(cards))
for _, c := range cards {
rows = append(rows, []string{
tenantNames[c.GetTenantId()],
format.SanitizedCell(c.GetCardholderName(), cardWidths[1]),
format.CardStatusLabel(c.GetStatus()),
format.Money(c.GetLimitMinorUnits(), c.GetCurrency()),
c.GetLimitInterval(),
format.Unix(c.GetCreatedAtUnix()),
})
}
return Table(cardHeaders, rows, cardWidths, cursor, 2, cardStatusStyle)
}