Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 9475763

Browse files
committed
Merge pull request #206 from allthedrones/master
Exclude password from exception and debug messages.
2 parents 1b5ff47 + c96c3f2 commit 9475763

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/ServiceStack.Redis/RedisNativeClient_Utils.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,11 @@ private bool HandleSocketException(SocketException ex)
270270
private RedisResponseException CreateResponseError(string error)
271271
{
272272
HadExceptions = true;
273+
string safeLastCommand = (Password == null) ? lastCommand : lastCommand.Replace(Password, "");
274+
273275
var throwEx = new RedisResponseException(
274276
string.Format("{0}, sPort: {1}, LastCommand: {2}",
275-
error, clientPort, lastCommand));
277+
error, clientPort, safeLastCommand));
276278
log.Error(throwEx.Message);
277279
throw throwEx;
278280
}
@@ -586,10 +588,13 @@ protected void CmdLog(byte[][] args)
586588
var sb = new StringBuilder();
587589
foreach (var arg in args)
588590
{
591+
var strArg = arg.FromUtf8Bytes();
592+
if (strArg == Password) continue;
593+
589594
if (sb.Length > 0)
590595
sb.Append(" ");
591-
592-
sb.Append(arg.FromUtf8Bytes());
596+
597+
sb.Append(strArg);
593598

594599
if (sb.Length > 100)
595600
break;
@@ -599,7 +604,7 @@ protected void CmdLog(byte[][] args)
599604
{
600605
this.lastCommand = this.lastCommand.Substring(0, 100) + "...";
601606
}
602-
607+
603608
log.Debug("S: " + this.lastCommand);
604609
}
605610

tests/ServiceStack.Redis.Tests/RedisPasswordTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace ServiceStack.Redis.Tests
44
{
5-
[Explicit("Integration")]
65
[TestFixture]
76
public class RedisPasswordTests
87
{
8+
9+
[Explicit("Integration")]
910
[Test]
1011
public void Can_connect_to_Slaves_and_Masters_with_Password()
1112
{
@@ -22,5 +23,25 @@ public void Can_connect_to_Slaves_and_Masters_with_Password()
2223
Assert.That(value, Is.EqualTo("Bar"));
2324
}
2425
}
26+
27+
[Test]
28+
[ExpectedException(typeof(ServiceStack.Redis.RedisResponseException), UserMessage = "Expected an exception after Redis AUTH command; try using a password that doesn't match.")]
29+
public void Passwords_are_not_leaked_in_exception_messages()
30+
{
31+
const string password = "yesterdayspassword";
32+
try
33+
{
34+
var factory = new PooledRedisClientManager(password + "@" + TestConfig.SingleHost); // redis will throw when using password and it's not configured
35+
using (var redis = factory.GetClient())
36+
{
37+
redis.SetEntry("Foo", "Bar");
38+
}
39+
}
40+
catch (RedisResponseException ex)
41+
{
42+
Assert.That(ex.Message, Is.Not.StringContaining(password));
43+
throw;
44+
}
45+
}
2546
}
2647
}

0 commit comments

Comments
 (0)