Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-29606: urllib throwing an exception on any URLs that contain one of '\r\n' for the FTP protocol. #1216

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,22 @@ def test_ftp_nonexisting(self):
self.assertFalse(e.exception.filename)
self.assertTrue(e.exception.reason)

def test_ftp_illegalhost(self):
illegal_ftp_hosts = [
'ftp://foo:bar%0d%[email protected]/file.png',
'fTp://foo:bar%0d%[email protected]/file.png',
'FTP://foo:bar%0d%[email protected]/file.png',
'ftp://foo:bar%[email protected]/file.png',
'fTp://foo:bar%[email protected]/file.png',
'FTP://foo:bar%[email protected]/file.png'
Copy link
Member

Choose a reason for hiding this comment

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

I don't think that it's interesting to test different cases, I suggest to only test %0d%0a and %0a, so only keep two tests using ftp:// prefix.

]

for host in illegal_ftp_hosts:
with self.assertRaises(urllib.error.URLError) as e:
urlopen(host)
self.assertFalse(e.exception.filename)
self.assertTrue(e.exception.reason)
Copy link
Member

Choose a reason for hiding this comment

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

The test is invalid: it must check that the error contains "illegal host". In my case, the test only succeded because of another URLError.

Copy link
Member

Choose a reason for hiding this comment

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

For example, the test pass if the connection fails with <urlopen error ftp error ConnectionRefusedError(111, 'Connection refused')>

Copy link

Choose a reason for hiding this comment

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

As I mentioned above, the tests should also include injection attempts on directories and filenames. After all, these portions of a URL illicit different FTP commands at different times.


@patch.object(urllib.request, 'MAXFTPCACHE', 0)
def test_ftp_cache_pruning(self):
self.fakeftp()
Expand Down
3 changes: 3 additions & 0 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,9 @@ def ftp_open(self, req):
host = req.host
if not host:
raise URLError('ftp error: no host given')
target_host = unquote(host)
if '\n' in target_host:
Copy link
Member

Choose a reason for hiding this comment

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

Should we also blacklist \r?

raise URLError("illegal ftp host")
Copy link
Member

Choose a reason for hiding this comment

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

I suggest to keep the "ftp error" prefix for the error message, so:

ftp error: illegal host

host, port = splitport(host)
if port is None:
port = ftplib.FTP_PORT
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ Extension Modules
Library
-------

- bpo-29606: urllib throwing an URL exception on any URLs that
contain one of '\n' for the FTP protocol. Patch by Dong-hee Na

- [Security] bpo-29591: Update expat copy from 2.1.1 to 2.2.0 to get fixes
of CVE-2016-0718 and CVE-2016-4472. See
https://sourceforge.net/p/expat/bugs/537/ for more information.
Expand Down