From: Forest Date: Wed, 14 Oct 2020 05:42:18 +0000 (-0700) Subject: Cleanup old, unused doc project and fix #7 X-Git-Tag: 1.0.0~23^2~1 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=9e39b2ec65ac607be1ca89717afecead6984b827;p=rhonda%2Fimpostor.hazel.git Cleanup old, unused doc project and fix #7 --- diff --git a/Hazel.Documentation/Content Layout.content b/Hazel.Documentation/Content Layout.content deleted file mode 100644 index dd7c5b4..0000000 --- a/Hazel.Documentation/Content Layout.content +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/Hazel.Documentation/Content/Introduction.aml b/Hazel.Documentation/Content/Introduction.aml deleted file mode 100644 index f7a891e..0000000 --- a/Hazel.Documentation/Content/Introduction.aml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - Welcome to the Hazel documentation! Here you will find technical - details, API references and tutorials for getting started with Hazel. - - - Hazel Networking is an open source low level networking library for C# providing - connection orientated, message bassed communication via TCP, UDP and - RUDP. You can download it from Github - - here - https://github.com/DarkRiftNetworking/Hazel-Networking - _blank - - . - - - - - \ No newline at end of file diff --git a/Hazel.Documentation/Content/Quickstart.aml b/Hazel.Documentation/Content/Quickstart.aml deleted file mode 100644 index 6cc3943..0000000 --- a/Hazel.Documentation/Content/Quickstart.aml +++ /dev/null @@ -1,337 +0,0 @@ - - - - - - - Hazel is a low level networking library that takes away a lot of the pain of writing sockets. Hazel provides the guarantee of connection orientated, message based communication across TCP, UDP and RUDP. - - - This guide will take you through the stages of writing a console based server and connecting to it from a console based client. - - - - - -
- Creating a Server - - - Creating a Listener - - - - Create a new solution containing a Console project named "Server" (or at least something obvious). - - - - - Add a reference to Hazel.dll in the project. - - - - - Modify the default file to look like this, we'll go through each part individually. - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Net; - -using Hazel; -using Hazel.Tcp; - -namespace HazelExample -{ - class ServerExample - { - static ConnectionListener listener; - - public static void Main(string[] args) - { - listener = new TcpConnectionListener(IPAddress.Any, 4296); - - listener.NewConnection += NewConnectionHandler; - - Console.WriteLine("Starting server!"); - - listener.Start(); - - Console.WriteLine("Press any key to continue..."); - - Console.ReadKey(); - - listener.Close(); - } - } -} - - - Firstly we need to tell the compiler that we are using Hazel; and using Hazel.Tcp; so we have access to Hazel's general and TCP specific types. Secondly we create a T:Hazel.ConnectionListener, these wait on a specified port and accept new clients to the server invoking the E:Hazel.ConnectionListener.NewConnection event each time a new client connects. - - - We then instruct the ConnectionListener to begin listening for new clients by calling M:ConnectionListener.Start and then finally we close the listener using M:ConnectionListener.Close. - - - In this circumstance it would be much better if we enclosed the listener within a using block as it implements IDisposable, however for the majority of use cases you are more likely to use the listener in this way. If you want to see it used in a using block then look at the unit tests. - - - If you want to use UDP instead of TCP then it is as simple as including the Hazel.Udp namespace and creating a T:UdpConnectionListener instead. - - - - - - - - Handling Events - - - - In the last example we subscribed to the E:ConnectionListener.NewConnection event but we never spsecified what to do in that event. Add the following method to your code. - -static void NewConnectionHandler(object sender, NewConnectionEventArgs args) -{ - Console.WriteLine("New connection from " + args.Connection.EndPoint.ToString(); - - args.Connection.DataReceived += DataReceivedHandler; -} - - This method is fairly simple, it follows the standard event handler delegate and take a sender (in this case it will be the ConnectionListener that called the event) and some args. The args contain a Connection which is the main object we use for communication with clients. - You can imagine this process in a similar way to TCP. You create a listener which creates sockets on the server side for each client socket that connects to it. - In this method we simply print out the IP of the client that just connected and then we subscribe to any data that this client sends us. You may also want to store the connection here for later reference as Hazel doesn't maintain a list of connection for you. - - - - - Once again we've got an undeclared event handler so lets fill that in now. - -private static void DataReceivedHandler(object sender, DataEventArgs args) -{ - Connection connection = (Connection)sender; - - Console.WriteLine("Received (" + string.Join<byte>(", ", args.Bytes) + ") from " + connection.EndPoint.ToString()); - - connection.SendBytes(args.Bytes, args.SendOption); - - args.Recycle(); -} - - Again this method follows the standard event handler delegate and this time we make use of the sender parameter to get the connection that received the data. We then go on to print out the data received and then we send it back to the client using M:ConnectionListener.SendBytes. When we send we specify that the send option should be the same as the data that was received, we'll talk more about send options later. - You have also probably noticed that I didn't mention the M:DataEventArgs.Recycle call. Recycle is an optional call that tells Hazel that it is now safe to use the object again rather than creating a new one and having to wait for the GC to collect the old object. If you are sending a lot of data then you will get less GC runs if you call Recycle but it is not necerssary to call it and if you dont the GC will collect it as normal. Also note that you should only call Recycle once you are done using the object otherwise the data inside it may change! - - - - - - In total, you should have something like this. - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Net; - -using Hazel; -using Hazel.Tcp; - -namespace HazelExample -{ - class ServerExample - { - static ConnectionListener listener; - - public static void Main(string[] args) - { - listener = new TcpConnectionListener(IPAddress.Any, 4296); - - listener.NewConnection += NewConnectionHandler; - - Console.WriteLine("Starting server!"); - - listener.Start(); - - Console.WriteLine("Press any key to continue..."); - - Console.ReadKey(); - - listener.Close(); - } - - static void NewConnectionHandler(object sender, NewConnectionEventArgs args) - { - Console.WriteLine("New connection from " + args.Connection.EndPoint.ToString(); - - args.Connection.DataReceived += DataReceivedHandler; - - args.Recycle(); - } - - private static void DataReceivedHandler(object sender, DataEventArgs args) - { - Connection connection = (Connection)sender; - - Console.WriteLine("Received (" + string.Join<byte>(", ", args.Bytes) + ") from " + connection.EndPoint.ToString()); - - connection.SendBytes(args.Bytes, args.SendOption); - - args.Recycle(); - } - } -} - - -
- -
- Creating a Client - - - Connecting to a Server - - - - Create a new project in your solution for th client and add a reference to Hazel.dll. - - - - - - Modify the default file to contain the following. - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using Hazel; -using Hazel.Tcp; - -namespace HazelExample -{ - class ClientExample - { - static Connection connection; - - public static void Main(string[] args) - { - NetworkEndPoint endPoint = new NetworkEndPoint("127.0.0.1", 4296); - - connection = new TcpConnection(endPoint); - - connection.DataReceived += DataReceived; - - Console.WriteLine("Connecting!"); - - connection.Connect(); - - Console.WriteLine("Press any key to continue..."); - - Console.ReadKey(); - - connection.Close(); - } - } -} - - As you can see you simply create a T:NetworkEndPoint for the remote server and then pass it into a new T:Connection. Then you can setup any events needed and finally call M:Connection.Connect to begin the connection. - Finally we close the connection using M:Connection.Close. Again, Connection implements IDisposable and so must be closed, if it is easier you could wrap it in a using block. - - If you are using UDP instead of TCP then include the Hazel.Udp namespace and create a T:UdpClientConnection instead. - - - - - - - - Handling Events - - - - Add the following event handler to receive data, you'll notice that this is the same event we used in the server and so it will not be explained. - -private static void DataReceived(object sender, DataEventArgs args) -{ - Console.WriteLine("Received (" + string.Join<byte>(", ", args.Bytes) + ") from " + connection.EndPoint.ToString()); - - args.Recycle(); -} - - - - - - - - Sending Messages - - - - Sending a message is the same for both the server and client, you simply call M:Connection.SendBytes on your connection. - Add the following line after the call to Connect - -connection.SendBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); - - - - - - - You should have something like this. - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using Hazel; -using Hazel.Tcp; - -namespace HazelExample -{ - class ClientExample - { - static Connection connection; - - public static void Main(string[] args) - { - NetworkEndPoint endPoint = new NetworkEndPoint("127.0.0.1", 4296); - - connection = new TcpConnection(endPoint); - - connection.DataReceived += DataReceived; - - Console.WriteLine("Connecting!"); - - connection.Connect(); - - connection.SendBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }); - - Console.WriteLine("Press any key to continue..."); - - Console.ReadKey(); - - connection.Close(); - } - - private static void DataReceived(object sender, DataEventArgs args) - { - Console.WriteLine("Received (" + string.Join<byte>(", ", args.Bytes) + ") from " + connection.EndPoint.ToString()); - - args.Recycle(); - } - } -} - - -
-
-
\ No newline at end of file diff --git a/Hazel.Documentation/Content/Technical Details/Protocols/Protocols.aml b/Hazel.Documentation/Content/Technical Details/Protocols/Protocols.aml deleted file mode 100644 index 46e03af..0000000 --- a/Hazel.Documentation/Content/Technical Details/Protocols/Protocols.aml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - In order to provide the guarantees that it does, Hazel augments each - transport protocol with its own meta data (or header data) that is - hidden from users. - - - - - \ No newline at end of file diff --git a/Hazel.Documentation/Content/Technical Details/Protocols/TCP.aml b/Hazel.Documentation/Content/Technical Details/Protocols/TCP.aml deleted file mode 100644 index a6259c4..0000000 --- a/Hazel.Documentation/Content/Technical Details/Protocols/TCP.aml +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - - Hazel's TCP protocol is fairly simple as TCP already provides connection - based communication and thus only needs a message system implementing. - - - -
- Header Data - - - The TCP protocol is fairly simple interms of header. 4 bytes are - added to mark the number of bytes in each message. - - - - Length (MSB) - Length - Length - Length (LSB) - Data... - -
- All header data in Hazel is encoded in big endian format. -
-
- -
-
\ No newline at end of file diff --git a/Hazel.Documentation/Content/Technical Details/Protocols/UDP-RUDP.aml b/Hazel.Documentation/Content/Technical Details/Protocols/UDP-RUDP.aml deleted file mode 100644 index 31dfb62..0000000 --- a/Hazel.Documentation/Content/Technical Details/Protocols/UDP-RUDP.aml +++ /dev/null @@ -1,146 +0,0 @@ - - - - - - - UDP provides message based communication however it is not connection - oriented nor does it have any built in functionality for reliable or - fragmented delivery of messages hence the implementation to cover this - if fairly complicated. - - - -
- Header Data - - - The UDP protocol has multiple layers of header data depending on the - send options that are requested with the message. - - - - Unreliable - Type - Data... -   -   - - - Reliable - Type - ID (MSB) - ID (LSB) - Data... - -
- All header data in Hazel is encoded in big endian format. - - In both of these, Type is an identifier - that holds the SendOption or another value indicating a control - packet of data. The most significant 4 bits of - Type are reserved for future use and the - least significant bits specify the send option flags for the message. - - - - - 128 - 64 - 32 - 16 - 8 - 4 - 2 - 1 - - - - Reserved - Reserved - Reserved - Reserved - Control - Reserved - Fragmented - Reliable - -
- - Reliable and - Fragmented are both flags for their - respective send option, Control, if set, - specifies that the 3 least significant bits refer to a control code: - - - - 0 - Hello - - - 1 - Disconnect - - - 2 - Acknowledgment - -
-
-
- -
- Reliable Delivery - - - To implement reliable delivery 2 ID bytes are sent which identify the - packet. When the receiver receives the data is replys with an - acknowledgement packet as follows: - - - - Acknowledgement - Type - ID (MSB) - ID (LSB) - -
- - Where type is specifying an acknowledgement packet as laid out above. - - - If the sending client does not receive an acknowledgement after a - specific amount of time elapses from the pakcet being sent then it - resends that packet and thetime before the next resend of that packet - is doubled. When the sending client receives the acknowledgement is - should not resend the data again. - - - The receiving client must also ensure that it does not present the - same packet to the user twice but must acknowledge any packets it - receives, even if it has already received that packet, in case an - acknowledgement is lost. - - - After a specific number of resends without acknowledgement a sending - client may mark assume that communication has been interrupted and - thus mark the connection as disconnected. - -
-
- -
- Keepalive packets - - - Keepalive packets should be sent after a specific time has elapsed - since the last packet (either keepalive, acknowledgement or user sent) - was transmitted and should be sent using reliable delivery so that an - acknowledgement can be received to indicate communication has not been - lost. As packets that are not acknowledged should cause a - disconnection no additional logic is required for keepalives. - - -
-
-
\ No newline at end of file diff --git a/Hazel.Documentation/Content/Technical Details/Technical Details.aml b/Hazel.Documentation/Content/Technical Details/Technical Details.aml deleted file mode 100644 index 9b4729e..0000000 --- a/Hazel.Documentation/Content/Technical Details/Technical Details.aml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - Underneath Hazel there are a lot of technicalities, this section will - help outline those details so you understand Hazel's implementations - better. - - - - - \ No newline at end of file diff --git a/Hazel.Documentation/Content/Welcome.aml b/Hazel.Documentation/Content/Welcome.aml deleted file mode 100644 index 3143860..0000000 --- a/Hazel.Documentation/Content/Welcome.aml +++ /dev/null @@ -1,55 +0,0 @@ - - - - - This is a sample conceptual topic. You can use this as a starting point for adding more conceptual -content to your help project. - - -
- Getting Started - - To get started, add a documentation source to the project (a Visual Studio solution, project, or -assembly and XML comments file). See the Getting Started topics in the Sandcastle Help -File Builder's help file for more information. The following default items are included in this project: - - - - ContentLayout.content - Use the content layout file to manage the -conceptual content in the project and define its layout in the table of contents. - - - - The .\media folder - Place images in this folder that you will reference -from conceptual content using medialLink or mediaLinkInline -elements. If you will not have any images in the file, you may remove this folder. - - - - The .\icons folder - This contains a default logo for the help file. You -may replace it or remove it and the folder if not wanted. If removed or if you change the file name, update -the Transform Args project properties page by removing or changing the filename in the -logoFile transform argument. Note that unlike images referenced from conceptual topics, -the logo file should have its BuildAction property set to Content. - - - - The .\Content folder - Use this to store your conceptual topics. You may -name the files and organize them however you like. One suggestion is to lay the files out on disk as you have -them in the content layout file as shown in this project but the choice is yours. Files can be added via the -Solution Explorer or from within the content layout file editor. Files must appear in the content layout file -in order to be compiled into the help file. - - - - See the Conceptual Content topics in the Sandcastle Help File Builder's -help file for more information. See the Sandcastle MAML Guide for details on Microsoft -Assistance Markup Language (MAML) which is used to create these topics. - -
- - - - -
-
diff --git a/Hazel.Documentation/Hazel.Documentation.shfbproj b/Hazel.Documentation/Hazel.Documentation.shfbproj deleted file mode 100644 index fdb8d71..0000000 --- a/Hazel.Documentation/Hazel.Documentation.shfbproj +++ /dev/null @@ -1,121 +0,0 @@ - - - - - Debug - AnyCPU - 2.0 - b90bb577-2080-4c22-ae86-fb0ccfb40920 - 2015.6.5.0 - - Hazel.Documentation - Hazel.Documentation - Hazel.Documentation - - .NET Framework 3.5 - .\Help\ - Hazel.Documentation - en-US - - - - - - - - - - - - - 100 - OnlyWarningsAndErrors - Website - False - False - False - False - 1.0.0.0 - 2 - False - Standard - Blank - False - VS2013 - False - MemberName - Hazel Documentation - BelowNamespaces - Msdn - Msdn - False - True - Hazel Networking is a low-level networking library for C# providing connection orientated, message based communication via TCP, UDP and RUDP. -Its aim is to provide a standardized interface for web communication so that using and switching between protocols is incredibly simple. -Hazel is going to be the basis of DarkRift 2 and it is being released completely open source so that members of the community can make use of it, improve it and help find any bugs before DarkRift 2 is released. -Hazel can be downloaded as a NuGet package here or you can get the latest build directly from the releases page here! - - - Namespace for all classes shared between multiple protocol types. -Namespace for all TCP communication classes. -Namespace for all UDP communication classes. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - OnBuildSuccess - - \ No newline at end of file diff --git a/Hazel.Documentation/docinclude/TcpClientExample.cs b/Hazel.Documentation/docinclude/TcpClientExample.cs deleted file mode 100644 index 8138f6b..0000000 --- a/Hazel.Documentation/docinclude/TcpClientExample.cs +++ /dev/null @@ -1,24 +0,0 @@ -class TcpClientExample -{ - static void Main(string[] args) - { - using (TcpConnection connection = new TcpConnection()) - { - ManualResetEvent e = new ManualResetEvent(false); - - //Whenever we receive data print the number of bytes and how it was sent - connection.DataReceived += (object sender, DataReceivedEventArgs a) => - Console.WriteLine("Received {0} bytes via {1}!", a.Bytes.Length, a.SendOption); - - //When the end point disconnects from us then release the main thread and exit - connection.Disconnected += (object sender, DisconnectedEventArgs a) => - e.Set(); - - //Connect to a server - connection.Connect(new NetworkEndPoint("127.0.0.1", 4296)); - - //Wait until the end point disconnects from us - e.WaitOne(); - } - } -} diff --git a/Hazel.Documentation/docinclude/TcpListenerExample.cs b/Hazel.Documentation/docinclude/TcpListenerExample.cs deleted file mode 100644 index 468452b..0000000 --- a/Hazel.Documentation/docinclude/TcpListenerExample.cs +++ /dev/null @@ -1,23 +0,0 @@ -class TcpListenerExample -{ - static void Main(string[] args) - { - //Setup listener - using (TcpConnectionListener listener = new TcpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296))) - { - //Start listening for new connection events - listener.NewConnection += delegate(object sender, NewConnectionEventArgs a) - { - //Send the client some data - a.Connection.SendBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }, SendOption.Reliable); - - //Disconnect from the client - a.Connection.Close(); - }; - - listener.Start(); - - Console.ReadKey(); - } - } -} diff --git a/Hazel.Documentation/docinclude/UdpClientExample.cs b/Hazel.Documentation/docinclude/UdpClientExample.cs deleted file mode 100644 index e77d5c5..0000000 --- a/Hazel.Documentation/docinclude/UdpClientExample.cs +++ /dev/null @@ -1,24 +0,0 @@ -class UdpClientExample -{ - static void Main(string[] args) - { - using (UdpConnection connection = new UdpConnection()) - { - ManualResetEvent e = new ManualResetEvent(false); - - //Whenever we receive data print the number of bytes and how it was sent - connection.DataReceived += (object sender, DataReceivedEventArgs a) => - Console.WriteLine("Received {0} bytes via {1}!", a.Bytes.Length, a.SendOption); - - //When the end point disconnects from us then release the main thread and exit - connection.Disconnected += (object sender, DisconnectedEventArgs a) => - e.Set(); - - //Connect to a server - connection.Connect(new NetworkEndPoint("127.0.0.1", 4296)); - - //Wait until the end point disconnects from us - e.WaitOne(); - } - } -} diff --git a/Hazel.Documentation/docinclude/UdpListenerExample.cs b/Hazel.Documentation/docinclude/UdpListenerExample.cs deleted file mode 100644 index 4c26c49..0000000 --- a/Hazel.Documentation/docinclude/UdpListenerExample.cs +++ /dev/null @@ -1,23 +0,0 @@ -class UdpListenerExample -{ - static void Main(string[] args) - { - //Setup listener - using (UdpConnectionListener listener = new UdpConnectionListener(new NetworkEndPoint(IPAddress.Any, 4296))) - { - //Start listening for new connection events - listener.NewConnection += delegate(object sender, NewConnectionEventArgs a) - { - //Send the client some data - a.Connection.SendBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }, SendOption.Reliable); - - //Disconnect from the client - a.Connection.Close(); - }; - - listener.Start(); - - Console.ReadKey(); - } - } -} diff --git a/Hazel.Documentation/docinclude/common.xml b/Hazel.Documentation/docinclude/common.xml deleted file mode 100644 index 5e9ab9e..0000000 --- a/Hazel.Documentation/docinclude/common.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - As with all Hazel events it is invoked on a thread from the .NET - ThreadPool and hence any subscribers should ensure their - handling code is thread safe. Implementing connections are not bound to invoking this event in the sequence - messages are received, in fact implementers are only required to ensure this method is always and only invoked - for a user sent message, therefore subscribers should be aware that this event may be called out of order and - may be called whilst another thread is still handling an invocation of the event. - - - - - This object implements IRecyclable and hence can be recycled in order to reduce the number of objects the - GC has to deal with. When you are done with the object you can either leave it unreferenced as you usually - would and the GC will collect it or you can call to inform Hazel that the object - should be reused. Once recycle has been called the contents can be overwritten at any time and so only - call it once you are completely finished with the object. - - - - - This method sends a number of bytes in a message to the end point of this client using the given - to describe how the data should be sent. Sending messages requires that the - this connection is connected to a remote end point and SendBytes will throw an exception if that is not - the case. See the property for information on whether a connection is connected or not. - - - \ No newline at end of file diff --git a/Hazel.Documentation/icons/Help.png b/Hazel.Documentation/icons/Help.png deleted file mode 100644 index 945e89f..0000000 Binary files a/Hazel.Documentation/icons/Help.png and /dev/null differ diff --git a/Hazel.UnitTests/MessageReaderTests.cs b/Hazel.UnitTests/MessageReaderTests.cs index 04a727d..ad713ba 100644 --- a/Hazel.UnitTests/MessageReaderTests.cs +++ b/Hazel.UnitTests/MessageReaderTests.cs @@ -144,7 +144,6 @@ namespace Hazel.UnitTests msg.Write("HO"); msg.EndMessage(); msg.StartMessage(2); - msg.Write("NO"); msg.EndMessage(); msg.EndMessage(); @@ -160,9 +159,8 @@ namespace Hazel.UnitTests Assert.AreEqual("HO", sub.ReadString()); sub = reader.ReadMessage(); - Assert.AreEqual(3, sub.Length); + Assert.AreEqual(0, sub.Length); Assert.AreEqual(2, sub.Tag); - Assert.AreEqual("NO", sub.ReadString()); } [TestMethod] @@ -207,6 +205,47 @@ namespace Hazel.UnitTests catch (InvalidDataException) { } } + [TestMethod] + public void ReadMessageProtectsAgainstOverrun() + { + const string TestDataFromAPreviousPacket = "You shouldn't be able to see this data"; + + // An extra byte from the length of TestData when written via MessageWriter + // Extra 3 bytes for the length + tag header for ReadMessage. + int DataLength = TestDataFromAPreviousPacket.Length + 1 + 3; + + // THE BUG + // + // No bound checks. When the server wants to read a message, it + // reads the uint16 at that offset, treats it as a length without any bound checks. + // This can be allow a later ReadString or ReadBytes to create an infoleak. + + MessageWriter writer = MessageWriter.Get(SendOption.None); + + // This is the malicious length. No data in this message, so it should be zero. + writer.Write((ushort)1); + writer.Write((byte)0); // Tag + + // This is data from a "previous packet" + writer.Write(TestDataFromAPreviousPacket); + + byte[] testData = writer.ToByteArray(includeHeader: false); + + Assert.AreEqual(DataLength, testData.Length); + + var outer = MessageReader.Get(testData); + + // Length is just the malicious message header. + outer.Length = 3; + + try + { + outer.ReadMessage(); + Assert.Fail("ReadMessage is expected to throw"); + } + catch (InvalidDataException) { } + } + [TestMethod] public void GetLittleEndian() { diff --git a/Hazel.sln b/Hazel.sln index 10d0195..bc27b0a 100644 --- a/Hazel.sln +++ b/Hazel.sln @@ -1,14 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.539 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30523.141 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hazel", "Hazel\Hazel.csproj", "{02CFBD30-D77D-400F-94B2-700F60EFDD7F}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hazel.UnitTests", "Hazel.UnitTests\Hazel.UnitTests.csproj", "{1394E4CA-E17A-42F5-9216-8046ACA8D16B}" EndProject -Project("{7CF6DF6D-3B04-46F8-A40B-537D21BCA0B4}") = "Hazel.Documentation", "Hazel.Documentation\Hazel.Documentation.shfbproj", "{B90BB577-2080-4C22-AE86-FB0CCFB40920}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -23,9 +21,6 @@ Global {1394E4CA-E17A-42F5-9216-8046ACA8D16B}.Debug|Any CPU.Build.0 = Debug|Any CPU {1394E4CA-E17A-42F5-9216-8046ACA8D16B}.Release|Any CPU.ActiveCfg = Release|Any CPU {1394E4CA-E17A-42F5-9216-8046ACA8D16B}.Release|Any CPU.Build.0 = Release|Any CPU - {B90BB577-2080-4C22-AE86-FB0CCFB40920}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B90BB577-2080-4C22-AE86-FB0CCFB40920}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B90BB577-2080-4C22-AE86-FB0CCFB40920}.Release|Any CPU.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Hazel/MessageReader.cs b/Hazel/MessageReader.cs index e19d90a..cc3ef2a 100644 --- a/Hazel/MessageReader.cs +++ b/Hazel/MessageReader.cs @@ -108,7 +108,7 @@ namespace Hazel public MessageReader ReadMessage() { // Ensure there is at least a header - if (this.readHead + 3 > this.Buffer.Length) return null; + if (this.BytesRemaining < 3) throw new InvalidDataException($"ReadMessage header is longer than message length: 3 of {this.BytesRemaining}"); var output = new MessageReader(); @@ -122,6 +122,8 @@ namespace Hazel output.Offset += 3; output.Position = 0; + if (this.BytesRemaining < output.Length + 3) throw new InvalidDataException($"Message length is longer than message length: {output.Length + 3} of {this.BytesRemaining}"); + this.Position += output.Length + 3; return output; } diff --git a/Hazel/Properties/AssemblyInfo.cs b/Hazel/Properties/AssemblyInfo.cs index 0e00915..3d0a292 100644 --- a/Hazel/Properties/AssemblyInfo.cs +++ b/Hazel/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ using System.Runtime.InteropServices; [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("Hazel")] -[assembly: AssemblyCopyright("Copyright © 2019")] +[assembly: AssemblyCopyright("Copyright © 2020")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -29,14 +29,8 @@ using System.Runtime.InteropServices; // Build Number // Revision // -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.0.1.0")] [assembly: AssemblyFileVersion("1.0.0.0")] -// NuGet version information -[assembly: AssemblyInformationalVersion("0.1.2-beta")] - // Show internals to unit testing assembly so it can test [assembly:InternalsVisibleTo("Hazel.UnitTests")] \ No newline at end of file