From 5a6f560736e7bce318bdb18e19633ad13cb1953d Mon Sep 17 00:00:00 2001 From: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com> Date: Sat, 18 Oct 2025 11:07:07 -0700 Subject: [PATCH 1/2] Harden config loading against exceptions - Extra files in the `config` folder are now skipped - Exceptions deserializing now output what file failed and the exception more clearly --- .../Servers/ConfigServer.cs | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Servers/ConfigServer.cs b/Libraries/SPTarkov.Server.Core/Servers/ConfigServer.cs index 3361fad2..37e0dafb 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/ConfigServer.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/ConfigServer.cs @@ -1,4 +1,5 @@ using System.Collections.Frozen; +using System.Text.Json; using SPTarkov.DI.Annotations; using SPTarkov.Server.Core.Models.Enums; using SPTarkov.Server.Core.Models.Spt.Config; @@ -75,12 +76,27 @@ public class ConfigServer if (acceptableFileExtensions.Contains(FileUtil.GetFileExtension(file))) { var type = GetConfigTypeByFilename(file); - var deserializedContent = JsonUtil.DeserializeFromFile(file, type); + if (type == null) + { + Logger.Error($"Config file: {file} has no associated ConfigTypes entry. Skipping"); + continue; + } + + object? deserializedContent = null; + try + { + deserializedContent = JsonUtil.DeserializeFromFile(file, type); + } + catch (JsonException ex) + { + Logger.Error($"Config file: {file} failed to deserialize", ex); + throw new Exception($"Server will not run until the: {file} config error mentioned above is fixed"); + } if (deserializedContent == null) { Logger.Error($"Config file: {file} is corrupt. Use a site like: https://jsonlint.com to find the issue."); - throw new Exception($"Server will not run until the: {file} config error mentioned above is fixed"); + throw new Exception($"Server will not run until the: {file} config error mentioned below is fixed"); } _configs[$"spt-{FileUtil.StripExtension(file)}"] = deserializedContent; @@ -88,9 +104,16 @@ public class ConfigServer } } - private Type GetConfigTypeByFilename(string filename) + private Type? GetConfigTypeByFilename(string filename) { - var type = Enum.GetValues().First(en => en.GetValue().Contains(FileUtil.StripExtension(filename))); + Func filterMethod = (entry => entry.GetValue().Contains(FileUtil.StripExtension(filename))); + + if (!Enum.GetValues().Any(filterMethod)) + { + return null; + } + + var type = Enum.GetValues().First(filterMethod); return type.GetConfigType(); } } From a5fe584cee466f7e2d9988fba2ce1cc231696b5a Mon Sep 17 00:00:00 2001 From: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com> Date: Sat, 18 Oct 2025 11:13:10 -0700 Subject: [PATCH 2/2] - Pass through main exception on json deserialization failure - Fix below to above --- Libraries/SPTarkov.Server.Core/Servers/ConfigServer.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Servers/ConfigServer.cs b/Libraries/SPTarkov.Server.Core/Servers/ConfigServer.cs index 37e0dafb..669c15d3 100644 --- a/Libraries/SPTarkov.Server.Core/Servers/ConfigServer.cs +++ b/Libraries/SPTarkov.Server.Core/Servers/ConfigServer.cs @@ -89,14 +89,14 @@ public class ConfigServer } catch (JsonException ex) { - Logger.Error($"Config file: {file} failed to deserialize", ex); - throw new Exception($"Server will not run until the: {file} config error mentioned above is fixed"); + Logger.Error($"Config file: {file} failed to deserialize"); + throw new Exception($"Server will not run until the: {file} config error mentioned above is fixed", ex); } if (deserializedContent == null) { Logger.Error($"Config file: {file} is corrupt. Use a site like: https://jsonlint.com to find the issue."); - throw new Exception($"Server will not run until the: {file} config error mentioned below is fixed"); + throw new Exception($"Server will not run until the: {file} config error mentioned above is fixed"); } _configs[$"spt-{FileUtil.StripExtension(file)}"] = deserializedContent;