diff --git a/Robust.Shared.Tests/Networking/NetEncryptionDoSTest.cs b/Robust.Shared.Tests/Networking/NetEncryptionDoSTest.cs index 6c341bb869..27de8e789f 100644 --- a/Robust.Shared.Tests/Networking/NetEncryptionDoSTest.cs +++ b/Robust.Shared.Tests/Networking/NetEncryptionDoSTest.cs @@ -22,9 +22,8 @@ public sealed class NetEncryptionDoSTest var packet = Receive(server); - Assert.That(packet, Is.Not.Null); - Assert.That(packet.ReadVariableUInt64(), Is.EqualTo(Magic)); + server.Shutdown(null); } [Test] @@ -44,9 +43,8 @@ public sealed class NetEncryptionDoSTest var packet = Receive(server); - Assert.That(packet, Is.Not.Null); - Assert.That(serverEnc.TryDecrypt(packet), Is.True); + server.Shutdown(null); } [Test] @@ -64,25 +62,28 @@ public sealed class NetEncryptionDoSTest client.SendMessage(message, NetDeliveryMethod.ReliableOrdered); - var packet = server.WaitMessage(1000); - - Assert.That(packet, Is.Not.Null); + var packet = Receive(server); Assert.That(serverEnc.TryDecrypt(packet), Is.False); + server.Shutdown(null); } - private static byte[][] _badMessages = + private static int[] _badMessages = [ - [1, 1, 1, 1, 1], - [1, 2], - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,] + 5, + 1, + 4, + 16, + 1024, ]; [Test] [Description("Attempt to decrypt a packet that is bogus, ensuring it doesn't throw.")] [TestCaseSource(nameof(_badMessages))] - public void BadMessageDoesNotThrow(byte[] badMessage) + public void BadMessageDoesNotThrow(int badMessageLength) { + var badMessage = new byte[badMessageLength]; + System.Random.Shared.NextBytes(badMessage); var (_, serverEnc) = MakeEncryptionPair(disjointKey: true); var (client, server) = MakeConnectionPair(); @@ -94,11 +95,13 @@ public sealed class NetEncryptionDoSTest client.SendMessage(message, NetDeliveryMethod.ReliableOrdered); - var packet = server.WaitMessage(1000); + var packet = Receive(server); - Assert.That(packet, Is.Not.Null); + Assert.That(packet.LengthBytes, Is.EqualTo(badMessageLength)); Assert.That(serverEnc.TryDecrypt(packet), Is.False); + + server.Shutdown(null); } diff --git a/Robust.Shared/Network/NetEncryption.cs b/Robust.Shared/Network/NetEncryption.cs index be09abe52b..625df53cf2 100644 --- a/Robust.Shared/Network/NetEncryption.cs +++ b/Robust.Shared/Network/NetEncryption.cs @@ -91,6 +91,11 @@ internal sealed class NetEncryption /// Whether the operation was successful. If this fails, you likely want to drop the connection. public unsafe bool TryDecrypt(NetIncomingMessage message) { + // Minimum possible size a message can be is the nonce + 16 bytes of message. + // So we immediately bail on anything smaller. + if (message.LengthBytes < sizeof(ulong) + CryptoAeadXChaCha20Poly1305Ietf.AddBytes) + return false; + var nonce = message.ReadUInt64(); var cipherText = message.Data.AsSpan(sizeof(ulong), message.LengthBytes - sizeof(ulong)); diff --git a/Robust.Shared/Network/NetManager.cs b/Robust.Shared/Network/NetManager.cs index 27a7081fe6..7fcc42afde 100644 --- a/Robust.Shared/Network/NetManager.cs +++ b/Robust.Shared/Network/NetManager.cs @@ -961,7 +961,7 @@ namespace Robust.Shared.Network } // Attempt to decrypt the message, only logging if we fail to decrypt and we actually have encryption. - if ((!channel.Encryption?.TryDecrypt(msg)) ?? true) + if ((!channel.Encryption?.TryDecrypt(msg)) ?? false) { if (_logPacketIssues) _logger.Debug($"{msg.SenderConnection.RemoteEndPoint}: Got a packet that fails to decrypt.");