mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #20566 from AndrewGuenther/20371-validate-hostname
Ensure --hostname is valid
This commit is contained in:
commit
f9f8708dc6
2 changed files with 30 additions and 3 deletions
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"io/ioutil"
|
||||
"path"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
|
@ -240,6 +241,13 @@ func Parse(cmd *flag.FlagSet, args []string) (*container.Config, *container.Host
|
|||
if *flEntrypoint != "" {
|
||||
entrypoint = strslice.StrSlice{*flEntrypoint}
|
||||
}
|
||||
// Validate if the given hostname is RFC 1123 (https://tools.ietf.org/html/rfc1123) compliant.
|
||||
if hostname != "" {
|
||||
matched, _ := regexp.MatchString("^(([[:alnum:]]|[[:alnum:]][[:alnum:]\\-]*[[:alnum:]])\\.)*([[:alnum:]]|[[:alnum:]][[:alnum:]\\-]*[[:alnum:]])$", hostname)
|
||||
if !matched {
|
||||
return nil, nil, nil, cmd, fmt.Errorf("invalid hostname format for --hostname: %s", hostname)
|
||||
}
|
||||
}
|
||||
|
||||
ports, portBindings, err := nat.ParsePortSpecs(flPublish.GetAll())
|
||||
if err != nil {
|
||||
|
|
|
@ -385,11 +385,30 @@ func TestParseWithMemorySwap(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestParseHostname(t *testing.T) {
|
||||
hostname := "--hostname=hostname"
|
||||
validHostnames := map[string]string{
|
||||
"hostname": "hostname",
|
||||
"host-name": "host-name",
|
||||
"hostname123": "hostname123",
|
||||
"123hostname": "123hostname",
|
||||
}
|
||||
invalidHostnames := map[string]string{
|
||||
"^hostname": "invalid hostname format for --hostname: ^hostname",
|
||||
"hostname%": "invalid hostname format for --hostname: hostname%",
|
||||
"host&name": "invalid hostname format for --hostname: host&name",
|
||||
"-hostname": "invalid hostname format for --hostname: -hostname",
|
||||
"host_name": "invalid hostname format for --hostname: host_name",
|
||||
}
|
||||
hostnameWithDomain := "--hostname=hostname.domainname"
|
||||
hostnameWithDomainTld := "--hostname=hostname.domainname.tld"
|
||||
if config, _ := mustParse(t, hostname); config.Hostname != "hostname" && config.Domainname != "" {
|
||||
t.Fatalf("Expected the config to have 'hostname' as hostname, got '%v'", config.Hostname)
|
||||
for hostname, expectedHostname := range validHostnames {
|
||||
if config, _ := mustParse(t, fmt.Sprintf("--hostname=%s", hostname)); config.Hostname != expectedHostname {
|
||||
t.Fatalf("Expected the config to have 'hostname' as hostname, got '%v'", config.Hostname)
|
||||
}
|
||||
}
|
||||
for hostname, expectedError := range invalidHostnames {
|
||||
if _, _, err := parse(t, fmt.Sprintf("--hostname=%s", hostname)); err == nil || err.Error() != expectedError {
|
||||
t.Fatalf("Expected error '%v' with '--hostname=%s', got '%s'", expectedError, hostname, err)
|
||||
}
|
||||
}
|
||||
if config, _ := mustParse(t, hostnameWithDomain); config.Hostname != "hostname.domainname" && config.Domainname != "" {
|
||||
t.Fatalf("Expected the config to have 'hostname' as hostname.domainname, got '%v'", config.Hostname)
|
||||
|
|
Loading…
Add table
Reference in a new issue