From 38dfeebf8d788669edb65ac21f8f5e9c1160cd99 Mon Sep 17 00:00:00 2001 From: Steve Evans Date: Thu, 13 May 2021 11:06:09 -0400 Subject: [PATCH 1/3] Data lengths longer than stream position when data lengths are greater than int.maxvalue are ignored and do not throw an exception --- src/Renci.SshNet/Messages/Transport/IgnoreMessage.cs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Renci.SshNet/Messages/Transport/IgnoreMessage.cs b/src/Renci.SshNet/Messages/Transport/IgnoreMessage.cs index 580cd3b21..2b75eda02 100644 --- a/src/Renci.SshNet/Messages/Transport/IgnoreMessage.cs +++ b/src/Renci.SshNet/Messages/Transport/IgnoreMessage.cs @@ -61,19 +61,18 @@ public IgnoreMessage(byte[] data) protected override void LoadData() { var dataLength = ReadUInt32(); - if (dataLength > int.MaxValue) - { - throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Data longer than {0} is not supported.", int.MaxValue)); - } - if (dataLength > (DataStream.Length - DataStream.Position)) { DiagnosticAbstraction.Log("SSH_MSG_IGNORE: Length exceeds data bytes, data ignored."); Data = Array.Empty; } + else if (dataLength > int.MaxValue) + { + throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Data longer than {0} is not supported.", int.MaxValue)); + } else { - Data = ReadBytes((int) dataLength); + Data = ReadBytes((int)dataLength); } } From 4b3b0cca3fc80a6b180e71ee0b7e49f3c8dfe8ba Mon Sep 17 00:00:00 2001 From: Steve Evans Date: Fri, 14 May 2021 09:14:10 -0400 Subject: [PATCH 2/3] Removed unreachable test --- .../Messages/Transport/IgnoreMessageTest.cs | 25 +------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/src/Renci.SshNet.Tests/Classes/Messages/Transport/IgnoreMessageTest.cs b/src/Renci.SshNet.Tests/Classes/Messages/Transport/IgnoreMessageTest.cs index 1c82d3a6d..7818468a3 100644 --- a/src/Renci.SshNet.Tests/Classes/Messages/Transport/IgnoreMessageTest.cs +++ b/src/Renci.SshNet.Tests/Classes/Messages/Transport/IgnoreMessageTest.cs @@ -94,7 +94,7 @@ public void Load() } [TestMethod] - public void Load_ShouldIgnoreDataWhenItsLengthIsGreatherThanItsActualBytes() + public void Load_ShouldIgnoreDataWhenItsLengthIsGreaterThanItsActualBytes() { var ssh = new SshDataStream(1); ssh.WriteByte(2); // Type @@ -108,28 +108,5 @@ public void Load_ShouldIgnoreDataWhenItsLengthIsGreatherThanItsActualBytes() Assert.IsNotNull(ignoreMessage.Data); Assert.AreEqual(0, ignoreMessage.Data.Length); } - - [TestMethod] - public void Load_ShouldThrowNotSupportedExceptionWhenDataLengthIsGreaterThanInt32MaxValue() - { - var ssh = new SshDataStream(1); - ssh.WriteByte(2); // Type - ssh.Write(uint.MaxValue); // Data length - ssh.Write(new byte[3]); - - var ignoreMessageBytes = ssh.ToArray(); - var ignoreMessage = new IgnoreMessage(); - - try - { - ignoreMessage.Load(ignoreMessageBytes, 1, ignoreMessageBytes.Length - 1); - Assert.Fail(); - } - catch (NotSupportedException ex) - { - Assert.IsNull(ex.InnerException); - Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, "Data longer than {0} is not supported.", int.MaxValue), ex.Message); - } - } } } From 001212185b3656ede2a4b67e1ecbcfcc6259970f Mon Sep 17 00:00:00 2001 From: Rob Hague Date: Fri, 17 Nov 2023 19:47:01 +0100 Subject: [PATCH 3/3] Do not try to load the data (just ignore it) --- .../Messages/Transport/IgnoreMessage.cs | 20 +++-------------- .../Messages/Transport/IgnoreMessageTest.cs | 22 ++----------------- 2 files changed, 5 insertions(+), 37 deletions(-) diff --git a/src/Renci.SshNet/Messages/Transport/IgnoreMessage.cs b/src/Renci.SshNet/Messages/Transport/IgnoreMessage.cs index 3e087eaeb..d657d2d46 100644 --- a/src/Renci.SshNet/Messages/Transport/IgnoreMessage.cs +++ b/src/Renci.SshNet/Messages/Transport/IgnoreMessage.cs @@ -1,6 +1,4 @@ using System; -using System.Globalization; -using Renci.SshNet.Abstractions; namespace Renci.SshNet.Messages.Transport { @@ -13,7 +11,8 @@ public class IgnoreMessage : Message internal const byte MessageNumber = 2; /// - /// Gets ignore message data if any. + /// Gets ignore message data if this message has been initialised + /// with data to be sent. Otherwise, returns an empty array. /// public byte[] Data { get; private set; } @@ -61,20 +60,7 @@ protected override int BufferCapacity /// protected override void LoadData() { - var dataLength = ReadUInt32(); - if (dataLength > (DataStream.Length - DataStream.Position)) - { - DiagnosticAbstraction.Log("SSH_MSG_IGNORE: Length exceeds data bytes, data ignored."); - Data = Array.Empty(); - } - else if (dataLength > int.MaxValue) - { - throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Data longer than {0} is not supported.", int.MaxValue)); - } - else - { - Data = ReadBytes((int)dataLength); - } + // Do nothing - this data is supposed to be ignored. } /// diff --git a/test/Renci.SshNet.Tests/Classes/Messages/Transport/IgnoreMessageTest.cs b/test/Renci.SshNet.Tests/Classes/Messages/Transport/IgnoreMessageTest.cs index 7818468a3..ecdbf3310 100644 --- a/test/Renci.SshNet.Tests/Classes/Messages/Transport/IgnoreMessageTest.cs +++ b/test/Renci.SshNet.Tests/Classes/Messages/Transport/IgnoreMessageTest.cs @@ -1,5 +1,4 @@ using System; -using System.Globalization; using System.Linq; using Renci.SshNet.Common; using Renci.SshNet.Messages.Transport; @@ -80,7 +79,7 @@ public void GetBytes() } [TestMethod] - public void Load() + public void Load_IgnoresData() { var ignoreMessage = new IgnoreMessage(_data); var bytes = ignoreMessage.GetBytes(); @@ -89,24 +88,7 @@ public void Load() target.Load(bytes, 1, bytes.Length - 1); Assert.IsNotNull(target.Data); - Assert.AreEqual(_data.Length, target.Data.Length); - Assert.IsTrue(target.Data.SequenceEqual(_data)); - } - - [TestMethod] - public void Load_ShouldIgnoreDataWhenItsLengthIsGreaterThanItsActualBytes() - { - var ssh = new SshDataStream(1); - ssh.WriteByte(2); // Type - ssh.Write(5u); // Data length - ssh.Write(new byte[3]); // Data - - var ignoreMessageBytes = ssh.ToArray(); - - var ignoreMessage = new IgnoreMessage(); - ignoreMessage.Load(ignoreMessageBytes, 1, ignoreMessageBytes.Length - 1); - Assert.IsNotNull(ignoreMessage.Data); - Assert.AreEqual(0, ignoreMessage.Data.Length); + Assert.AreEqual(0, target.Data.Length); } } }