Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix User-Agent when OSDescription has unmatched parenthesis #4991

Merged
merged 2 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/NuGet.Core/NuGet.Protocol/UserAgentStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ public UserAgentStringBuilder(string clientName)

public UserAgentStringBuilder WithOSDescription(string osInfo)
{
_osInfo = osInfo;
#if NETCOREAPP2_0_OR_GREATER
_osInfo = osInfo.Replace("(", @"\(", StringComparison.Ordinal).Replace(")", @"\)", StringComparison.Ordinal);
#else
_osInfo = osInfo.Replace("(", @"\(").Replace(")", @"\)");
#endif

return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Net.Http;
using NuGet.Protocol.Core.Types;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -96,5 +97,21 @@ public void UsesComputedNuGetClientVersion()
Assert.True(userAgentString3.Contains(builder.NuGetClientVersion));
Assert.True(userAgentString4.Contains(builder.NuGetClientVersion));
}

[Theory]
[InlineData("Custom Kernel (123")]
[InlineData("Custom Kernel 123)")]
public void Build_OsDescriptionWithUnmatchedParenthesis_IsValid(string osDescription)
{
// Arrange
UserAgentStringBuilder target = new();

// Act
string result = target.WithOSDescription(osDescription).Build();

// Assert
HttpRequestMessage httpRequest = new();
httpRequest.Headers.Add("User-Agent", result);
}
}
}