Skip to content

Commit 13d6d02

Browse files
committed
Added invalid message size checks
1 parent 2d5ed8c commit 13d6d02

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

MLAPI/NetworkingManagerComponents/Core/MessageManager.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ internal static BitStream UnwrapMessage(BitStream inputStream, uint clientId, ou
2929
{
3030
try
3131
{
32+
if (inputStream.Length < 1)
33+
{
34+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogError("The incomming message was too small");
35+
messageType = MLAPIConstants.INVALID;
36+
return null;
37+
}
38+
3239
bool isEncrypted = inputHeaderReader.ReadBit();
3340
bool isAuthenticated = inputHeaderReader.ReadBit();
3441

@@ -89,7 +96,15 @@ internal static BitStream UnwrapMessage(BitStream inputStream, uint clientId, ou
8996

9097
if (isEncrypted)
9198
{
92-
inputStream.Read(IV_BUFFER, 0, IV_BUFFER.Length);
99+
int ivRead = inputStream.Read(IV_BUFFER, 0, IV_BUFFER.Length);
100+
101+
if (ivRead != IV_BUFFER.Length)
102+
{
103+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogError("Invalid IV size");
104+
messageType = MLAPIConstants.INVALID;
105+
return null;
106+
}
107+
93108
PooledBitStream outputStream = PooledBitStream.Get();
94109

95110
using (RijndaelManaged rijndael = new RijndaelManaged())
@@ -114,6 +129,14 @@ internal static BitStream UnwrapMessage(BitStream inputStream, uint clientId, ou
114129
}
115130

116131
outputStream.Position = 0;
132+
133+
if (outputStream.Length == 0)
134+
{
135+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogError("The incomming message was too small");
136+
messageType = MLAPIConstants.INVALID;
137+
return null;
138+
}
139+
117140
int msgType = outputStream.ReadByte();
118141
messageType = msgType == -1 ? MLAPIConstants.INVALID : (byte)msgType;
119142
}
@@ -122,6 +145,13 @@ internal static BitStream UnwrapMessage(BitStream inputStream, uint clientId, ou
122145
}
123146
else
124147
{
148+
if (inputStream.Length - inputStream.Position <= 0)
149+
{
150+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogError("The incomming message was too small");
151+
messageType = MLAPIConstants.INVALID;
152+
return null;
153+
}
154+
125155
int msgType = inputStream.ReadByte();
126156
messageType = msgType == -1 ? MLAPIConstants.INVALID : (byte)msgType;
127157
return inputStream;

0 commit comments

Comments
 (0)