Skip to content

Commit f4e9805

Browse files
committed
session: Scrape API key out of requests exceptions
Via the params dictionary this can leak into requests errors, depending on the way it fails. Perform a straight string replacement on the exception string and re-raise the exception https://bugzilla.redhat.com/show_bug.cgi?id=1896791 Signed-off-by: Cole Robinson <crobinso@redhat.com>
1 parent 209ef83 commit f4e9805

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

bugzilla/_session.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from logging import getLogger
55

66
import os
7+
import sys
8+
79
import requests
810

911
from ._compatimports import urlparse
@@ -97,6 +99,7 @@ def request(self, *args, **kwargs):
9799
timeout = self._get_timeout()
98100
if "timeout" not in kwargs:
99101
kwargs["timeout"] = timeout
102+
100103
response = self._session.request(*args, **kwargs)
101104

102105
if self._is_xmlrpc:
@@ -106,5 +109,11 @@ def request(self, *args, **kwargs):
106109
# Set response cookies
107110
self.set_response_cookies(response)
108111

109-
response.raise_for_status()
112+
try:
113+
response.raise_for_status()
114+
except Exception as e:
115+
# Scrape the api key out of the returned exception string
116+
message = str(e).replace(self._api_key or "", "")
117+
raise type(e)(message).with_traceback(sys.exc_info()[2])
118+
110119
return response

tests/test_ro_functional.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99
Unit tests that do readonly functional tests against real bugzilla instances.
1010
"""
11+
import pytest
1112

1213
import bugzilla
1314
import tests
@@ -63,6 +64,22 @@ def test_rest_xmlrpc_detection():
6364
assert bz.is_xmlrpc()
6465

6566

67+
def test_apikey_error_scraping():
68+
# Ensure the API key does not leak into any requests exceptions
69+
fakekey = "FOOBARMYKEY"
70+
with pytest.raises(Exception) as e:
71+
_open_bz("https://httpstat.us/502&foo",
72+
force_xmlrpc=True, api_key=fakekey)
73+
assert "400 Client Error" in str(e.value)
74+
assert fakekey not in str(e.value)
75+
76+
with pytest.raises(Exception) as e:
77+
_open_bz("https://httpstat.us/502&foo",
78+
force_rest=True, api_key=fakekey)
79+
assert "400 Client Error" in str(e.value)
80+
assert fakekey not in str(e.value)
81+
82+
6683
###################
6784
# mozilla testing #
6885
###################

0 commit comments

Comments
 (0)