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.
This commit is contained in:
caelum0x 2026-10-29 13:36:20 +03:00
parent 9b4e470dce
commit 143ad32a84
3 changed files with 19 additions and 5 deletions

View File

@ -155,7 +155,7 @@ func printCards(out io.Writer, cards []*gatewaypb.Card) {
fmt.Fprintln(w, " CARDHOLDER\tSTATUS\tLIMIT\tINTERVAL\tCARD ID")
for _, card := range cards {
fmt.Fprintf(w, " %s\t%s\t%s\t%s\t%s\n",
card.CardholderName,
sanitizeControlChars(card.CardholderName),
card.Status.String(),
moneyfmt.Minor(card.LimitMinorUnits, card.Currency),
card.LimitInterval,

View File

@ -9,6 +9,7 @@ import (
gatewaypb "github.com/caelum0x/ration/pkg/pb/gateway"
ledgerpb "github.com/caelum0x/ration/pkg/pb/ledger"
"github.com/caelum0x/ration/pkg/textsanitize"
"github.com/caelum0x/ration/services/tui/internal/client"
)
@ -103,9 +104,15 @@ func newIssueCardForm(tenants []*ledgerpb.Tenant, selectedIdx int) *cardForm {
// reason.
func newAdjustLimitForm(card *gatewaypb.Card, tenantName string) *cardForm {
return &cardForm{
kind: cardFormAdjustLimit,
cardID: card.GetId(),
infoLine: fmt.Sprintf("%s — %s — current limit %s", tenantName, card.GetCardholderName(), card.GetLimitInterval()),
kind: cardFormAdjustLimit,
cardID: card.GetId(),
// card.GetCardholderName() is operator-authored free text (set via
// "ration card issue --cardholder" or this same form's own issue
// path) — stripped of control characters before going into infoLine
// so a malicious/careless cardholder name can't inject terminal
// escape sequences into the adjust-limit modal's rendering (same
// class of gap closed for the Cards tab's own table in view/cards.go).
infoLine: fmt.Sprintf("%s — %s — current limit %s", tenantName, textsanitize.StripControlChars(card.GetCardholderName()), card.GetLimitInterval()),
fields: []*formField{
{label: "New limit (minor units)", kind: fieldNumeric, value: []rune(strconv.FormatInt(card.GetLimitMinorUnits(), 10))},
{label: "Reason", kind: fieldText, value: []rune("ration tui adjust-limit")},

View File

@ -12,12 +12,19 @@ var cardWidths = []int{16, 18, 10, 14, 9, 19}
// 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()],
c.GetCardholderName(),
format.SanitizedCell(c.GetCardholderName(), cardWidths[1]),
format.CardStatusLabel(c.GetStatus()),
format.Money(c.GetLimitMinorUnits(), c.GetCurrency()),
c.GetLimitInterval(),