From a7d99f351cb82dbe0e160f1d8da9f7e36d5f5a6b Mon Sep 17 00:00:00 2001 From: Bogdan Date: Sun, 11 May 2025 00:05:29 +0300 Subject: [PATCH] Fixed: Parsing user agents without a version Fixes #2392 --- .../Http/UserAgentParserFixture.cs | 2 ++ src/NzbDrone.Common/Http/UserAgentParser.cs | 14 +++++--------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/NzbDrone.Common.Test/Http/UserAgentParserFixture.cs b/src/NzbDrone.Common.Test/Http/UserAgentParserFixture.cs index e262a1466..dfd4c18f4 100644 --- a/src/NzbDrone.Common.Test/Http/UserAgentParserFixture.cs +++ b/src/NzbDrone.Common.Test/Http/UserAgentParserFixture.cs @@ -16,6 +16,8 @@ namespace NzbDrone.Common.Test.Http [TestCase("Readarr/1.0.0.2300 (ubuntu 20.04)", "Readarr")] [TestCase("Sonarr/3.0.6.9999 (ubuntu 20.04)", "Sonarr")] [TestCase("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36", "Other")] + [TestCase("appbrr", "appbrr")] + [TestCase(" appbrr ", "appbrr")] public void should_parse_user_agent(string userAgent, string parsedAgent) { UserAgentParser.ParseSource(userAgent).Should().Be(parsedAgent); diff --git a/src/NzbDrone.Common/Http/UserAgentParser.cs b/src/NzbDrone.Common/Http/UserAgentParser.cs index a73b627b8..8cc16e926 100644 --- a/src/NzbDrone.Common/Http/UserAgentParser.cs +++ b/src/NzbDrone.Common/Http/UserAgentParser.cs @@ -1,15 +1,16 @@ +using System; using System.Text.RegularExpressions; namespace NzbDrone.Common.Http { public static class UserAgentParser { - private static readonly Regex AppSourceRegex = new Regex(@"(?[a-z0-9]*)\/.*(?:\(.*\))?", + private static readonly Regex AppSourceRegex = new (@"^(?[a-z0-9]+)(?:\/.+(?:\(.*\))?|$)", RegexOptions.IgnoreCase | RegexOptions.Compiled); public static string SimplifyUserAgent(string userAgent) { - if (userAgent == null || userAgent.StartsWith("Mozilla/5.0")) + if (userAgent == null || userAgent.StartsWith("Mozilla/5.0", StringComparison.Ordinal)) { return null; } @@ -19,14 +20,9 @@ namespace NzbDrone.Common.Http public static string ParseSource(string userAgent) { - var match = AppSourceRegex.Match(SimplifyUserAgent(userAgent) ?? string.Empty); + var match = AppSourceRegex.Match(SimplifyUserAgent(userAgent?.Trim()) ?? string.Empty); - if (match.Groups["agent"].Success) - { - return match.Groups["agent"].Value; - } - - return "Other"; + return match.Groups["agent"].Success ? match.Groups["agent"].Value : "Other"; } } }