Skip to content

Detect CR/LF and URL-encoded CR/LF in FtpWebRequest URI and command parameters#128983

Merged
mrek-msft merged 9 commits into
mainfrom
copilot/support-url-encoded-check-for-new-line
Jun 29, 2026
Merged

Detect CR/LF and URL-encoded CR/LF in FtpWebRequest URI and command parameters#128983
mrek-msft merged 9 commits into
mainfrom
copilot/support-url-encoded-check-for-new-line

Conversation

Copilot AI commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

FtpWebRequest already rejects URIs containing a literal \r\n with a clear FormatException, but standalone \r or \n and the URL-encoded forms %0D / %0A slip past the check and fail later with a less informative exception — a common case when a stray CR/LF from upstream parsing gets URL-encoded for safety, or when a non-compliant URI contains only one of the two characters. The same gap existed in FtpControlStream.FormatFtpCommand, which only checked for literal \r\n in command parameters.

Changes

  • src/libraries/System.Net.Requests/src/System/Net/FtpWebRequest.cs: Extend the constructor's newline check to detect any of \r, \n, %0D, %0A individually (case-insensitive for the encoded forms), throwing the same FormatException (net_ftp_no_newlines) up front.
  • src/libraries/System.Net.Requests/src/System/Net/FtpControlStream.cs: Extend FormatFtpCommand to check for literal \r and \n in command parameters. Note: URL-encoded forms are not checked here because parameters are already unescaped when they reach FormatFtpCommand, so checking for literal "%0A" or "%0D" strings would incorrectly reject legitimate filenames containing those character sequences.
  • src/libraries/System.Net.Requests/src/Resources/Strings.resx: Updated the net_ftp_no_newlines resource string from "CRLF character pair is not allowed in FtpWebRequest inputs." to "CR and LF characters are not allowed in FtpWebRequest inputs." to accurately reflect that individual CR and LF characters (and their URL-encoded forms in URIs) are each disallowed.
  • src/libraries/System.Net.Requests/tests/FtpWebRequestTest.cs:
    • Add Ctor_NewLineInUri_ThrowsFormatException theory covering literal \r\n, standalone \r and \n, and the %0D%0A / %0D / %0A (upper and lower case) variants.
    • Add Ftp_NewLineInRenameTo_GetResponse_Throws_FormatException_As_InnerException theory that exercises the new FormatFtpCommand check via the RenameTo property, gated with [ConditionalTheory(typeof(FtpWebRequestTest), nameof(LocalServerAvailable))] and using the shared absoluteUri, asserting that each literal \r, \n, \r\n variant results in a WebException whose InnerException is a FormatException.
    • Extend the existing Ftp_Ignore_NewLine_GetRequestStream_And_GetResponse_Throws_FormatException_As_InnerException test (previously a [ConditionalFact] covering only literal \r\n) into a [ConditionalTheory] with InlineData for each literal \r, \n, \r\n variant applied to both the username and password of the NetworkCredential.
// URI validation (checks both literal and URL-encoded)
if (uri.OriginalString.AsSpan().ContainsAny('\r', '\n') ||
    uri.OriginalString.Contains("%0A", StringComparison.OrdinalIgnoreCase) ||
    uri.OriginalString.Contains("%0D", StringComparison.OrdinalIgnoreCase))
    throw new FormatException(SR.net_ftp_no_newlines);

// FormatFtpCommand validation (checks only literal CR/LF)
if (parameter is not null && parameter.AsSpan().ContainsAny('\r', '\n'))
    throw new FormatException(SR.net_ftp_no_newlines);

Copilot AI self-assigned this Jun 4, 2026
Copilot AI review requested due to automatic review settings June 4, 2026 11:51
Copilot AI removed the request for review from Copilot June 4, 2026 11:51
Copilot AI linked an issue Jun 4, 2026 that may be closed by this pull request
@MihaZupan MihaZupan assigned mrek-msft and unassigned MihaZupan Jun 4, 2026
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 4, 2026 12:25
Copilot AI changed the title [WIP] Add support for URL encoded check for new line in FtpWebRequest Detect URL-encoded CRLF (%0D%0A) in FtpWebRequest URI Jun 4, 2026
Copilot AI requested a review from MihaZupan June 4, 2026 12:26
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@mrek-msft

Copy link
Copy Markdown
Member

@copilot add support for checking all \n, \r, %0D, %0A separately, so we detect even wrong and non-compliant at once requests.

Co-authored-by: mrek-msft <188900745+mrek-msft@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 26, 2026 12:14

Copilot AI commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

@copilot add support for checking all \n, \r, %0D, %0A separately, so we detect even wrong and non-compliant at once requests.

Done in de62365 — the check now matches any of \r, \n, %0D, %0A individually (case-insensitive for the encoded forms), and the test theory covers each variant.

Note

This response was generated by GitHub Copilot.

Copilot AI changed the title Detect URL-encoded CRLF (%0D%0A) in FtpWebRequest URI Detect CR/LF and URL-encoded CR/LF in FtpWebRequest URI Jun 26, 2026
Copilot AI requested a review from mrek-msft June 26, 2026 12:15
@mrek-msft

Copy link
Copy Markdown
Member

@copilot. Add similar check (for each 4 separately) to FormatFtpCommand in src\System\Net\FtpControlStream.cs.

Co-authored-by: mrek-msft <188900745+mrek-msft@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 26, 2026 14:24

Copilot AI commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

@copilot process review comment and along the way you can extend already existing test Ftp_Ignore_NewLine_GetRequestStream_And_GetResponse_Throws_FormatException_As_InnerException with support for o...

Done in 7096392 — converted Ftp_Ignore_NewLine_GetRequestStream_And_GetResponse_Throws_FormatException_As_InnerException to a ConditionalTheory with InlineData for each variant (\r\n, \r, \n, %0D%0A, %0D, %0A, upper/lower case) applied to both the username and password of the NetworkCredential, matching the same set covered by the URI ctor and RenameTo theories.

Note

This response was generated by GitHub Copilot.

@mrek-msft mrek-msft marked this pull request as ready for review June 27, 2026 13:26
Copilot AI review requested due to automatic review settings June 27, 2026 13:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR tightens FTP input validation to reject newline characters earlier and more consistently, both in FtpWebRequest URI handling and when formatting FTP control commands, with corresponding test coverage.

Changes:

  • Extend FtpWebRequest construction-time validation to reject \r, \n, and %0D/%0A in the URI’s original string.
  • Extend FtpControlStream.FormatFtpCommand validation similarly for command parameters.
  • Add/expand xUnit theories to cover the new rejection cases (URI ctor, credentials, and RenameTo).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/libraries/System.Net.Requests/src/System/Net/FtpWebRequest.cs Broaden newline detection in the internal constructor to reject CR/LF and %0D/%0A.
src/libraries/System.Net.Requests/src/System/Net/FtpControlStream.cs Broaden FormatFtpCommand validation to reject CR/LF and %0D/%0A in parameters.
src/libraries/System.Net.Requests/tests/FtpWebRequestTest.cs Add theories covering literal and encoded newline variants across URI construction and command-parameter paths.

Comment thread src/libraries/System.Net.Requests/src/System/Net/FtpControlStream.cs Outdated
Comment thread src/libraries/System.Net.Requests/src/System/Net/FtpWebRequest.cs
Co-authored-by: mrek-msft <188900745+mrek-msft@users.noreply.github.com>
Co-authored-by: mrek-msft <188900745+mrek-msft@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 27, 2026 14:01
Comment thread src/libraries/System.Net.Requests/src/System/Net/FtpWebRequest.cs
Copilot AI requested a review from mrek-msft June 28, 2026 16:02
@mrek-msft mrek-msft merged commit 17be26d into main Jun 29, 2026
88 of 90 checks passed
@mrek-msft mrek-msft deleted the copilot/support-url-encoded-check-for-new-line branch June 29, 2026 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for URL encoded check for new line in FtpWebRequest

5 participants