{"record":{"id":"1c73158e800ae4a4","repo":"netchx/netch","slug":"dns-format-invalid","errorCode":null,"errorMessage":"DNS format invalid","messagePattern":"DNS format invalid","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Netch/Utils/NetworkInterfaceUtils.cs","lineNumber":77,"sourceCode":"    public static int GetIndex(this NetworkInterface ni)\n    {\n        var ipProperties = ni.GetIPProperties();\n        if (ni.Supports(NetworkInterfaceComponent.IPv4))\n            return ipProperties.GetIPv4Properties().Index;\n\n        if (ni.Supports(NetworkInterfaceComponent.IPv6))\n            return ipProperties.GetIPv6Properties().Index;\n\n        throw new Exception();\n    }\n\n    public static void SetDns(this NetworkInterface ni, string primaryDns, string? secondDns = null)\n    {\n        void VerifyDns(ref string s)\n        {\n            s = s.Trim();\n            if (primaryDns.IsNullOrEmpty())\n                throw new ArgumentException(\"DNS format invalid\", nameof(primaryDns));\n        }\n\n        VerifyDns(ref primaryDns);\n        if (secondDns != null)\n            VerifyDns(ref primaryDns);\n\n        var wmi = new ManagementClass(\"Win32_NetworkAdapterConfiguration\");\n        var mos = wmi.GetInstances().Cast<ManagementObject>();\n\n        var mo = mos.First(m => m[\"Description\"].ToString() == ni.Description);\n\n        var dns = new[] { primaryDns };\n        if (secondDns != null)\n            dns = dns.Append(secondDns).ToArray();\n\n        var inPar = mo.GetMethodParameters(\"SetDNSServerSearchOrder\");\n        inPar[\"DNSServerSearchOrder\"] = dns;\n","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/netchx/netch/blob/9d99eb1c5a2acbf2a34f2600f94242601019a300/Netch/Utils/NetworkInterfaceUtils.cs#L59-L95","documentation":"SetDns validates the primary DNS via a local VerifyDns that trims and rejects null/empty/whitespace by throwing ArgumentException('DNS format invalid'). Important bug: the secondDns branch on line 82 calls VerifyDns(ref primaryDns) again instead of verifying secondDns, so an invalid secondary DNS is never caught here and only the primary is ever validated. Also, VerifyDns does not actually parse the IP format - it checks non-emptiness only.","triggerScenarios":"Calling SetDns with primaryDns = null, empty, or whitespace. It does NOT fire for an invalid secondary DNS due to the copy-paste bug; it also does not fire for a syntactically invalid-but-non-empty IP string.","commonSituations":"User leaves the primary DNS field blank; a config migration nulls the DNS field; a code path setting DNS from an unset setting string.","solutions":["Pass a valid DNS IP string (e.g. '8.8.8.8') or skip the SetDns call entirely when blank.","Fix the bug: change the secondDns branch to VerifyDns(ref secondDns) and make VerifyDns validate with IPAddress.Parse/TryParse.","Validate the IP format with IPAddress.TryParse rather than only checking emptiness.","Skip (no-op) when the value is blank instead of throwing, so the existing DNS is preserved."],"exampleFix":"// before\nvoid VerifyDns(ref string s)\n{\n    s = s.Trim();\n    if (primaryDns.IsNullOrEmpty())\n        throw new ArgumentException(\"DNS format invalid\", nameof(primaryDns));\n}\nVerifyDns(ref primaryDns);\nif (secondDns != null)\n    VerifyDns(ref primaryDns); // BUG: should verify secondDns\n// after - verify each value and its IP format\nvoid VerifyDns(ref string s, string name)\n{\n    if (string.IsNullOrWhiteSpace(s) || !IPAddress.TryParse(s.Trim(), out _))\n        throw new ArgumentException(\"DNS format invalid\", name);\n    s = s.Trim();\n}\nVerifyDns(ref primaryDns, nameof(primaryDns));\nif (secondDns != null)\n    VerifyDns(ref secondDns, nameof(secondDns));","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(primaryDns) || !IPAddress.TryParse(primaryDns.Trim(), out _))\n    return; // skip setting DNS, or surface a UI error\nni.SetDns(primaryDns.Trim(), secondDns?.Trim());","typeGuard":"bool IsValidDns(string? s) => !string.IsNullOrWhiteSpace(s) && IPAddress.TryParse(s!.Trim(), out _);","tryCatchPattern":"try { ni.SetDns(primaryDns, secondDns); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"DNS format invalid\"))\n{\n    Log.Warning(\"Invalid DNS config, skipping: {Primary}\", primaryDns);\n}","preventionTips":["Validate DNS strings with IPAddress.TryParse before calling SetDns.","Skip the call (and keep the existing DNS) when the field is blank rather than throwing.","Fix the secondDns verification bug so both servers are validated.","Make the DNS setter idempotent and tolerant of blank values."],"tags":["dns","network","config","validation","bug"],"backgroundTag":null,"analyzedSha":"9d99eb1c5a2acbf2a34f2600f94242601019a300","analyzedAt":"2026-08-13T14:12:19.105Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}