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-19773: block forbidden hostname #7270

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions internal/host/hostutil/host_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

const (
MaxHostnameLength = 63
MaxHostnameLength = 253
HostnamePattern = "^[a-z0-9][a-z0-9-]{0,62}(?:[.][a-z0-9-]{1,63})*$"
)

Expand Down Expand Up @@ -77,7 +77,7 @@ func GetEventSeverityFromHostStatus(status string) string {

func ValidateHostname(hostname string) error {
if len(hostname) > MaxHostnameLength {
return common.NewApiError(http.StatusBadRequest, errors.Errorf("hostname is too long, must be 63 characters or less. Hostname: %s has %d characters", hostname, len(hostname)))
return common.NewApiError(http.StatusBadRequest, errors.Errorf("hostname is too long, must be 253 characters or less. Hostname: %s has %d characters", hostname, len(hostname)))
}
b, err := regexp.MatchString(HostnamePattern, hostname)
if err != nil {
Expand Down
16 changes: 12 additions & 4 deletions internal/host/hostutil/host_utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hostutil

import (
"strings"
"testing"

"github.com/go-openapi/strfmt"
Expand Down Expand Up @@ -88,22 +89,29 @@ var _ = Describe("Validation", func() {
}
})

section := strings.Repeat("a", 63) + "."
endsection := strings.Repeat("a", 61)
It("Should allow permitted hostnames", func() {
for _, hostName := range []string{
"foobar",
"foobar.local",
"arbitrary.hostname",
strings.Repeat(section, 3) + endsection, // 253 chars
} {
err := ValidateHostname(hostName)
Expect(err).NotTo(HaveOccurred())
}
})

It("Should not allow hostnames longer than 63 characters", func() {
It("Should not allow hostnames violating naming rules", func() {
for _, hostName := range []string{
"foobar.local.arbitrary.hostname.longer.than.64-characters.inthis.name",
"foobar1234-foobar1234-foobar1234-foobar1234-foobar1234-foobar1234-foobar1234",
"this-host.name-iss.exactly-64.characters.long.so.itt-should.fail",
strings.Repeat(section, 3) + endsection + "a", // more than 253 chars
"toolong-2nd-section." + "a" + section + "com", // has section longer than 63 chars
"-invalid-start.com", // starts with hyphen
"invalid-end.com.", // ends with dot
"two..dots.com", // double dots
"UPPERCASE.com", // uppercase letters
"invalid$.com", // special character `$`
} {
err := ValidateHostname(hostName)
Expect(err).To(HaveOccurred())
Expand Down