Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/zenrows/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import numbers
import os
import random
import re
import sys
import time
import warnings
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down
18 changes: 18 additions & 0 deletions tests/test_retries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ----


Expand Down