Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop sending raw exception message as part of Stripe user agent. #736

Merged
merged 3 commits into from
Oct 8, 2021
Merged
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
4 changes: 2 additions & 2 deletions stripe/api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ def request_headers(self, api_key, method):
]:
try:
val = func()
except Exception as e:
val = "!! %s" % (e,)
except Exception:
val = "(disabled)"
ua[attr] = val
if stripe.app_info:
ua["application"] = stripe.app_info
Expand Down
42 changes: 31 additions & 11 deletions tests/test_api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,36 @@ def __init__(
user_agent=None,
app_info=None,
idempotency_key=None,
fail_platform_call=False,
):
self.request_method = request_method
self.api_key = api_key or stripe.api_key
self.extra = extra
self.user_agent = user_agent
self.app_info = app_info
self.idempotency_key = idempotency_key
self.fail_platform_call = fail_platform_call

def __eq__(self, other):
return (
self._keys_match(other)
and self._auth_match(other)
and self._user_agent_match(other)
and self._x_stripe_ua_contains_app_info(other)
and self._x_stripe_ua_handles_failed_platform_function(other)
and self._idempotency_key_match(other)
and self._extra_match(other)
)

def __repr__(self):
return (
"APIHeaderMatcher(request_method=%s, api_key=%s, extra=%s, "
"user_agent=%s, app_info=%s, idempotency_key=%s)"
% (
repr(self.request_method),
repr(self.api_key),
repr(self.extra),
repr(self.user_agent),
repr(self.app_info),
repr(self.idempotency_key),
)
return "APIHeaderMatcher(request_method=%s, api_key=%s, extra=%s, " "user_agent=%s, app_info=%s, idempotency_key=%s, fail_platform_call=%s)" % (
repr(self.request_method),
repr(self.api_key),
repr(self.extra),
repr(self.user_agent),
repr(self.app_info),
repr(self.idempotency_key),
repr(self.fail_platform_call),
)

def _keys_match(self, other):
Expand Down Expand Up @@ -111,6 +111,12 @@ def _x_stripe_ua_contains_app_info(self, other):

return True

def _x_stripe_ua_handles_failed_platform_function(self, other):
if self.fail_platform_call:
ua = json.loads(other["X-Stripe-Client-User-Agent"])
return ua["platform"] == "(disabled)"
return True

def _extra_match(self, other):
for k, v in six.iteritems(self.extra):
if other[k] != v:
Expand Down Expand Up @@ -592,6 +598,20 @@ def test_uses_app_info(self, requestor, mock_response, check_call):
finally:
stripe.app_info = old

def test_handles_failed_platform_call(
self, requestor, mocker, mock_response, check_call
):
mock_response("{}", 200)

def fail():
raise RuntimeError

mocker.patch("platform.platform", side_effect=fail)

requestor.request("get", self.valid_path, {}, {})

check_call("get", headers=APIHeaderMatcher(fail_platform_call=True))

def test_uses_given_idempotency_key(
self, requestor, mock_response, check_call
):
Expand Down