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
This commit is contained in:
Jiahao Lu 2023-03-21 19:36:49 +08:00 committed by GitHub
parent d808576f98
commit 8f9b39c62e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 3 deletions

View File

@ -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)

View File

@ -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
}