{"record":{"id":"a29f6b9621275ca1","repo":"TechnitiumSoftware/DnsServer","slug":"invalid-domain-name-domain-invalid-character","errorCode":null,"errorMessage":"Invalid domain name [{domain}]: invalid character [{labelChar}] was found.","messagePattern":"Invalid domain name \\[(.+?)\\]: invalid character \\[(.+?)\\] was found\\.","errorType":"validation","errorClass":"InvalidDomainNameException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/Trees/DomainTree.cs","lineNumber":166,"sourceCode":"                    if (throwException)\n                        throw new InvalidDomainNameException(\"Invalid domain name [\" + domain + \"]: label length cannot exceed 63 bytes.\");\n\n                    return null;\n                }\n\n                if ((labelLength == 1) && (domain[labelStart + 1] == '*')) //[*]\n                {\n                    key[keyOffset++] = 1;\n                }\n                else\n                {\n                    for (i = labelStart + 1; i <= labelEnd; i++)\n                    {\n                        labelChar = domain[i];\n                        if (labelChar >= _keyMap.Length)\n                        {\n                            if (throwException)\n                                throw new InvalidDomainNameException(\"Invalid domain name [\" + domain + \"]: invalid character [\" + labelChar + \"] was found.\");\n\n                            return null;\n                        }\n\n                        labelKeyCode = _keyMap[labelChar];\n                        if (labelKeyCode == 0xff)\n                        {\n                            if (throwException)\n                                throw new InvalidDomainNameException(\"Invalid domain name [\" + domain + \"]: invalid character [\" + labelChar + \"] was found.\");\n\n                            return null;\n                        }\n\n                        key[keyOffset++] = labelKeyCode;\n                    }\n                }\n\n                key[keyOffset++] = 0; //[.]","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/Trees/DomainTree.cs#L148-L184","documentation":"Thrown by DomainTree.ConvertToByteKey when a character in a label has a code point value greater than or equal to the size of the internal _keyMap lookup table. The library's character map only covers a limited ASCII-ish range, so any character whose int value falls outside that range (e.g. high Unicode codepoints or characters beyond the table) cannot be mapped to a key byte. Only thrown when throwException is true.","triggerScenarios":"Passing a domain name containing a character whose numeric value is >= _keyMap.Length to a DomainTree-derived tree operation.","commonSituations":"Non-ASCII / Unicode hostnames not converted to Punycode (IDNA) before being passed to the tree; a stray emoji or accented character in a record owner name; binary garbage in a name.","solutions":["Encode internationalized domain names with IdnMapping.GetAscii (Punycode) before passing them to the tree.","Sanitize names to the allowed DNS character set [a-zA-Z0-9-._*] at ingestion.","Probe with throwException=false to obtain null for unmappable characters."],"exampleFix":"// before\n_tree.GetOrAdd(\"café.example.com\", value);\n\n// after\nvar idn = new System.Globalization.IdnMapping();\nstring ascii = idn.GetAscii(domain);\n_tree.GetOrAdd(ascii, value);","handlingStrategy":"validation","validationCode":"string ToAsciiDomain(string d)\n{\n    var idn = new System.Globalization.IdnMapping();\n    try { return idn.GetAscii(d); } catch { return null; }\n}","typeGuard":"static bool IsAsciiDomain(string d) =>\n    d.Length > 0 && d.All(c => (int)c < KEYMAP_SIZE);","tryCatchPattern":null,"preventionTips":["Convert Unicode/IDN names to Punycode (IdnMapping.GetAscii) before tree operations.","Reject names with characters outside the DNS character set at ingestion.","Probe with throwException=false to detect unmappable characters silently."],"tags":["dns","csharp","validation","idna","domain-name","encoding"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}