diff --git a/src/zenrows/client.py b/src/zenrows/client.py index 851ab4e..4251646 100644 --- a/src/zenrows/client.py +++ b/src/zenrows/client.py @@ -16,6 +16,7 @@ import numbers import os import random +import re import sys import time import warnings @@ -42,6 +43,9 @@ DEFAULT_RETRY_MAX_WAIT = 30.0 DEFAULT_RETRY_BUDGET = 90.0 +# The API key as it appears in a request URL. +_APIKEY_IN_URL = re.compile(r"(?<=[?&])apikey=[^&\s]*") + # Clock for the retry budget; tests replace it. _monotonic = time.monotonic @@ -125,6 +129,23 @@ def _failed_to_connect(error: requests.ConnectionError) -> bool: return isinstance(reason, ConnectTimeoutError) # NewConnectionError is one too +def _redact_apikey(error: BaseException) -> None: + """Replace the API key with REDACTED in the messages of `error` and of the + exceptions it wraps (its `args`). A failed connection names the request + URL, which carries the key in `apikey`, so logging the error would log the + key. The exceptions stay the same objects, and an exception the caller was + handling when it made the request is left alone.""" + for arg in error.args: + if isinstance(arg, BaseException): + _redact_apikey(arg) + args = tuple( + _APIKEY_IN_URL.sub("apikey=REDACTED", arg) if isinstance(arg, str) else arg + for arg in error.args + ) + if args != error.args: + error.args = args + + def _from_target(status: int, params: dict) -> bool: """True when `status` may be the target site's own answer, which another try cannot change: any status with `original_status`, or one listed in @@ -607,7 +628,7 @@ def _worker( final_headers["Connection"] = None final_headers.update(headers) - send = partial( + request = partial( self.requests_session.request, method, self.api_url, @@ -616,6 +637,14 @@ def _worker( data=data, **kwargs, ) + + def send() -> requests.Response: + try: + return request() + except requests.RequestException as error: + _redact_apikey(error) + raise + if not self.retries or not _can_send_again(method, data, kwargs): return send() started = _monotonic() diff --git a/tests/test_retries.py b/tests/test_retries.py index 87fa20e..e3ce1a4 100644 --- a/tests/test_retries.py +++ b/tests/test_retries.py @@ -11,6 +11,7 @@ import socket import sys import threading +import traceback import warnings from collections.abc import Callable, Iterator from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer @@ -319,6 +320,23 @@ def test_failure_to_connect_is_retried_for_a_get_only( assert len(sleeps) == retried +@pytest.mark.parametrize("retries", [0, 2]) +def test_failure_to_connect_hides_the_api_key(sleeps: list[float], retries: int): + zr = ZenRowsClient("secret+key/=", base_url=closed_port_url(), retries=retries) + handled = ValueError("?apikey=not-ours") + + with pytest.raises(requests.ConnectionError) as raised: + try: + raise handled + except ValueError: + zr.get(TARGET) + + assert "apikey=REDACTED" in str(raised.value) + assert "secret" not in "".join(traceback.format_exception(raised.value)) + # The exception the caller was handling is not the client's to change. + assert str(handled) == "?apikey=not-ours" + + # ---- which requests are sent again ----