Skip to content

Commit 04031cf

Browse files
committed
fix pub/sub command check
1 parent 2f49a03 commit 04031cf

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

toys/StackExchange.Redis.Server/RedisServer.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,16 @@ public override TypedRedisValue Execute(RedisClient client, in RedisRequest requ
158158
var pw = Password;
159159
if (pw.Length != 0 & !client.IsAuthenticated)
160160
{
161-
if (!Literals.IsAuthCommand(in request)) return TypedRedisValue.Error("NOAUTH Authentication required.");
161+
if (!Literals.IsAuthCommand(in request))
162+
return TypedRedisValue.Error("NOAUTH Authentication required.");
162163
}
163-
else if (client.Protocol is RedisProtocol.Resp2 && client.IsSubscriber && !Literals.IsPubSubCommand(in request))
164+
else if (client.Protocol is RedisProtocol.Resp2 && client.IsSubscriber &&
165+
!Literals.IsPubSubCommand(in request, out var cmd))
164166
{
165-
return TypedRedisValue.Error("ERR only (P|S)SUBSCRIBE / (P|S)UNSUBSCRIBE / PING / QUIT allowed in this context");
167+
return TypedRedisValue.Error(
168+
$"ERR only (P|S)SUBSCRIBE / (P|S)UNSUBSCRIBE / PING / QUIT allowed in this context (got: '{cmd}')");
166169
}
170+
167171
return base.Execute(client, request);
168172
}
169173

@@ -186,9 +190,19 @@ public static bool IsAuthCommand(in RedisRequest request) =>
186190
request.Count != 0 && request.TryGetCommandBytes(0, out var command)
187191
&& (command.Equals(AUTH) || command.Equals(HELLO));
188192

189-
public static bool IsPubSubCommand(in RedisRequest request) =>
190-
request.Count != 0 && request.TryGetCommandBytes(0, out var command)
191-
&& (command.Equals(AUTH) || command.Equals(HELLO));
193+
public static bool IsPubSubCommand(in RedisRequest request, out CommandBytes command)
194+
{
195+
if (request.Count == 0 || !request.TryGetCommandBytes(0, out command))
196+
{
197+
command = default;
198+
return false;
199+
}
200+
201+
return command.Equals(SUBSCRIBE) || command.Equals(UNSUBSCRIBE)
202+
|| command.Equals(SSUBSCRIBE) || command.Equals(SUNSUBSCRIBE)
203+
|| command.Equals(PSUBSCRIBE) || command.Equals(PUNUBSCRIBE)
204+
|| command.Equals(PING) || command.Equals(QUIT);
205+
}
192206
}
193207

194208
[RedisCommand(2)]

0 commit comments

Comments
 (0)