tcalmant / jsonrpclib

A Python (2 & 3) JSON-RPC over HTTP that mirrors the syntax of xmlrpclib (aka jsonrpclib-pelix)
https://jsonrpclib-pelix.readthedocs.io/
Apache License 2.0
53 stars 24 forks source link

ServerProxy context argument doesn't work in Python 2.7.9+ #39

Closed tjwalton closed 5 years ago

tjwalton commented 5 years ago

If I run the following code on Python 2.7.13 against a server with an invalid certificate then it raises an ssl.CertificateError exception:

from jsonrpclib.jsonrpc import ServerProxy
import ssl

j = ServerProxy('https://<ip>', context=ssl._create_unverified_context())
j.status()

It should not give a CertificateError exception.

The problem occurs because a context parameter was added to xmlrpclib.SafeTransport in https://github.com/python/cpython/commit/efa3cf84d2a05650a7d2e83a4914b66f7d0a65be, so when the line at https://github.com/tcalmant/jsonrpclib/blob/3bfd2f3/jsonrpclib/jsonrpc.py#L467 calls TransportMixin.__init__(self, config, context) (which sets self.context), the line XMLSafeTransport.__init__(self) resets self.context to None.

From code inspection, it looks likely to be broken in Python 3 too.

tcalmant commented 5 years ago

(fast answer, as I don't have access to the repo on this computer) I'll try to reproduce the bug this evening (UTC+1).

From what I understand, could you try to replace the SafeTransport of jsonrpclib by the following code?

class SafeTransport(TransportMixIn, XMLSafeTransport):
    """
    Mixed-in HTTPS transport
    """
    def __init__(self, config, context):
        TransportMixIn.__init__(self, config, context)
        try:
            # Use the context argument when available
            XMLSafeTransport.__init__(self, context=context)
        except TyperError:
            # Compatibility with previous Python versions
            XMLSafeTransport.__init__(self)
tjwalton commented 5 years ago

Yes, that works (with the typo TyperError changed to TypeError). Thanks for the quick response.