Files
SSH.NET/test/Renci.SshNet.Tests/Classes/ScpClientTest_Download_ControlLineTooLong.cs
Rob Hague ece424a85e Limit the length of SCP control lines
ReadString accumulated bytes until a line feed with no upper bound, so a
server could exhaust client memory by never sending one. Control lines and
error messages are now limited to 64 KiB; a longer line throws ScpException.
2026-10-14 09:40:28 +02:00

55 lines
1.8 KiB
C#

using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Renci.SshNet.Common;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes
{
/// <summary>
/// Verifies that the length of a control line received from the server is bounded.
/// </summary>
[TestClass]
public class ScpClientTest_Download_ControlLineTooLong
{
[TestMethod]
public void LineLongerThanMaximum_ShouldThrowScpException()
{
var transcript = new ScpServerTranscript()
.Raw("C0644 1 " + new string('a', ScpClient.MaxControlLineLength))
.Raw("\n");
using (var destination = new MemoryStream())
{
var result = transcript.Run(client => client.Download("/remote/file", destination), closeChannel: true);
Assert.IsTrue(result.Completed);
Assert.IsInstanceOfType<ScpException>(result.Exception);
Assert.Contains("maximum length", result.Exception.Message, StringComparison.Ordinal);
}
}
[TestMethod]
public void LineOfMaximumLength_ShouldBeAccepted()
{
const string Header = "C0644 1 ";
var transcript = new ScpServerTranscript()
.Line(Header + new string('a', ScpClient.MaxControlLineLength - Header.Length))
.Raw("x")
.Raw([0]);
using (var destination = new MemoryStream())
{
var result = transcript.Run(client => client.Download("/remote/file", destination), closeChannel: true);
Assert.IsTrue(result.Completed);
Assert.IsNull(result.Exception, result.Exception?.ToString());
Assert.AreEqual(1, destination.Length);
}
}
}
}