Skip to content

Commit 9e6e478

Browse files
prvykbadrishcTalZaccai
authored
Local connection check was accidentally inverted. Add test to ensure this does not repeat. (#1290)
Co-authored-by: Badrish Chandramouli <[email protected]> Co-authored-by: Tal Zaccai <[email protected]>
1 parent aac4f5a commit 9e6e478

File tree

8 files changed

+73
-13
lines changed

8 files changed

+73
-13
lines changed

libs/server/Resp/RespServerSession.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ internal bool CanRunDebug()
413413
return
414414
(enableDebugCommand == ConnectionProtectionOption.Yes) ||
415415
((enableDebugCommand == ConnectionProtectionOption.Local) &&
416-
!networkSender.IsLocalConnection());
416+
networkSender.IsLocalConnection());
417417
}
418418

419419
internal bool CanRunModule()
@@ -423,7 +423,7 @@ internal bool CanRunModule()
423423
return
424424
(enableModuleCommand == ConnectionProtectionOption.Yes) ||
425425
((enableModuleCommand == ConnectionProtectionOption.Local) &&
426-
!networkSender.IsLocalConnection());
426+
networkSender.IsLocalConnection());
427427
}
428428

429429
public override int TryConsumeMessages(byte* reqBuffer, int bytesReceived)

test/Garnet.test/GarnetJSON/JsonCommandsTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void Setup()
2828
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
2929
binPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
3030
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, lowMemory: true,
31-
enableModuleCommand: true,
31+
enableModuleCommand: Garnet.server.Auth.Settings.ConnectionProtectionOption.Yes,
3232
extensionAllowUnsignedAssemblies: true,
3333
extensionBinPaths: [binPath]);
3434
server.Start();

test/Garnet.test/GarnetServerConfigTests.cs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,63 @@ public void UnixSocketPermission_InvalidPermissionFails()
860860
ClassicAssert.IsFalse(parseSuccessful);
861861
}
862862

863+
[Test]
864+
[TestCase(ConnectionProtectionOption.No)]
865+
[TestCase(ConnectionProtectionOption.Local)]
866+
[TestCase(ConnectionProtectionOption.Yes)]
867+
public async Task ConnectionProtectionTest(ConnectionProtectionOption connectionProtectionOption)
868+
{
869+
List<IPAddress> addresses = [IPAddress.IPv6Loopback, IPAddress.Loopback];
870+
871+
var hostname = TestUtils.GetHostName();
872+
873+
var address = Dns.GetHostAddresses(hostname).Where(x => !IPAddress.IsLoopback(x)).FirstOrDefault();
874+
if (address == default)
875+
{
876+
if (connectionProtectionOption == ConnectionProtectionOption.Local)
877+
Assert.Ignore("No nonloopback address");
878+
}
879+
else
880+
{
881+
addresses.Add(address);
882+
}
883+
884+
var endpoints = addresses.Select(address => new IPEndPoint(address, TestUtils.TestPort)).ToArray();
885+
var server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, endpoints: endpoints,
886+
enableDebugCommand: connectionProtectionOption);
887+
server.Start();
888+
889+
foreach (var endpoint in endpoints)
890+
{
891+
var shouldfail = connectionProtectionOption == ConnectionProtectionOption.No ||
892+
(!IPAddress.IsLoopback(endpoint.Address) && connectionProtectionOption == ConnectionProtectionOption.Local);
893+
var client = TestUtils.GetGarnetClientSession(endPoint: endpoint);
894+
client.Connect();
895+
896+
try
897+
{
898+
var result = await client.ExecuteAsync("DEBUG", "LOG", "Loopback test");
899+
if (shouldfail)
900+
Assert.Fail("Connection protection should have not allowed the command to run");
901+
else
902+
ClassicAssert.AreEqual("OK", result);
903+
}
904+
catch (Exception ex)
905+
{
906+
if (shouldfail)
907+
ClassicAssert.AreEqual("ERR", ex.Message[0..3]);
908+
else
909+
Assert.Fail("Connection protection should have allowed command from this address");
910+
}
911+
finally
912+
{
913+
client.Dispose();
914+
}
915+
}
916+
917+
server.Dispose();
918+
}
919+
863920
[Test]
864921
public async Task MultiTcpSocketTest()
865922
{
@@ -868,7 +925,7 @@ public async Task MultiTcpSocketTest()
868925
var addresses = Dns.GetHostAddresses(hostname);
869926
addresses = [.. addresses, IPAddress.IPv6Loopback, IPAddress.Loopback];
870927

871-
var endpoints = addresses.Select(address => new IPEndPoint(address, TestUtils.TestPort)).ToArray();
928+
var endpoints = addresses.Distinct().Select(address => new IPEndPoint(address, TestUtils.TestPort)).ToArray();
872929
var server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, endpoints: endpoints);
873930
server.Start();
874931

test/Garnet.test/Resp/ACL/RespCommandTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public void Setup()
3333
{
3434
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
3535
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, defaultPassword: DefaultPassword,
36-
useAcl: true, enableLua: true, enableModuleCommand: true);
36+
useAcl: true, enableLua: true,
37+
enableModuleCommand: Garnet.server.Auth.Settings.ConnectionProtectionOption.Yes);
3738

3839
// Register custom commands so we can test ACL'ing them
3940
ClassicAssert.IsTrue(TestUtils.TryGetCustomCommandsInfo(out respCustomCommandsInfo));

test/Garnet.test/RespCommandTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ .. respCommandsInfo.Values.Where(ci => ci.IsInternal).Select(ci => ci.Command)
7777
];
7878

7979
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, disablePubSub: true,
80-
enableModuleCommand: true, extensionBinPaths: [extTestDir]);
80+
enableModuleCommand: Garnet.server.Auth.Settings.ConnectionProtectionOption.Yes,
81+
extensionBinPaths: [extTestDir]);
8182
server.Start();
8283
}
8384

test/Garnet.test/RespCustomCommandTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public void Setup()
238238
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
239239
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir,
240240
disablePubSub: true,
241-
enableModuleCommand: true,
241+
enableModuleCommand: Garnet.server.Auth.Settings.ConnectionProtectionOption.Yes,
242242
extensionBinPaths: [_extTestDir1, _extTestDir2],
243243
extensionAllowUnsignedAssemblies: true);
244244
server.Start();

test/Garnet.test/RespModuleTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void Setup()
2525
TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
2626
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir,
2727
disablePubSub: true,
28-
enableModuleCommand: true,
28+
enableModuleCommand: Garnet.server.Auth.Settings.ConnectionProtectionOption.Yes,
2929
extensionBinPaths: [testModuleDir, binPath],
3030
extensionAllowUnsignedAssemblies: true);
3131
server.Start();
@@ -386,7 +386,7 @@ public void TestNoAllowedPathsForModuleLoading()
386386
using var server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir,
387387
disableObjects: true,
388388
disablePubSub: true,
389-
enableModuleCommand: true,
389+
enableModuleCommand: Garnet.server.Auth.Settings.ConnectionProtectionOption.Yes,
390390
extensionBinPaths: null,
391391
extensionAllowUnsignedAssemblies: true);
392392
server.Start();
@@ -415,7 +415,7 @@ public void TestModuleCommandNotEnabled()
415415
using var server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir,
416416
disableObjects: true,
417417
disablePubSub: true,
418-
enableModuleCommand: false,
418+
enableModuleCommand: Garnet.server.Auth.Settings.ConnectionProtectionOption.No,
419419
extensionBinPaths: [testModuleDir, binPath],
420420
extensionAllowUnsignedAssemblies: true);
421421
server.Start();

test/Garnet.test/TestUtils.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ public static GarnetServer CreateGarnetServer(
248248
bool getSG = false,
249249
int indexResizeFrequencySecs = 60,
250250
IAuthenticationSettings authenticationSettings = null,
251-
bool enableModuleCommand = false,
251+
ConnectionProtectionOption enableDebugCommand = ConnectionProtectionOption.Yes,
252+
ConnectionProtectionOption enableModuleCommand = ConnectionProtectionOption.No,
252253
bool enableLua = false,
253254
bool enableReadCache = false,
254255
bool enableObjectStoreReadCache = false,
@@ -345,8 +346,8 @@ public static GarnetServer CreateGarnetServer(
345346
ThreadPoolMinThreads = threadPoolMinThreads,
346347
LoadModuleCS = loadModulePaths,
347348
EnableCluster = enableCluster,
348-
EnableDebugCommand = ConnectionProtectionOption.Yes,
349-
EnableModuleCommand = enableModuleCommand ? ConnectionProtectionOption.Yes : ConnectionProtectionOption.No,
349+
EnableDebugCommand = enableDebugCommand,
350+
EnableModuleCommand = enableModuleCommand,
350351
EnableReadCache = enableReadCache,
351352
EnableObjectStoreReadCache = enableObjectStoreReadCache,
352353
ReplicationOffsetMaxLag = asyncReplay ? -1 : 0,

0 commit comments

Comments
 (0)