fix(auth): better DNS verification errors and wrong-selector hint (#1202)

## Summary

DNS auth users have repeatedly hit two failure modes that both produce
the same opaque "Ed25519 signature verification failed" error, making
them indistinguishable to debug. This PR makes both diagnosable
end-to-end without changing the auth protocol or any successful path.

The two recurring failure modes:

1. **Wrong placement** — TXT record put under a selector
(`_mcp-auth.<domain>` or `_mcp-registry.<domain>`) instead of the apex.
DKIM-style intuition, but MCP DNS auth uses SPF-style apex placement.
See #385 (PR #388 fixed the docs but the failure recurred), #1103,
#1126.
2. **Stale apex record** — old TXT record left behind after a key
rotation, silently tried first and rejected.

## Changes

**`internal/api/handlers/v0/auth/dns.go`** — when the apex has no
`v=MCPv1` record, probe `_mcp-auth.<domain>` and
`_mcp-registry.<domain>` in parallel (2s individual timeout). If a
record is found there, return a targeted error pointing the user at the
apex. Costs zero extra DNS work in the success path.

**`internal/api/handlers/v0/auth/common.go`** — when verification fails
against records that *are* present, include short fingerprints of every
key the registry tried (`ed25519:YJLsHFhu`-style 8-char prefix). A new
`ErrSignatureMismatch` sentinel keeps structural errors (wrong signature
size, unsupported algorithm) passing through unchanged via `errors.Is`,
so existing assertions on those messages still hold.

**`docs/modelcontextprotocol-io/authentication.mdx`** — anti-pattern
`<Warning>` callout in the DNS section explicitly warning against
selector placement and stale records.

**Tests** — two new tests:
- `TestDNSAuthHandler_WrongSelectorProbe` — verifies the probe surfaces
records found at `_mcp-auth.` / `_mcp-registry.` and falls through to
the standard error when nothing is found anywhere
- `TestDNSAuthHandler_StaleKeyFingerprintInError` — verifies the
fingerprint hint appears in the failure message for #1126's exact
scenario

One existing test assertion was updated from a generic `"signature
verification failed"` substring to the more specific `"invalid signature
size for ECDSA P-384"` it should always have been asserting; the new
sentinel-based logic surfaces structural errors verbatim.

## Test plan

- [x] `make lint` — clean
- [x] `make test-unit` — all auth tests pass (existing + 2 new)
- [x] Verified existing single-key and multi-key paths preserve their
original (more specific) error messages where they were already specific
- [ ] Reviewer to sanity-check the docs callout renders well in the
Mintlify theme

## Out of scope

The publisher-side preflight DNS check (cross-link in #845) is
intentionally **not** in this PR — that belongs in the broader CLI UX
work tracked by #845.

Refs #845, #385, #1103
Fixes #1126

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Radoslav Dimitrov 2026-04-25 11:22:06 -04:00 • committed by GitHub
commit c5f50e817b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -546,7 +546,7 @@ func TestDNSAuthHandler_ExchangeToken_ECDSAP384(t *testing.T) {
timestamp: time.Now().UTC().Format(time.RFC3339),
signedTimestamp: "abcdef1234", // too short for ECDSA P-384
expectError: true,
errorContains: "signature verification failed", // general error when trying all keys
errorContains: "invalid signature size for ECDSA P-384",
},
{
name: "wrong ECDSA P-384 key for signature",
@ -990,3 +990,100 @@ func TestDNSAuthHandler_Mixed_Algorithm_Support(t *testing.T) {
assert.NotNil(t, result)
})
}
// TestDNSAuthHandler_WrongSelectorProbe covers the case where the user mistakenly placed
// the MCPv1 TXT record under a selector (e.g. _mcp-auth.<domain>) instead of the apex,
// which has been a recurring source of confusion (#385, #1103, #1126).
func TestDNSAuthHandler_WrongSelectorProbe(t *testing.T) {
cfg := &config.Config{
JWTPrivateKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
}
publicKey, _, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
publicKeyB64 := base64.StdEncoding.EncodeToString(publicKey)
mcpRecord := fmt.Sprintf("v=MCPv1; k=ed25519; p=%s", publicKeyB64)
tests := []struct {
name string
txtRecords map[string][]string
expectInError string
}{
{
name: "record placed at _mcp-auth selector",
txtRecords: map[string][]string{
"_mcp-auth." + testDomain: {mcpRecord},
},
expectInError: "_mcp-auth." + testDomain,
},
{
name: "record placed at _mcp-registry selector",
txtRecords: map[string][]string{
"_mcp-registry." + testDomain: {mcpRecord},
},
expectInError: "_mcp-registry." + testDomain,
},
{
name: "no record anywhere falls through to standard error",
txtRecords: map[string][]string{
testDomain: {"v=spf1 ~all"},
},
expectInError: "no MCP public key found in DNS TXT records",
},
{
name: "malformed MCPv1 string at apex still triggers selector probe",
txtRecords: map[string][]string{
// Looks like an MCPv1 record but missing the public key field.
testDomain: {"v=MCPv1; k=ed25519"},
"_mcp-auth." + testDomain: {mcpRecord},
},
expectInError: "_mcp-auth." + testDomain,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := auth.NewDNSAuthHandler(cfg)
handler.SetResolver(&MockDNSResolver{txtRecords: tt.txtRecords})
timestamp := time.Now().UTC().Format(time.RFC3339)
_, err := handler.ExchangeToken(context.Background(), testDomain, timestamp, hex.EncodeToString(make([]byte, 64)))
require.Error(t, err)
assert.Contains(t, err.Error(), tt.expectInError)
})
}
}
// TestDNSAuthHandler_StaleKeyFingerprintInError covers #1126: when only one apex record is
// published and it doesn't match the key being signed with (the typical "rotated and forgot
// to update DNS" failure), the error message should include a fingerprint of the published
// key so the user can tell their CLI is signing with a different key than what's published.
func TestDNSAuthHandler_StaleKeyFingerprintInError(t *testing.T) {
cfg := &config.Config{
JWTPrivateKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
}
stalePublicKey, _, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
stalePublicKeyB64 := base64.StdEncoding.EncodeToString(stalePublicKey)
_, currentPrivateKey, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
handler := auth.NewDNSAuthHandler(cfg)
handler.SetResolver(&MockDNSResolver{
txtRecords: map[string][]string{
testDomain: {fmt.Sprintf("v=MCPv1; k=ed25519; p=%s", stalePublicKeyB64)},
},
})
timestamp := time.Now().UTC().Format(time.RFC3339)
signature := ed25519.Sign(currentPrivateKey, []byte(timestamp))
_, err = handler.ExchangeToken(context.Background(), testDomain, timestamp, hex.EncodeToString(signature))
require.Error(t, err)
expectedFingerprint := "ed25519:" + stalePublicKeyB64[:8]
assert.Contains(t, err.Error(), expectedFingerprint, "error should expose the published key's fingerprint")
assert.Contains(t, err.Error(), "stale", "error should hint at the stale-record cause")
}