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

MGMT-15653: Fix DNS regex validation #5482

Merged
merged 1 commit into from
Sep 12, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions pkg/validations/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
)

const (
baseDomainRegex = `^[a-z\d][\-]*[a-z\d]+$`
dnsNameRegex = `^(validateNoWildcardDNS\.)?([a-z\d]([\-]*[a-z\d]+)*\.)+[a-z\d]+[\-]*[a-z\d]+[\.]?$`
baseDomainRegex = `^[a-z\d]+[\-]*[a-z\d]+$`
dnsNameRegex = `^([a-z\d]([\-]*[a-z\d]+)*\.)+[a-z\d]+[\-]*[a-z\d]+$`
wildCardDomainRegex = `^(validateNoWildcardDNS\.).+\.$`
hostnameRegex = `^[a-z0-9][a-z0-9\-\.]{0,61}[a-z0-9]$`
installerArgsValuesRegex = `^[A-Za-z0-9@!#$%*()_+-=//.,";':{}\[\]]+$`
)
Expand Down Expand Up @@ -45,14 +46,20 @@ func ValidateInstallerArgs(args []string) error {
}

func ValidateDomainNameFormat(dnsDomainName string) (int32, error) {
matched, err := regexp.MatchString(baseDomainRegex, dnsDomainName)
domainName := dnsDomainName
wildCardMatched, wildCardMatchErr := regexp.MatchString(wildCardDomainRegex, dnsDomainName)
if wildCardMatchErr == nil && wildCardMatched {
trimmedDomain := strings.TrimPrefix(dnsDomainName, "validateNoWildcardDNS.")
domainName = strings.TrimSuffix(trimmedDomain, ".")
}
matched, err := regexp.MatchString(baseDomainRegex, domainName)
if err != nil {
return http.StatusInternalServerError, errors.Wrapf(err, "Single DNS base domain validation for %s", dnsDomainName)
}
if matched && len(dnsDomainName) > 1 {
if matched && len(domainName) > 1 {
return 0, nil
}
matched, err = regexp.MatchString(dnsNameRegex, dnsDomainName)
matched, err = regexp.MatchString(dnsNameRegex, domainName)
if err != nil {
return http.StatusInternalServerError, errors.Wrapf(err, "DNS name validation for %s", dnsDomainName)
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/validations/validations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,26 @@ var _ = Describe("dns name", func() {
domainName: "0-example.com0",
valid: true,
},
{
domainName: "example-example-example.com",
valid: true,
},
{
domainName: "example.example-example.com",
valid: true,
},
{
domainName: "exam--ple.example--example.com",
valid: true,
},
{
domainName: "exam-ple",
valid: true,
},
{
domainName: "exam--ple",
valid: true,
},
{
domainName: "a.c",
valid: false,
Expand Down