JuliusBrussee/caveman · error

ssrf: invalid host

Error message

ssrf: invalid host

What it means

validateHostInput rejects host strings that are empty or that look like they still contain URL syntax (path, query, userinfo, port, escapes) before they can reach DNS or an error formatter. The first variant fires when the host is the empty string. This prevents SSRF bypasses and confusing errors caused by malformed host inputs.

Solutions

  1. Check the host is non-empty before calling the ssrf APIs and fail with a caller-side validation error naming the missing config field.
  2. Fix the config/env loading so the host value is actually populated (typo in key name, wrong env file).
  3. If deriving the host from a URL, use u.Hostname() rather than manual string splitting so the value is never empty mid-token.

Example fix

// before
conn, err := ssrf.ValidateHostPort(os.Getenv("UPSTREAM_HOST"), "443")
// after
host := strings.TrimSpace(os.Getenv("UPSTREAM_HOST"))
if host == "" {
    return errors.New("UPSTREAM_HOST must be set to a hostname or IP")
}
conn, err := ssrf.ValidateHostPort(host, "443")
Defensive patterns

Strategy: validation

Validate before calling

func hostIsSet(s string) bool { return strings.TrimSpace(s) != "" }
if !hostIsSet(cfg.UpstreamHost) { return errors.New("upstream host is required") }

Try / catch

if err := ssrf.ValidateHostPort(host, port); err != nil {
    if err.Error() == "ssrf: invalid host" {
        return fmt.Errorf("host %q empty or malformed", host)
    }
    return err
}

Prevention

When it happens

Trigger: Calling ValidateURLNoResolve or validateHostPort with host = ""; also reached from the anonymous dial-address hook when SplitHostPort yields an empty host.

Common situations: An env var or config key holding the target host is unset/blank; a config struct zero-value is passed through; string splitting produced an empty component (e.g. ":443" or trimming "host" out of "host:port" incorrectly).

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/2fb12bc1e1f9a51a. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/ssrf/ssrf.go:293

	for _, ia := range addrs {
		a, ok := netip.AddrFromSlice(ia.IP)
		if !ok {
			return fmt.Errorf("ssrf: could not parse resolved IP %v for host %q", ia.IP, host)
		}
		a = a.Unmap() // normalise ::ffff:x.x.x.x → x.x.x.x
		if err := checkAddr(a, host, port, cfg); err != nil {
			return err
		}
	}
	return nil
}

// validateHostInput rejects URL/userinfo-shaped values before they reach DNS
// or an error formatter. IP literals (including zoned IPv6) are handled by
// netip.ParseAddr and may contain colons or a zone identifier.
func validateHostInput(host string) error {
	if host == "" {
		return errors.New("ssrf: invalid host")
	}
	if _, err := netip.ParseAddr(host); err == nil {
		return nil
	}
	if strings.ContainsAny(host, "/?#@\\:%") {
		return errors.New("ssrf: invalid host")
	}
	return nil
}

// checkAddr returns an error if addr is in any blocked range.
//
// host is the original hostname (or IP literal) used for allowlist matching.
// At dial time host will itself be an IP literal; the allowlist check must
// therefore accept both the hostname form and the resolved IP string.
func checkAddr(addr netip.Addr, host, port string, cfg Config) error {
	// Strip any IPv6 zone identifier (e.g. fe80::1%eth0) before range checks:
	// netip.Prefix.Contains returns false for ANY zoned address, so without this

View on GitHub (pinned to 3ee70a1026)