10000 Fixes AttributeError: module 'ssl' has no attribute 'wrap_socket' by juanvalino · Pull Request #70 · xmpppy/xmpppy · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fixes AttributeError: module 'ssl' has no attribute 'wrap_socket' #70

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

juanvalino
Copy 8000 link

Fixes #69

@juanvalino
Copy link
Author

I've updated PR disabling certificate checking to maintain backward compability

@juanvalino
Copy link
Author

@gdt I`m going to create a new issue to ask for an explicit and configurable way to enable/disable certificate checking, but I think this fix should be enough to get the library work with latest python versions maintaining previous behavior.

@gdt
Copy link
gdt commented Dec 1, 2023

Fair enough to match previous behavior. My comment was mainly aimed at "As a reviewer, it should be clear whether this is a change or not".

Secondarily, I think defaulting to not validating is a bug.

@juanvalino
Copy link
Author

@gdt I think that defaulting to not validating is no ideal (security matters) but necessary if we want to maintain previous behavior because when creating a default context and using context's wrap_socket always did certificate validation (since Python 2.7.x for some x < 20 IIRC). But the xmpppy library uses the top-level wrap_socket function, which got removed in Python 3.12; that one never did certificate validation.

So the way I understand it, with Python < 3.12 xmpppy doesn't seem to validate certificates, and with Python >= 3.12 it doesn't work if you use TLS (because the function it uses was removed).

Copy link
@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks correct to me. I agree that enabling certificate validation by default would be better, but this didn't happen before.

This PR might cause problems with very early 2.7.x releases, since the ssl module changed a lot for the first couple of x. This has been a looong time ago, though, so I think it's OK to ignore that :)

@juanvalino
Copy link
Author

Please, anyone could merge the proposed patch and release a new version with this fixed?

More than one year with the solution ready...

@glefait
Copy link
glefait commented Sep 4, 2024

@juanvalino , I feel you and thanks for your patch.

An alternative, when security is not a concern (like in a lab) and until this PR is merged, is to use secure=0.
https://github.com/glefait/CVE-2023-25355-25356/blob/main/src/main.py#L48

If I were to use this library in a "real" context (not in a lab), I would probably prefer to be able to control specifically which security flag is ignored.

@@ -386,6 +386,7 @@ def _startSSL(self):
tcpsock=self._owner.Connection
context=ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have applied the diff from this pull request, more or less, to pkgsrc. I am not meaning to ask you to change the PR, but rather just adding comments in case they are helpful to you or to others.

I left checking enabled, because I think it's a bug for TLS not to validate. But, it always failed. I then added

context.set_default_verify_paths()

and that let the python ssl implementation find the system trust anchors on both NetBSD and macOS.

While trying various things, I think I found that check_hostname = False without setting verify_mode = ssl.CERT_NONE threw an error.

But, with my patch (largely taken from yours):

--- xmpp/transports.py.orig     2022-04-10 18:25:44.000000000 +0000
+++ xmpp/transports.py
@@ -383,8 +383,14 @@ class TLS(PlugIn):
     def _startSSL(self):
         """ Immidiatedly switch socket to TLS mode. Used internally."""
         """ Here we should switch pending_data to hint mode."""
+        context=ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
+        context.set_default_verify_paths()
+        # Uncomment if you need to work around trust anchor configuration.
+        #context.check_hostname = False
+        #context.verify_mode = ssl.CERT_NONE
+
         tcpsock=self._owner.Connection
-        tcpsock._sslObj    = ssl.wrap_socket(tcpsock._sock, None, None)
+        tcpsock._sslObj    = context.wrap_socket(tcpsock._sock, server_hostname=self._owner.Server)
         tcpsock._sslIssuer = tcpsock._sslObj.getpeercert().get('issuer')
         tcpsock._sslServer = tcpsock._sslObj.getpeercert().get('server')
         tcpsock._recv = tcpsock._sslObj.read

I am able to use xmpppy to send jabber messages to a server that requires TLS.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to enable certificate checking, better create context with ssl.create_default_context() (https://docs.python.org/3/library/ssl.html#ssl.create_default_context), and don't manually call set_default_verify_paths and set check_hostname and verify_mode.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - I have changed pkgsrc as you suggest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AttributeError: module 'ssl' has no attribute 'wrap_socket'
4 participants
0