-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Closed as not planned
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-SSLtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Line 1342 in 986a4e1
self._sslobj = None |
Consider the following very simple http 1.0 client:
import ssl
import socket
c = ssl.create_default_context()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('google.com', 443))
ss = c.wrap_socket(s, server_hostname='google.com')
rq = 'GET / HTTP/1.0\r\n\r\n'
ss.sendall(rq.encode())
ss.shutdown(socket.SHUT_WR)
while True:
rs = ss.recv(1024)
if rs == b'':
break
print(rs)
Expected result would be a html page, but instead this script prints encrypted data.
The shutdown function is used to close the write end of the socket, to signal the peer that no more data will be sent. The peer can still send data though. In this case, the google server sends an encrypted response but the sslsocket.recv returns it as-is because the shutdown function set _sslobj to None.
A workaround:
import ssl
import socket
c = ssl.create_default_context()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('google.com', 443))
ss = c.wrap_socket(s, server_hostname='google.com')
rq = 'GET / HTTP/1.0\r\n\r\n'
ss.sendall(rq.encode())
tmp = ss._sslobj
ss.shutdown(socket.SHUT_WR)
ss._sslobj = tmp
while True:
rs = ss.recv(1024)
if rs == b'':
break
print(rs)
This might be a trivial example because we could just not choose to shutdown. But when implementing things like proxies, this can become a problem.
in my opinion the referenced line should be deleted.
Linked PRs
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-SSLtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error