JuliusBrussee/caveman · error

ssrf: invalid URL

Error message

ssrf: invalid URL

What it means

This error means the URL string passed to the ssrf package's URL validation could not be parsed by net/url.Parse at all. Because net/url.Error embeds the raw URL (which may contain credentials or query secrets), the library deliberately returns a stable, field-only message with no URL content. It guards callers from feeding malformed or attacker-controlled URLs into SSRF-protected fetches.

Solutions

  1. Run url.Parse(raw) on the input before calling ValidateURL and fix the parse error at the source (trim whitespace, percent-encode the query/path).
  2. If the URL comes from config/env, validate and trim it at load time (strings.TrimSpace, url.QueryEscape on interpolated values).
  3. If the input is user-supplied, reject it with a 400-style validation error before it reaches the ssrf layer — the library intentionally hides the URL from the error message.

Example fix

// before
client, err := ssrf.ValidateURL(cfg.WebhookBase + "/v1/notify?msg=" + msg)
// after
u := cfg.WebhookBase + "/v1/notify?msg=" + url.QueryEscape(msg)
u = strings.TrimSpace(u)
if _, perr := url.Parse(u); perr != nil {
    return fmt.Errorf("invalid webhook URL: %w", perr)
}
client, err := ssrf.ValidateURL(u)
Defensive patterns

Strategy: validation

Validate before calling

func isParseableURL(raw string) bool {
    u, err := url.Parse(strings.TrimSpace(raw))
    return err == nil && u.Scheme != "" && u.Host != ""
}

Try / catch

client, err := ssrf.ValidateURL(raw)
if err != nil {
    if err.Error() == "ssrf: invalid URL" {
        return fmt.Errorf("malformed URL rejected by ssrf guard: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling ValidateURL or ValidateURLNoResolve with a raw string that net/url.Parse rejects — e.g. "https://exa mple.com" (space), "http://%zz", "ht tp://host", or any string with a malformed control character or bad percent-escape.

Common situations: Building a URL by string concatenation with unescaped user input or env vars; passing a proxy or webhook URL from config that contains a trailing space or stray characters; copying a URL with a hidden non-ASCII character; handing attacker-supplied input directly to ValidateURL.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


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

Appendix: source

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

		return err
	}
	if addr, parseErr := netip.ParseAddr(host); parseErr == nil {
		return checkAddr(addr, host, port, cfg)
	}
	if strings.EqualFold(host, "localhost") && !(!cfg.ManagedMode && isInAllowList(host, port, cfg.AllowList)) {
		return fmt.Errorf("ssrf: host %q is blocked (loopback)", host)
	}
	return nil
}

// parseValidatedURL applies the URL-shape policy — scheme, embedded
// credentials, managed-mode port — and returns the host/port to check.
func parseValidatedURL(raw string, cfg Config) (host, port string, err error) {
	u, parseErr := url.Parse(raw)
	if parseErr != nil {
		// net/url.Error includes the raw URL (and may therefore include
		// credentials or query secrets). Keep this error field-only and stable.
		return "", "", errors.New("ssrf: invalid URL")
	}
	if u.Scheme != "https" && !(u.Scheme == "http" && !cfg.ManagedMode) {
		return "", "", fmt.Errorf("ssrf: scheme %q not permitted (managed mode requires https)", u.Scheme)
	}
	if u.User != nil {
		return "", "", fmt.Errorf("ssrf: credentials embedded in URL are forbidden")
	}
	host = u.Hostname()
	if host == "" {
		return "", "", fmt.Errorf("ssrf: URL must contain a host")
	}
	port = u.Port()
	if cfg.ManagedMode && port != "" && port != "443" {
		return "", "", errors.New("ssrf: managed mode requires port 443")
	}
	if port == "" {
		if u.Scheme == "https" {
			port = "443"

View on GitHub (pinned to 3ee70a1026)