Skip to content

Commit

Permalink
internal/export/idna: use nontransitional processing in Go 1.18
Browse files Browse the repository at this point in the history
Updates golang/go#46001
Updates golang/go#47510

Change-Id: I1e978a3c6230abfd0b1aaab0c7343b33dda1ba64
Reviewed-on: https://go-review.googlesource.com/c/text/+/359634
Trust: Damien Neil <dneil@google.com>
Run-TryBot: Damien Neil <dneil@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Timothy Gu <timothygu99@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
neild committed Nov 1, 2021
1 parent 593da8d commit 835dae6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
27 changes: 13 additions & 14 deletions internal/export/idna/example_test.go
Expand Up @@ -13,27 +13,26 @@ import (
func ExampleProfile() {
// Raw Punycode has no restrictions and does no mappings.
fmt.Println(idna.ToASCII(""))
fmt.Println(idna.ToASCII("*.faß.com"))
fmt.Println(idna.Punycode.ToASCII("*.faß.com"))
fmt.Println(idna.ToASCII("*.GÖPHER.com"))
fmt.Println(idna.Punycode.ToASCII("*.GÖPHER.com"))

// Rewrite IDN for lookup. This (currently) uses transitional mappings to
// find a balance between IDNA2003 and IDNA2008 compatibility.
// Rewrite IDN for lookup.
fmt.Println(idna.Lookup.ToASCII(""))
fmt.Println(idna.Lookup.ToASCII("www.faß.com"))
fmt.Println(idna.Lookup.ToASCII("www.GÖPHER.com"))

// Convert an IDN to ASCII for registration purposes. This changes the
// encoding, but reports an error if the input was illformed.
fmt.Println(idna.Registration.ToASCII(""))
fmt.Println(idna.Registration.ToASCII("www.faß.com"))
// Convert an IDN to ASCII for registration purposes.
// This reports an error if the input was illformed.
fmt.Println(idna.Registration.ToASCII("www.GÖPHER.com"))
fmt.Println(idna.Registration.ToASCII("www.göpher.com"))

// Output:
// <nil>
// *.xn--fa-hia.com <nil>
// *.xn--fa-hia.com <nil>
// *.xn--GPHER-1oa.com <nil>
// *.xn--GPHER-1oa.com <nil>
// <nil>
// www.fass.com <nil>
// idna: invalid label ""
// www.xn--fa-hia.com <nil>
// www.xn--gpher-jua.com <nil>
// www.xn--GPHER-1oa.com idna: disallowed rune U+0047
// www.xn--gpher-jua.com <nil>
}

func ExampleNew() {
Expand Down
12 changes: 12 additions & 0 deletions internal/export/idna/go118.go
@@ -0,0 +1,12 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.18
// +build go1.18

package idna

// Transitional processing is disabled by default in Go 1.18.
// https://golang.org/issue/47510
const transitionalLookup = false
2 changes: 1 addition & 1 deletion internal/export/idna/idna10.0.0.go
Expand Up @@ -284,7 +284,7 @@ var (

punycode = &Profile{}
lookup = &Profile{options{
transitional: true,
transitional: transitionalLookup,
useSTD3Rules: true,
checkHyphens: true,
checkJoiners: true,
Expand Down
14 changes: 11 additions & 3 deletions internal/export/idna/idna10.0.0_test.go
Expand Up @@ -102,7 +102,7 @@ func TestLabelErrors(t *testing.T) {
// Chrome, modern Firefox, Safari, and IE.
{resolve, "lab⒐be", "xn--labbe-zh9b", "P1"}, // encode("lab⒐be")
{display, "lab⒐be", "lab⒐be", "P1"},
{resolve, "plan⒐faß.de", "xn--planfass-c31e.de", "P1"}, // encode("plan⒐fass") + ".de"
{transitional, "plan⒐faß.de", "xn--planfass-c31e.de", "P1"}, // encode("plan⒐fass") + ".de"
{display, "Plan⒐faß.de", "plan⒐faß.de", "P1"},

// Transitional vs Nontransitional processing
Expand All @@ -115,10 +115,10 @@ func TestLabelErrors(t *testing.T) {
// punycode on the result using transitional mapping.
// Firefox 49.0.1 goes haywire on this string and prints a bunch of what
// seems to be nested punycode encodings.
{resolve, "日本⒈co.ßßß.de", "xn--co-wuw5954azlb.ssssss.de", "P1"},
{transitional, "日本⒈co.ßßß.de", "xn--co-wuw5954azlb.ssssss.de", "P1"},
{display, "日本⒈co.ßßß.de", "日本⒈co.ßßß.de", "P1"},

{resolve, "a\u200Cb", "ab", ""},
{transitional, "a\u200Cb", "ab", ""},
{display, "a\u200Cb", "a\u200Cb", "C"},

{resolve, encode("a\u200Cb"), encode("a\u200Cb"), "C"},
Expand Down Expand Up @@ -153,3 +153,11 @@ func TestLabelErrors(t *testing.T) {
doTest(t, tc.f, tc.name, tc.input, tc.want, tc.wantErr)
}
}

func TestTransitionalDefault(t *testing.T) {
want := "xn--strae-oqa.de"
if transitionalLookup {
want = "strasse.de"
}
doTest(t, Lookup.ToASCII, "Lookup", "straße.de", want, "")
}
2 changes: 1 addition & 1 deletion internal/export/idna/idna_test.go
Expand Up @@ -45,7 +45,7 @@ func TestProfiles(t *testing.T) {
VerifyDNSLength(true),
BidiRule(),
)},
{"Lookup", lookup, New(MapForLookup(), BidiRule(), Transitional(true))},
{"Lookup", lookup, New(MapForLookup(), BidiRule(), Transitional(transitionalLookup))},
{"Display", display, New(MapForLookup(), BidiRule())},
}
for _, tc := range testCases {
Expand Down
10 changes: 10 additions & 0 deletions internal/export/idna/pre_go118.go
@@ -0,0 +1,10 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !go1.18
// +build !go1.18

package idna

const transitionalLookup = true

0 comments on commit 835dae6

Please sign in to comment.