Skip to content

Commit 9ea5c63

Browse files
committed
mask the API key in the error chained to an API error
1 parent 06201ce commit 9ea5c63

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
- A connection error no longer prints the API key: requests put the request URL, `key` included, in its message
3131
and in the urllib3 errors chained to it, and an `HTTPError` from an error status printed it too. The key now
3232
reads `key=REDACTED`.
33+
- An API error raised from an error status no longer chains the `HTTPError` it came from, whose URL carries the API
34+
key: a traceback, `logger.exception` or Sentry printed the key. The chained error reads `key=REDACTED`.
3335
- `ScrapflySpider` crawls again on Scrapy 2.16 and later: it now defines `start()`, which Scrapy reads in place
3436
of `start_requests()` (Scrapy 2.19 no longer calls `start_requests()` at all).
3537
- The Scrapy integration emits no deprecation warning from Scrapy 2.11 to 2.19: the middlewares no longer

‎scrapfly/api_response.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
ApiHttpServerError, UpstreamHttpError, HttpError, \
3434
ExtraUsageForbidden, WebhookSignatureMissMatch, ContentError, api_error_args
3535
from .frozen_dict import FrozenDict
36+
from .transport import without_key
3637

3738
logger = logging.getLogger(__name__)
3839

@@ -452,13 +453,13 @@ def raise_for_result(
452453
args.update(request=e.request, response=e.response, api_response=self)
453454

454455
if e.response.status_code >= 500:
455-
raise ApiHttpServerError(**args) from e
456+
raise ApiHttpServerError(**args) from without_key(e)
456457

457458
# respect raise_on_upstream_error with screenshot and extraction only
458459
if error_class in (ScreenshotAPIError, ExtractionAPIError) and not raise_on_upstream_error:
459460
return
460461

461-
raise error_class(**args) from e
462+
raise error_class(**args) from without_key(e)
462463

463464

464465
class LargeObjectApiResponse(ApiResponse):

‎tests/test_clients.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import gzip
44
import inspect
55
import json
6+
import traceback
67
from io import BytesIO
78
from dataclasses import dataclass
89
from typing import Callable, Dict, List, Tuple
@@ -267,7 +268,8 @@ async def scenario(scrapfly):
267268
with pytest.raises(error) as raised:
268269
run(kind, server, scenario)
269270

270-
assert KEY not in str(raised.value)
271+
# A traceback prints the chained causes too, as Sentry and logger.exception do.
272+
assert KEY not in ''.join(traceback.format_exception(raised.value))
271273

272274

273275
@pytest.mark.parametrize('content_encoding,body,expected', [

0 commit comments

Comments
 (0)