Skip to content

Commit daa1acc

Browse files
se006Steve EvansRob-Hague
authored
Fix avoidable exception when data length is too long (#823)
* Data lengths longer than stream position when data lengths are greater than int.maxvalue are ignored and do not throw an exception * Removed unreachable test * Do not try to load the data (just ignore it) --------- Co-authored-by: Steve Evans <[email protected]> Co-authored-by: Rob Hague <[email protected]>
1 parent f9f2b0e commit daa1acc

File tree

2 files changed

+5
-61
lines changed

2 files changed

+5
-61
lines changed

src/Renci.SshNet/Messages/Transport/IgnoreMessage.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Globalization;
3-
using Renci.SshNet.Abstractions;
42

53
namespace Renci.SshNet.Messages.Transport
64
{
@@ -13,7 +11,8 @@ public class IgnoreMessage : Message
1311
internal const byte MessageNumber = 2;
1412

1513
/// <summary>
16-
/// Gets ignore message data if any.
14+
/// Gets ignore message data if this message has been initialised
15+
/// with data to be sent. Otherwise, returns an empty array.
1716
/// </summary>
1817
public byte[] Data { get; private set; }
1918

@@ -61,21 +60,7 @@ protected override int BufferCapacity
6160
/// </summary>
6261
protected override void LoadData()
6362
{
64-
var dataLength = ReadUInt32();
65-
if (dataLength > int.MaxValue)
66-
{
67-
throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Data longer than {0} is not supported.", int.MaxValue));
68-
}
69-
70-
if (dataLength > (DataStream.Length - DataStream.Position))
71-
{
72-
DiagnosticAbstraction.Log("SSH_MSG_IGNORE: Length exceeds data bytes, data ignored.");
73-
Data = Array.Empty<byte>();
74-
}
75-
else
76-
{
77-
Data = ReadBytes((int) dataLength);
78-
}
63+
// Do nothing - this data is supposed to be ignored.
7964
}
8065

8166
/// <summary>

test/Renci.SshNet.Tests/Classes/Messages/Transport/IgnoreMessageTest.cs

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Globalization;
32
using System.Linq;
43
using Renci.SshNet.Common;
54
using Renci.SshNet.Messages.Transport;
@@ -80,7 +79,7 @@ public void GetBytes()
8079
}
8180

8281
[TestMethod]
83-
public void Load()
82+
public void Load_IgnoresData()
8483
{
8584
var ignoreMessage = new IgnoreMessage(_data);
8685
var bytes = ignoreMessage.GetBytes();
@@ -89,47 +88,7 @@ public void Load()
8988
target.Load(bytes, 1, bytes.Length - 1);
9089

9190
Assert.IsNotNull(target.Data);
92-
Assert.AreEqual(_data.Length, target.Data.Length);
93-
Assert.IsTrue(target.Data.SequenceEqual(_data));
94-
}
95-
96-
[TestMethod]
97-
public void Load_ShouldIgnoreDataWhenItsLengthIsGreatherThanItsActualBytes()
98-
{
99-
var ssh = new SshDataStream(1);
100-
ssh.WriteByte(2); // Type
101-
ssh.Write(5u); // Data length
102-
ssh.Write(new byte[3]); // Data
103-
104-
var ignoreMessageBytes = ssh.ToArray();
105-
106-
var ignoreMessage = new IgnoreMessage();
107-
ignoreMessage.Load(ignoreMessageBytes, 1, ignoreMessageBytes.Length - 1);
108-
Assert.IsNotNull(ignoreMessage.Data);
109-
Assert.AreEqual(0, ignoreMessage.Data.Length);
110-
}
111-
112-
[TestMethod]
113-
public void Load_ShouldThrowNotSupportedExceptionWhenDataLengthIsGreaterThanInt32MaxValue()
114-
{
115-
var ssh = new SshDataStream(1);
116-
ssh.WriteByte(2); // Type
117-
ssh.Write(uint.MaxValue); // Data length
118-
ssh.Write(new byte[3]);
119-
120-
var ignoreMessageBytes = ssh.ToArray();
121-
var ignoreMessage = new IgnoreMessage();
122-
123-
try
124-
{
125-
ignoreMessage.Load(ignoreMessageBytes, 1, ignoreMessageBytes.Length - 1);
126-
Assert.Fail();
127-
}
128-
catch (NotSupportedException ex)
129-
{
130-
Assert.IsNull(ex.InnerException);
131-
Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, "Data longer than {0} is not supported.", int.MaxValue), ex.Message);
132-
}
91+
Assert.AreEqual(0, target.Data.Length);
13392
}
13493
}
13594
}

0 commit comments

Comments
 (0)