{"record":{"id":"348a1220792abecd","repo":"TechnitiumSoftware/DnsServer","slug":"dnsserver-allowed-zone-file-format-is-invalid","errorCode":null,"errorMessage":"DnsServer allowed zone file format is invalid.","messagePattern":"DnsServer allowed zone file format is invalid\\.","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/ZoneManagers/AllowedZoneManager.cs","lineNumber":193,"sourceCode":"            _dnsServer.LogManager.Write(\"DNS Server allowed zone file was saved: \" + allowedZoneFile);\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)) != \"AZ\") //format\n                throw new InvalidDataException(\"DnsServer allowed 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":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/ZoneManagers/AllowedZoneManager.cs#L175-L211","documentation":"Thrown by AllowedZoneManager.ReadConfigFrom when the first 2 bytes of the allowed-zones config stream are not the ASCII magic 'AZ'. The persisted allowed-zones file is a custom binary format that begins with a 2-byte format marker followed by a 1-byte version; a missing/wrong marker means the file is not a valid allowed-zones config or is corrupt.","triggerScenarios":"Loading the allowed-zones config file (e.g. on DnsServer start or config transfer) when the file is empty, truncated, written by a different component, or corrupted so its first two bytes are not 'AZ'.","commonSituations":"An empty or zero-byte config file left by a crashed write, a file from an incompatible/forked build, manual editing of the binary config, or a partial write during a power loss.","solutions":["Restore the allowed-zones config from a backup or delete it so the manager recreates an empty one on next save.","Verify the config directory is writable and on a journaled filesystem to avoid partial writes.","Upgrade/reinstall from a matching Technitium version to regenerate a valid config."],"exampleFix":"// before: corrupt config crashes startup\nserver.Start();\n\n// after: detect and reset bad config\nvar path = Path.Combine(configDir, \"allowedZones.bin\");\nusing var fs = File.OpenRead(path);\nif (Encoding.ASCII.GetString(new BinaryReader(fs).ReadBytes(2)) != \"AZ\") {\n    File.Delete(path); // let manager recreate empty config\n}\nserver.Start();","handlingStrategy":"try-catch","validationCode":"bool IsValidAllowedZonesFile(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)) == \"AZ\";\n}","typeGuard":null,"tryCatchPattern":"try { allowedZoneManager.Load(...); }\ncatch (InvalidDataException ex) when (ex.Message.Contains(\"allowed zone file format is invalid\"))\n{\n    File.Delete(configPath); // regenerate empty on next save\n    allowedZoneManager.Load(...);\n}","preventionTips":["Write config files atomically (temp + rename) to avoid truncation.","Keep backups of config files.","Validate the 'AZ' magic before trusting a config file."],"tags":["dns","csharp","config","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"}