From 97195bad5870e2ad0915ebfef1616083aacf5818 Mon Sep 17 00:00:00 2001 From: Ferenc Hammerl <31069338+fhammerl@users.noreply.github.com> Date: Wed, 8 Feb 2023 17:42:27 +0100 Subject: [PATCH] Replace '(' and ')' with '[' and '] from OS.Description so it doesn't fail User-Agent header validation (#2288) * Sanitize OS Desc for UserAgents * Only drop brackets if needed, refactoring * Add missing ')' * Readd missing brackets around '(header)' * Add comments * Use bracket solution from SDK * Rename tests --- src/Runner.Sdk/ActionPlugin.cs | 2 +- src/Runner.Sdk/Util/StringUtil.cs | 7 +++++++ src/Runner.Sdk/Util/VssUtil.cs | 2 +- src/Test/L0/Util/StringUtilL0.cs | 17 +++++++++++++++-- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Runner.Sdk/ActionPlugin.cs b/src/Runner.Sdk/ActionPlugin.cs index 35ffa9ab431..441f8ec024f 100644 --- a/src/Runner.Sdk/ActionPlugin.cs +++ b/src/Runner.Sdk/ActionPlugin.cs @@ -73,7 +73,7 @@ public VssConnection InitializeVssConnection() { var headerValues = new List(); headerValues.Add(new ProductInfoHeaderValue($"GitHubActionsRunner-Plugin", BuildConstants.RunnerPackage.Version)); - headerValues.Add(new ProductInfoHeaderValue($"({RuntimeInformation.OSDescription.Trim()})")); + headerValues.Add(new ProductInfoHeaderValue($"({StringUtil.SanitizeUserAgentHeader(RuntimeInformation.OSDescription)})")); if (VssClientHttpRequestSettings.Default.UserAgent != null && VssClientHttpRequestSettings.Default.UserAgent.Count > 0) { diff --git a/src/Runner.Sdk/Util/StringUtil.cs b/src/Runner.Sdk/Util/StringUtil.cs index 3110e7e21de..69cff219b35 100644 --- a/src/Runner.Sdk/Util/StringUtil.cs +++ b/src/Runner.Sdk/Util/StringUtil.cs @@ -123,5 +123,12 @@ public static string SubstringPrefix(string value, int count) { return value?.Substring(0, Math.Min(value.Length, count)); } + + // Fixes format violations e.g. https://github.com/actions/runner/issues/2165 + public static string SanitizeUserAgentHeader(string header) + { + return header.Replace("(", "[").Replace(")", "]").Trim(); + } + } } diff --git a/src/Runner.Sdk/Util/VssUtil.cs b/src/Runner.Sdk/Util/VssUtil.cs index 12cc5c11826..b9b0f92d8aa 100644 --- a/src/Runner.Sdk/Util/VssUtil.cs +++ b/src/Runner.Sdk/Util/VssUtil.cs @@ -19,7 +19,7 @@ public static void InitializeVssClientSettings(List addi { var headerValues = new List(); headerValues.AddRange(additionalUserAgents); - headerValues.Add(new ProductInfoHeaderValue($"({RuntimeInformation.OSDescription.Trim()})")); + headerValues.Add(new ProductInfoHeaderValue($"({StringUtil.SanitizeUserAgentHeader(RuntimeInformation.OSDescription)})")); if (VssClientHttpRequestSettings.Default.UserAgent != null && VssClientHttpRequestSettings.Default.UserAgent.Count > 0) { diff --git a/src/Test/L0/Util/StringUtilL0.cs b/src/Test/L0/Util/StringUtilL0.cs index e6618187478..f6eed8ccf47 100644 --- a/src/Test/L0/Util/StringUtilL0.cs +++ b/src/Test/L0/Util/StringUtilL0.cs @@ -1,5 +1,4 @@ -using GitHub.Runner.Common.Util; -using GitHub.Runner.Sdk; +using GitHub.Runner.Sdk; using System.Globalization; using Xunit; @@ -186,5 +185,19 @@ public void ConvertStringToBool() Assert.False(result9, $"'{undefineString3}' should convert to false."); } } + + [Theory] + [InlineData("", "")] + [InlineData("(())", "[[]]")] + [InlineData("()()", "[][]")] + [InlineData(" Liquorix kernel OS Description is poorly formatted (linux version ", "Liquorix kernel OS Description is poorly formatted [linux version")] + [InlineData("Liquorix kernel OS Description is poorly formatted (linux version", "Liquorix kernel OS Description is poorly formatted [linux version")] + [InlineData("()((.", "[][[.")] + [Trait("Level", "L0")] + [Trait("Category", "Common")] + public void SanitizeUserAgentHeader(string input, string expected) + { + Assert.Equal(expected, StringUtil.SanitizeUserAgentHeader(input)); + } } }