make host name parsing case-insensitive

Signed-off-by: Ashish Amarnath <ashish.amarnath@broadcom.com>

Co-authored-by: Ryan Richard <richardry@vmware.com>
This commit is contained in:
Ashish Amarnath
2024-07-29 14:05:07 -07:00
parent dd80627dfa
commit 7c7f0fdae3
2 changed files with 26 additions and 10 deletions

View File

@@ -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)
}

View File

@@ -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
{