From 0ec31bb8258ac3677fdd4fffb7b10e00e5834e3b Mon Sep 17 00:00:00 2001 From: Rafael Dantas Justo Date: Thu, 16 Jul 2015 13:02:51 -0300 Subject: [PATCH 1/2] encode function should return nil when an invalid character is detected and ToPunycode function should return an empty string when there's an invalid domain name --- idn/punycode.go | 16 +++++++++++----- idn/punycode_test.go | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/idn/punycode.go b/idn/punycode.go index 28c7dcc7..670dc76d 100644 --- a/idn/punycode.go +++ b/idn/punycode.go @@ -27,8 +27,8 @@ const ( ) // ToPunycode converts unicode domain names to DNS-appropriate punycode names. -// This function would return incorrect result for strings for non-canonical -// unicode strings. +// This function would return an empty string result for domain names with +// invalid unicode strings. This function expects domain names in lowercase. func ToPunycode(s string) string { tokens := dns.SplitDomainName(s) switch { @@ -41,7 +41,11 @@ func ToPunycode(s string) string { } for i := range tokens { - tokens[i] = string(encode([]byte(tokens[i]))) + t := encode([]byte(tokens[i])) + if t == nil { + return "" + } + tokens[i] = string(t) } return strings.Join(tokens, ".") } @@ -139,14 +143,16 @@ func tfunc(k, bias rune) rune { return k - bias } -// encode transforms Unicode input bytes (that represent DNS label) into punycode bytestream +// encode transforms Unicode input bytes (that represent DNS label) into +// punycode bytestream. This function would return nil if there's an invalid +// character in the label func encode(input []byte) []byte { n, bias := _N, _BIAS b := bytes.Runes(input) for i := range b { if !isValidRune(b[i]) { - return input + return nil } b[i] = preprune(b[i]) diff --git a/idn/punycode_test.go b/idn/punycode_test.go index c6b88992..f8b355ca 100644 --- a/idn/punycode_test.go +++ b/idn/punycode_test.go @@ -108,7 +108,7 @@ var invalidUnicodes = []string{ func TestInvalidUnicodes(t *testing.T) { for _, d := range invalidUnicodes { s := ToPunycode(d) - if s != d { + if s != "" { t.Errorf("Changed invalid name %s to %#v", d, s) } } From c1abafe6ad7079593170ce82bc7b43607d39bc70 Mon Sep 17 00:00:00 2001 From: Rafael Dantas Justo Date: Fri, 17 Jul 2015 07:17:18 -0300 Subject: [PATCH 2/2] add final dot in function comment --- idn/punycode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/idn/punycode.go b/idn/punycode.go index 670dc76d..09363821 100644 --- a/idn/punycode.go +++ b/idn/punycode.go @@ -145,7 +145,7 @@ func tfunc(k, bias rune) rune { // encode transforms Unicode input bytes (that represent DNS label) into // punycode bytestream. This function would return nil if there's an invalid -// character in the label +// character in the label. func encode(input []byte) []byte { n, bias := _N, _BIAS