From dfed9e2357823af41c2c8b4a07a4280862935c6e Mon Sep 17 00:00:00 2001 From: Bogdan Novosad Date: Sat, 14 Nov 2026 15:21:03 +0100 Subject: [PATCH 1/2] fix(client): keep the API key out of connection errors The key travels in the apikey query parameter, and a requests connection error (refused connection, DNS failure, connect timeout) names the request URL, so printing or logging it showed the key. The client now replaces it with REDACTED in the messages of the error and of the exceptions it wraps; the exception objects and their types stay the same. --- src/zenrows/client.py | 37 ++++++++++++++++++++++++++++++++++++- tests/test_client.py | 19 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/zenrows/client.py b/src/zenrows/client.py index 851ab4e..5f7bd2f 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,29 @@ 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. 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.""" + seen: set[int] = set() + pending = [error] + while pending: + exc = pending.pop() + if id(exc) in seen: + continue + seen.add(id(exc)) + exc.args = tuple( + _APIKEY_IN_URL.sub("apikey=REDACTED", arg) if isinstance(arg, str) else arg + for arg in exc.args + ) + pending += [ + inner + for inner in (*exc.args, exc.__cause__, exc.__context__) + if isinstance(inner, BaseException) + ] + + 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 +634,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 +643,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_client.py b/tests/test_client.py index fff8bde..70a11c3 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,5 +1,8 @@ +import socket +import traceback from unittest import TestCase, mock +import requests import urllib3 from requests import Session @@ -152,3 +155,19 @@ def test_put_with_data(self, mock_request): "key2": "value2", }, ) + + def test_a_connection_error_hides_the_api_key(self): + with socket.socket() as sock: + sock.bind(("127.0.0.1", 0)) + closed = f"http://127.0.0.1:{sock.getsockname()[1]}/v1/" + for retries in (0, 1): + client = ZenRowsClient("secret+key/=", base_url=closed, retries=retries) + with ( + mock.patch("time.sleep"), + self.assertRaises(requests.ConnectionError) as raised, + ): + client.get(url) + + printed = "".join(traceback.format_exception(raised.exception)) + self.assertIn("apikey=REDACTED", str(raised.exception)) + self.assertNotIn("secret", printed) From 818fc2fb94d4eca6ac315b13cf0caae0e481d87f Mon Sep 17 00:00:00 2001 From: Bogdan Novosad Date: Sat, 14 Nov 2026 15:51:28 +0100 Subject: [PATCH 2/2] fix(client): leave the caller's own exception alone when redacting The redaction followed __context__, so a request made inside an except block rewrote the exception the caller was handling, and failed when that exception did not allow it. It now walks only the args of the error. The pattern matches apikey only as a query parameter, and the test moves next to the other connection-failure tests. --- src/zenrows/client.py | 34 ++++++++++++++-------------------- tests/test_client.py | 19 ------------------- tests/test_retries.py | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 39 deletions(-) diff --git a/src/zenrows/client.py b/src/zenrows/client.py index 5f7bd2f..4251646 100644 --- a/src/zenrows/client.py +++ b/src/zenrows/client.py @@ -44,7 +44,7 @@ DEFAULT_RETRY_BUDGET = 90.0 # The API key as it appears in a request URL. -_APIKEY_IN_URL = re.compile(r"apikey=[^&\s]*") +_APIKEY_IN_URL = re.compile(r"(?<=[?&])apikey=[^&\s]*") # Clock for the retry budget; tests replace it. _monotonic = time.monotonic @@ -131,25 +131,19 @@ def _failed_to_connect(error: requests.ConnectionError) -> bool: def _redact_apikey(error: BaseException) -> None: """Replace the API key with REDACTED in the messages of `error` and of the - exceptions it wraps. 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.""" - seen: set[int] = set() - pending = [error] - while pending: - exc = pending.pop() - if id(exc) in seen: - continue - seen.add(id(exc)) - exc.args = tuple( - _APIKEY_IN_URL.sub("apikey=REDACTED", arg) if isinstance(arg, str) else arg - for arg in exc.args - ) - pending += [ - inner - for inner in (*exc.args, exc.__cause__, exc.__context__) - if isinstance(inner, BaseException) - ] + 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: diff --git a/tests/test_client.py b/tests/test_client.py index 70a11c3..fff8bde 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,8 +1,5 @@ -import socket -import traceback from unittest import TestCase, mock -import requests import urllib3 from requests import Session @@ -155,19 +152,3 @@ def test_put_with_data(self, mock_request): "key2": "value2", }, ) - - def test_a_connection_error_hides_the_api_key(self): - with socket.socket() as sock: - sock.bind(("127.0.0.1", 0)) - closed = f"http://127.0.0.1:{sock.getsockname()[1]}/v1/" - for retries in (0, 1): - client = ZenRowsClient("secret+key/=", base_url=closed, retries=retries) - with ( - mock.patch("time.sleep"), - self.assertRaises(requests.ConnectionError) as raised, - ): - client.get(url) - - printed = "".join(traceback.format_exception(raised.exception)) - self.assertIn("apikey=REDACTED", str(raised.exception)) - self.assertNotIn("secret", printed) 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 ----