fix: close Ticket 11 hardening review gaps

This commit is contained in:
proxyctl coordinator
2026-07-18 02:04:26 +08:00
parent f15ffc2301
commit ed8b527f1f
15 changed files with 1045 additions and 175 deletions
+227 -19
View File
@@ -3,6 +3,7 @@ package tui
import (
"context"
"errors"
"fmt"
"strings"
"testing"
@@ -215,6 +216,108 @@ func TestCompleteModelBoundsUnicodeAndKeepsLongContentReachable(t *testing.T) {
}
}
func TestCompleteModelLongFieldsAndDialogsRemainReachableAcrossResizeAtNonterminalOffsets(t *testing.T) {
t.Run("long URL field and Bypass preview", func(t *testing.T) {
snapshot := hardeningSnapshot()
snapshot.Profiles[0].SingleURL = "http://start.example:8080/" + strings.Repeat("a", 90) + " URL-MIDDLE " + strings.Repeat("z", 90) + "URL-END"
bypass := make([]string, 40)
for i := range bypass {
bypass[i] = fmt.Sprintf("host-%02d.example", i+1)
}
snapshot.Profiles[0].Bypass = bypass
model := resizedModel(t, &recordingModule{snapshot: snapshot}, themeModeNoColor, 80, 24)
model = updateKey(model, "m")
model = updateKey(model, "e")
model = updateKey(model, "tab")
for range 115 {
model = updateKey(model, "left")
}
assertCellBoundedFrame(t, model.View(), 80, 24, "URL-MIDDLE")
model = resize(model, 120, 36)
assertCellBoundedFrame(t, model.View(), 120, 36, "URL-MIDDLE")
model = resize(model, 80, 24)
model = updateKey(model, "tab")
model = updateKey(model, "home")
model = updateKey(model, "pgdown")
model = updateKey(model, "pgdown")
view := model.View()
assertCellBoundedFrame(t, view, 80, 24, "host-10.example")
if strings.Contains(view, "host-40.example") {
t.Fatalf("Bypass preview unexpectedly jumped to its terminal offset:\n%s", view)
}
model = resize(model, 120, 36)
assertCellBoundedFrame(t, model.View(), 120, 36, "host-10.example")
})
t.Run("expanded error", func(t *testing.T) {
snapshot := hardeningSnapshot()
lines := make([]string, 60)
for i := range lines {
lines[i] = fmt.Sprintf("ERROR-LINE-%02d", i+1)
}
module := &targetModule{snapshot: snapshot, result: app.Result{Snapshot: snapshot, Err: errors.New(strings.Join(lines, "\n"))}}
model := resizedModel(t, module, themeModeNoColor, 80, 24)
updated, command := model.Update(tea.KeyMsg{Type: tea.KeySpace})
model = updated.(Model)
updated, _ = model.Update(command())
model = updated.(Model)
model = updateKey(model, "e")
model = updateKey(model, "pgdown")
model = updateKey(model, "pgdown")
view := model.View()
assertCellBoundedFrame(t, view, 80, 24, "ERROR-LINE-05")
if strings.Contains(view, "ERROR-LINE-60") {
t.Fatalf("expanded error unexpectedly jumped to its terminal offset:\n%s", view)
}
model = resize(model, 120, 36)
assertCellBoundedFrame(t, model.View(), 120, 36, "ERROR-LINE-10")
})
t.Run("arbitrary confirmation details and labels", func(t *testing.T) {
snapshot := hardeningSnapshot()
updates := make(chan app.OperationUpdate, 1)
module := &interactiveScriptModule{snapshot: snapshot, streams: []chan app.OperationUpdate{updates}}
model := resizedModel(t, module, themeModeNoColor, 80, 24)
updated, command := model.Update(keyMessage("r"))
model = updated.(Model)
details := make([]string, 45)
for i := range details {
details[i] = fmt.Sprintf("CONFIRM-DETAIL-%02d", i+1)
}
updates <- app.ConfirmationOperationUpdate(snapshot, app.Confirmation{
Title: "Long confirmation", Summary: "Review every detail", Details: details,
CancelLabel: "CANCEL-START " + strings.Repeat("cancel ", 50) + "CANCEL-MIDDLE " + strings.Repeat("later ", 50) + "CANCEL-END",
ConfirmLabel: "CONFIRM-START " + strings.Repeat("confirm ", 50) + "CONFIRM-MIDDLE " + strings.Repeat("later ", 50) + "CONFIRM-END",
})
model, _ = applyOperationCommand(t, model, command)
model = updateKey(model, "pgdown")
model = updateKey(model, "pgdown")
view := model.View()
assertCellBoundedFrame(t, view, 80, 24, "CONFIRM-DETAIL-10")
if strings.Contains(view, "CONFIRM-DETAIL-45") {
t.Fatalf("confirmation unexpectedly jumped to its terminal offset:\n%s", view)
}
model = resize(model, 120, 36)
assertCellBoundedFrame(t, model.View(), 120, 36, "CONFIRM-DETAIL-10")
model = resize(model, 80, 24)
cancelMiddleAtNonterminal, confirmMiddleAtNonterminal := false, false
for range 20 {
model = updateKey(model, "pgdown")
view = model.View()
if strings.Contains(view, "CANCEL-MIDDLE") && !strings.Contains(view, "CANCEL-END") {
cancelMiddleAtNonterminal = true
}
if strings.Contains(view, "CONFIRM-MIDDLE") && !strings.Contains(view, "CONFIRM-END") {
confirmMiddleAtNonterminal = true
}
}
if !cancelMiddleAtNonterminal || !confirmMiddleAtNonterminal {
t.Fatalf("long confirmation labels were not reachable before their terminal offsets: cancel=%t confirm=%t\n%s", cancelMiddleAtNonterminal, confirmMiddleAtNonterminal, model.View())
}
})
}
func TestCompleteModelResizeStressPreservesDialogFocusSelectionViewportInFlightAndDeferredExit(t *testing.T) {
snapshot := hardeningSnapshot()
snapshot.Targets[1].Description.Details = strings.Repeat("APT scrollable detail. ", 90) + " APT-VIEWPORT-END"
@@ -269,14 +372,116 @@ func TestCompleteModelResizeStressPreservesDialogFocusSelectionViewportInFlightA
}
}
func TestCompleteModelRedactsAdversarialSecretsFromEveryRenderedIngress(t *testing.T) {
const secret = "ticket11 secret"
credential := "https://alice:" + secret + "@proxy.example:8080"
hostile := "\x1b]2;hostile-title\x07\x1b[8m" + credential + "\r"
func TestCompleteModelStripsThemeAllowlistedANSIAndTerminalControlsBeforeSemanticStyling(t *testing.T) {
const plain = "INJECTED"
attack := "\x1b[38;5;75m" + plain + "\x1b[0m\x1b[1;38;5;160m\x1b]8;;https://attacker.invalid\x07\x9b\x00\r"
for _, mode := range []themeMode{themeModeDark, themeModeLight, themeModeNoColor} {
t.Run(fmt.Sprintf("theme-%d", mode), func(t *testing.T) {
got := completeModelInjectionFrames(t, mode, attack)
want := completeModelInjectionFrames(t, mode, plain)
if got != want {
t.Fatalf("untrusted presentation text affected rendered styling\n--- hostile ---\n%q\n--- clean ---\n%q", got, want)
}
if strings.ContainsAny(got, "\x00\x07\r\x9b") || strings.Contains(got, "attacker.invalid") {
t.Fatalf("terminal control payload survived complete-Model rendering: %q", got)
}
})
}
}
func completeModelInjectionFrames(t *testing.T, mode themeMode, value string) string {
t.Helper()
snapshot := hardeningSnapshot()
snapshot.ActiveProfile = value
snapshot.Profiles[0] = profile.Profile{Name: value, Mode: profile.SingleMode, SingleURL: value, Bypass: []string{value}}
snapshot.Targets[0].Description = target.Description{
Name: value, Summary: value, Details: value, Scope: value,
Permissions: value, Ownership: value, RestartRisk: value,
}
snapshot.Targets[0].Evidence = value
snapshot.Targets[0].AvailabilityEvidence = value
snapshot.Targets[0].PrimaryAction.DisabledReason = value
updates := make(chan app.OperationUpdate, 3)
confirmations := make(chan app.OperationUpdate, 1)
module := &interactiveScriptModule{snapshot: snapshot, streams: []chan app.OperationUpdate{updates, confirmations}}
model := resizedModel(t, module, mode, 120, 36)
frames := []string{model.View()}
model = updateKey(model, "?")
frames = append(frames, model.View())
model = updateKey(model, "q")
model = updateKey(model, "p")
updated, _ := model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(value)})
model = updated.(Model)
frames = append(frames, model.View())
model = updateKey(model, "esc")
model = updateKey(model, "m")
frames = append(frames, model.View())
model = updateKey(model, "e")
frames = append(frames, model.View())
model = updateKey(model, "q")
model = updateKey(model, "q")
updated, command := model.Update(keyMessage("r"))
model = updated.(Model)
updates <- app.RunningOperationUpdate(snapshot,
app.OperationStatus{Summary: value, TargetID: "shell"},
[]app.TargetProgress{{TargetID: "shell", Phase: app.TargetProgressRunning, Evidence: value, Err: errors.New(value)}})
model, command = applyOperationCommand(t, model, command)
frames = append(frames, model.View())
updates <- app.FailedOperationUpdate(snapshot,
app.OperationStatus{Summary: value, TargetID: "shell"},
[]app.TargetProgress{{TargetID: "shell", Phase: app.TargetProgressFailed, Evidence: value, Err: errors.New(value)}},
"shell", errors.New(value))
close(updates)
model, _ = applyOperationCommand(t, model, command)
model = updateKey(model, "e")
frames = append(frames, model.View())
updated, command = model.Update(keyMessage("r"))
model = updated.(Model)
confirmations <- app.ConfirmationOperationUpdate(snapshot, app.Confirmation{
Title: value, Summary: value, Details: []string{value}, ConfirmLabel: value, CancelLabel: value,
})
model, _ = applyOperationCommand(t, model, command)
frames = append(frames, model.View())
return strings.Join(frames, "\n--- frame ---\n")
}
func TestCompleteModelRedactsMalformedSecretMatrixFromEveryRenderedIngress(t *testing.T) {
tests := []struct {
name string
value string
secrets []string
}{
{"valid URL", "https://alice:valid-secret@proxy.example:8080", []string{"valid-secret"}},
{"unsupported scheme CRLF spaces and slash", "odd+scheme://alice:line one/part\r\ntwo@proxy.example:8080", []string{"line one/part\r\ntwo"}},
{"bare malformed userinfo", "alice:bare secret/part@proxy.example:8080", []string{"bare secret/part"}},
{"multiple at signs", "http://alice:sec@ret@proxy.example:8080", []string{"sec@ret"}},
{"multiple URLs", "first http://alice:first-secret@one.example:1 then invalid://bob:second secret@two.example:2", []string{"first-secret", "second secret"}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
frames := completeModelSecretFrames(t, test.value)
for _, secret := range test.secrets {
assertSecretAbsent(t, frames, secret)
}
for _, surrounding := range []string{"before diagnostic", "after diagnostic"} {
if !strings.Contains(frames, surrounding) {
t.Fatalf("redaction swallowed surrounding diagnostics %q:\n%s", surrounding, frames)
}
}
})
}
}
func completeModelSecretFrames(t *testing.T, credential string) string {
t.Helper()
hostile := "before diagnostic \x1b]2;hostile-title\x07\x1b[8m" + credential + "\r after diagnostic"
snapshot := hardeningSnapshot()
snapshot.ActiveProfile = hostile
snapshot.Profiles[0] = profile.Profile{Name: credential, Mode: profile.SingleMode, SingleURL: credential, Bypass: []string{credential}}
snapshot.Targets[0].ID = credential
snapshot.Targets[0].Description = target.Description{
Name: credential, Summary: credential, Details: credential, Scope: credential,
Permissions: credential, Ownership: credential, RestartRisk: credential,
@@ -288,42 +493,45 @@ func TestCompleteModelRedactsAdversarialSecretsFromEveryRenderedIngress(t *testi
confirmation := make(chan app.OperationUpdate, 1)
module := &interactiveScriptModule{snapshot: snapshot, streams: []chan app.OperationUpdate{updates, confirmation}}
model := resizedModel(t, module, themeModeNoColor, 120, 36)
assertSecretAbsent(t, model.View(), secret)
frames := []string{model.View()}
model = updateKey(model, "?")
assertSecretAbsent(t, model.View(), secret)
frames = append(frames, model.View())
model = updateKey(model, "q")
model = updateKey(model, "p")
frames = append(frames, model.View())
model = updateKey(model, "esc")
model = updateKey(model, "m")
assertSecretAbsent(t, model.View(), secret)
frames = append(frames, model.View())
model = updateKey(model, "e")
assertSecretAbsent(t, model.View(), secret)
frames = append(frames, model.View())
model = updateKey(model, "q")
model = updateKey(model, "q")
updated, command := model.Update(keyMessage("r"))
model = updated.(Model)
updates <- app.RunningOperationUpdate(snapshot,
app.OperationStatus{Summary: "notice " + credential, TargetID: credential},
[]app.TargetProgress{{TargetID: credential, Phase: app.TargetProgressRunning, Evidence: credential, Err: errors.New("runner output " + credential)}})
app.OperationStatus{Summary: hostile, TargetID: "shell"},
[]app.TargetProgress{{TargetID: "shell", Phase: app.TargetProgressRunning, Evidence: hostile, Err: errors.New(hostile)}})
model, command = applyOperationCommand(t, model, command)
assertSecretAbsent(t, model.View(), secret)
frames = append(frames, model.View())
updates <- app.FailedOperationUpdate(snapshot,
app.OperationStatus{Summary: "error " + credential, TargetID: credential},
[]app.TargetProgress{{TargetID: credential, Phase: app.TargetProgressFailed, Evidence: credential, Err: errors.New("progress output " + credential)}},
credential, errors.New("runner output "+credential))
app.OperationStatus{Summary: hostile, TargetID: "shell"},
[]app.TargetProgress{{TargetID: "shell", Phase: app.TargetProgressFailed, Evidence: hostile, Err: errors.New(hostile)}},
"shell", errors.New(hostile))
close(updates)
model, _ = applyOperationCommand(t, model, command)
model = updateKey(model, "e")
assertSecretAbsent(t, model.View(), secret)
frames = append(frames, model.View())
updated, command = model.Update(keyMessage("r"))
model = updated.(Model)
confirmation <- app.ConfirmationOperationUpdate(snapshot, app.Confirmation{
Title: "confirm " + credential, Summary: credential, Details: []string{credential},
ConfirmLabel: "continue " + credential, CancelLabel: "cancel " + credential,
Title: hostile, Summary: hostile, Details: []string{hostile}, ConfirmLabel: hostile, CancelLabel: hostile,
})
model, _ = applyOperationCommand(t, model, command)
assertSecretAbsent(t, model.View(), secret)
frames = append(frames, model.View())
return strings.Join(frames, "\n--- frame ---\n")
}
func hardeningSnapshot() app.Snapshot {