Skip to content

Commit

Permalink
Split commands in FtpWebRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
liveans authored and carlossanlop committed Oct 5, 2023
1 parent 437356c commit 4796219
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/libraries/System.Net.Requests/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@
<data name="net_ftp_receivefailure" xml:space="preserve">
<value>The underlying connection was closed: An unexpected error occurred on a receive</value>
</data>
<data name="net_ftp_no_newlines" xml:space="preserve">
<value>CRLF character pair is not allowed in FtpWebRequest inputs.</value>
</data>
<data name="net_webstatus_NameResolutionFailure" xml:space="preserve">
<value>The remote name could not be resolved</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,11 @@ private string GetPortCommandLine()
/// </summary>
private static string FormatFtpCommand(string command, string? parameter)
{
if (parameter is not null && parameter.Contains("\r\n", StringComparison.Ordinal))
{
throw new FormatException(SR.net_ftp_no_newlines);
}

return string.IsNullOrEmpty(parameter) ?
command + "\r\n" :
command + " " + parameter + "\r\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ internal FtpWebRequest(Uri uri)
if ((object)uri.Scheme != (object)Uri.UriSchemeFtp)
throw new ArgumentOutOfRangeException(nameof(uri));

if (uri.OriginalString.Contains("\r\n", StringComparison.Ordinal))
throw new FormatException(SR.net_ftp_no_newlines);

_timerCallback = new TimerThread.Callback(TimerCallback);
_syncObject = new object();

Expand Down
21 changes: 21 additions & 0 deletions src/libraries/System.Net.Requests/tests/FtpWebRequestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@ public void Ftp_RenameFileSubDir_Success(FtpExecutionMode mode)
Assert.False(DirExists(mode, dir));
}

[Fact]
public void Ftp_Ignore_NewLine_Constructor_Throws_FormatException()
{
string uri = absoluteUri + Guid.NewGuid().ToString();

Assert.Throws<FormatException>(() => WebRequest.Create($"{uri}\r\n{WebRequestMethods.Ftp.AppendFile} {Guid.NewGuid().ToString()}"));
}

[ConditionalFact(nameof(LocalServerAvailable))]
public void Ftp_Ignore_NewLine_GetRequestStream_And_GetResponse_Throws_FormatException_As_InnerException()
{
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(absoluteUri + Guid.NewGuid().ToString());
ftpWebRequest.Method = "APPE";
ftpWebRequest.Credentials = new NetworkCredential("test\r\ntest2", "test\r\ntest2");
var requestException = Assert.Throws<WebException>(() => ftpWebRequest.GetRequestStream());
Assert.True(requestException.InnerException is FormatException);

var responseException = Assert.Throws<WebException>(() => ftpWebRequest.GetResponse());
Assert.True(responseException.InnerException is FormatException);
}

private static async Task<MemoryStream> DoAsync(FtpWebRequest request, MemoryStream requestBody)
{
if (requestBody != null)
Expand Down

0 comments on commit 4796219

Please sign in to comment.