{"record":{"id":"bf79eb4122d724b8","repo":"TechnitiumSoftware/DnsServer","slug":"dnsserver-blocked-zone-file-format-is-invalid","errorCode":null,"errorMessage":"DnsServer blocked zone file format is invalid.","messagePattern":"DnsServer blocked zone file format is invalid\\.","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/ZoneManagers/BlockedZoneManager.cs","lineNumber":209,"sourceCode":"            _dnsServer.LogManager.Write(\"DNS Server blocked zone file was saved: \" + blockedZoneFile);\n        }\n\n        public void SaveZoneFile()\n        {\n            lock (_saveLock)\n            {\n                if (_pendingSave)\n                    return;\n\n                _pendingSave = true;\n                _saveTimer.Change(SAVE_TIMER_INITIAL_INTERVAL, Timeout.Infinite);\n            }\n        }\n\n        private void ReadConfigFrom(Stream s)\n        {\n            if (Encoding.ASCII.GetString(s.ReadExactly(2)) != \"BZ\") //format\n                throw new InvalidDataException(\"DnsServer blocked zone file format is invalid.\");\n\n            BinaryReader bR = new BinaryReader(s);\n\n            byte version = bR.ReadByte();\n            switch (version)\n            {\n                case 1:\n                    int length = bR.ReadInt32();\n                    int i = 0;\n\n                    AuthZoneManager zoneManager = new AuthZoneManager(_dnsServer);\n\n                    zoneManager.LoadSpecialPrimaryZones(delegate ()\n                    {\n                        if (i++ < length)\n                            return s.ReadShortString();\n\n                        return null;","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/ZoneManagers/BlockedZoneManager.cs#L191-L227","documentation":"Thrown when loading a blocked-zone config file whose first 2 bytes are not the ASCII magic 'BZ'. ReadConfigFrom checks the header before reading the version byte and the zone count; any other content (wrong file, truncation, corruption) is rejected as an invalid blocked-zone format.","triggerScenarios":"A non-blocked-zone file placed at the blocked-zone config path; truncated/empty file from a crash during write; a 'BL' block-list file mistakenly used here.","commonSituations":"Restoring backups into the wrong path; partial write left by a power loss; file copied between features by mistake.","solutions":["Move the bad blocked-zone config aside so the server recreates a valid 'BZ' file on next save.","Restore the file from a known-good backup if the previously blocked zones matter.","Verify the correct feature's .bin is in the expected location."],"exampleFix":"// before: blockedZone.bin header != 'BZ' -> blocked-zone load fails\n// after: remove to regenerate\n//   mv config/blockedZone.bin config/blockedZone.bin.bak   (then restart)","handlingStrategy":"validation","validationCode":"static bool IsValidBlockedZoneFile(string path)\n{\n    using var fs = File.OpenRead(path);\n    Span<byte> hdr = stackalloc byte[2];\n    return fs.Read(hdr) == 2 && hdr[0] == (byte)'B' && hdr[1] == (byte)'Z';\n}","typeGuard":null,"tryCatchPattern":"try { blockedZoneManager.Load(); }\ncatch (InvalidDataException ex) when (ex.Message.Contains(\"blocked zone file format\"))\n{ File.Move(blockedPath, blockedPath + \".bad\"); blockedZoneManager.Load(); }","preventionTips":["Verify the 'BZ' header before loading.","Keep blocked-zone and block-list files in clearly named paths to avoid swaps.","Restore backups feature-by-feature, not by dumping all .bin files together.","Shut the server down cleanly to prevent truncated writes."],"tags":["dns","blocked-zone","config-file","serialization","startup"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}