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

Sanitize client setinfo metadata #2654

Merged
merged 5 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions src/StackExchange.Redis/ServerEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal sealed partial class ServerEndPoint : IDisposable
{
internal volatile ServerEndPoint? Primary;
internal volatile ServerEndPoint[] Replicas = Array.Empty<ServerEndPoint>();
private static readonly Regex nameSanitizer = new Regex("[^!-~]", RegexOptions.Compiled);
private static readonly Regex nameSanitizer = new Regex("[^!-~]+", RegexOptions.Compiled);

private readonly Hashtable knownScripts = new Hashtable(StringComparer.Ordinal);

Expand Down Expand Up @@ -1024,6 +1024,8 @@ private async Task HandshakeAsync(PhysicalConnection connection, ILogger? log)
// it, they should set SetClientLibrary to false, not set the name to empty string)
libName = config.Defaults.LibraryName;
}

libName = ClientInfoSanitize(libName);
if (!string.IsNullOrWhiteSpace(libName))
{
msg = Message.Create(-1, CommandFlags.FireAndForget, RedisCommand.CLIENT,
Expand All @@ -1032,7 +1034,7 @@ private async Task HandshakeAsync(PhysicalConnection connection, ILogger? log)
await WriteDirectOrQueueFireAndForgetAsync(connection, msg, ResultProcessor.DemandOK).ForAwait();
}

var version = Utils.GetLibVersion();
var version = ClientInfoSanitize(Utils.GetLibVersion());
if (!string.IsNullOrWhiteSpace(version))
{
msg = Message.Create(-1, CommandFlags.FireAndForget, RedisCommand.CLIENT,
Expand Down Expand Up @@ -1091,6 +1093,11 @@ private void SetConfig<T>(ref T field, T value, [CallerMemberName] string? calle
Multiplexer?.ReconfigureIfNeeded(EndPoint, false, caller!);
}
}
internal static string ClientInfoSanitize(string? value)
{
mgravell marked this conversation as resolved.
Show resolved Hide resolved
if (string.IsNullOrWhiteSpace(value)) return "";
mgravell marked this conversation as resolved.
Show resolved Hide resolved
return nameSanitizer.Replace(value!.Trim(), "-");
}

private void ClearMemoized()
{
Expand Down
16 changes: 16 additions & 0 deletions tests/StackExchange.Redis.Tests/Issues/Issue2653.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Xunit;

namespace StackExchange.Redis.Tests.Issues;

public class Issue2653
{
[Theory]
[InlineData(null, "")]
[InlineData("", "")]
[InlineData("abcdef", "abcdef")]
[InlineData("abc.def", "abc.def")]
[InlineData("abc d \t ef", "abc-d-ef")]
[InlineData(" abc\r\ndef\n", "abc-def")]
public void CheckLibraySanitization(string input, string expected)
=> Assert.Equal(expected, ServerEndPoint.ClientInfoSanitize(input));
}
Loading