Skip to content

Commit dcb5a6f

Browse files
authored
Add 5s timeout to DNS auth lookup to prevent resource exhaustion (#825)
The DNS auth endpoint was vulnerable to resource exhaustion if an attacker controlled a domain with a slow/non-responding authoritative DNS server. Without a timeout, handler goroutines would block on DNS resolution indefinitely, allowing an attacker to pile up goroutines until OOM. Production is protected by NGINX ingress's default 60s proxy timeout, but this adds defense-in-depth with a tighter 5s bound.
1 parent d840378 commit dcb5a6f

File tree

1 file changed

+6
-1
lines changed
  • internal/api/handlers/v0/auth

1 file changed

+6
-1
lines changed

internal/api/handlers/v0/auth/dns.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net"
77
"net/http"
88
"strings"
9+
"time"
910

1011
"github.com/danielgtaylor/huma/v2"
1112
v0 "github.com/modelcontextprotocol/registry/internal/api/handlers/v0"
@@ -77,11 +78,15 @@ func RegisterDNSEndpoint(api huma.API, pathPrefix string, cfg *config.Config) {
7778
// ExchangeToken exchanges DNS signature for a Registry JWT token
7879
func (h *DNSAuthHandler) ExchangeToken(ctx context.Context, domain, timestamp, signedTimestamp string) (*auth.TokenResponse, error) {
7980
keyFetcher := func(ctx context.Context, domain string) ([]string, error) {
81+
// Apply a timeout to DNS lookup to prevent resource exhaustion from slow/malicious DNS servers
82+
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
83+
defer cancel()
84+
8085
// Lookup DNS TXT records
8186
// DNS implies a hierarchy where subdomains are treated as part of the parent domain,
8287
// therefore we grant permissions for all subdomains (e.g., com.example.*)
8388
// This is in line with other DNS-based authentication methods e.g. ACME DNS-01 challenges
84-
txtRecords, err := h.resolver.LookupTXT(ctx, domain)
89+
txtRecords, err := h.resolver.LookupTXT(timeoutCtx, domain)
8590
if err != nil {
8691
return nil, fmt.Errorf("failed to lookup DNS TXT records: %w", err)
8792
}

0 commit comments

Comments
 (0)