From 7c7f0fdae3470c7cb6c3aeb3972b9daede7f866e Mon Sep 17 00:00:00 2001 From: Ashish Amarnath Date: Mon, 29 Jul 2024 14:05:07 -0700 Subject: [PATCH] make host name parsing case-insensitive Signed-off-by: Ashish Amarnath Co-authored-by: Ryan Richard --- internal/endpointaddr/endpointaddr.go | 3 +- internal/endpointaddr/endpointaddr_test.go | 33 ++++++++++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/internal/endpointaddr/endpointaddr.go b/internal/endpointaddr/endpointaddr.go index c54ca421e..c2766daea 100644 --- a/internal/endpointaddr/endpointaddr.go +++ b/internal/endpointaddr/endpointaddr.go @@ -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) } diff --git a/internal/endpointaddr/endpointaddr_test.go b/internal/endpointaddr/endpointaddr_test.go index 1d560629d..13e203e84 100644 --- a/internal/endpointaddr/endpointaddr_test.go +++ b/internal/endpointaddr/endpointaddr_test.go @@ -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) @@ -292,11 +299,19 @@ func TestParseFromURL(t *testing.T) { defaultPort: 443, expectErr: `host "___.example.com" is not a valid hostname or IP address`, }, + { - name: "invalid host with uppercase", - input: "http://HOST.EXAMPLE.COM", + 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: "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 {