{"record":{"id":"72863b0166c92a37","repo":"CloakHQ/CloakBrowser","slug":"invalid-port-s","errorCode":null,"errorMessage":"Invalid port: {s}","messagePattern":"Invalid port: (.+?)","errorType":"validation","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"dotnet/src/CloakBrowser/ProxyResolver.cs","lineNumber":114,"sourceCode":"                p.Host = hostport[..colon];\n                p.Port = ParsePort(hostport[(colon + 1)..]);\n            }\n            else\n            {\n                p.Host = hostport;\n            }\n        }\n        // Python's urlparse().hostname cosmetically lowercases the host; match it so\n        // the assembled proxy URL/server string is byte-for-byte identical.\n        p.Host = p.Host.ToLowerInvariant();\n        return p;\n    }\n\n    private static int? ParsePort(string s)\n    {\n        if (string.IsNullOrEmpty(s)) return null;\n        if (!int.TryParse(s, out var port) || port < 0 || port > 65535)\n            throw new FormatException($\"Invalid port: {s}\");\n        return port;\n    }\n\n    /// <summary>Percent-encode like Python's <c>quote(safe=\"\")</c>.</summary>\n    private static string Quote(string s) => Uri.EscapeDataString(s);\n\n    /// <summary>Percent-decode like Python's <c>unquote</c>.</summary>\n    private static string Unquote(string s) => Uri.UnescapeDataString(s);\n\n    private static string AssembleProxyUrl(\n        string scheme, string host, int? port,\n        string encUser, string? encPass,\n        string path = \"\", string query = \"\", string fragment = \"\")\n    {\n        if (host.Contains(':')) // IPv6 literal - re-add brackets\n            host = $\"[{host}]\";\n        string userinfo;\n        if (encPass != null)","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/CloakHQ/CloakBrowser/blob/d6bad5de261bedf025280ace1d14e800aee13923/dotnet/src/CloakBrowser/ProxyResolver.cs#L96-L132","documentation":"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.","triggerScenarios":"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.","commonSituations":"Typos in PROXY env vars, proxy strings built by string concatenation with an extra port, or non-numeric suffixes after the host.","solutions":["Validate/normalize the proxy URL before passing it (Uri.TryCreate)","Fix the port value to a valid 1-65535 integer","Omit the port entirely to use the scheme default instead of writing garbage"],"exampleFix":"# before\nproxy = \"http://myproxy:70000\"\n\n# after\nproxy = \"http://myproxy:8080\"","handlingStrategy":"validation","validationCode":"if (!Uri.TryCreate(proxyUrl, UriKind.Absolute, out var u) || !u.IsDefaultPort && u.Port > 65535)\n    throw new ArgumentException(\"bad proxy url\");","typeGuard":null,"tryCatchPattern":"catch (FormatException e) when (e.Message.StartsWith(\"Invalid port\")) { normalize proxy string and retry }","preventionTips":["Validate proxy URLs with Uri.TryCreate before passing","Build proxy strings from components, not concatenation","Lint env-provided proxies at startup"],"tags":["proxy","configuration","url-parsing"],"backgroundTag":"invalid-url-port","analyzedSha":"d6bad5de261bedf025280ace1d14e800aee13923","analyzedAt":"2026-08-28T14:13:12.918Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}