Skip to content

Commit

Permalink
Fix User-Agent when OSDescription has unmatched parenthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkan committed Dec 27, 2022
1 parent 743503c commit 85dc801
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
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,18 @@ 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)
{
var target = new UserAgentStringBuilder();

var result = target.WithOSDescription(osDescription).Build();

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

0 comments on commit 85dc801

Please sign in to comment.