{"record":{"id":"2fb12bc1e1f9a51a","repo":"JuliusBrussee/caveman","slug":"ssrf-invalid-host","errorCode":null,"errorMessage":"ssrf: invalid host","messagePattern":"ssrf: invalid host","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/ssrf/ssrf.go","lineNumber":293,"sourceCode":"\tfor _, ia := range addrs {\n\t\ta, ok := netip.AddrFromSlice(ia.IP)\n\t\tif !ok {\n\t\t\treturn fmt.Errorf(\"ssrf: could not parse resolved IP %v for host %q\", ia.IP, host)\n\t\t}\n\t\ta = a.Unmap() // normalise ::ffff:x.x.x.x → x.x.x.x\n\t\tif err := checkAddr(a, host, port, cfg); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\n// validateHostInput rejects URL/userinfo-shaped values before they reach DNS\n// or an error formatter. IP literals (including zoned IPv6) are handled by\n// netip.ParseAddr and may contain colons or a zone identifier.\nfunc validateHostInput(host string) error {\n\tif host == \"\" {\n\t\treturn errors.New(\"ssrf: invalid host\")\n\t}\n\tif _, err := netip.ParseAddr(host); err == nil {\n\t\treturn nil\n\t}\n\tif strings.ContainsAny(host, \"/?#@\\\\:%\") {\n\t\treturn errors.New(\"ssrf: invalid host\")\n\t}\n\treturn nil\n}\n\n// checkAddr returns an error if addr is in any blocked range.\n//\n// host is the original hostname (or IP literal) used for allowlist matching.\n// At dial time host will itself be an IP literal; the allowlist check must\n// therefore accept both the hostname form and the resolved IP string.\nfunc checkAddr(addr netip.Addr, host, port string, cfg Config) error {\n\t// Strip any IPv6 zone identifier (e.g. fe80::1%eth0) before range checks:\n\t// netip.Prefix.Contains returns false for ANY zoned address, so without this","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/ssrf/ssrf.go#L275-L311","documentation":"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.","triggerScenarios":"Calling ValidateURLNoResolve or validateHostPort with host = \"\"; also reached from the anonymous dial-address hook when SplitHostPort yields an empty host.","commonSituations":"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).","solutions":["Check the host is non-empty before calling the ssrf APIs and fail with a caller-side validation error naming the missing config field.","Fix the config/env loading so the host value is actually populated (typo in key name, wrong env file).","If deriving the host from a URL, use u.Hostname() rather than manual string splitting so the value is never empty mid-token."],"exampleFix":"// before\nconn, err := ssrf.ValidateHostPort(os.Getenv(\"UPSTREAM_HOST\"), \"443\")\n// after\nhost := strings.TrimSpace(os.Getenv(\"UPSTREAM_HOST\"))\nif host == \"\" {\n    return errors.New(\"UPSTREAM_HOST must be set to a hostname or IP\")\n}\nconn, err := ssrf.ValidateHostPort(host, \"443\")","handlingStrategy":"validation","validationCode":"func hostIsSet(s string) bool { return strings.TrimSpace(s) != \"\" }\nif !hostIsSet(cfg.UpstreamHost) { return errors.New(\"upstream host is required\") }","typeGuard":null,"tryCatchPattern":"if err := ssrf.ValidateHostPort(host, port); err != nil {\n    if err.Error() == \"ssrf: invalid host\" {\n        return fmt.Errorf(\"host %q empty or malformed\", host)\n    }\n    return err\n}","preventionTips":["Fail fast at startup when required host config values are blank","Use url.Hostname() instead of manual splitting to derive hosts","Trim whitespace from env/config values before use"],"tags":["ssrf","host-validation","empty-input","go"],"backgroundTag":"empty-required-field","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}