Unable to catch the ssl.SSLCertVerificationError Exception #1582
-
I have implemented http2 client using httpx. While testing an invalid cert issue, I have come across a scenario where the underlying asyncio loop logs the exception but doesn't raise it so that the application is notified of the error. Attaching the code used to simulate the issue. The code when run on a debian box simply logs the exception but the code does not catch the exception.
The below similar version when run on windows doesn't result in this issue.
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
If you want to catch that case you need to be catching We always ensure that raw network errors are mapped neatly onto the However, something that we should probably change here is that we're not preserving the entire traceback, so you'll see eg, this... Traceback (most recent call last):
File "/Users/tomchristie/GitHub/encode/httpx/httpx/_transports/default.py", line 61, in map_httpcore_exceptions
yield
File "/Users/tomchristie/GitHub/encode/httpx/httpx/_transports/default.py", line 291, in handle_async_request
ext=extensions,
File "/Users/tomchristie/GitHub/encode/httpx/venv/lib/python3.7/site-packages/httpcore/_async/connection_pool.py", line 219, in arequest
method, url, headers=headers, stream=stream, ext=ext
File "/Users/tomchristie/GitHub/encode/httpx/venv/lib/python3.7/site-packages/httpcore/_async/connection.py", line 92, in arequest
self.socket = await self._open_socket(timeout)
File "/Users/tomchristie/GitHub/encode/httpx/venv/lib/python3.7/site-packages/httpcore/_async/connection.py", line 123, in _open_socket
local_address=self.local_address,
File "/Users/tomchristie/GitHub/encode/httpx/venv/lib/python3.7/site-packages/httpcore/_backends/auto.py", line 45, in open_tcp_stream
hostname, port, ssl_context, timeout, local_address=local_address
File "/Users/tomchristie/GitHub/encode/httpx/venv/lib/python3.7/site-packages/httpcore/_backends/asyncio.py", line 244, in open_tcp_stream
stream_reader=stream_reader, stream_writer=stream_writer
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "/Users/tomchristie/GitHub/encode/httpx/venv/lib/python3.7/site-packages/httpcore/_exceptions.py", line 12, in map_exceptions
raise to_exc(exc) from None
httpcore.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./example.py", line 32, in <module>
asyncio.run(main(),debug=True)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 583, in run_until_complete
return future.result()
File "./example.py", line 30, in main
ret = await asyncio.gather(task)
File "./example.py", line 14, in test
await client.get(url="https://incomplete-chain.badssl.com/")
File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 1622, in get
timeout=timeout,
File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 1429, in request
request, auth=auth, allow_redirects=allow_redirects, timeout=timeout
File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 1468, in send
history=[],
File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 1502, in _send_handling_auth
history=history,
File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 1532, in _send_handling_redirects
response = await self._send_single_request(request, timeout)
File "/Users/tomchristie/GitHub/encode/httpx/httpx/_client.py", line 1578, in _send_single_request
extensions={"timeout": timeout.as_dict()},
File "/Users/tomchristie/GitHub/encode/httpx/httpx/_transports/default.py", line 291, in handle_async_request
ext=extensions,
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "/Users/tomchristie/GitHub/encode/httpx/httpx/_transports/default.py", line 78, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076) When really we'd also like to include the full low level trackback all the way back to the original That's occurring because we're currently masking the originating traceback when mapping to httpcore exceptions. Eg asyncio case and sync case We almost certainly want to drop the @contextlib.contextmanager
def map_exceptions(map: Dict[Type[Exception], Type[Exception]]) -> Iterator[None]:
try:
yield
except Exception as exc: # noqa: PIE786
for from_exc, to_exc in map.items():
if isinstance(exc, from_exc):
raise to_exc(exc) from None
raise I haven't dug through to see our issue history with Note that the simplest reproduction of the case you're talking about is this... httpx.get(url="https://incomplete-chain.badssl.com/") Aside to team - we're often seeing issue reports against asyncio cases, which to my mind generally over-complicates things, unless it's genuinely an asyncio-specific error. Usually on a first pass I'll start by working through a sync case instead. There might be some good stuff for us to do here with guiding contributors and helping get incoming potential issues framed as simply as possible. Not sure. |
Beta Was this translation helpful? Give feedback.
-
@tomchristie I was able to isolate the issue. It was a result of using a https_proxy param. The statement |
Beta Was this translation helpful? Give feedback.
-
try the
|
Beta Was this translation helpful? Give feedback.
@tomchristie I was able to isolate the issue. It was a result of using a https_proxy param. The statement
raise ProxyError(exc)
in async def _tunnel_request was never getting raised. The httpx version I was using was 0.12.3.With latest release of httpx I no longer see this issue. Thanks a lot for the help.