From 143ad32a848643a67294badd1fe522fcdf6e235d Mon Sep 17 00:00:00 2001 From: caelum0x Date: Thu, 29 Oct 2026 13:36:20 +0300 Subject: [PATCH] fix(cli,tui): sanitize CardholderName on ration status/card list and the TUI Cards tab + adjust-limit modal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- services/cli/cmd/ration/status.go | 2 +- services/tui/internal/model/cardform.go | 13 ++++++++++--- services/tui/internal/view/cards.go | 9 ++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/services/cli/cmd/ration/status.go b/services/cli/cmd/ration/status.go index 375145f..db241d4 100644 --- a/services/cli/cmd/ration/status.go +++ b/services/cli/cmd/ration/status.go @@ -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, diff --git a/services/tui/internal/model/cardform.go b/services/tui/internal/model/cardform.go index 810da0a..89b85b0 100644 --- a/services/tui/internal/model/cardform.go +++ b/services/tui/internal/model/cardform.go @@ -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")}, diff --git a/services/tui/internal/view/cards.go b/services/tui/internal/view/cards.go index 756381d..ef99b00 100644 --- a/services/tui/internal/view/cards.go +++ b/services/tui/internal/view/cards.go @@ -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(),