mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Ensure --hostname is valid
Validates whether the given hostname is RFC 1123 (https://tools.ietf.org/html/rfc1123) compliant. Fixes #20371 Signed-off-by: Andrew Guenther <guenther.andrew.j@gmail.com>
This commit is contained in:
parent
675617bc85
commit
3b6ffc8022
2 changed files with 30 additions and 3 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path"
|
"path"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -248,6 +249,13 @@ func Parse(cmd *flag.FlagSet, args []string) (*container.Config, *container.Host
|
||||||
hostname = parts[0]
|
hostname = parts[0]
|
||||||
domainname = parts[1]
|
domainname = parts[1]
|
||||||
}
|
}
|
||||||
|
// 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())
|
ports, portBindings, err := nat.ParsePortSpecs(flPublish.GetAll())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -385,12 +385,31 @@ func TestParseWithMemorySwap(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseHostname(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"
|
hostnameWithDomain := "--hostname=hostname.domainname"
|
||||||
hostnameWithDomainTld := "--hostname=hostname.domainname.tld"
|
hostnameWithDomainTld := "--hostname=hostname.domainname.tld"
|
||||||
if config, _ := mustParse(t, hostname); config.Hostname != "hostname" && config.Domainname != "" {
|
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)
|
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" && config.Domainname != "domainname" {
|
if config, _ := mustParse(t, hostnameWithDomain); config.Hostname != "hostname" && config.Domainname != "domainname" {
|
||||||
t.Fatalf("Expected the config to have 'hostname' as hostname, got '%v'", config.Hostname)
|
t.Fatalf("Expected the config to have 'hostname' as hostname, got '%v'", config.Hostname)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue