Skip to content

Commit a8f723f

Browse files
authored
Catch another couple sources of netencryption exceptions. (space-wizards#6498)
* Catch another couple sources of netencryption exceptions. * Clean up tests a bit. * prevent nonsense.
1 parent 3136118 commit a8f723f

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

Robust.Shared.Tests/Networking/NetEncryptionDoSTest.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ public void ConnectionWorks()
2222

2323
var packet = Receive(server);
2424

25-
Assert.That(packet, Is.Not.Null);
26-
2725
Assert.That(packet.ReadVariableUInt64(), Is.EqualTo(Magic));
26+
server.Shutdown(null);
2827
}
2928

3029
[Test]
@@ -44,9 +43,8 @@ public void EncryptionWorks()
4443

4544
var packet = Receive(server);
4645

47-
Assert.That(packet, Is.Not.Null);
48-
4946
Assert.That(serverEnc.TryDecrypt(packet), Is.True);
47+
server.Shutdown(null);
5048
}
5149

5250
[Test]
@@ -64,25 +62,28 @@ public void WrongKeyFailureDoesNotThrow()
6462

6563
client.SendMessage(message, NetDeliveryMethod.ReliableOrdered);
6664

67-
var packet = server.WaitMessage(1000);
68-
69-
Assert.That(packet, Is.Not.Null);
65+
var packet = Receive(server);
7066

7167
Assert.That(serverEnc.TryDecrypt(packet), Is.False);
68+
server.Shutdown(null);
7269
}
7370

74-
private static byte[][] _badMessages =
71+
private static int[] _badMessages =
7572
[
76-
[1, 1, 1, 1, 1],
77-
[1, 2],
78-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,]
73+
5,
74+
1,
75+
4,
76+
16,
77+
1024,
7978
];
8079

8180
[Test]
8281
[Description("Attempt to decrypt a packet that is bogus, ensuring it doesn't throw.")]
8382
[TestCaseSource(nameof(_badMessages))]
84-
public void BadMessageDoesNotThrow(byte[] badMessage)
83+
public void BadMessageDoesNotThrow(int badMessageLength)
8584
{
85+
var badMessage = new byte[badMessageLength];
86+
System.Random.Shared.NextBytes(badMessage);
8687
var (_, serverEnc) = MakeEncryptionPair(disjointKey: true);
8788
var (client, server) = MakeConnectionPair();
8889

@@ -94,11 +95,13 @@ public void BadMessageDoesNotThrow(byte[] badMessage)
9495

9596
client.SendMessage(message, NetDeliveryMethod.ReliableOrdered);
9697

97-
var packet = server.WaitMessage(1000);
98+
var packet = Receive(server);
9899

99-
Assert.That(packet, Is.Not.Null);
100+
Assert.That(packet.LengthBytes, Is.EqualTo(badMessageLength));
100101

101102
Assert.That(serverEnc.TryDecrypt(packet), Is.False);
103+
104+
server.Shutdown(null);
102105
}
103106

104107

Robust.Shared/Network/NetEncryption.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public unsafe void Encrypt(NetOutgoingMessage message)
9191
/// <returns>Whether the operation was successful. If this fails, you likely want to drop the connection.</returns>
9292
public unsafe bool TryDecrypt(NetIncomingMessage message)
9393
{
94+
// Minimum possible size a message can be is the nonce + 16 bytes of message.
95+
// So we immediately bail on anything smaller.
96+
if (message.LengthBytes < sizeof(ulong) + CryptoAeadXChaCha20Poly1305Ietf.AddBytes)
97+
return false;
98+
9499
var nonce = message.ReadUInt64();
95100
var cipherText = message.Data.AsSpan(sizeof(ulong), message.LengthBytes - sizeof(ulong));
96101

0 commit comments

Comments
 (0)