From e6c75d2b8671ff6041c73d50bed7522ae3dcc7df Mon Sep 17 00:00:00 2001 From: FeTetra Date: Mon, 26 May 2025 22:34:11 -0400 Subject: [PATCH] Test patchwork user agent with regex instead --- .../Controllers/Login/LoginController.cs | 2 +- .../Helpers/PatchworkHelper.cs | 26 ++++------- .../Unit/Helpers/PatchworkUserAgentTests.cs | 45 +++++++++++++++++++ 3 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 ProjectLighthouse.Tests.GameApiTests/Unit/Helpers/PatchworkUserAgentTests.cs diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs index f981cf34..000b84aa 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs @@ -215,7 +215,7 @@ public class LoginController : ControllerBase if (ServerConfiguration.Instance.Authentication.RequirePatchworkUserAgent) { - if (!PatchworkHelper.UserHasValidPatchworkUserAgent(this.Request.Headers.UserAgent.ToString())) + if (!PatchworkHelper.IsValidPatchworkUserAgent(this.Request.Headers.UserAgent.ToString())) { return this.Forbid(); } diff --git a/ProjectLighthouse.Servers.GameServer/Helpers/PatchworkHelper.cs b/ProjectLighthouse.Servers.GameServer/Helpers/PatchworkHelper.cs index f0f5aaed..b981e050 100644 --- a/ProjectLighthouse.Servers.GameServer/Helpers/PatchworkHelper.cs +++ b/ProjectLighthouse.Servers.GameServer/Helpers/PatchworkHelper.cs @@ -1,31 +1,21 @@ using LBPUnion.ProjectLighthouse.Configuration; +using System.Text.RegularExpressions; namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers; public static class PatchworkHelper { - static int patchworkMajorVer = ServerConfiguration.Instance.Authentication.PatchworkMajorVersionMinimum; - static int patchworkMinorVer = ServerConfiguration.Instance.Authentication.PatchworkMinorVersionMinimum; - public static bool UserHasValidPatchworkUserAgent(string userAgent) + static int requiredMajor = ServerConfiguration.Instance.Authentication.PatchworkMajorVersionMinimum; + static int requiredMinor = ServerConfiguration.Instance.Authentication.PatchworkMinorVersionMinimum; + public static bool IsValidPatchworkUserAgent(string userAgent) { - string userAgentPrefix = "PatchworkLBP"; - char gameVersion = userAgent[userAgentPrefix.Length]; + Match result = Regex.Match(userAgent, @"^PatchworkLBP[123V] (\d{1,5})\.(\d{1,5})$"); + if (!result.Success) return false; - if (!userAgent.StartsWith(userAgentPrefix)) + if (!int.TryParse(result.Groups[1].Value, out int major) || !int.TryParse(result.Groups[2].Value, out int minor)) return false; - switch (gameVersion) { - case '1': - case '2': - case '3': - case 'V': - break; - default: - return false; - } - - string[] patchworkVer = userAgent.Split(' ')[1].Split('.'); - if (int.Parse(patchworkVer[0]) !>= patchworkMajorVer || int.Parse(patchworkVer[1]) !>= patchworkMinorVer) + if (major < requiredMajor || minor < requiredMinor) return false; return true; diff --git a/ProjectLighthouse.Tests.GameApiTests/Unit/Helpers/PatchworkUserAgentTests.cs b/ProjectLighthouse.Tests.GameApiTests/Unit/Helpers/PatchworkUserAgentTests.cs new file mode 100644 index 00000000..786082a3 --- /dev/null +++ b/ProjectLighthouse.Tests.GameApiTests/Unit/Helpers/PatchworkUserAgentTests.cs @@ -0,0 +1,45 @@ +using System; +using LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers; +using Xunit; + +namespace ProjectLighthouse.Tests.GameApiTests.Unit.Helpers; + +[Trait("Category", "Unit")] +public class PatchworkUserAgentTests +{ + [Fact] + public void CanValidatePatchworkUserAgents() + { + string[] validUserAgents = { + "PatchworkLBP1 1.0", + "PatchworkLBP2 2.0", + "PatchworkLBP3 3.0", + "PatchworkLBPV 4.0", + "PatchworkLBP1 1.5", + }; + + string[] invalidUserAgents = { + // Matching + "patchworklbp1 1.0", // Case sensitive + "ptchwrklbp1 1.0", // Misspelled + "PatchworkLBP1 1", // Missing major/minor + "PatchworkLBP1 1.000001", // Major/minor too long + + // Data + "PatchworkLBP1 0.5", // Version number too low + "PatchworkLBP1 A.0" // Int cannot be parsed + }; + + bool result; + for (int i = 0; i < validUserAgents.Length; i++) + { + result = PatchworkHelper.IsValidPatchworkUserAgent(validUserAgents[i]); + Assert.True(result, $"Valid user agent: \"{validUserAgents[i]}\" was evaluated as {result}."); + } + for (int i = 0; i < invalidUserAgents.Length; i++) + { + result = PatchworkHelper.IsValidPatchworkUserAgent(invalidUserAgents[i]); + Assert.False(result, $"Invalid user agent: \"{invalidUserAgents[i]}\" was evaluated as {result}."); + } + } +} \ No newline at end of file