{"record":{"id":"713d1fa43eaae7e8","repo":"TechnitiumSoftware/DnsServer","slug":"dns-server-config-file-format-is-invalid","errorCode":null,"errorMessage":"DNS Server config file format is invalid.","messagePattern":"DNS Server config file format is invalid\\.","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"DnsServerCore/Dns/DnsServer.cs","lineNumber":674,"sourceCode":"                if (immediately)\n                {\n                    SaveConfigFileInternal();\n                    _pendingSave = false;\n                    return;\n                }\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, bool isConfigTransfer)\n        {\n            if (Encoding.ASCII.GetString(s.ReadExactly(2)) != \"DC\") //format\n                throw new InvalidDataException(\"DNS Server config file format is invalid.\");\n\n            BinaryReader bR = new BinaryReader(s);\n\n            int version = bR.ReadByte();\n            if ((version < 1) || (version > 5))\n                throw new InvalidDataException(\"DNS Server config version not supported.\");\n\n            //general\n            string serverDomain = s.ReadShortString();\n            if (!isConfigTransfer)\n            {\n                try\n                {\n                    ServerDomain = serverDomain;\n                }\n                catch\n                {\n                    //server domain failed validation","sourceCodeStart":656,"sourceCodeEnd":692,"githubUrl":"https://github.com/TechnitiumSoftware/DnsServer/blob/d0484b6c1e7439cdc53d67d81e9c876cda2ad756/DnsServerCore/Dns/DnsServer.cs#L656-L692","documentation":"Thrown by ReadConfigFrom when the first two bytes of the configuration stream are not the ASCII magic 'DC'. The server's binary config format starts with a 2-byte signature; a mismatch means the file/stream is not a Technitium DNS Server config.","triggerScenarios":"Loading a config file that is actually JSON/text, a different format, truncated, encrypted, or transferred with wrong content-type. Triggered during startup import, config transfer between nodes, or restore from backup.","commonSituations":"User selects the wrong file in the import UI (e.g. a zone file or app config); a config saved by a newer/incompatible fork; corruption from a partial write or disk full; base64-encoding applied during transfer not decoded.","solutions":["Confirm the file is a Technitium DNS Server .scfg/.cfg export and not another format.","Re-export the config from a working server instance and retry.","If transferred over HTTP, ensure binary-safe transport (no re-encoding, correct Content-Type).","Restore from a known-good backup if the file is corrupted."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"static readonly byte[] Magic = { (byte)'D', (byte)'C' };\nbool HasDnsCfgMagic(string path)\n{\n    using var f = File.OpenRead(path);\n    Span<byte> buf = stackalloc byte[2];\n    return f.Read(buf) == 2 && buf.SequenceEqual(Magic);\n}","typeGuard":"static bool IsDnsServerConfigFile(string path)\n{\n    if (!File.Exists(path)) return false;\n    using var f = File.OpenRead(path);\n    Span<byte> b = stackalloc byte[2];\n    return f.Read(b) == 2 && b[0]=='D' && b[1]=='C';\n}","tryCatchPattern":"try { server.ReadConfigFrom(stream, isConfigTransfer: false); }\ncatch (InvalidDataException ex) when (ex.Message.Contains(\"format is invalid\")) { return BadRequest(\"Not a valid DNS Server config file\"); }","preventionTips":["Validate the 'DC' magic before importing.","Confirm the file came from a Technitium DNS Server export.","Use binary-safe transfer (no re-encoding) for config files."],"tags":["config","file-format","import","corruption"],"backgroundTag":null,"analyzedSha":"d0484b6c1e7439cdc53d67d81e9c876cda2ad756","analyzedAt":"2026-08-13T22:57:35.508Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}