Skip to content

Commit

Permalink
CanonicalName should casefold non-US-ASCII chars (#1470)
Browse files Browse the repository at this point in the history
According to Section 6.2 [1] of RFC 4034,all uppercase US-ASCII letters in the
owner name of the RR are replaced by the corresponding lowercase US-ASCII
letters.

This updates CanonicalName to conform to the RFC.

[1] https://www.rfc-editor.org/rfc/rfc4034#section-6.2

Fixes #1434
  • Loading branch information
retornam committed Sep 12, 2023
1 parent 9ec1b44 commit a9ed73a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 12 additions & 1 deletion defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"strconv"
"strings"
"unicode"
)

const hexDigit = "0123456789abcdef"
Expand Down Expand Up @@ -330,8 +331,18 @@ func Fqdn(s string) string {

// CanonicalName returns the domain name in canonical form. A name in canonical
// form is lowercase and fully qualified. See Section 6.2 in RFC 4034.
// According to the RFC all uppercase US-ASCII letters in the owner name of the
// RR areeplaced by the corresponding lowercase US-ASCII letters.
func CanonicalName(s string) string {
return strings.ToLower(Fqdn(s))
var result strings.Builder
for _, ch := range s {
if unicode.IsUpper(ch) && (ch >= 0x00 && ch <= 0x7F) {
result.WriteRune(unicode.ToLower(ch))
} else {
result.WriteRune(ch)
}
}
return Fqdn(result.String())
}

// Copied from the official Go code.
Expand Down
2 changes: 2 additions & 0 deletions labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ func TestCanonicalName(t *testing.T) {
"example.test": "example.test.",
"Lower.CASE.test.": "lower.case.test.",
"*.Test": "*.test.",
"ÉxamplE.com": "Éxample.com.",
"É.com": "É.com.",
} {
if got := CanonicalName(s); got != expect {
t.Errorf("CanonicalName(%q) = %q, expected %q", s, got, expect)
Expand Down

0 comments on commit a9ed73a

Please sign in to comment.