dgraph-io/dgraph · error
Invalid hostname: %v
Error message
Invalid hostname: %v
What it means
The hostname fails the RFC hostname regular expression (regExpHostName). After passing the 255-length check, the string must match the legal hostname character/label rules; otherwise it is rejected as invalid.
Source
Thrown at x/x.go:863
// ValidateAddress checks whether given address can be used with grpc dial function
func ValidateAddress(addr string) error {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return err
}
if p, err := strconv.Atoi(port); err != nil || p <= 0 || p >= 65536 {
return errors.Errorf("Invalid port: %v", p)
}
if ip := net.ParseIP(host); ip != nil {
return nil
}
// try to parse as hostname as per hostname RFC
if len(strings.Replace(host, ".", "", -1)) > 255 {
return errors.Errorf("Hostname should be less than or equal to 255 characters")
}
if !regExpHostName.MatchString(host) {
return errors.Errorf("Invalid hostname: %v", host)
}
return nil
}
// RemoveDuplicates sorts the slice of strings and removes duplicates. changes the input slice.
// This function should be called like: someSlice = RemoveDuplicates(someSlice)
func RemoveDuplicates(s []string) (out []string) {
sort.Strings(s)
out = s[:0]
for i := range s {
if i > 0 && s[i] == s[i-1] {
continue
}
out = append(out, s[i])
}
return
}
View on GitHub (pinned to 759e242be6)
Solutions
- Fix the hostname to match RFC rules: letters, digits, hyphens, dot-separated labels
- Strip scheme/port from the value before validating (use url.Parse and pass u.Hostname())
- Replace invalid characters like underscores with hyphens or use the IP address
Example fix
// before
err := ValidateHost("https://my_server:9000") // invalid
// after
u, _ := url.Parse("https://my-server:9000")
err := ValidateHost(u.Hostname()) // "my-server" Defensive patterns
Strategy: validation
Validate before calling
var hostRe = regexp.MustCompile(`^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*$`)
func validHostname(host string) bool {
if net.ParseIP(host) != nil { return true }
return hostRe.MatchString(host)
} Type guard
func looksLikeHostname(s string) bool { return s != "" && !strings.ContainsAny(s, " _:/@") } Prevention
- Strip scheme and port with url.Parse before storing/validating a host
- Avoid underscores in hostnames even if some resolvers accept them
- Trim whitespace from config values before use
When it happens
Trigger: Passing a host with illegal characters (underscores, spaces, symbols), empty labels ('..'), leading/trailing hyphens, or an empty string to the hostname validation function.
Common situations: Using hostnames with '_' (common in internal configs), IPv6 without brackets pasted raw, hostnames containing ports ('host:9000') or schemes ('https://host'), or copy-paste artifacts like whitespace.
Related errors
- Hostname should be less than or equal to 255 characters
- NQuad failed sanity check. Subject: %q, Predicate: %q, Objec
- empty variable name in function call
- empty facetKeys not allowed
- Key size value is too large (x > 4096)
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/e6ddd6c51503792d.
Report an issue: GitHub.