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

ssl.wrap_socket doesn't handle virtual TLS hosts #68031

Closed
nagle mannequin opened this issue Apr 1, 2015 · 4 comments
Closed

ssl.wrap_socket doesn't handle virtual TLS hosts #68031

nagle mannequin opened this issue Apr 1, 2015 · 4 comments
Labels
docs Documentation in the Doc dir stdlib Python modules in the Lib dir

Comments

@nagle
Copy link
Mannequin

nagle mannequin commented Apr 1, 2015

BPO 23843
Nosy @pitrou, @tiran, @alex, @dstufft

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2016-09-08.15:21:18.270>
created_at = <Date 2015-04-01.18:32:18.790>
labels = ['library', 'docs']
title = "ssl.wrap_socket doesn't handle virtual TLS hosts"
updated_at = <Date 2016-09-08.15:22:12.901>
user = 'https://bugs.python.org/nagle'

bugs.python.org fields:

activity = <Date 2016-09-08.15:22:12.901>
actor = 'giampaolo.rodola'
assignee = 'docs@python'
closed = True
closed_date = <Date 2016-09-08.15:21:18.270>
closer = 'christian.heimes'
components = ['Documentation', 'Library (Lib)']
creation = <Date 2015-04-01.18:32:18.790>
creator = 'nagle'
dependencies = []
files = []
hgrepos = []
issue_num = 23843
keywords = []
message_count = 4.0
messages = ['239834', '239866', '239887', '275044']
nosy_count = 7.0
nosy_names = ['janssen', 'nagle', 'pitrou', 'christian.heimes', 'alex', 'docs@python', 'dstufft']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue23843'
versions = ['Python 3.6']

@nagle
Copy link
Mannequin Author

nagle mannequin commented Apr 1, 2015

ssl.wrap_socket() always uses the SSL certificate associated with the raw IP address, rather than using the server_host feature of TLS. Even when wrap_socket is used before calling "connect(port, host)", the "host" parameter isn't used by TLS.

To get proper TLS behavior (which only works in recent Python versions), it's necessary to create an SSLContext, then use

context.wrap_socket(sock, server_hostname="example.com")

This behavior is backwards-compatible (the SSL module didn't talk TLS until very recently) but confusing. The documentation does not reflect this difference. There's a lot of old code and online advice which suggests using ssl.wrap_socket(). It works until you hit a virtual host with TLS support. Then you get the wrong server cert and an unexpected "wrong host" SSL error.

Possible fixes:

  1. Deprecate ssl.wrap_socket(), and modify the documentation to tell users to always use context.wrap_socket().

  2. Add a "server_hostname" parameter to ssl.wrap_socket(). It doesn't accept that parameter; only context.wrap_socket() does. Modify documentation accordingly.

@nagle nagle mannequin assigned docspython Apr 1, 2015
@nagle nagle mannequin added docs Documentation in the Doc dir stdlib Python modules in the Lib dir labels Apr 1, 2015
@pitrou
Copy link
Member

pitrou commented Apr 2, 2015

Not sure why you're using wrap_socket() directly. Most of the time you should be using a higher-level library instead (for example a HTTP(S) library).

In any case, the doc already mentions that "Starting from Python 3.2, it can be more flexible to use SSLContext.wrap_socket() instead".

I leave this open in case other people feel positively about it.

@nagle
Copy link
Mannequin Author

nagle mannequin commented Apr 2, 2015

I'm using wrap_socket because I want to read the details of a server's SSL certificate.

"Starting from Python 3.2, it can be more flexible to use SSLContext.wrap_socket() instead" does not convey that ssl.wrap_socket() will fail to connect to some servers because it will silently check the wrong certificate.

@tiran
Copy link
Member

tiran commented Sep 8, 2016

ssl.wrap_socket() will be deprecated in 3.6. Please use a context. You can still inspect the server cert with a context. In fact ssl.wrap_socket() uses a context internally.

@tiran tiran closed this as completed Sep 8, 2016
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

2 participants