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
5 changes: 5 additions & 0 deletions docs/api/rest-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ preserved) and the [`config-to-secret`](#post-apiv1serversnameconfig-to-secret)
endpoint reads the real value server-side. Flip the flag only if you
need to inspect a raw value through the API for debugging.

On the MCP channel the flag also requires an **authenticated** caller
(issue #1148): an unauthenticated `/mcp` client is admin only for backward
compatibility, and gets the masked values regardless of the flag. The REST
API always requires an API key, so it is unaffected.

The MCP `upstream_servers` tool was the original motivator for redaction
(see [PR #425](https://github.com/smart-mcp-proxy/mcpproxy-go/pull/425)) —
a prompt-injected agent could otherwise read another upstream's PAT via
Expand Down
57 changes: 57 additions & 0 deletions docs/configuration/upstream-servers.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,63 @@ To disable redaction (for debugging only), set `reveal_secret_headers: true`
in `mcp_config.json`. **It's not normally needed**: the editing flow
described below works without ever exposing the plaintext to the client.

Since v0.64 (issue #1148) the flag additionally requires an **authenticated**
caller. `/mcp` is deliberately unprotected by default
(`require_mcp_auth: false`), so an unauthenticated MCP client is treated as
admin for backward compatibility — but that is not an identity, and it no
longer satisfies a check that hands back raw credentials. With the flag on,
raw values are returned to a caller presenting the API key or an admin agent
token, to a tray/socket connection (authenticated by OS-level socket
permissions), and over stdio; an unauthenticated TCP `/mcp` caller gets the
masked values. Nothing else changes: every other operation an unauthenticated
MCP client can perform today still works.

Argument vectors are masked too: a credential passed as `--api-key sk-…` in
`args` is masked by flag name, by value shape and — since the review round on
the same issue — by URL shape in every spelling (`--flag <url>`, `--flag=<url>`
and a bare positional token all mask `?token=…` and `https://user:pass@host`).

For the **keyed** fields, a masked value echoed back on the write path is
reverted to the stored value rather than persisted over it, and every revert is
**bound to where the value was read from**:

| Field | Bound by |
|-------|----------|
| `env_json` / `headers_json` | the map key |
| `url` | the stored scheme, host:port and username |
| `oauth_json` → `client_secret` | the field |
| `args_json` | *nothing — an argv mask is **refused**, never reverted* |

**`args_json` is different: an echoed mask is rejected with an error, not
restored.** An argv token has no key to bind a secret to — only its index and
its neighbours — and `args_json` replaces the whole vector while the same patch
also chooses `command`. Every candidate binding is therefore caller-controlled:
an index can be picked, a preceding flag copied verbatim, and a byte-identical
argv means nothing once `command` moves from `mcp-foo` to `curl`. Any revert
rule would let a caller relocate a stored credential into a command line of its
own choosing.

So a write whose `args_json` still carries a mask this proxy rendered fails
with:

```
args_json[2] is a redaction placeholder, not an argument value: credential-shaped
argv tokens are masked on read and are never restored on write, because an argv
slot carries no key to bind the secret to. Resend the real value for that
argument, or omit args_json to leave the stored arguments unchanged
```

Do not build a read-modify-write loop that feeds a masked `args` list back in:
**resend the real values**, or omit `args_json` entirely when you are not
changing the arguments (omitting it leaves the stored vector untouched).
Rejecting is deliberate rather than silently keeping the stored vector —
`args_json` replaces the vector, so ignoring it would make the write look
applied when it was not.

The argv mask is a rendering of the **MCP** read surface. The REST API returns
`args` unredacted and accepts it unchanged, so REST reads and writes remain
self-consistent; only the MCP write path refuses an MCP-rendered mask.

### How you edit them

#### Web UI / macOS tray
Expand Down
45 changes: 45 additions & 0 deletions internal/auth/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ type AuthContext struct {
DisplayName string // User's display name
Role string // "admin" or "user" (empty for API key / agent token auth)
Provider string // OAuth provider used (e.g., "google", "github")

// Anonymous marks a context that was NOT authenticated and only carries
// admin type for backward compatibility (issue #1148).
//
// /mcp is deliberately unprotected by default (require_mcp_auth=false) so
// MCP clients that cannot send an API key keep working, and the middleware
// therefore hands an unauthenticated request an admin context. That is
// fine for the operations those clients need — and it is NOT an identity,
// so it must not satisfy a check that hands back raw credentials. See
// CanRevealSecrets.
Anonymous bool
}

// contextKey is an unexported type used as context key to avoid collisions.
Expand Down Expand Up @@ -106,6 +117,40 @@ func AdminContext() *AuthContext {
}
}

// AnonymousContext returns the back-compat admin context for an
// UNAUTHENTICATED MCP request (issue #1148).
//
// Type stays AuthTypeAdmin on purpose: every existing IsAdmin()-based
// allowance — ordinary tool calls, retrieve_tools, server add/update/patch,
// quarantine approvals — behaves exactly as it did, so no MCP client that
// works today stops working. The only thing the Anonymous bit changes is
// CanRevealSecrets.
func AnonymousContext() *AuthContext {
return &AuthContext{
Type: AuthTypeAdmin,
Anonymous: true,
}
}

// CanRevealSecrets reports whether this caller may be handed RAW credential
// values (issue #1148) — today, `upstream_servers list` under the opt-in
// `reveal_secret_headers` flag.
//
// It is deliberately stricter than IsAdmin and deliberately nil-safe:
//
// - an unauthenticated /mcp caller is admin for back-compat but has proved no
// identity, so it gets masked values like everyone else;
// - a nil context (the stdio transport before it installs one, an in-process
// caller, a test) is unprivileged rather than admin-by-absence — the
// opposite of the `authCtx != nil && !authCtx.IsAdmin()` shape, which lets
// the ABSENCE of a token pass a gate that a scoped token fails.
//
// Authenticated admins — the API key, a tray/socket connection, stdio, an
// OAuth admin user in the server edition — are unaffected.
func (ac *AuthContext) CanRevealSecrets() bool {
return ac != nil && ac.IsAdmin() && !ac.Anonymous
}

// UserContext returns an AuthContext for a regular OAuth-authenticated user.
func UserContext(userID, email, displayName, provider string) *AuthContext {
return &AuthContext{
Expand Down
34 changes: 34 additions & 0 deletions internal/auth/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,37 @@ func TestUserContext_NoImplicitAccess(t *testing.T) {
assert.False(t, ac.CanAccessServer("github"))
assert.False(t, ac.HasPermission(PermRead))
}

// TestAnonymousContext_CanRevealSecrets pins the distinction issue #1148 turns
// on: an unauthenticated /mcp caller is still treated as admin for every
// operation that worked before (that back-compat is deliberate), but it is NOT
// an identity, so it cannot ask for raw secrets. A nil context — the stdio
// transport before it installs one, an in-process caller, a test — must fail
// the same way rather than inheriting admin by absence.
func TestAnonymousContext_CanRevealSecrets(t *testing.T) {
anon := AnonymousContext()
if !anon.IsAdmin() {
t.Fatal("AnonymousContext must stay admin so existing unauthenticated MCP operations keep working")
}
if anon.CanRevealSecrets() {
t.Error("anonymous caller must not be allowed to reveal secrets")
}

if !AdminContext().CanRevealSecrets() {
t.Error("an authenticated admin must still be allowed to reveal secrets")
}

var nilCtx *AuthContext
if nilCtx.CanRevealSecrets() {
t.Error("a nil auth context must not be treated as a secret-revealing admin")
}

agent := &AuthContext{Type: AuthTypeAgent, AgentName: "bot", AllowedServers: []string{"*"}}
if agent.CanRevealSecrets() {
t.Error("agent tokens must not be allowed to reveal secrets")
}

if !AdminUserContext("u1", "a@b.c", "A", "google").CanRevealSecrets() {
t.Error("an OAuth-authenticated admin user must be allowed to reveal secrets")
}
}
50 changes: 48 additions & 2 deletions internal/oauth/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,52 @@ var tokenPattern = regexp.MustCompile(`(?i)(bearer\s+)[a-zA-Z0-9\-_\.]+`)
// secretPattern matches common secret patterns.
var secretPattern = regexp.MustCompile(`(?i)(secret|password|token|key)["']?\s*[:=]\s*["']?[a-zA-Z0-9\-_\.]+`)

// urlUserinfoPattern matches the `scheme://user:password@` prefix of a URL
// embedded in free-form text (issue #1148, review round 2).
//
// RedactURLQueryParams has masked the userinfo password since #872, but that
// function only ever sees a value the caller already knows is a URL. The
// free-form scrubbers — connection errors, tailed log lines, health.detail —
// see a URL buried in a sentence, and neither tokenPattern (bearer only) nor
// secretPattern (`<name>=<value>`) nor the `<param>=` sweep below has any
// `user:pass@` shape, so a basic-auth password travelled through all of them in
// the clear.
//
// The username is deliberately kept: it is the operator's signal for WHICH
// credential failed, and it is not the secret.
var urlUserinfoPattern = regexp.MustCompile(`([a-zA-Z][a-zA-Z0-9+.\-]*://)([^\s/:@]+):([^\s/@]+)@`)

// redactURLUserinfo masks the password half of every `scheme://user:pass@`
// prefix in s, keeping the username. Shared by RedactSensitiveData (free-form
// text) and RedactURL (the parse-failure fallback) so the two cannot disagree
// about whether a basic-auth password is a secret.
func redactURLUserinfo(s string) string {
return urlUserinfoPattern.ReplaceAllStringFunc(s, func(match string) string {
groups := urlUserinfoPattern.FindStringSubmatch(match)
if len(groups) != 4 {
return match
}
// A ${keyring:…}/${env:…} reference is a label, not a secret; masking
// it would erase exactly what makes the line diagnosable.
if isConfigReference(groups[3]) {
return match
}
return groups[1] + groups[2] + ":***REDACTED***@"
})
}

// RedactSensitiveData redacts sensitive information from a string.
// It replaces tokens, secrets, and other sensitive data with redacted placeholders.
func RedactSensitiveData(data string) string {
if data == "" {
return data
}

// Redact URL userinfo passwords (scheme://user:pass@host).
result := redactURLUserinfo(data)

// Redact Bearer tokens
result := tokenPattern.ReplaceAllString(data, "${1}***REDACTED***")
result = tokenPattern.ReplaceAllString(result, "${1}***REDACTED***")

// Redact secrets and passwords
result = secretPattern.ReplaceAllStringFunc(result, func(match string) string {
Expand Down Expand Up @@ -707,12 +744,21 @@ func unmaskMapValues(incoming, stored map[string]string, rendered func(k, v stri
}

// RedactURL redacts sensitive query parameters from a URL string.
//
// Issue #1148, round 4: this is not only a standalone helper — it is the
// fallback RedactURLQueryParams takes when url.Parse FAILS, and a URL that
// fails to parse is precisely the URL a connection error is being logged
// about. It masked the query params but had no `user:pass@` rule, so the
// basic-auth password survived every logSafeURL() site in
// internal/upstream/core, internal/upstream/managed and internal/transport
// whenever the configured URL was malformed. Reuse RedactSensitiveData's
// urlUserinfoPattern so the two scrubbers cannot drift apart.
func RedactURL(urlStr string) string {
if urlStr == "" {
return urlStr
}

result := urlStr
result := redactURLUserinfo(urlStr)
for _, param := range sensitiveParams {
pattern := regexp.MustCompile(`(?i)(` + param + `=)[^&]+`)
result = pattern.ReplaceAllString(result, "${1}***REDACTED***")
Expand Down
36 changes: 36 additions & 0 deletions internal/oauth/redact_hardening_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,39 @@ func TestUnmaskEnvValues_URLValue_HostEditDoesNotRestorePassword(t *testing.T) {
assert.NotContains(t, got["DATABASE_URL"], "realdbpassword",
"password must not be moved to a different host")
}

// TestRedactURL_MasksUserinfoPassword covers issue #1148, round 4 (final
// sweep). RedactURL is the regex fallback RedactURLQueryParams takes whenever
// url.Parse fails — and a URL that fails to parse is exactly the URL a
// connection error is being logged about. It masked the sensitive QUERY params
// but had no `user:pass@` rule at all, so every logSafeURL() call site in
// internal/upstream/core, internal/upstream/managed and internal/transport
// leaked the basic-auth password down that fallback path.
//
// RedactSensitiveData has masked userinfo since round 2 (urlUserinfoPattern);
// this brings its sibling to the same contract. The username is kept, as
// everywhere else: it says WHICH credential, and is not itself the secret.
func TestRedactURL_MasksUserinfoPassword(t *testing.T) {
// The DEL byte is what makes url.Parse fail, which is how a real caller
// reaches this function through RedactURLQueryParams.
raw := "https://alice:hunter2phrase@host/mcp?token=urlsecret999&debug=1\x7f"

for name, got := range map[string]string{
"RedactURL": RedactURL(raw),
"RedactURLQueryParams": RedactURLQueryParams(raw),
} {
assert.NotContains(t, got, "hunter2phrase", "%s must mask the userinfo password", name)
assert.NotContains(t, got, "urlsecret999", "%s must mask the query credential", name)
assert.Contains(t, got, "alice", "%s must keep the username", name)
assert.Contains(t, got, "host/mcp", "%s must keep host and path", name)
assert.Contains(t, got, "debug=1", "%s must keep non-sensitive parameters", name)
}

// A ${keyring:…}/${env:…} reference is a label, not a secret — same carve-out
// RedactSensitiveData makes.
ref := RedactURL("https://alice:${keyring:UPSTREAM_PW}@host/mcp\x7f")
assert.Contains(t, ref, "${keyring:UPSTREAM_PW}", "config references are labels, not secrets")

assert.Equal(t, "", RedactURL(""))
assert.Equal(t, "https://host/mcp", RedactURL("https://host/mcp"), "a clean URL is untouched")
}
Loading
Loading