Skip to content

Commit 6440815

Browse files
authored
Merge pull request #194 from m13253/fix/idna-conversion
Fix Punycode conversion and harden validations
2 parents 5314c82 + a248ef2 commit 6440815

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

‎doh-client/client.go‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,16 @@ func NewClient(conf *config.Config) (c *Client, err error) {
182182
},
183183
}
184184
if len(conf.Other.Passthrough) != 0 {
185-
c.passthrough = make([]string, len(conf.Other.Passthrough))
186-
for i, passthrough := range conf.Other.Passthrough {
187-
if punycode, err := idna.ToASCII(passthrough); err != nil {
188-
passthrough = punycode
185+
for _, passthrough := range conf.Other.Passthrough {
186+
normalized := passthrough
187+
if punycode, err := idna.ToASCII(normalized); err == nil {
188+
normalized = punycode
189189
}
190-
c.passthrough[i] = "." + strings.ToLower(strings.Trim(passthrough, ".")) + "."
190+
normalized = strings.Trim(normalized, ".")
191+
if _, ok := dns.IsDomainName(normalized); !ok {
192+
return nil, fmt.Errorf("passthrough %q is not a valid domain name", passthrough)
193+
}
194+
c.passthrough = append(c.passthrough, normalized)
191195
}
192196
}
193197
}
@@ -449,12 +453,12 @@ func (c *Client) handlerFunc(w dns.ResponseWriter, r *dns.Msg, isTCP bool) {
449453

450454
shouldPassthrough := false
451455
passthroughQuestionName := questionName
452-
if punycode, err := idna.ToASCII(passthroughQuestionName); err != nil {
456+
if punycode, err := idna.ToASCII(passthroughQuestionName); err == nil {
453457
passthroughQuestionName = punycode
454458
}
455-
passthroughQuestionName = "." + strings.ToLower(strings.Trim(passthroughQuestionName, ".")) + "."
459+
passthroughQuestionName = strings.Trim(passthroughQuestionName, ".")
456460
for _, passthrough := range c.passthrough {
457-
if strings.HasSuffix(passthroughQuestionName, passthrough) {
461+
if dns.IsSubDomain(passthrough, passthroughQuestionName) {
458462
shouldPassthrough = true
459463
break
460464
}

‎doh-server/google.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ func (s *Server) parseRequestGoogle(ctx context.Context, w http.ResponseWriter,
4848
errtext: "Invalid argument value: \"name\"",
4949
}
5050
}
51+
// idna.ToASCII is O(L^2), so we want to check the length before calling it.
52+
// Because of a separate \DDD escaping rule, the maximum allowed input length actually has a "*4".
53+
if len(name) > 255*4 {
54+
return &DNSRequest{
55+
errcode: 400,
56+
errtext: "Invalid argument value: \"name\" (too long)",
57+
}
58+
}
5159
if punycode, err := idna.ToASCII(name); err == nil {
5260
name = punycode
5361
} else {

0 commit comments

Comments
 (0)