{"record":{"id":"95ff3b4609d5f86d","repo":"TechnitiumSoftware/DnsServer","slug":"dnsserver-zone-file-format-is-invalid","errorCode":null,"errorMessage":"DnsServer zone file format is invalid.","messagePattern":"DnsServer zone file format is invalid\\.","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/ZoneManagers/AuthZoneManager.cs","lineNumber":434,"sourceCode":"                        uint minExpiryTtl = GetMinExpiryTtlFor(records);\n                        if (minExpiryTtl > 0u)\n                            apexZone.StartRecordExpiryTimer(minExpiryTtl);\n                    }\n                    break;\n\n                case AuthZoneType.SecondaryCatalog:\n                    {\n                        (apexZone as SecondaryZone).TriggerRefresh();\n                        (apexZone as SecondaryCatalogZone).BuildMembersIndex();\n                    }\n                    break;\n            }\n        }\n\n        public AuthZoneInfo LoadZoneFrom(Stream s, DateTime lastModified)\n        {\n            if (Encoding.ASCII.GetString(s.ReadExactly(2)) != \"DZ\")\n                throw new InvalidDataException(\"DnsServer zone file format is invalid.\");\n\n            BinaryReader bR = new BinaryReader(s);\n\n            switch (bR.ReadByte())\n            {\n                case 2:\n                    {\n                        DnsResourceRecord[] records = new DnsResourceRecord[bR.ReadInt32()];\n                        if (records.Length == 0)\n                            throw new InvalidDataException(\"Zone does not contain SOA record.\");\n\n                        DnsResourceRecord soaRecord = null;\n\n                        for (int i = 0; i < records.Length; i++)\n                        {\n                            records[i] = new DnsResourceRecord(s);\n\n                            if (records[i].Type == DnsResourceRecordType.SOA)","sourceCodeStart":416,"sourceCodeEnd":452,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/ZoneManagers/AuthZoneManager.cs#L416-L452","documentation":"Thrown by AuthZoneManager.LoadZoneFrom when the first 2 bytes of a zone file stream are not the ASCII magic 'DZ'. Technitium persists each authoritative zone in a custom binary format starting with 'DZ' then a 1-byte version; a wrong marker means the stream is not a Technitium zone file or is corrupt/truncated.","triggerScenarios":"Loading a zone from a stream (zone import, restore, or startup load) whose first two bytes are not 'DZ' — e.g. a plain RFC zone text file, a different binary format, or a zero-byte/corrupt file.","commonSituations":"Importing an RFC-format text zone file through a binary loader, a truncated zone file from a crash mid-write, restoring a backup that was incompletely copied, or pointing the loader at a non-zone file.","solutions":["Import RFC text zone files via the text/import code path, not LoadZoneFrom (which expects the binary 'DZ' format).","Restore the zone file from a known-good backup or delete it and re-create the zone.","Ensure zone files are written atomically (write to temp + rename) so partial files never exist."],"exampleFix":"// before\nusing var fs = File.OpenRead(path);\nzoneManager.LoadZoneFrom(fs, lastModified); // throws if not 'DZ'\n\n// after\nusing var fs = File.OpenRead(path);\nvar magic = Encoding.ASCII.GetString(new BinaryReader(fs).ReadBytes(2));\nfs.Position = 0;\nif (magic != \"DZ\") throw new InvalidDataException(\"Not a Technitium binary zone file: \" + path);\nzoneManager.LoadZoneFrom(fs, lastModified);","handlingStrategy":"validation","validationCode":"bool IsTechnitiumZoneFile(string path)\n{\n    using var fs = File.OpenRead(path);\n    if (fs.Length < 3) return false;\n    using var br = new BinaryReader(fs);\n    return Encoding.ASCII.GetString(br.ReadBytes(2)) == \"DZ\";\n}","typeGuard":null,"tryCatchPattern":"try { zoneManager.LoadZoneFrom(fs, lastModified); }\ncatch (InvalidDataException ex) when (ex.Message.Contains(\"zone file format is invalid\"))\n{\n    // not a binary zone — fall back to text import, or restore from backup\n}","preventionTips":["Use the text-import path for RFC-format zone files, not LoadZoneFrom.","Validate the 'DZ' magic before loading.","Write zone files atomically (temp + rename) to prevent partial files."],"tags":["dns","csharp","zone-file","binary-format","technitium","data-corruption"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}