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:
parent
8b893ae1f1
commit
c5f50e817b
1 changed files with 236 additions and 11 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
"crypto/sha512"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"regexp"
|
||||
|
|
@ -18,6 +19,17 @@ import (
|
|||
"github.com/modelcontextprotocol/registry/internal/config"
|
||||
)
|
||||
|
||||
// ErrSignatureMismatch is returned by VerifySignature when the signature is structurally
|
||||
// valid but does not verify against the public key. Distinguishing this from structural
|
||||
// failures (wrong size, bad key format) lets the caller add fingerprint hints only when
|
||||
// the failure is actually a "wrong key" situation.
|
||||
var ErrSignatureMismatch = errors.New("signature does not match public key")
|
||||
|
||||
// MCPProofRecordPattern matches a well-formed MCPv1 DNS/HTTP proof record:
|
||||
// "v=MCPv1; k=<algo>; p=<base64-public-key>". Shared so callers checking for the
|
||||
// presence of a valid record see exactly what the parser will accept.
|
||||
var MCPProofRecordPattern = regexp.MustCompile(`v=MCPv1;\s*k=([^;]+);\s*p=([A-Za-z0-9+/=]+)`)
|
||||
|
||||
// CryptoAlgorithm represents the cryptographic algorithm used for a public key
|
||||
type CryptoAlgorithm string
|
||||
|
||||
|
|
@ -90,18 +102,72 @@ func DecodeAndValidateSignature(signedTimestamp string) ([]byte, error) {
|
|||
}
|
||||
|
||||
func VerifySignatureWithKeys(publicKeys []PublicKeyInfo, messageBytes []byte, signature []byte) error {
|
||||
var lastErr error
|
||||
allMismatch := true
|
||||
for _, publicKeyInfo := range publicKeys {
|
||||
err := publicKeyInfo.VerifySignature(messageBytes, signature)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(publicKeys) == 1 {
|
||||
return err
|
||||
lastErr = err
|
||||
if !errors.Is(err, ErrSignatureMismatch) {
|
||||
allMismatch = false
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("signature verification failed")
|
||||
// If at least one key failed for a structural reason (wrong size, unsupported algorithm),
|
||||
// surface that error directly — it's more actionable than a generic "didn't match" message.
|
||||
if !allMismatch {
|
||||
return lastErr
|
||||
}
|
||||
|
||||
// Every key was tried and produced a clean cryptographic mismatch. Include short
|
||||
// fingerprints of every key that was tried so users can tell which published keys the
|
||||
// registry actually saw — the most common cause of this error is a stale record left
|
||||
// behind after a key rotation, which is otherwise indistinguishable from a generic
|
||||
// crypto failure.
|
||||
fingerprints := make([]string, 0, len(publicKeys))
|
||||
for _, publicKeyInfo := range publicKeys {
|
||||
fingerprints = append(fingerprints, publicKeyInfo.Fingerprint())
|
||||
}
|
||||
if len(publicKeys) == 1 {
|
||||
return fmt.Errorf(
|
||||
"signature verification failed (tried published key %s); "+
|
||||
"if this is not the key you are signing with, the published record may be stale",
|
||||
fingerprints[0],
|
||||
)
|
||||
}
|
||||
return fmt.Errorf(
|
||||
"signature verification failed against all %d published keys (tried: %s); "+
|
||||
"if you recently rotated keys, remove any stale records from the apex domain",
|
||||
len(publicKeys), strings.Join(fingerprints, ", "),
|
||||
)
|
||||
}
|
||||
|
||||
// Fingerprint returns a short, human-readable identifier for the public key.
|
||||
// Format: "<algorithm>:<first 8 base64 chars of the raw key>". Public keys are not secret,
|
||||
// but truncating keeps error messages readable.
|
||||
func (pki *PublicKeyInfo) Fingerprint() string {
|
||||
const prefixLen = 8
|
||||
var raw []byte
|
||||
switch pki.Algorithm {
|
||||
case AlgorithmEd25519:
|
||||
if k, ok := pki.Key.(ed25519.PublicKey); ok {
|
||||
raw = k
|
||||
}
|
||||
case AlgorithmECDSAP384:
|
||||
if k, ok := pki.Key.(ecdsa.PublicKey); ok {
|
||||
raw = elliptic.MarshalCompressed(k.Curve, k.X, k.Y) //nolint:staticcheck // SA1019: matches the encoding used in DNS records
|
||||
}
|
||||
}
|
||||
if len(raw) == 0 {
|
||||
return fmt.Sprintf("%s:unknown", pki.Algorithm)
|
||||
}
|
||||
encoded := base64.StdEncoding.EncodeToString(raw)
|
||||
if len(encoded) > prefixLen {
|
||||
encoded = encoded[:prefixLen]
|
||||
}
|
||||
return fmt.Sprintf("%s:%s", pki.Algorithm, encoded)
|
||||
}
|
||||
|
||||
// VerifySignature verifies a signature using the appropriate algorithm
|
||||
|
|
@ -113,7 +179,7 @@ func (pki *PublicKeyInfo) VerifySignature(message, signature []byte) error {
|
|||
return fmt.Errorf("invalid signature size for Ed25519")
|
||||
}
|
||||
if !ed25519.Verify(ed25519Key, message, signature) {
|
||||
return fmt.Errorf("Ed25519 signature verification failed")
|
||||
return fmt.Errorf("Ed25519: %w", ErrSignatureMismatch)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -126,7 +192,7 @@ func (pki *PublicKeyInfo) VerifySignature(message, signature []byte) error {
|
|||
s := new(big.Int).SetBytes(signature[48:])
|
||||
digest := sha512.Sum384(message)
|
||||
if !ecdsa.Verify(&ecdsaKey, digest[:], r, s) {
|
||||
return fmt.Errorf("ECDSA P-384 signature verification failed")
|
||||
return fmt.Errorf("ECDSA P-384: %w", ErrSignatureMismatch)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -246,11 +312,8 @@ func ParseMCPKeysFromStrings(inputs []string) []struct {
|
|||
error
|
||||
}
|
||||
|
||||
// proof record pattern: v=MCPv1; k=<algo>; p=<base64-public-key>
|
||||
cryptoPattern := regexp.MustCompile(`v=MCPv1;\s*k=([^;]+);\s*p=([A-Za-z0-9+/=]+)`)
|
||||
|
||||
for _, record := range inputs {
|
||||
if matches := cryptoPattern.FindStringSubmatch(record); len(matches) == 3 {
|
||||
if matches := MCPProofRecordPattern.FindStringSubmatch(record); len(matches) == 3 {
|
||||
publicKey, err := ParsePublicKey(matches[1], matches[2])
|
||||
publicKeys = append(publicKeys, struct {
|
||||
*PublicKeyInfo
|
||||
|
|
|
|||
Loading…
Reference in a new issue