From 8f9b39c62ec4529a130cd47dd42d5e9c15b3c05b Mon Sep 17 00:00:00 2001 From: Jiahao Lu Date: Tue, 21 Mar 2023 19:36:49 +0800 Subject: [PATCH] Fix: potential panic in putMsgToCache (#2634) When the upstream DNS server returns a message that contains no questions (i.e. QDCOUNT == 0), `putMsgToCache` will trigger an out-of-range panic. Issue: #2524 Comment: https://github.com/Dreamacro/clash/issues/2524#issuecomment-1477477601 --- dns/resolver.go | 2 +- dns/util.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dns/resolver.go b/dns/resolver.go index 96da175..fa20764 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -162,7 +162,7 @@ func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.M msg := result.(*D.Msg) - putMsgToCache(r.lruCache, q.String(), msg) + putMsgToCache(r.lruCache, q.String(), q, msg) }() isIPReq := isIPRequest(q) diff --git a/dns/util.go b/dns/util.go index 05c734e..90d2ff9 100644 --- a/dns/util.go +++ b/dns/util.go @@ -16,9 +16,9 @@ import ( D "github.com/miekg/dns" ) -func putMsgToCache(c *cache.LruCache, key string, msg *D.Msg) { +func putMsgToCache(c *cache.LruCache, key string, q D.Question, msg *D.Msg) { // skip dns cache for acme challenge - if q := msg.Question[0]; q.Qtype == D.TypeTXT && strings.HasPrefix(q.Name, "_acme-challenge") { + if q.Qtype == D.TypeTXT && strings.HasPrefix(q.Name, "_acme-challenge.") { log.Debugln("[DNS] dns cache ignored because of acme challenge for: %s", q.Name) return }