diff --git a/src/Runner.Sdk/ActionPlugin.cs b/src/Runner.Sdk/ActionPlugin.cs index 35ffa9ab4..441f8ec02 100644 --- a/src/Runner.Sdk/ActionPlugin.cs +++ b/src/Runner.Sdk/ActionPlugin.cs @@ -73,7 +73,7 @@ namespace GitHub.Runner.Sdk { 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 3110e7e21..69cff219b 100644 --- a/src/Runner.Sdk/Util/StringUtil.cs +++ b/src/Runner.Sdk/Util/StringUtil.cs @@ -123,5 +123,12 @@ namespace GitHub.Runner.Sdk { 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 12cc5c118..b9b0f92d8 100644 --- a/src/Runner.Sdk/Util/VssUtil.cs +++ b/src/Runner.Sdk/Util/VssUtil.cs @@ -19,7 +19,7 @@ namespace GitHub.Runner.Sdk { 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 e66181874..f6eed8ccf 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 @@ namespace GitHub.Runner.Common.Tests.Util 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)); + } } }