-
-
Notifications
You must be signed in to change notification settings - Fork 98
Add TLSSocket abstraction for uniform SSL/TLS handling
#799
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| [mypy] | ||
| python_version = 3.8 | ||
| python_version = 3.9 | ||
| color_output = true | ||
| error_summary = true | ||
| files = | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| # prefer slower Python-based io module | ||
| import _pyio as io | ||
| import io as stdlib_io | ||
|
Member
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. Why is this needed? |
||
| import socket | ||
|
|
||
|
|
||
|
|
@@ -38,9 +39,16 @@ def _flush_unlocked(self): | |
| class StreamReader(io.BufferedReader): | ||
| """Socket stream reader.""" | ||
|
|
||
| def __init__(self, sock, mode='r', bufsize=io.DEFAULT_BUFFER_SIZE): | ||
| """Initialize socket stream reader.""" | ||
| super().__init__(socket.SocketIO(sock, mode), bufsize) | ||
| def __init__(self, sock, bufsize=io.DEFAULT_BUFFER_SIZE): | ||
| """Initialize with socket or raw IO object.""" | ||
| # If already a RawIOBase (like TLSSocket), use directly | ||
| if isinstance(sock, (io.RawIOBase, stdlib_io.RawIOBase)): | ||
| raw_io = sock | ||
| else: | ||
| # Wrap raw socket with SocketIO | ||
| raw_io = socket.SocketIO(sock, 'rb') | ||
|
|
||
| super().__init__(raw_io, bufsize) | ||
|
Member
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. This is still checking obscure properties of objects to infer TLS relation. We shouldn't be relying on whether something is TLS or not in here. This is an abstraction leak no matter the method of checking. |
||
| self.bytes_read = 0 | ||
|
|
||
| def read(self, *args, **kwargs): | ||
|
|
@@ -57,19 +65,20 @@ def has_data(self): | |
| class StreamWriter(BufferedWriter): | ||
| """Socket stream writer.""" | ||
|
|
||
| def __init__(self, sock, mode='w', bufsize=io.DEFAULT_BUFFER_SIZE): | ||
| """Initialize socket stream writer.""" | ||
| super().__init__(socket.SocketIO(sock, mode), bufsize) | ||
| def __init__(self, sock, bufsize=io.DEFAULT_BUFFER_SIZE): | ||
|
Member
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. Removing arguments or changing their order is a backwards incompatible change. We should avoid those and at least have a deprecation period to allow for downstream adoption. Deprecations would have to be dedicated multi-stage coordinated processes. |
||
| """Initialize with socket or raw IO object.""" | ||
| # If already a RawIOBase (like TLSSocket), use directly | ||
| if isinstance(sock, (io.RawIOBase, stdlib_io.RawIOBase)): | ||
| raw_io = sock | ||
| else: | ||
| # Wrap raw socket with SocketIO | ||
| raw_io = socket.SocketIO(sock, 'wb') | ||
|
|
||
| super().__init__(raw_io, bufsize) | ||
| self.bytes_written = 0 | ||
|
|
||
| def write(self, val, *args, **kwargs): | ||
| """Capture bytes written.""" | ||
| res = super().write(val, *args, **kwargs) | ||
| self.bytes_written += len(val) | ||
| return res | ||
|
|
||
|
|
||
| def MakeFile(sock, mode='r', bufsize=io.DEFAULT_BUFFER_SIZE): | ||
| """File object attached to a socket object.""" | ||
| cls = StreamReader if 'r' in mode else StreamWriter | ||
| return cls(sock, mode, bufsize) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,7 +83,7 @@ | |
|
|
||
| from . import __version__, connections, errors | ||
| from ._compat import IS_PPC, bton | ||
| from .makefile import MakeFile, StreamWriter | ||
| from .makefile import StreamReader, StreamWriter | ||
| from .workers import threadpool | ||
|
|
||
|
|
||
|
|
@@ -1275,19 +1275,18 @@ class HTTPConnection: | |
| # Fields set by ConnectionManager. | ||
| last_used = None | ||
|
|
||
| def __init__(self, server, sock, makefile=MakeFile): | ||
|
Member
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. We'll probably have to keep this for a while (with a sentinel default). And issue a warning when the value isn't that sentinel. |
||
| def __init__(self, server, sock): | ||
| """Initialize HTTPConnection instance. | ||
|
|
||
| Args: | ||
| server (HTTPServer): web server object receiving this request | ||
| sock (socket._socketobject): the raw socket object (usually | ||
| TCP) for this connection | ||
| makefile (file): a fileobject class for reading from the socket | ||
| """ | ||
| self.server = server | ||
| self.socket = sock | ||
| self.rfile = makefile(sock, 'rb', self.rbufsize) | ||
| self.wfile = makefile(sock, 'wb', self.wbufsize) | ||
| self.rfile = StreamReader(sock, self.rbufsize) | ||
| self.wfile = StreamWriter(sock, self.wbufsize) | ||
| self.requests_seen = 0 | ||
|
|
||
| self.peercreds_enabled = self.server.peercreds_enabled | ||
|
|
@@ -1363,7 +1362,7 @@ def _handle_no_ssl(self, req): | |
| except AttributeError: | ||
| # self.socket is of OpenSSL.SSL.Connection type | ||
|
Member
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. Oh, look — another abstraction leak to fight.. |
||
| resp_sock = self.socket._socket | ||
| self.wfile = StreamWriter(resp_sock, 'wb', self.wbufsize) | ||
| self.wfile = StreamWriter(resp_sock, self.wbufsize) | ||
| msg = ( | ||
| 'The client sent a plain HTTP request, but ' | ||
| 'this server only speaks HTTPS on this 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.
This would have to go into a separate PR and be coupled with changes to the core packaging metadata, linting configuration, dev env and CI setup.