Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make host name parsing case-insensitive #2022

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/endpointaddr/endpointaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func Parse(endpoint string, defaultPort uint16) (HostPort, error) {
// Check if the host part is a IPv4 or IPv6 address or a valid hostname according to RFC 1123.
switch {
case len(validation.IsValidIP(field.NewPath("UNKNOWN_PATH"), host)) == 0:
case len(validation.IsDNS1123Subdomain(host)) == 0:
// the host name should be case-insensitive.
case len(validation.IsDNS1123Subdomain(strings.ToLower(host))) == 0:
default:
return HostPort{}, fmt.Errorf("host %q is not a valid hostname or IP address", host)
}
Expand Down
33 changes: 24 additions & 9 deletions internal/endpointaddr/endpointaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,25 @@ func TestParse(t *testing.T) {
defaultPort: 443,
expectErr: `host "___.example.com" is not a valid hostname or IP address`,
},
{
name: "invalid host with uppercase",
input: "HOST.EXAMPLE.COM",
defaultPort: 443,
expectErr: `host "HOST.EXAMPLE.COM" is not a valid hostname or IP address`,
},
{
name: "invalid host with extra port",
input: "host.example.com:port1:port2",
defaultPort: 443,
expectErr: `host "host.example.com:port1:port2" is not a valid hostname or IP address`,
},
{
name: "hostname with upper case letters should be valid",
input: "HoSt.EXamplE.cOM",
defaultPort: 443,
expect: HostPort{Host: "HoSt.EXamplE.cOM", Port: 443},
expectEndpoint: "HoSt.EXamplE.cOM:443",
},
{
name: "unicode chars are disallowed in host names",
input: "Hello.世界🙂.com",
defaultPort: 443,
expectErr: `host "Hello.世界🙂.com" is not a valid hostname or IP address`,
},
} {
t.Run(tt.name, func(t *testing.T) {
got, err := Parse(tt.input, tt.defaultPort)
Expand Down Expand Up @@ -292,11 +299,19 @@ func TestParseFromURL(t *testing.T) {
defaultPort: 443,
expectErr: `host "___.example.com" is not a valid hostname or IP address`,
},

{
name: "hostname with upper case letters should be valid",
input: "https://HoSt.EXamplE.cOM",
defaultPort: 443,
expect: HostPort{Host: "HoSt.EXamplE.cOM", Port: 443},
expectEndpoint: "HoSt.EXamplE.cOM:443",
},
{
name: "invalid host with uppercase",
input: "http://HOST.EXAMPLE.COM",
name: "unicode chars are disallowed in host names",
input: "https://Hello.世界🙂.com",
defaultPort: 443,
expectErr: `host "HOST.EXAMPLE.COM" is not a valid hostname or IP address`,
expectErr: `host "Hello.世界🙂.com" is not a valid hostname or IP address`,
},
// new tests for new functionality
{
Expand Down
Loading