DOS patch

This commit is contained in:
Tayrtahn
2026-05-01 16:56:47 -04:00
parent a12555988a
commit 0c82001a99
3 changed files with 23 additions and 15 deletions
@@ -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);
}
+5
View File
@@ -91,6 +91,11 @@ internal sealed class NetEncryption
/// <returns>Whether the operation was successful. If this fails, you likely want to drop the connection.</returns>
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));
+1 -1
View File
@@ -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.");