CloakHQ/CloakBrowser · error · FormatException

Invalid port: {s}

Error message

Invalid port: {s}

What it means

FormatException from ProxyResolver.ParsePort when a proxy URL's port component is present but is not an integer in 0-65535. The resolver refuses to guess a port for a malformed proxy string.

Source

Thrown at dotnet/src/CloakBrowser/ProxyResolver.cs:114

                p.Host = hostport[..colon];
                p.Port = ParsePort(hostport[(colon + 1)..]);
            }
            else
            {
                p.Host = hostport;
            }
        }
        // Python's urlparse().hostname cosmetically lowercases the host; match it so
        // the assembled proxy URL/server string is byte-for-byte identical.
        p.Host = p.Host.ToLowerInvariant();
        return p;
    }

    private static int? ParsePort(string s)
    {
        if (string.IsNullOrEmpty(s)) return null;
        if (!int.TryParse(s, out var port) || port < 0 || port > 65535)
            throw new FormatException($"Invalid port: {s}");
        return port;
    }

    /// <summary>Percent-encode like Python's <c>quote(safe="")</c>.</summary>
    private static string Quote(string s) => Uri.EscapeDataString(s);

    /// <summary>Percent-decode like Python's <c>unquote</c>.</summary>
    private static string Unquote(string s) => Uri.UnescapeDataString(s);

    private static string AssembleProxyUrl(
        string scheme, string host, int? port,
        string encUser, string? encPass,
        string path = "", string query = "", string fragment = "")
    {
        if (host.Contains(':')) // IPv6 literal - re-add brackets
            host = $"[{host}]";
        string userinfo;
        if (encPass != null)

View on GitHub (pinned to d6bad5de26)

Solutions

  1. Validate/normalize the proxy URL before passing it (Uri.TryCreate)
  2. Fix the port value to a valid 1-65535 integer
  3. Omit the port entirely to use the scheme default instead of writing garbage

Example fix

# before
proxy = "http://myproxy:70000"

# after
proxy = "http://myproxy:8080"
Defensive patterns

Strategy: validation

Validate before calling

if (!Uri.TryCreate(proxyUrl, UriKind.Absolute, out var u) || !u.IsDefaultPort && u.Port > 65535)
    throw new ArgumentException("bad proxy url");

Try / catch

catch (FormatException e) when (e.Message.StartsWith("Invalid port")) { normalize proxy string and retry }

Prevention

When it happens

Trigger: Passing a proxy string like 'http://host:80880', 'host:abc', or one with a stray colon (e.g. 'host:80:443') so the parsed port segment fails int.TryParse or the range check.

Common situations: Typos in PROXY env vars, proxy strings built by string concatenation with an extra port, or non-numeric suffixes after the host.

Related errors


AI-assisted analysis of CloakHQ/CloakBrowser@d6bad5de26 (2026-08-28). Data as JSON: /api/errors/72863b0166c92a37. Report an issue: GitHub.