[New-bugs-announce] [issue42987] HTTP header injection in urllib on windows

bfpiaoran report at bugs.python.org
Wed Jan 20 23:02:35 EST 2021

New submission from bfpiaoran <bfpiaoran at qq.com>:

Recently,(on windows) I discovered a security issue during a security review due to urllib.I checked the document and found that it has been fixed at https://bugs.python.org/issue22928.but My python version is 3.7.2 over the fix bug version .Then tried to find the reason
The code is probably as follows under the django framework

from urllib.request import urlopen

remote_urls = request.POST.getlist("source[]", [])
for remote_url in remote_urls:
    remote_image = urlopen(remote_url)


Changing the address will make urlopen connect to the local machine 6379 and set the key value


Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.utils.six.moves.urllib.request import urlopen
>>> remote_image = urlopen('\r\nset ce test\r\n/1.jpg')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\cuijianxiong\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 222, in urlopen        return opener.open(url, data, timeout)
  File "C:\Users\cuijianxiong\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 525, in open           response = self._open(req, data)
  File "C:\Users\cuijianxiong\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 543, in _open          '_open', req)
  File "C:\Users\cuijianxiong\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain    result = func(*args)
  File "C:\Users\cuijianxiong\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 1345, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Users\cuijianxiong\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 1320, in do_open
    r = h.getresponse()
  File "C:\Users\cuijianxiong\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 1321, in getresponse
  File "C:\Users\cuijianxiong\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 296, in begin
    version, status, reason = self._read_status()
  File "C:\Users\cuijianxiong\AppData\Local\Programs\Python\Python37-32\lib\http\client.py", line 278, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: $-1

Django will encode and decode urlopen because of the url specification, and accept requests with "\r\n" in the url

It is the responsibility of the developer leveraging Python and its HTTP client libraries to ensure that their (web) application acts in accordance to official HTTP specifications and that no threats to security will arise from their code.
However, newlines inside headers are arguably a special case of breaking the conformity with RFC's in regard to the allowed character set. No illegal character used inside a HTTP header is likely to have a compromising side effect on back-end clients and servers and the integrity of their communication, as a result of the leniency of most web servers. However, a newline character (0x0A) embedded in a HTTP header invariably has the semantic consequence of denoting the start of an additional header line. To put it differently, not sanitizing headers in complete accordance to RFC's could be seen as as virtue in that it gives the programmer a maximum amount of freedom, without having to trade it for any likely or severe security ramifications, so that they may use illegal characters in testing environments and environments that are outlined by an expliticly less strict interpretation of the HTTP protocol. Newlines are special in that they enable anyone who is able to influence the header content, to, in effect, perform additional invocations to add_header().

urlopen('\r\Auth: test\r\n/1.jpg')

Ncat: Version 7.70 ( https://nmap.org/ncat )
Ncat: Listening on :::8888
Ncat: Listening on
Ncat: Connection from
Ncat: Connection from
\Auth: test
/1.jpg HTTP/1.1
Accept-Encoding: identity
User-Agent: Python-urllib/3.7
Connection: close

I think https://bugs.python.org/issue22928 is not very thorough and does not fully detect line breaks

messages: 385389
nosy: bfpiaoran, lemburg, orsenthil
priority: normal
severity: normal
status: open
title: HTTP header injection in urllib on windows
type: security
versions: Python 3.10

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list