diff --git a/pkgs/mcpx/internal/cli/auth_describe_test.go b/pkgs/mcpx/internal/cli/auth_describe_test.go new file mode 100644 index 0000000000..351dbc87de --- /dev/null +++ b/pkgs/mcpx/internal/cli/auth_describe_test.go @@ -0,0 +1,64 @@ +package cli + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" +) + +// #1926: `mcpx config` surfaces Auth.Describe() per server — the variable +// reference, never the resolved secret — and doctor reports the same +// redacted posture. +const authTestConfig = `{"mcpServers":{ + "api": {"url": "https://example.test/mcp", "auth": {"type": "bearer", "token": "${MCPX_AUTH_TEST_TOKEN}"}}, + "local": {"command": "true"} +}}` + +func writeAuthConfig(t *testing.T) string { + t.Helper() + t.Setenv("MCPX_AUTH_TEST_TOKEN", "the-resolved-secret-value") + p := filepath.Join(t.TempDir(), "c.json") + if err := os.WriteFile(p, []byte(authTestConfig), 0o600); err != nil { + t.Fatal(err) + } + return p +} + +func TestConfigPrintsRedactedAuth(t *testing.T) { + cfg := writeAuthConfig(t) + a := &App{ConfigPath: cfg, JSON: true} + out, err := captureStdout(t, func() error { + return a.CmdConfig(context.Background(), nil) + }) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(out, `"auth": "bearer ${MCPX_AUTH_TEST_TOKEN}"`) { + t.Fatalf("config output lacks the redacted bearer reference:\n%s", out) + } + if !strings.Contains(out, `"auth": "none"`) { + t.Fatalf("config output lacks auth=none for the unauthenticated server:\n%s", out) + } + if strings.Contains(out, "the-resolved-secret-value") { + t.Fatalf("config output leaked the resolved secret:\n%s", out) + } +} + +func TestDoctorReportsAuthPosture(t *testing.T) { + cfg := writeAuthConfig(t) + a := &App{ConfigPath: cfg, JSON: true} + // Doctor's exit reflects every check (a daemon-less test box fails + // the connection check); the auth posture is what this test is for, + // so the error is deliberately not asserted. + out, _ := captureStdout(t, func() error { + return a.CmdDoctor(context.Background(), []string{"-v"}) + }) + if !strings.Contains(out, "api: bearer ${MCPX_AUTH_TEST_TOKEN}") { + t.Fatalf("doctor output lacks the auth posture:\n%s", out) + } + if strings.Contains(out, "the-resolved-secret-value") { + t.Fatalf("doctor output leaked the resolved secret:\n%s", out) + } +} diff --git a/pkgs/mcpx/internal/cli/doctor.go b/pkgs/mcpx/internal/cli/doctor.go index 5e8f1d9249..6b7691e947 100644 --- a/pkgs/mcpx/internal/cli/doctor.go +++ b/pkgs/mcpx/internal/cli/doctor.go @@ -137,6 +137,18 @@ func (a *App) CmdDoctor(ctx context.Context, args []string) error { add(check{"server commands", "ok", fmt.Sprintf("all %d runnable", len(servers)), ""}) } + + // Auth posture, from the same redacted summary `mcpx config` + // prints (#1926): which credential shape each server declares, + // by variable reference only — doctor output gets pasted into + // issues too, so the resolved value must never appear here. + if len(servers) > 0 { + var parts []string + for _, s := range servers { + parts = append(parts, fmt.Sprintf("%s: %s", s.Name, s.Auth.Describe())) + } + add(check{"auth", "ok", strings.Join(parts, "; "), ""}) + } } // Directories. diff --git a/pkgs/mcpx/internal/cli/root.go b/pkgs/mcpx/internal/cli/root.go index dc6451d7c5..fb93ebe0fe 100644 --- a/pkgs/mcpx/internal/cli/root.go +++ b/pkgs/mcpx/internal/cli/root.go @@ -558,6 +558,11 @@ func (a *App) CmdConfig(ctx context.Context, args []string) error { IdleTimeout string `json:"idleTimeout"` CallTimeout string `json:"callTimeout"` Protocol string `json:"protocol,omitempty"` + // Auth is mcpauth.Auth.Describe(): the redacted summary + // ("bearer ${VAR}", "none", …) — the reference, never the + // resolved value, since this output gets pasted into issues + // (#1926). + Auth string `json:"auth"` } out := struct { Path string `json:"path"` @@ -570,6 +575,7 @@ func (a *App) CmdConfig(ctx context.Context, args []string) error { Sharing: string(s.Sharing), Scope: string(s.Scope), Max: s.Max, IdleTimeout: s.IdleTimeout.String(), CallTimeout: s.CallTimeout.String(), Protocol: s.Protocol, + Auth: s.Auth.Describe(), } if s.Stdio() { r.Transport, r.Command = "stdio", s.Command diff --git a/pkgs/mcpx/internal/mcpauth/mcpauth.go b/pkgs/mcpx/internal/mcpauth/mcpauth.go index b55525cc54..66c4b06ddb 100644 --- a/pkgs/mcpx/internal/mcpauth/mcpauth.go +++ b/pkgs/mcpx/internal/mcpauth/mcpauth.go @@ -223,15 +223,69 @@ func (a *Auth) Describe() string { // A reference is safe and useful to print: it tells the reader which variable // to set. A literal is the secret itself, and the whole point of printing // anything is that somebody will paste it somewhere. +// +// Only a value made up ENTIRELY of variable references is shown. A mixed +// value such as ${TOKEN_PREFIX}.private-secret-suffix expands (os.Expand) +// to a string whose literal half is secret, so showing any part of the raw +// text would print that half; it is redacted whole. func redactRef(v string) string { v = strings.TrimSpace(v) - switch { - case v == "": + if v == "" { return "(unset)" - case strings.HasPrefix(v, "${") && strings.HasSuffix(v, "}"): - return v - case strings.HasPrefix(v, "$"): + } + if isPureRef(v) { return v } return "(literal, redacted)" } + +// isPureRef reports whether v consists solely of $VAR / ${VAR} references +// (the two forms os.Expand resolves), with no literal characters. +// +// The name scan mirrors os.Expand's own (getShellName), because the two +// must agree about where a reference ends: an unbraced name that starts +// with a shell special variable character -- a digit or one of * # $ @ +// ! ? - -- is exactly ONE character long there, so $1privateSecret +// expands $1 and leaves privateSecret as literal text. A scan that let +// the name run on would call the whole value a reference and print a +// secret suffix. +func isPureRef(v string) bool { + for i := 0; i < len(v); { + if v[i] != '$' { + return false + } + i++ + braced := i < len(v) && v[i] == '{' + if braced { + i++ + } + start := i + if !braced && i < len(v) && isShellSpecialVar(v[i]) { + i++ + } else { + for i < len(v) && (v[i] == '_' || '0' <= v[i] && v[i] <= '9' || 'a' <= v[i] && v[i] <= 'z' || 'A' <= v[i] && v[i] <= 'Z') { + i++ + } + } + if i == start { + return false + } + if braced { + if i >= len(v) || v[i] != '}' { + return false + } + i++ + } + } + return len(v) > 0 +} + +// isShellSpecialVar is os.Expand's set: the characters that, directly +// after $, form a complete one-character variable reference. +func isShellSpecialVar(c byte) bool { + switch c { + case '*', '#', '$', '@', '!', '?', '-': + return true + } + return '0' <= c && c <= '9' +} diff --git a/pkgs/mcpx/internal/mcpauth/mcpauth_test.go b/pkgs/mcpx/internal/mcpauth/mcpauth_test.go index 551c6d55a4..565994ff6f 100644 --- a/pkgs/mcpx/internal/mcpauth/mcpauth_test.go +++ b/pkgs/mcpx/internal/mcpauth/mcpauth_test.go @@ -115,6 +115,45 @@ func TestDescribeNeverRevealsALiteral(t *testing.T) { } } +func TestDescribeRedactsANumericSpecialReferenceWithLiteralSuffix(t *testing.T) { + // os.Expand reads $1 as a complete one-character reference and + // leaves the rest of the string literal, so the scanner must too: + // $1privateSecret is a reference PLUS a secret suffix, and showing + // it whole would print the suffix (Codex finding on PR #2593). + a := &mcpauth.Auth{Type: "bearer", Token: "$1privateSecret"} + if got := a.Describe(); strings.Contains(got, "privateSecret") { + t.Errorf("Describe() = %q, which leaks the literal suffix of $1privateSecret", got) + } + // A reference composed with a special-variable reference, and + // nothing else, is still a pure reference and is shown. + b := &mcpauth.Auth{Type: "bearer", Token: "$TOKEN$1"} + if got := b.Describe(); got != "bearer $TOKEN$1" { + t.Errorf("Describe() = %q, want the pure reference shown", got) + } +} + +func TestDescribeRedactsAMixedReferenceAndLiteral(t *testing.T) { + // Codex finding on PR #2593: a credential that MIXES a reference with a + // literal suffix used to be printed whole, because redactRef trusted + // anything starting with $. The literal half is secret. + got := (&mcpauth.Auth{Type: "bearer", Token: "${TOKEN_PREFIX}.private-secret-suffix"}).Describe() + if strings.Contains(got, "private-secret-suffix") || strings.Contains(got, "TOKEN_PREFIX") { + t.Errorf("a mixed reference/literal must be redacted whole: %q", got) + } + got = (&mcpauth.Auth{Type: "bearer", Token: "$TOKEN-suffix"}).Describe() + if strings.Contains(got, "suffix") || strings.Contains(got, "TOKEN") { + t.Errorf("a $-reference with a literal tail must be redacted whole: %q", got) + } + // Pure references, singly or concatenated, are still shown: they name + // variables, not values. + for _, ref := range []string{"$TOKEN", "${TOKEN}", "${A}${B}", "$A$B"} { + got = (&mcpauth.Auth{Type: "bearer", Token: ref}).Describe() + if !strings.Contains(got, ref) { + t.Errorf("a pure reference should be shown: %q -> %q", ref, got) + } + } +} + func TestNilAuthIsNone(t *testing.T) { var a *mcpauth.Auth got, err := a.Resolve()