-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
bpo-29606: urllib throwing an exception on any URLs that contain one of '\r\n' for the FTP protocol. #1216
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
] | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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')> There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also blacklist \r? |
||
raise URLError("illegal ftp host") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
host, port = splitport(host) | ||
if port is None: | ||
port = ftplib.FTP_PORT | ||
|
There was a problem hiding this comment.
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.