diff --git a/internal/cli/doctor.go b/internal/cli/doctor.go index 0f02f62..524a2f5 100644 --- a/internal/cli/doctor.go +++ b/internal/cli/doctor.go @@ -405,7 +405,7 @@ func runQA(ctx context.Context, args []string, stdout, stderr io.Writer) int { _, _ = fmt.Fprintf(stderr, "%s\n", err.Error()) } if unavailable := (*qapi.BackendUnavailableError)(nil); errors.As(err, &unavailable) { - _, _ = fmt.Fprintf(stderr, "route_provider=%s route_model=%s\n", unavailable.Provider, unavailable.Model) + _, _ = fmt.Fprintf(stderr, "route_provider=%s route_model=%s\n", sanitizeRouteIdentity(unavailable.Provider), sanitizeRouteIdentity(unavailable.Model)) } if code == qaadmission.CodeModelUnavailable { _, _ = io.WriteString(stderr, qaModelUnavailableNote()) @@ -455,6 +455,30 @@ func sanitizeDiagnosticField(field string) string { return b.String() } +// sanitizeRouteIdentity renders one routed provider or model token for a +// terminal diagnostic. Unlike a field label these are values an operator must +// be able to compare with the runtime's model table, so they are escaped +// rather than flattened: printable ASCII passes through, and every other byte +// becomes \xNN. A profile is a caller-owned file, so this is what keeps an +// escape sequence in a provider or model name out of the operator's terminal +// (#374 S5). The length bound is the same one qaroute applies. +func sanitizeRouteIdentity(value string) string { + if value == "" || len(value) > 128 { + return "" + } + var b strings.Builder + b.Grow(len(value)) + for index := 0; index < len(value); index++ { + character := value[index] + if character >= 0x20 && character < 0x7f && character != '\\' { + b.WriteByte(character) + continue + } + _, _ = fmt.Fprintf(&b, "\\x%02x", character) + } + return b.String() +} + func qaFailure(stderr io.Writer, code string) int { writeError(stderr, code) _, _ = io.WriteString(stderr, qaReportNote) diff --git a/internal/cli/doctor_test.go b/internal/cli/doctor_test.go index 0288c27..1cb2038 100644 --- a/internal/cli/doctor_test.go +++ b/internal/cli/doctor_test.go @@ -637,3 +637,23 @@ func TestRunRejectsInvalidArguments(t *testing.T) { }) } } + +// TestSanitizeRouteIdentityBoundsAndEscapes pins the bound and the escaping the +// routed-pair diagnostic relies on: a provider or model comes from a caller-owned +// profile, so every byte outside printable ASCII is escaped and an over-long +// value is withheld instead of printed. +func TestSanitizeRouteIdentityBoundsAndEscapes(t *testing.T) { + for _, tc := range []struct{ name, value, want string }{ + {name: "plain ids pass through", value: "openai-codex/gpt-5.6-terra", want: "openai-codex/gpt-5.6-terra"}, + {name: "escape sequence is escaped", value: "evil\x1b[2Jmodel", want: `evil\x1b[2Jmodel`}, + {name: "newline cannot break the line", value: "a\nb", want: `a\x0ab`}, + {name: "backslash is escaped so it cannot forge an escape", value: `a\x1bb`, want: `a\x5cx1bb`}, + {name: "non-ascii bytes are escaped", value: "mödel", want: `m\xc3\xb6del`}, + {name: "empty is withheld", value: "", want: ""}, + {name: "over-long is withheld", value: strings.Repeat("a", 129), want: ""}, + } { + if got := sanitizeRouteIdentity(tc.value); got != tc.want { + t.Fatalf("%s: sanitizeRouteIdentity(%q) = %q, want %q", tc.name, tc.value, got, tc.want) + } + } +} diff --git a/internal/cli/qa_test.go b/internal/cli/qa_test.go index 10debd6..f3e690c 100644 --- a/internal/cli/qa_test.go +++ b/internal/cli/qa_test.go @@ -728,6 +728,30 @@ func TestQARunNamesThePreflightRefusalStage(t *testing.T) { }) } +// TestQARunModelProbeEscapesTheRoutedPair pins the safety half of S4/S5: the +// routed provider/model come from a caller-owned profile file, so they are +// bounded and escaped before they reach the operator's terminal. The profile is +// staged in the disposable catalog copy, never in the repository catalog. +func TestQARunModelProbeEscapesTheRoutedPair(t *testing.T) { + args := reportFixture(t) + catalogRoot := filepath.Join(must(os.Getwd()), "catalog-copy") + mustOK(os.CopyFS(catalogRoot, os.DirFS(qaCatalogArgument(args)))) + for index, arg := range args { + if arg == "--catalog" && index+1 < len(args) { + args[index+1] = catalogRoot + } + } + qaProfileBodyFixture(t, catalogRoot, `{"schemaVersion":1,"defaultProfile":"fleet-default","profiles":{"fleet-default":{"routes":{"requirements-analyst":{"pi":{"provider":"nan","model":"evil\u001b[2Jmodel","effort":"low"}}}}}}`) + mustOK(os.WriteFile("qa-report-scenario", []byte("model-unavailable"), 0o600)) + stderr, code := runQARefusal(t, args) + if code != exitFailure || strings.Contains(stderr, "\x1b") { + t.Fatalf("hostile routed pair = %d stderr %q, want escaped output", code, stderr) + } + if !strings.Contains(stderr, `route_model=evil\x1b[2Jmodel`) { + t.Fatalf("stderr %q does not escape the routed model", stderr) + } +} + // TestQAReportNoteClaimsNoRoute pins #374 S5: the prerequisite note printed // with every non-catalog refusal no longer claims the placeholder "nan" route, // because a refusal often happens with a profile route already resolved (or