Skip to content

Commit 313d2b8

Browse files
author
Arko Dasgupta
committed
Make DNS records and queries case-insensitive
RFC434 states that DNS Servers should be case insensitive This commit makes sure that all DNS queries will be translated to lower ASCII characters and all svcRecords will be saved in lower case to abide by the RFC Relates to moby#21169 Signed-off-by: Arko Dasgupta <arko.dasgupta@docker.com>
1 parent a926e65 commit 313d2b8

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

libnetwork/network.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,14 +1381,18 @@ func delIPToName(ipMap setmatrix.SetMatrix, name, serviceID string, ip net.IP) {
13811381
}
13821382

13831383
func addNameToIP(svcMap setmatrix.SetMatrix, name, serviceID string, epIP net.IP) {
1384-
svcMap.Insert(name, svcMapEntry{
1384+
// Since DNS name resolution is case-insensitive, Use the lower-case form
1385+
// of the name as the key into svcMap
1386+
lowerCaseName := strings.ToLower(name)
1387+
svcMap.Insert(lowerCaseName, svcMapEntry{
13851388
ip: epIP.String(),
13861389
serviceID: serviceID,
13871390
})
13881391
}
13891392

13901393
func delNameToIP(svcMap setmatrix.SetMatrix, name, serviceID string, epIP net.IP) {
1391-
svcMap.Remove(name, svcMapEntry{
1394+
lowerCaseName := strings.ToLower(name)
1395+
svcMap.Remove(lowerCaseName, svcMapEntry{
13921396
ip: epIP.String(),
13931397
serviceID: serviceID,
13941398
})
@@ -1956,6 +1960,7 @@ func (n *network) ResolveName(req string, ipType int) ([]net.IP, bool) {
19561960
}
19571961

19581962
req = strings.TrimSuffix(req, ".")
1963+
req = strings.ToLower(req)
19591964
ipSet, ok := sr.svcMap.Get(req)
19601965

19611966
if ipType == types.IPv6 {

libnetwork/resolver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ func (r *resolver) ServeDNS(w dns.ResponseWriter, query *dns.Msg) {
366366
if query == nil || len(query.Question) == 0 {
367367
return
368368
}
369-
name := query.Question[0].Name
370369

370+
name := query.Question[0].Name
371371
switch query.Question[0].Qtype {
372372
case dns.TypeA:
373373
resp, err = r.handleIPQuery(name, query, types.IPv4)

libnetwork/resolver_test.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,29 +126,33 @@ func TestDNSIPQuery(t *testing.T) {
126126
r := NewResolver(resolverIPSandbox, false, sb.Key(), sb.(*sandbox))
127127

128128
// test name1's IP is resolved correctly with the default A type query
129-
q := new(dns.Msg)
130-
q.SetQuestion("name1", dns.TypeA)
131-
r.(*resolver).ServeDNS(w, q)
132-
resp := w.GetResponse()
133-
checkNonNullResponse(t, resp)
134-
t.Log("Response: ", resp.String())
135-
checkDNSResponseCode(t, resp, dns.RcodeSuccess)
136-
checkDNSAnswersCount(t, resp, 1)
137-
checkDNSRRType(t, resp.Answer[0].Header().Rrtype, dns.TypeA)
138-
if answer, ok := resp.Answer[0].(*dns.A); ok {
139-
if !bytes.Equal(answer.A, net.ParseIP("192.168.0.1")) {
140-
t.Fatalf("IP response in Answer %v does not match 192.168.0.1", answer.A)
129+
// Also make sure DNS lookups are case insensitive
130+
names := []string{"name1", "NaMe1"}
131+
for _, name := range names {
132+
q := new(dns.Msg)
133+
q.SetQuestion(name, dns.TypeA)
134+
r.(*resolver).ServeDNS(w, q)
135+
resp := w.GetResponse()
136+
checkNonNullResponse(t, resp)
137+
t.Log("Response: ", resp.String())
138+
checkDNSResponseCode(t, resp, dns.RcodeSuccess)
139+
checkDNSAnswersCount(t, resp, 1)
140+
checkDNSRRType(t, resp.Answer[0].Header().Rrtype, dns.TypeA)
141+
if answer, ok := resp.Answer[0].(*dns.A); ok {
142+
if !bytes.Equal(answer.A, net.ParseIP("192.168.0.1")) {
143+
t.Fatalf("IP response in Answer %v does not match 192.168.0.1", answer.A)
144+
}
145+
} else {
146+
t.Fatal("Answer of type A not found")
141147
}
142-
} else {
143-
t.Fatal("Answer of type A not found")
148+
w.ClearResponse()
144149
}
145-
w.ClearResponse()
146150

147151
// test MX query with name1 results in Success response with 0 answer records
148-
q = new(dns.Msg)
152+
q := new(dns.Msg)
149153
q.SetQuestion("name1", dns.TypeMX)
150154
r.(*resolver).ServeDNS(w, q)
151-
resp = w.GetResponse()
155+
resp := w.GetResponse()
152156
checkNonNullResponse(t, resp)
153157
t.Log("Response: ", resp.String())
154158
checkDNSResponseCode(t, resp, dns.RcodeSuccess)

0 commit comments

Comments
 (0)