Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion internal/cli/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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 "<redacted>"
}
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)
Expand Down
20 changes: 20 additions & 0 deletions internal/cli/doctor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<redacted>"},
{name: "over-long is withheld", value: strings.Repeat("a", 129), want: "<redacted>"},
} {
if got := sanitizeRouteIdentity(tc.value); got != tc.want {
t.Fatalf("%s: sanitizeRouteIdentity(%q) = %q, want %q", tc.name, tc.value, got, tc.want)
}
}
}
24 changes: 24 additions & 0 deletions internal/cli/qa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading