Skip to content

Commit 4796219

Browse files
liveanscarlossanlop
authored andcommitted
Split commands in FtpWebRequest
1 parent 437356c commit 4796219

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/libraries/System.Net.Requests/src/Resources/Strings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@
195195
<data name="net_ftp_receivefailure" xml:space="preserve">
196196
<value>The underlying connection was closed: An unexpected error occurred on a receive</value>
197197
</data>
198+
<data name="net_ftp_no_newlines" xml:space="preserve">
199+
<value>CRLF character pair is not allowed in FtpWebRequest inputs.</value>
200+
</data>
198201
<data name="net_webstatus_NameResolutionFailure" xml:space="preserve">
199202
<value>The remote name could not be resolved</value>
200203
</data>

src/libraries/System.Net.Requests/src/System/Net/FtpControlStream.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,11 @@ private string GetPortCommandLine()
11181118
/// </summary>
11191119
private static string FormatFtpCommand(string command, string? parameter)
11201120
{
1121+
if (parameter is not null && parameter.Contains("\r\n", StringComparison.Ordinal))
1122+
{
1123+
throw new FormatException(SR.net_ftp_no_newlines);
1124+
}
1125+
11211126
return string.IsNullOrEmpty(parameter) ?
11221127
command + "\r\n" :
11231128
command + " " + parameter + "\r\n";

src/libraries/System.Net.Requests/src/System/Net/FtpWebRequest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,9 @@ internal FtpWebRequest(Uri uri)
486486
if ((object)uri.Scheme != (object)Uri.UriSchemeFtp)
487487
throw new ArgumentOutOfRangeException(nameof(uri));
488488

489+
if (uri.OriginalString.Contains("\r\n", StringComparison.Ordinal))
490+
throw new FormatException(SR.net_ftp_no_newlines);
491+
489492
_timerCallback = new TimerThread.Callback(TimerCallback);
490493
_syncObject = new object();
491494

src/libraries/System.Net.Requests/tests/FtpWebRequestTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,27 @@ public void Ftp_RenameFileSubDir_Success(FtpExecutionMode mode)
203203
Assert.False(DirExists(mode, dir));
204204
}
205205

206+
[Fact]
207+
public void Ftp_Ignore_NewLine_Constructor_Throws_FormatException()
208+
{
209+
string uri = absoluteUri + Guid.NewGuid().ToString();
210+
211+
Assert.Throws<FormatException>(() => WebRequest.Create($"{uri}\r\n{WebRequestMethods.Ftp.AppendFile} {Guid.NewGuid().ToString()}"));
212+
}
213+
214+
[ConditionalFact(nameof(LocalServerAvailable))]
215+
public void Ftp_Ignore_NewLine_GetRequestStream_And_GetResponse_Throws_FormatException_As_InnerException()
216+
{
217+
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(absoluteUri + Guid.NewGuid().ToString());
218+
ftpWebRequest.Method = "APPE";
219+
ftpWebRequest.Credentials = new NetworkCredential("test\r\ntest2", "test\r\ntest2");
220+
var requestException = Assert.Throws<WebException>(() => ftpWebRequest.GetRequestStream());
221+
Assert.True(requestException.InnerException is FormatException);
222+
223+
var responseException = Assert.Throws<WebException>(() => ftpWebRequest.GetResponse());
224+
Assert.True(responseException.InnerException is FormatException);
225+
}
226+
206227
private static async Task<MemoryStream> DoAsync(FtpWebRequest request, MemoryStream requestBody)
207228
{
208229
if (requestBody != null)

0 commit comments

Comments
 (0)